Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 23 | #include "ns3/log.h" |
| 24 | |
| 25 | #include <boost/mpl/for_each.hpp> |
| 26 | #include <boost/mpl/range_c.hpp> |
| 27 | |
| 28 | using namespace boost::mpl; |
| 29 | using namespace boost::tuples; |
| 30 | |
| 31 | NS_LOG_COMPONENT_DEFINE ("LoadStatsFace"); |
| 32 | |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame] | 33 | namespace ns3 |
| 34 | { |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 35 | namespace ndnSIM |
| 36 | { |
| 37 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 38 | std::ostream & |
| 39 | operator << (std::ostream &os, const LoadStats::stats_tuple &tuple); |
| 40 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 41 | void |
| 42 | LoadStatsFace::Step () |
| 43 | { |
| 44 | m_count.Step (); |
| 45 | m_satisfied.Step (); |
| 46 | m_unsatisfied.Step (); |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 47 | m_tx.Step (); |
| 48 | m_rx.Step (); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 49 | } |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 50 | |
| 51 | struct 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 Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 71 | LoadStats::stats_tuple |
| 72 | LoadStatsFace::GetSatisfiedRatio () const |
| 73 | { |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 74 | 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 Afanasyev | 27acad9 | 2012-07-20 15:32:20 -0700 | [diff] [blame] | 78 | // NS_LOG_DEBUG (retval.get<0> () << ", " << retval.get<1> () << ", " << retval.get<2> ()); |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 79 | return retval; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | LoadStats::stats_tuple |
| 83 | LoadStatsFace::GetUnsatisfiedRatio () const |
| 84 | { |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 85 | 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 Afanasyev | 27acad9 | 2012-07-20 15:32:20 -0700 | [diff] [blame] | 89 | // NS_LOG_DEBUG (retval.get<0> () << ", " << retval.get<1> () << ", " << retval.get<2> ()); |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 90 | return retval; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | LoadStatsFace & |
| 94 | LoadStatsFace::operator += (const LoadStatsFace &load) |
| 95 | { |
| 96 | m_count += load.m_count; |
| 97 | m_satisfied += load.m_satisfied; |
| 98 | m_unsatisfied += load.m_unsatisfied; |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 99 | m_tx += load.m_tx; |
| 100 | m_rx += load.m_rx; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 101 | |
| 102 | return *this; |
| 103 | } |
| 104 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 105 | bool |
| 106 | LoadStatsFace::IsZero () const |
| 107 | { |
Alexander Afanasyev | 1c0248b | 2012-07-24 15:59:50 -0700 | [diff] [blame] | 108 | return m_count.IsZero () && m_satisfied.IsZero () && m_unsatisfied.IsZero () && m_tx.IsZero () && m_rx.IsZero (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 111 | struct 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 | |
| 130 | std::ostream & |
| 131 | operator << (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 Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 139 | std::ostream & |
| 140 | operator << (std::ostream &os, const LoadStatsFace &stats) |
| 141 | { |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 142 | LoadStats::stats_tuple |
| 143 | satisfied = stats.GetSatisfiedRatio (), |
| 144 | unsatisfied = stats.GetUnsatisfiedRatio (); |
| 145 | |
| 146 | os << "ration satisfied: " << satisfied << "/ unsatisfied: " << unsatisfied; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 147 | return os; |
| 148 | } |
| 149 | |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame] | 150 | } // ndnSIM |
| 151 | } // ns3 |
| 152 | |