blob: 66786fd60e4d0a4ff59aeaa0abead7e7f82a91bc [file] [log] [blame]
Alexander Afanasyev0845c092012-07-13 17:45:33 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#include "load-stats.h"
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070022#include "ns3/log.h"
Alexander Afanasyev0845c092012-07-13 17:45:33 -070023
24using namespace ns3;
25
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070026// const double EXP_1 = (1-2.0/6.0);//exp (-1.0/5.0); /* 1/exp(1sec/5sec) */
27// const double EXP_2 = (1-2.0/31.0);//exp (-1.0/30.0); /* 1/exp(1sec/30sec) */
28// const double EXP_3 = (1-2.0/61.0);//exp (-1.0/60.0); /* 1/exp(1sec/60sec) */
Alexander Afanasyev0845c092012-07-13 17:45:33 -070029
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070030const double EXP_1 = exp (-1.0/5.0); /* 1/exp(1sec/5sec) */
31const double EXP_2 = exp (-1.0/30.0); /* 1/exp(1sec/30sec) */
32const double EXP_3 = exp (-1.0/60.0); /* 1/exp(1sec/60sec) */
Alexander Afanasyev0845c092012-07-13 17:45:33 -070033
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070034NS_LOG_COMPONENT_DEFINE ("LoadStats");
35
Alexander Afanasyev0845c092012-07-13 17:45:33 -070036namespace ndnSIM
37{
38
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070039const double LoadStats::PRECISION = 0.1;
40
Alexander Afanasyev0845c092012-07-13 17:45:33 -070041LoadStats::LoadStats ()
42 : counter_ (0)
43 , avg1_ (0)
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070044 , avg2_ (0)
45 , avg3_ (0)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070046{
47}
48
49void
50LoadStats::Step ()
51{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070052 // NS_LOG_FUNCTION (this);
53
Alexander Afanasyev0845c092012-07-13 17:45:33 -070054 // do magic
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070055 avg1_ = EXP_1 * avg1_ + (1 - EXP_1) * counter_;
56 avg2_ = EXP_2 * avg2_ + (1 - EXP_2) * counter_;
57 avg3_ = EXP_3 * avg3_ + (1 - EXP_3) * counter_;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070058
59 counter_ = 0;
60}
61
62LoadStats &
63LoadStats::operator ++ (int)
64{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070065 // NS_LOG_FUNCTION (this);
66
Alexander Afanasyev0845c092012-07-13 17:45:33 -070067 counter_ ++;
68 return *this;
69}
70
Alexander Afanasyev0845c092012-07-13 17:45:33 -070071LoadStats &
72LoadStats::operator += (const LoadStats &stats)
73{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070074 // NS_LOG_FUNCTION (this << &stats << stats.counter_);
75
Alexander Afanasyev0845c092012-07-13 17:45:33 -070076 counter_ += stats.counter_;
77 return *this;
78}
79
80LoadStats::stats_tuple
81LoadStats::GetStats () const
82{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070083 return stats_tuple (avg1_, avg2_, avg3_);
Alexander Afanasyev0845c092012-07-13 17:45:33 -070084}
85
86bool
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070087LoadStats::IsZero () const
Alexander Afanasyev0845c092012-07-13 17:45:33 -070088{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070089 return (counter_ == 0 &&
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070090 avg1_ < PRECISION &&
91 avg2_ < PRECISION &&
92 avg3_ < PRECISION);
Alexander Afanasyev0845c092012-07-13 17:45:33 -070093}
94
95std::ostream &
96operator << (std::ostream &os, const LoadStats &stats)
97{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070098 os << stats.avg1_ << ", " << stats.avg2_ << ", " << stats.avg3_;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070099 return os;
100}
101
102} // ndnSIM