blob: a7559b3ac4a474f412745c8ceee84cf612abf6c2 [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 Afanasyev70426a02012-08-15 15:39:18 -070023#include "ns3/simulator.h"
Alexander Afanasyev0845c092012-07-13 17:45:33 -070024
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070025// const double EXP_1 = (1-2.0/6.0);//exp (-1.0/5.0); /* 1/exp(1sec/5sec) */
26// const double EXP_2 = (1-2.0/31.0);//exp (-1.0/30.0); /* 1/exp(1sec/30sec) */
27// 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 -070028
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070029const double EXP_1 = exp (-1.0/5.0); /* 1/exp(1sec/5sec) */
30const double EXP_2 = exp (-1.0/30.0); /* 1/exp(1sec/30sec) */
31const double EXP_3 = exp (-1.0/60.0); /* 1/exp(1sec/60sec) */
Alexander Afanasyev0845c092012-07-13 17:45:33 -070032
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070033NS_LOG_COMPONENT_DEFINE ("ndn.LoadStats");
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070034
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070035namespace ns3 {
36namespace ndn {
37namespace ndnSIM {
Alexander Afanasyev0845c092012-07-13 17:45:33 -070038
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 Afanasyev70426a02012-08-15 15:39:18 -070044 , avg1Counter_ (0)
45 , avg1CounterOld_ (0)
46
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070047 , avg2_ (0)
Alexander Afanasyev70426a02012-08-15 15:39:18 -070048 , avg2Counter_ (0)
49 , avg2CounterOld_ (0)
50
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070051 , avg3_ (0)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070052{
53}
54
55void
56LoadStats::Step ()
57{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070058 // NS_LOG_FUNCTION (this);
59
Alexander Afanasyev0845c092012-07-13 17:45:33 -070060 // do magic
Alexander Afanasyev70426a02012-08-15 15:39:18 -070061 // avg1_ = EXP_1 * avg1_ + (1 - EXP_1) * counter_;
62
63 // highly experimental
64 // avg1Counter_ += counter_;
65 // avg1_ = 1.0 * (avg1Counter_ - avg1CounterOld_) / 1.0/*Seconds(1.0).ToDouble (Time::S)*/;
66 // avg1CounterOld_ = avg1Counter_;
67 // avg1LastUpdate_ = Simulator::Now ();
68 avg1_ = counter_;
69
70 // avg2_ = EXP_2 * avg2_ + (1 - EXP_2) * counter_;
71
72 avg2Counter_ += counter_;
73 if (Simulator::Now () - avg2LastUpdate_ >= Seconds (5.0))
74 {
75 avg2_ = 1.0 * (avg2Counter_ - avg2CounterOld_) / (Simulator::Now () - avg2LastUpdate_).ToDouble (Time::S);
76 avg2CounterOld_ = avg2Counter_;
77 avg2LastUpdate_ = Simulator::Now ();
78 }
79
80 // avg3_ = EXP_3 * avg3_ + (1 - EXP_3) * counter_;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070081
82 counter_ = 0;
83}
84
85LoadStats &
86LoadStats::operator ++ (int)
87{
88 counter_ ++;
89 return *this;
90}
91
Alexander Afanasyev0845c092012-07-13 17:45:33 -070092LoadStats &
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070093LoadStats::operator += (uint32_t amount)
94{
95 counter_ += amount;
96 return *this;
97}
98
99LoadStats &
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700100LoadStats::operator += (const LoadStats &stats)
101{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700102 // NS_LOG_FUNCTION (this << &stats << stats.counter_);
103
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700104 counter_ += stats.counter_;
105 return *this;
106}
107
108LoadStats::stats_tuple
109LoadStats::GetStats () const
110{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700111 return stats_tuple (avg1_, avg2_, avg3_);
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700112}
113
114bool
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700115LoadStats::IsZero () const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700116{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700117 return (counter_ == 0 &&
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700118 avg1_ < PRECISION &&
119 avg2_ < PRECISION &&
120 avg3_ < PRECISION);
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700121}
122
123std::ostream &
124operator << (std::ostream &os, const LoadStats &stats)
125{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700126 os << stats.avg1_ << ", " << stats.avg2_ << ", " << stats.avg3_;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700127 return os;
128}
129
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700130} // namespace ndnSIM
131} // namespace ndn
132} // namespace ns3