blob: 8a663e38ecd21db1b2cdd68e519f09ed6716e141 [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
26const double EXP_1 = exp (-5.0/60.0); /* 1/exp(5sec/1min) */
27const double EXP_5 = exp (-5.0/300.0); /* 1/exp(5sec/5min) */
28const double EXP_15 = exp (-5.0/900.0); /* 1/exp(5sec/15min) */
29
30const double EPSILON = 0.000001;
31
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070032NS_LOG_COMPONENT_DEFINE ("LoadStats");
33
Alexander Afanasyev0845c092012-07-13 17:45:33 -070034namespace ndnSIM
35{
36
37LoadStats::LoadStats ()
38 : counter_ (0)
39 , avg1_ (0)
40 , avg5_ (0)
41 , avg15_ (0)
42{
43}
44
45void
46LoadStats::Step ()
47{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070048 // NS_LOG_FUNCTION (this);
49
Alexander Afanasyev0845c092012-07-13 17:45:33 -070050 // do magic
51 avg1_ = EXP_1 * avg1_ + (1 - EXP_1) * counter_;
52 avg5_ = EXP_1 * avg5_ + (1 - EXP_5) * counter_;
53 avg15_ = EXP_1 * avg15_ + (1 - EXP_15) * counter_;
54
55 counter_ = 0;
56}
57
58LoadStats &
59LoadStats::operator ++ (int)
60{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070061 // NS_LOG_FUNCTION (this);
62
Alexander Afanasyev0845c092012-07-13 17:45:33 -070063 counter_ ++;
64 return *this;
65}
66
Alexander Afanasyev0845c092012-07-13 17:45:33 -070067LoadStats &
68LoadStats::operator += (const LoadStats &stats)
69{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070070 // NS_LOG_FUNCTION (this << &stats << stats.counter_);
71
Alexander Afanasyev0845c092012-07-13 17:45:33 -070072 counter_ += stats.counter_;
73 return *this;
74}
75
76LoadStats::stats_tuple
77LoadStats::GetStats () const
78{
79 return stats_tuple (avg1_, avg5_, avg15_);
80}
81
82bool
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070083LoadStats::IsZero () const
Alexander Afanasyev0845c092012-07-13 17:45:33 -070084{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070085 return (counter_ == 0 &&
86 avg1_ < EPSILON &&
Alexander Afanasyev0845c092012-07-13 17:45:33 -070087 avg5_ < EPSILON &&
88 avg15_ < EPSILON);
89}
90
91std::ostream &
92operator << (std::ostream &os, const LoadStats &stats)
93{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070094 os << stats.avg1_ << ", " << stats.avg5_ << ", " << stats.avg15_;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070095 return os;
96}
97
98} // ndnSIM