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