blob: 166a906bf01acc2d72264b30c2b7a72f569cd915 [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 Afanasyev0845c092012-07-13 17:45:33 -070023
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070024// 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 Afanasyev0845c092012-07-13 17:45:33 -070027
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070028const double EXP_1 = exp (-1.0/5.0); /* 1/exp(1sec/5sec) */
29const double EXP_2 = exp (-1.0/30.0); /* 1/exp(1sec/30sec) */
30const double EXP_3 = exp (-1.0/60.0); /* 1/exp(1sec/60sec) */
Alexander Afanasyev0845c092012-07-13 17:45:33 -070031
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070032NS_LOG_COMPONENT_DEFINE ("LoadStats");
33
Alexander Afanasyeve77db792012-08-09 11:10:58 -070034namespace ns3
35{
Alexander Afanasyev0845c092012-07-13 17:45:33 -070036namespace ndnSIM
37{
38
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 Afanasyeve55d1e32012-07-19 15:33:05 -070044 , avg2_ (0)
45 , avg3_ (0)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070046{
47}
48
49void
50LoadStats::Step ()
51{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070052 // NS_LOG_FUNCTION (this);
53
Alexander Afanasyev0845c092012-07-13 17:45:33 -070054 // do magic
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070055 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 Afanasyev0845c092012-07-13 17:45:33 -070058
59 counter_ = 0;
60}
61
62LoadStats &
63LoadStats::operator ++ (int)
64{
65 counter_ ++;
66 return *this;
67}
68
Alexander Afanasyev0845c092012-07-13 17:45:33 -070069LoadStats &
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070070LoadStats::operator += (uint32_t amount)
71{
72 counter_ += amount;
73 return *this;
74}
75
76LoadStats &
Alexander Afanasyev0845c092012-07-13 17:45:33 -070077LoadStats::operator += (const LoadStats &stats)
78{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070079 // NS_LOG_FUNCTION (this << &stats << stats.counter_);
80
Alexander Afanasyev0845c092012-07-13 17:45:33 -070081 counter_ += stats.counter_;
82 return *this;
83}
84
85LoadStats::stats_tuple
86LoadStats::GetStats () const
87{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070088 return stats_tuple (avg1_, avg2_, avg3_);
Alexander Afanasyev0845c092012-07-13 17:45:33 -070089}
90
91bool
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070092LoadStats::IsZero () const
Alexander Afanasyev0845c092012-07-13 17:45:33 -070093{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070094 return (counter_ == 0 &&
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070095 avg1_ < PRECISION &&
96 avg2_ < PRECISION &&
97 avg3_ < PRECISION);
Alexander Afanasyev0845c092012-07-13 17:45:33 -070098}
99
100std::ostream &
101operator << (std::ostream &os, const LoadStats &stats)
102{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700103 os << stats.avg1_ << ", " << stats.avg2_ << ", " << stats.avg3_;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700104 return os;
105}
106
107} // ndnSIM
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700108} // ns3