blob: df48f2baa93f54e0c2470d00d25af5edf14a173f [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-face.h"
22
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070023#include "ns3/log.h"
24
25#include <boost/mpl/for_each.hpp>
26#include <boost/mpl/range_c.hpp>
27
28using namespace boost::mpl;
29using namespace boost::tuples;
30
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070031NS_LOG_COMPONENT_DEFINE ("ndn.LoadStatsFace");
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070032
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070033namespace ns3 {
34namespace ndn {
35namespace ndnSIM {
Alexander Afanasyev0845c092012-07-13 17:45:33 -070036
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070037std::ostream &
38operator << (std::ostream &os, const LoadStats::stats_tuple &tuple);
39
Alexander Afanasyev0845c092012-07-13 17:45:33 -070040void
41LoadStatsFace::Step ()
42{
43 m_count.Step ();
44 m_satisfied.Step ();
45 m_unsatisfied.Step ();
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070046 m_tx.Step ();
47 m_rx.Step ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -070048}
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070049
50struct update_retval
51{
52 update_retval (const LoadStats &count, const LoadStats &other, LoadStats::stats_tuple &tuple)
53 : count_ (count)
54 , other_ (other)
55 , tuple_ (tuple) {}
56 const LoadStats &count_;
57 const LoadStats &other_;
58 LoadStats::stats_tuple &tuple_;
59
60 template< typename U >
61 void operator () (U)
62 {
63 if (count_.GetStats ().get<U::value> () < LoadStats::PRECISION)
64 tuple_.get<U::value> () = -1;
65 else
66 tuple_.get<U::value> () = other_.GetStats ().get<U::value> () / count_.GetStats ().get<U::value> ();
67 }
68};
69
Alexander Afanasyev0845c092012-07-13 17:45:33 -070070LoadStats::stats_tuple
71LoadStatsFace::GetSatisfiedRatio () const
72{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070073 LoadStats::stats_tuple retval;
74 for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> >
75 (update_retval (m_count, m_satisfied, retval));
76
Alexander Afanasyev27acad92012-07-20 15:32:20 -070077 // NS_LOG_DEBUG (retval.get<0> () << ", " << retval.get<1> () << ", " << retval.get<2> ());
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070078 return retval;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070079}
80
81LoadStats::stats_tuple
82LoadStatsFace::GetUnsatisfiedRatio () const
83{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070084 LoadStats::stats_tuple retval;
85 for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> >
86 (update_retval (m_count, m_unsatisfied, retval));
87
Alexander Afanasyev27acad92012-07-20 15:32:20 -070088 // NS_LOG_DEBUG (retval.get<0> () << ", " << retval.get<1> () << ", " << retval.get<2> ());
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070089 return retval;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070090}
91
92LoadStatsFace &
93LoadStatsFace::operator += (const LoadStatsFace &load)
94{
95 m_count += load.m_count;
96 m_satisfied += load.m_satisfied;
97 m_unsatisfied += load.m_unsatisfied;
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070098 m_tx += load.m_tx;
99 m_rx += load.m_rx;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700100
101 return *this;
102}
103
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700104bool
105LoadStatsFace::IsZero () const
106{
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700107 return m_count.IsZero () && m_satisfied.IsZero () && m_unsatisfied.IsZero () && m_tx.IsZero () && m_rx.IsZero ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700108}
109
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700110struct print_tuple
111{
112 print_tuple (const LoadStats::stats_tuple &tuple, std::ostream &os)
113 : tuple_ (tuple)
114 , os_ (os)
115 {
116 }
117
118 const LoadStats::stats_tuple &tuple_;
119 std::ostream &os_;
120
121 template< typename U >
122 void operator () (U)
123 {
124 os_ << tuple_.get< U::value > () << " ";
125 }
126};
127
128
129std::ostream &
130operator << (std::ostream &os, const LoadStats::stats_tuple &tuple)
131{
132 for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> >
133 (print_tuple (tuple, os));
134
135 return os;
136}
137
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700138std::ostream &
139operator << (std::ostream &os, const LoadStatsFace &stats)
140{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700141 LoadStats::stats_tuple
142 satisfied = stats.GetSatisfiedRatio (),
143 unsatisfied = stats.GetUnsatisfiedRatio ();
144
145 os << "ration satisfied: " << satisfied << "/ unsatisfied: " << unsatisfied;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700146 return os;
147}
148
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700149} // namespace ndnSIM
150} // namespace ndn
151} // namespace ns3
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700152