blob: d785845c99114ecf821e08036936a081c53730b3 [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 Afanasyev2b4c9472012-08-09 15:00:38 -070032NS_LOG_COMPONENT_DEFINE ("ndn.LoadStats");
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070033
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070034namespace ns3 {
35namespace ndn {
36namespace ndnSIM {
Alexander Afanasyev0845c092012-07-13 17:45:33 -070037
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070038const double LoadStats::PRECISION = 0.1;
39
Alexander Afanasyev0845c092012-07-13 17:45:33 -070040LoadStats::LoadStats ()
41 : counter_ (0)
42 , avg1_ (0)
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070043 , avg2_ (0)
44 , avg3_ (0)
Alexander Afanasyev0845c092012-07-13 17:45:33 -070045{
46}
47
48void
49LoadStats::Step ()
50{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070051 // NS_LOG_FUNCTION (this);
52
Alexander Afanasyev0845c092012-07-13 17:45:33 -070053 // do magic
Alexander Afanasyev92b5f832012-08-16 16:39:49 -070054 avg1_ = EXP_1 * avg1_ + (1 - EXP_1) * counter_;
Alexander Afanasyev70426a02012-08-15 15:39:18 -070055 // avg2_ = EXP_2 * avg2_ + (1 - EXP_2) * counter_;
Alexander Afanasyev70426a02012-08-15 15:39:18 -070056 // avg3_ = EXP_3 * avg3_ + (1 - EXP_3) * counter_;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070057
58 counter_ = 0;
59}
60
61LoadStats &
62LoadStats::operator ++ (int)
63{
64 counter_ ++;
65 return *this;
66}
67
Alexander Afanasyev0845c092012-07-13 17:45:33 -070068LoadStats &
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070069LoadStats::operator += (uint32_t amount)
70{
71 counter_ += amount;
72 return *this;
73}
74
75LoadStats &
Alexander Afanasyev0845c092012-07-13 17:45:33 -070076LoadStats::operator += (const LoadStats &stats)
77{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070078 // NS_LOG_FUNCTION (this << &stats << stats.counter_);
79
Alexander Afanasyev0845c092012-07-13 17:45:33 -070080 counter_ += stats.counter_;
81 return *this;
82}
83
84LoadStats::stats_tuple
85LoadStats::GetStats () const
86{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070087 return stats_tuple (avg1_, avg2_, avg3_);
Alexander Afanasyev0845c092012-07-13 17:45:33 -070088}
89
90bool
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070091LoadStats::IsZero () const
Alexander Afanasyev0845c092012-07-13 17:45:33 -070092{
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070093 return (counter_ == 0 &&
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070094 avg1_ < PRECISION &&
95 avg2_ < PRECISION &&
96 avg3_ < PRECISION);
Alexander Afanasyev0845c092012-07-13 17:45:33 -070097}
98
99std::ostream &
100operator << (std::ostream &os, const LoadStats &stats)
101{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700102 os << stats.avg1_ << ", " << stats.avg2_ << ", " << stats.avg3_;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700103 return os;
104}
105
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700106} // namespace ndnSIM
107} // namespace ndn
108} // namespace ns3