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 | |
| 26 | const double EXP_1 = exp (-5.0/60.0); /* 1/exp(5sec/1min) */ |
| 27 | const double EXP_5 = exp (-5.0/300.0); /* 1/exp(5sec/5min) */ |
| 28 | const double EXP_15 = exp (-5.0/900.0); /* 1/exp(5sec/15min) */ |
| 29 | |
| 30 | const double EPSILON = 0.000001; |
| 31 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 32 | NS_LOG_COMPONENT_DEFINE ("LoadStats"); |
| 33 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 34 | namespace ndnSIM |
| 35 | { |
| 36 | |
| 37 | LoadStats::LoadStats () |
| 38 | : counter_ (0) |
| 39 | , avg1_ (0) |
| 40 | , avg5_ (0) |
| 41 | , avg15_ (0) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | LoadStats::Step () |
| 47 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 48 | // NS_LOG_FUNCTION (this); |
| 49 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 50 | // 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 | |
| 58 | LoadStats & |
| 59 | LoadStats::operator ++ (int) |
| 60 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 61 | // NS_LOG_FUNCTION (this); |
| 62 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 63 | counter_ ++; |
| 64 | return *this; |
| 65 | } |
| 66 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 67 | LoadStats & |
| 68 | LoadStats::operator += (const LoadStats &stats) |
| 69 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 70 | // NS_LOG_FUNCTION (this << &stats << stats.counter_); |
| 71 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 72 | counter_ += stats.counter_; |
| 73 | return *this; |
| 74 | } |
| 75 | |
| 76 | LoadStats::stats_tuple |
| 77 | LoadStats::GetStats () const |
| 78 | { |
| 79 | return stats_tuple (avg1_, avg5_, avg15_); |
| 80 | } |
| 81 | |
| 82 | bool |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 83 | LoadStats::IsZero () const |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 84 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 85 | return (counter_ == 0 && |
| 86 | avg1_ < EPSILON && |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 87 | avg5_ < EPSILON && |
| 88 | avg15_ < EPSILON); |
| 89 | } |
| 90 | |
| 91 | std::ostream & |
| 92 | operator << (std::ostream &os, const LoadStats &stats) |
| 93 | { |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 94 | os << stats.avg1_ << ", " << stats.avg5_ << ", " << stats.avg15_; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 95 | return os; |
| 96 | } |
| 97 | |
| 98 | } // ndnSIM |