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 | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 33 | namespace ndnSIM |
| 34 | { |
| 35 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 36 | std::ostream & |
| 37 | operator << (std::ostream &os, const LoadStats::stats_tuple &tuple); |
| 38 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 39 | void |
| 40 | LoadStatsFace::Step () |
| 41 | { |
| 42 | m_count.Step (); |
| 43 | m_satisfied.Step (); |
| 44 | m_unsatisfied.Step (); |
| 45 | } |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 46 | |
| 47 | struct update_retval |
| 48 | { |
| 49 | update_retval (const LoadStats &count, const LoadStats &other, LoadStats::stats_tuple &tuple) |
| 50 | : count_ (count) |
| 51 | , other_ (other) |
| 52 | , tuple_ (tuple) {} |
| 53 | const LoadStats &count_; |
| 54 | const LoadStats &other_; |
| 55 | LoadStats::stats_tuple &tuple_; |
| 56 | |
| 57 | template< typename U > |
| 58 | void operator () (U) |
| 59 | { |
| 60 | if (count_.GetStats ().get<U::value> () < LoadStats::PRECISION) |
| 61 | tuple_.get<U::value> () = -1; |
| 62 | else |
| 63 | tuple_.get<U::value> () = other_.GetStats ().get<U::value> () / count_.GetStats ().get<U::value> (); |
| 64 | } |
| 65 | }; |
| 66 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 67 | LoadStats::stats_tuple |
| 68 | LoadStatsFace::GetSatisfiedRatio () const |
| 69 | { |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 70 | LoadStats::stats_tuple retval; |
| 71 | for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> > |
| 72 | (update_retval (m_count, m_satisfied, retval)); |
| 73 | |
Alexander Afanasyev | 27acad9 | 2012-07-20 15:32:20 -0700 | [diff] [blame^] | 74 | // NS_LOG_DEBUG (retval.get<0> () << ", " << retval.get<1> () << ", " << retval.get<2> ()); |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 75 | return retval; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | LoadStats::stats_tuple |
| 79 | LoadStatsFace::GetUnsatisfiedRatio () const |
| 80 | { |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 81 | LoadStats::stats_tuple retval; |
| 82 | for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> > |
| 83 | (update_retval (m_count, m_unsatisfied, retval)); |
| 84 | |
Alexander Afanasyev | 27acad9 | 2012-07-20 15:32:20 -0700 | [diff] [blame^] | 85 | // NS_LOG_DEBUG (retval.get<0> () << ", " << retval.get<1> () << ", " << retval.get<2> ()); |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 86 | return retval; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | LoadStatsFace & |
| 90 | LoadStatsFace::operator += (const LoadStatsFace &load) |
| 91 | { |
| 92 | m_count += load.m_count; |
| 93 | m_satisfied += load.m_satisfied; |
| 94 | m_unsatisfied += load.m_unsatisfied; |
| 95 | |
| 96 | return *this; |
| 97 | } |
| 98 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 99 | bool |
| 100 | LoadStatsFace::IsZero () const |
| 101 | { |
| 102 | return m_count.IsZero () && m_satisfied.IsZero () && m_unsatisfied.IsZero (); |
| 103 | } |
| 104 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 105 | struct print_tuple |
| 106 | { |
| 107 | print_tuple (const LoadStats::stats_tuple &tuple, std::ostream &os) |
| 108 | : tuple_ (tuple) |
| 109 | , os_ (os) |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | const LoadStats::stats_tuple &tuple_; |
| 114 | std::ostream &os_; |
| 115 | |
| 116 | template< typename U > |
| 117 | void operator () (U) |
| 118 | { |
| 119 | os_ << tuple_.get< U::value > () << " "; |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | |
| 124 | std::ostream & |
| 125 | operator << (std::ostream &os, const LoadStats::stats_tuple &tuple) |
| 126 | { |
| 127 | for_each< range_c<int, 0, length<LoadStats::stats_tuple>::value> > |
| 128 | (print_tuple (tuple, os)); |
| 129 | |
| 130 | return os; |
| 131 | } |
| 132 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 133 | std::ostream & |
| 134 | operator << (std::ostream &os, const LoadStatsFace &stats) |
| 135 | { |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 136 | LoadStats::stats_tuple |
| 137 | satisfied = stats.GetSatisfiedRatio (), |
| 138 | unsatisfied = stats.GetUnsatisfiedRatio (); |
| 139 | |
| 140 | os << "ration satisfied: " << satisfied << "/ unsatisfied: " << unsatisfied; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 141 | return os; |
| 142 | } |
| 143 | |
| 144 | } |