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 | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 24 | // const double EXP_1 = (1-2.0/6.0);//exp (-1.0/5.0); /* 1/exp(1sec/5sec) */ |
| 25 | // const double EXP_2 = (1-2.0/31.0);//exp (-1.0/30.0); /* 1/exp(1sec/30sec) */ |
| 26 | // 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] | 27 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 28 | const double EXP_1 = exp (-1.0/5.0); /* 1/exp(1sec/5sec) */ |
| 29 | const double EXP_2 = exp (-1.0/30.0); /* 1/exp(1sec/30sec) */ |
| 30 | 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] | 31 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 32 | NS_LOG_COMPONENT_DEFINE ("ndn.LoadStats"); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 34 | namespace ns3 { |
| 35 | namespace ndn { |
| 36 | namespace ndnSIM { |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 37 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 38 | const double LoadStats::PRECISION = 0.1; |
| 39 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 40 | LoadStats::LoadStats () |
| 41 | : counter_ (0) |
| 42 | , avg1_ (0) |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 43 | , avg2_ (0) |
| 44 | , avg3_ (0) |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | LoadStats::Step () |
| 50 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 51 | // NS_LOG_FUNCTION (this); |
| 52 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 53 | // do magic |
Alexander Afanasyev | 92b5f83 | 2012-08-16 16:39:49 -0700 | [diff] [blame^] | 54 | avg1_ = EXP_1 * avg1_ + (1 - EXP_1) * counter_; |
Alexander Afanasyev | 70426a0 | 2012-08-15 15:39:18 -0700 | [diff] [blame] | 55 | // avg2_ = EXP_2 * avg2_ + (1 - EXP_2) * counter_; |
Alexander Afanasyev | 70426a0 | 2012-08-15 15:39:18 -0700 | [diff] [blame] | 56 | // avg3_ = EXP_3 * avg3_ + (1 - EXP_3) * counter_; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 57 | |
| 58 | counter_ = 0; |
| 59 | } |
| 60 | |
| 61 | LoadStats & |
| 62 | LoadStats::operator ++ (int) |
| 63 | { |
| 64 | counter_ ++; |
| 65 | return *this; |
| 66 | } |
| 67 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 68 | LoadStats & |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 69 | LoadStats::operator += (uint32_t amount) |
| 70 | { |
| 71 | counter_ += amount; |
| 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | LoadStats & |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 76 | LoadStats::operator += (const LoadStats &stats) |
| 77 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 78 | // NS_LOG_FUNCTION (this << &stats << stats.counter_); |
| 79 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 80 | counter_ += stats.counter_; |
| 81 | return *this; |
| 82 | } |
| 83 | |
| 84 | LoadStats::stats_tuple |
| 85 | LoadStats::GetStats () const |
| 86 | { |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 87 | return stats_tuple (avg1_, avg2_, avg3_); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | bool |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 91 | LoadStats::IsZero () const |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 92 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 93 | return (counter_ == 0 && |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 94 | avg1_ < PRECISION && |
| 95 | avg2_ < PRECISION && |
| 96 | avg3_ < PRECISION); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | std::ostream & |
| 100 | operator << (std::ostream &os, const LoadStats &stats) |
| 101 | { |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 102 | os << stats.avg1_ << ", " << stats.avg2_ << ", " << stats.avg3_; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 103 | return os; |
| 104 | } |
| 105 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 106 | } // namespace ndnSIM |
| 107 | } // namespace ndn |
| 108 | } // namespace ns3 |