Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 22 | #include "ns3/log.h" |
Alexander Afanasyev | 70426a0 | 2012-08-15 15:39:18 -0700 | [diff] [blame^] | 23 | #include "ns3/simulator.h" |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 25 | // 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 Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 29 | const double EXP_1 = exp (-1.0/5.0); /* 1/exp(1sec/5sec) */ |
| 30 | const double EXP_2 = exp (-1.0/30.0); /* 1/exp(1sec/30sec) */ |
| 31 | const double EXP_3 = exp (-1.0/60.0); /* 1/exp(1sec/60sec) */ |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 33 | NS_LOG_COMPONENT_DEFINE ("ndn.LoadStats"); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 35 | namespace ns3 { |
| 36 | namespace ndn { |
| 37 | namespace ndnSIM { |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 38 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 39 | const double LoadStats::PRECISION = 0.1; |
| 40 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 41 | LoadStats::LoadStats () |
| 42 | : counter_ (0) |
| 43 | , avg1_ (0) |
Alexander Afanasyev | 70426a0 | 2012-08-15 15:39:18 -0700 | [diff] [blame^] | 44 | , avg1Counter_ (0) |
| 45 | , avg1CounterOld_ (0) |
| 46 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 47 | , avg2_ (0) |
Alexander Afanasyev | 70426a0 | 2012-08-15 15:39:18 -0700 | [diff] [blame^] | 48 | , avg2Counter_ (0) |
| 49 | , avg2CounterOld_ (0) |
| 50 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 51 | , avg3_ (0) |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | LoadStats::Step () |
| 57 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 58 | // NS_LOG_FUNCTION (this); |
| 59 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 60 | // do magic |
Alexander Afanasyev | 70426a0 | 2012-08-15 15:39:18 -0700 | [diff] [blame^] | 61 | // 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 Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 81 | |
| 82 | counter_ = 0; |
| 83 | } |
| 84 | |
| 85 | LoadStats & |
| 86 | LoadStats::operator ++ (int) |
| 87 | { |
| 88 | counter_ ++; |
| 89 | return *this; |
| 90 | } |
| 91 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 92 | LoadStats & |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 93 | LoadStats::operator += (uint32_t amount) |
| 94 | { |
| 95 | counter_ += amount; |
| 96 | return *this; |
| 97 | } |
| 98 | |
| 99 | LoadStats & |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 100 | LoadStats::operator += (const LoadStats &stats) |
| 101 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 102 | // NS_LOG_FUNCTION (this << &stats << stats.counter_); |
| 103 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 104 | counter_ += stats.counter_; |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | LoadStats::stats_tuple |
| 109 | LoadStats::GetStats () const |
| 110 | { |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 111 | return stats_tuple (avg1_, avg2_, avg3_); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | bool |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 115 | LoadStats::IsZero () const |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 116 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 117 | return (counter_ == 0 && |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 118 | avg1_ < PRECISION && |
| 119 | avg2_ < PRECISION && |
| 120 | avg3_ < PRECISION); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | std::ostream & |
| 124 | operator << (std::ostream &os, const LoadStats &stats) |
| 125 | { |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 126 | os << stats.avg1_ << ", " << stats.avg2_ << ", " << stats.avg3_; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 127 | return os; |
| 128 | } |
| 129 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 130 | } // namespace ndnSIM |
| 131 | } // namespace ndn |
| 132 | } // namespace ns3 |