blob: de202ee8a0fe7e973f4684fbbcebaaa6c7c168b2 [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
31NS_LOG_COMPONENT_DEFINE ("LoadStatsFace");
32
Alexander Afanasyeve77db792012-08-09 11:10:58 -070033namespace ns3
34{
Alexander Afanasyev0845c092012-07-13 17:45:33 -070035namespace ndnSIM
36{
37
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070038std::ostream &
39operator << (std::ostream &os, const LoadStats::stats_tuple &tuple);
40
Alexander Afanasyev0845c092012-07-13 17:45:33 -070041void
42LoadStatsFace::Step ()
43{
44 m_count.Step ();
45 m_satisfied.Step ();
46 m_unsatisfied.Step ();
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070047 m_tx.Step ();
48 m_rx.Step ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -070049}
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070050
51struct update_retval
52{
53 update_retval (const LoadStats &count, const LoadStats &other, LoadStats::stats_tuple &tuple)
54 : count_ (count)
55 , other_ (other)
56 , tuple_ (tuple) {}
57 const LoadStats &count_;
58 const LoadStats &other_;
59 LoadStats::stats_tuple &tuple_;
60
61 template< typename U >
62 void operator () (U)
63 {
64 if (count_.GetStats ().get<U::value> () < LoadStats::PRECISION)
65 tuple_.get<U::value> () = -1;
66 else
67 tuple_.get<U::value> () = other_.GetStats ().get<U::value> () / count_.GetStats ().get<U::value> ();
68 }
69};
70
Alexander Afanasyev0845c092012-07-13 17:45:33 -070071LoadStats::stats_tuple
72LoadStatsFace::GetSatisfiedRatio () const
73{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070074 LoadStats::stats_tuple retval;
75 for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> >
76 (update_retval (m_count, m_satisfied, retval));
77
Alexander Afanasyev27acad92012-07-20 15:32:20 -070078 // NS_LOG_DEBUG (retval.get<0> () << ", " << retval.get<1> () << ", " << retval.get<2> ());
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070079 return retval;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070080}
81
82LoadStats::stats_tuple
83LoadStatsFace::GetUnsatisfiedRatio () const
84{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070085 LoadStats::stats_tuple retval;
86 for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> >
87 (update_retval (m_count, m_unsatisfied, retval));
88
Alexander Afanasyev27acad92012-07-20 15:32:20 -070089 // NS_LOG_DEBUG (retval.get<0> () << ", " << retval.get<1> () << ", " << retval.get<2> ());
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070090 return retval;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070091}
92
93LoadStatsFace &
94LoadStatsFace::operator += (const LoadStatsFace &load)
95{
96 m_count += load.m_count;
97 m_satisfied += load.m_satisfied;
98 m_unsatisfied += load.m_unsatisfied;
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070099 m_tx += load.m_tx;
100 m_rx += load.m_rx;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700101
102 return *this;
103}
104
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700105bool
106LoadStatsFace::IsZero () const
107{
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700108 return m_count.IsZero () && m_satisfied.IsZero () && m_unsatisfied.IsZero () && m_tx.IsZero () && m_rx.IsZero ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700109}
110
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700111struct print_tuple
112{
113 print_tuple (const LoadStats::stats_tuple &tuple, std::ostream &os)
114 : tuple_ (tuple)
115 , os_ (os)
116 {
117 }
118
119 const LoadStats::stats_tuple &tuple_;
120 std::ostream &os_;
121
122 template< typename U >
123 void operator () (U)
124 {
125 os_ << tuple_.get< U::value > () << " ";
126 }
127};
128
129
130std::ostream &
131operator << (std::ostream &os, const LoadStats::stats_tuple &tuple)
132{
133 for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> >
134 (print_tuple (tuple, os));
135
136 return os;
137}
138
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700139std::ostream &
140operator << (std::ostream &os, const LoadStatsFace &stats)
141{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700142 LoadStats::stats_tuple
143 satisfied = stats.GetSatisfiedRatio (),
144 unsatisfied = stats.GetUnsatisfiedRatio ();
145
146 os << "ration satisfied: " << satisfied << "/ unsatisfied: " << unsatisfied;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700147 return os;
148}
149
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700150} // ndnSIM
151} // ns3
152