blob: 53edb6a51168389369f7b47f7d4613a7b106fced [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 Afanasyev0845c092012-07-13 17:45:33 -070033namespace ndnSIM
34{
35
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070036std::ostream &
37operator << (std::ostream &os, const LoadStats::stats_tuple &tuple);
38
Alexander Afanasyev0845c092012-07-13 17:45:33 -070039void
40LoadStatsFace::Step ()
41{
42 m_count.Step ();
43 m_satisfied.Step ();
44 m_unsatisfied.Step ();
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070045 m_tx.Step ();
46 m_rx.Step ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -070047}
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070048
49struct update_retval
50{
51 update_retval (const LoadStats &count, const LoadStats &other, LoadStats::stats_tuple &tuple)
52 : count_ (count)
53 , other_ (other)
54 , tuple_ (tuple) {}
55 const LoadStats &count_;
56 const LoadStats &other_;
57 LoadStats::stats_tuple &tuple_;
58
59 template< typename U >
60 void operator () (U)
61 {
62 if (count_.GetStats ().get<U::value> () < LoadStats::PRECISION)
63 tuple_.get<U::value> () = -1;
64 else
65 tuple_.get<U::value> () = other_.GetStats ().get<U::value> () / count_.GetStats ().get<U::value> ();
66 }
67};
68
Alexander Afanasyev0845c092012-07-13 17:45:33 -070069LoadStats::stats_tuple
70LoadStatsFace::GetSatisfiedRatio () const
71{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070072 LoadStats::stats_tuple retval;
73 for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> >
74 (update_retval (m_count, m_satisfied, retval));
75
Alexander Afanasyev27acad92012-07-20 15:32:20 -070076 // NS_LOG_DEBUG (retval.get<0> () << ", " << retval.get<1> () << ", " << retval.get<2> ());
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070077 return retval;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070078}
79
80LoadStats::stats_tuple
81LoadStatsFace::GetUnsatisfiedRatio () const
82{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070083 LoadStats::stats_tuple retval;
84 for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> >
85 (update_retval (m_count, m_unsatisfied, retval));
86
Alexander Afanasyev27acad92012-07-20 15:32:20 -070087 // NS_LOG_DEBUG (retval.get<0> () << ", " << retval.get<1> () << ", " << retval.get<2> ());
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070088 return retval;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070089}
90
91LoadStatsFace &
92LoadStatsFace::operator += (const LoadStatsFace &load)
93{
94 m_count += load.m_count;
95 m_satisfied += load.m_satisfied;
96 m_unsatisfied += load.m_unsatisfied;
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070097 m_tx += load.m_tx;
98 m_rx += load.m_rx;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070099
100 return *this;
101}
102
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700103bool
104LoadStatsFace::IsZero () const
105{
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700106 return m_count.IsZero () && m_satisfied.IsZero () && m_unsatisfied.IsZero () && m_tx.IsZero () && m_rx.IsZero ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700107}
108
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700109struct print_tuple
110{
111 print_tuple (const LoadStats::stats_tuple &tuple, std::ostream &os)
112 : tuple_ (tuple)
113 , os_ (os)
114 {
115 }
116
117 const LoadStats::stats_tuple &tuple_;
118 std::ostream &os_;
119
120 template< typename U >
121 void operator () (U)
122 {
123 os_ << tuple_.get< U::value > () << " ";
124 }
125};
126
127
128std::ostream &
129operator << (std::ostream &os, const LoadStats::stats_tuple &tuple)
130{
131 for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> >
132 (print_tuple (tuple, os));
133
134 return os;
135}
136
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700137std::ostream &
138operator << (std::ostream &os, const LoadStatsFace &stats)
139{
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700140 LoadStats::stats_tuple
141 satisfied = stats.GetSatisfiedRatio (),
142 unsatisfied = stats.GetUnsatisfiedRatio ();
143
144 os << "ration satisfied: " << satisfied << "/ unsatisfied: " << unsatisfied;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700145 return os;
146}
147
148}