Histogram changes. Untested.
authorPedro Fortuna <pedro.fortuna@inescporto.pt>
Tue May 05 18:16:31 2009 +0100 (9 months ago)
changeset 3952c963a058058d
parent 3945 91e35f21dcd0
child 3953 04352235725f
Histogram changes. Untested.
examples/flowmon.py
src/contrib/flow-monitor/flow-monitor.cc
src/contrib/flow-monitor/flow-monitor.h
src/contrib/flow-monitor/histogram.cc
src/contrib/flow-monitor/histogram.h
     1.1 --- a/examples/flowmon.py	Thu Apr 30 19:25:52 2009 +0100
     1.2 +++ b/examples/flowmon.py	Tue May 05 18:16:31 2009 +0100
     1.3 @@ -116,6 +116,8 @@
     1.4          if st.rxPackets > 0:
     1.5              print >> os, "  Mean{Delay}: ", (st.delaySum.GetSeconds() / st.rxPackets)
     1.6              print >> os, "  Mean{Hop Count}: ", float(st.timesForwarded) / st.rxPackets + 1
     1.7 +	for i in st.delayHistogram.GetSize ():
     1.8 +	  print >> os, " Bin[", st.delayHistogram.GetBinStart (i), "-", st.delayHistogram.GetBinEnd (i), "]: ", st.delayHistogram.GetBinCount (i)
     1.9  
    1.10      monitor.CheckForLostPackets()
    1.11      classifier = flowmon_helper.GetClassifier()
     2.1 --- a/src/contrib/flow-monitor/flow-monitor.cc	Thu Apr 30 19:25:52 2009 +0100
     2.2 +++ b/src/contrib/flow-monitor/flow-monitor.cc	Tue May 05 18:16:31 2009 +0100
     2.3 @@ -21,6 +21,7 @@
     2.4  #include "flow-monitor.h"
     2.5  #include "ns3/simulator.h"
     2.6  #include "ns3/log.h"
     2.7 +#include "ns3/double.h"
     2.8  
     2.9  #define PERIODIC_CHECK_INTERVAL (Seconds (1))
    2.10  
    2.11 @@ -46,6 +47,10 @@
    2.12                     TimeValue (Seconds (0.0)),
    2.13                     MakeTimeAccessor (&FlowMonitor::Start),
    2.14                     MakeTimeChecker ())
    2.15 +    /*.AddAttribute ("HistogramBinWidth", ("The width used in the histograms."),
    2.16 +                   DoubleValue (1.0),
    2.17 +                   MakeDoubleAccessor (&FlowMonitor::m_histogramBinWidth),
    2.18 +                   MakeDoubleChecker ())*/
    2.19      ;
    2.20    return tid;
    2.21  }
    2.22 @@ -59,6 +64,7 @@
    2.23  FlowMonitor::FlowMonitor ()
    2.24   : m_enabled (false)
    2.25  {
    2.26 +  m_histogramBinWidth=DEFAULT_BIN_WIDTH;
    2.27  }
    2.28  
    2.29  
    2.30 @@ -157,7 +163,9 @@
    2.31  
    2.32    FlowStats &stats = GetStatsForFlow (flowId);
    2.33    stats.delaySum += delay;
    2.34 +  stats.delayHistogram.AddHistogramValue ((double)delay.GetSeconds());
    2.35    stats.rxBytes += packetSize;
    2.36 +  stats.packetSizeHistogram.AddHistogramValue ((double)packetSize);
    2.37    stats.rxPackets++;
    2.38    if (stats.rxPackets == 1)
    2.39      {
     3.1 --- a/src/contrib/flow-monitor/flow-monitor.h	Thu Apr 30 19:25:52 2009 +0100
     3.2 +++ b/src/contrib/flow-monitor/flow-monitor.h	Tue May 05 18:16:31 2009 +0100
     3.3 @@ -28,6 +28,7 @@
     3.4  #include "ns3/object.h"
     3.5  #include "ns3/flow-probe.h"
     3.6  #include "ns3/flow-classifier.h"
     3.7 +#include "ns3/histogram.h"
     3.8  #include "ns3/nstime.h"
     3.9  #include "ns3/event-id.h"
    3.10  
    3.11 @@ -51,6 +52,9 @@
    3.12      uint32_t rxPackets;
    3.13      uint32_t lostPackets;
    3.14      uint32_t timesForwarded; // number of times a packet was forwarded, summed for all received packets
    3.15 +    Histogram delayHistogram;
    3.16 +    Histogram jitterHistogram;
    3.17 +    Histogram packetSizeHistogram;
    3.18    };
    3.19  
    3.20    // --- basic methods ---
    3.21 @@ -65,7 +69,6 @@
    3.22  
    3.23    // --- methods to be used by the FlowMonitorProbe's only ---
    3.24    void AddProbe (Ptr<FlowProbe> probe);
    3.25 -  void AddDelayVal (double delay);
    3.26    void ReportFirstTx (Ptr<FlowProbe> probe, FlowId flowId, FlowPacketId packetId, uint32_t packetSize);
    3.27    void ReportForwarding (Ptr<FlowProbe> probe, FlowId flowId, FlowPacketId packetId, uint32_t packetSize);
    3.28    void ReportLastRx (Ptr<FlowProbe> probe, FlowId flowId, FlowPacketId packetId, uint32_t packetSize);
    3.29 @@ -106,6 +109,7 @@
    3.30    EventId m_startEvent;
    3.31    EventId m_stopEvent;
    3.32    bool m_enabled;
    3.33 +  double m_histogramBinWidth;
    3.34  
    3.35    FlowStats& GetStatsForFlow (FlowId flowId);
    3.36    void PeriodicCheckForLostPackets ();
     4.1 --- a/src/contrib/flow-monitor/histogram.cc	Thu Apr 30 19:25:52 2009 +0100
     4.2 +++ b/src/contrib/flow-monitor/histogram.cc	Tue May 05 18:16:31 2009 +0100
     4.3 @@ -15,91 +15,156 @@
     4.4  // along with this program; if not, write to the Free Software
     4.5  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     4.6  //
     4.7 -// Author: Gustavo J. A. M. Carneiro  <gjc@inescporto.pt> <gjcarneiro@gmail.com>
     4.8 +// Author: Pedro Fortuna  <pedro.fortuna@inescporto.pt> <pedro.fortuna@gmail.com>
     4.9  //
    4.10  
    4.11  #include "histogram.h"
    4.12  #include "ns3/simulator.h"
    4.13  #include "ns3/log.h"
    4.14  
    4.15 -#define DEFAULT_BIN_WIDTH	1
    4.16 -#define RESERVED_BINS_INC	10
    4.17 -
    4.18  namespace ns3 {
    4.19  
    4.20  NS_LOG_COMPONENT_DEFINE ("Histogram");
    4.21 + 
    4.22 +uint32_t 
    4.23 +Histogram::GetSize () const
    4.24 +{
    4.25 +  return m_histogram.size ();
    4.26 +}
    4.27  
    4.28 -NS_OBJECT_ENSURE_REGISTERED (Histogram);
    4.29 -  
    4.30 -void 
    4.31 -Histogram::SetBinWidth(double binWidth)
    4.32 +uint32_t 
    4.33 +Histogram::GetNBins () const
    4.34  {
    4.35 -  this->bin_width=binWidth;
    4.36 -}
    4.37 -  
    4.38 -uint32_t 
    4.39 -Histogram::GetNBins() const
    4.40 -{
    4.41 -  return this->number_bins;
    4.42 +  return m_numberBins;
    4.43  }
    4.44    
    4.45  double 
    4.46 -Histogram::GetBinStart(uint32_t index)
    4.47 +Histogram::GetBinStart (uint32_t index)
    4.48  {
    4.49 -  return (double)((index+1)*bin_width);
    4.50 +  return (double)(index * m_binWidth);
    4.51  }
    4.52 -  
    4.53 +
    4.54  double 
    4.55 -Histogram::GetBinWidth(uint32_t index) const
    4.56 +Histogram::GetBinEnd (uint32_t index)
    4.57  {
    4.58 -  return this->bin_width;
    4.59 +  return (double)((index+1)* m_binWidth);
    4.60 +}
    4.61 +
    4.62 +double 
    4.63 +Histogram::GetBinWidth (uint32_t index) const
    4.64 +{
    4.65 +  return m_binWidth;
    4.66 +}
    4.67 +
    4.68 +void 
    4.69 +Histogram::SetBinWidth (double binWidth)
    4.70 +{
    4.71 +  NS_ASSERT(!m_valueAdded); //we can only change the bin width if no values were added
    4.72 +  m_binWidth=binWidth;
    4.73  }
    4.74  
    4.75  uint32_t 
    4.76 -Histogram::GetBinCount(uint32_t index) 
    4.77 +Histogram::GetBinCount (uint32_t index) 
    4.78  {
    4.79 -  return this->histogram.size();
    4.80 +  return m_histogram[index];
    4.81  }
    4.82    
    4.83  void 
    4.84 -Histogram::AddHistogramValue(double value)
    4.85 +Histogram::AddHistogramValue (double value)
    4.86  {
    4.87 -  //check if we need to expand the vector
    4.88 -  if (histogram.size() == histogram.capacity())
    4.89 -    histogram.resize(histogram.size()+RESERVED_BINS_INC);
    4.90 +  if (!m_valueAdded) m_valueAdded=true;
    4.91 +  uint32_t index = floor(value/m_binWidth);
    4.92 +  
    4.93 +  //check if we need to resize the vector
    4.94 +  if (index >= m_histogram.size ())
    4.95 +  {
    4.96 +    //calculate the number of RESERVED_BINS_INC increments 
    4.97 +    int bin_increments=(int)(floor((index-m_histogram.size ())/RESERVED_BINS_INC)+1);
    4.98 +    m_histogram.resize(m_histogram.size ()+bin_increments*RESERVED_BINS_INC);
    4.99 +  }
   4.100  
   4.101 -  uint32_t index = floor(value/bin_width);
   4.102 -  
   4.103 -  // see
   4.104 -//  if (index+1<histogram.capacity()
   4.105 -  
   4.106 -  //uint32_t counter = histogram.at(index)++;
   4.107 -  this->histogram[index]++;
   4.108 +  if (m_histogram[index] == 0) m_numberBins++;
   4.109 +  m_histogram[index]++;
   4.110  }
   4.111    
   4.112 -TypeId 
   4.113 -Histogram::GetTypeId (void)
   4.114 +Histogram::Histogram (double binWidth)
   4.115  {
   4.116 -  static TypeId tid = TypeId ("ns3::Histogram")
   4.117 -    .SetParent<Object> ()
   4.118 -    .AddConstructor<Histogram> ()
   4.119 -    ;
   4.120 -  return tid;
   4.121 -}
   4.122 -
   4.123 -TypeId 
   4.124 -Histogram::GetInstanceTypeId (void) const
   4.125 -{
   4.126 -  return GetTypeId ();
   4.127 +  m_histogram.resize (RESERVED_BINS_INC);
   4.128 +  m_binWidth=binWidth;
   4.129 +  m_numberBins=0;
   4.130 +  m_valueAdded=false;
   4.131  }
   4.132  
   4.133  Histogram::Histogram ()
   4.134  {
   4.135 -  //no need to init Vector
   4.136 -  histogram.reserve(RESERVED_BINS_INC);
   4.137 -  bin_width=DEFAULT_BIN_WIDTH;
   4.138 -  number_bins=0;
   4.139 +  Histogram (DEFAULT_BIN_WIDTH);
   4.140  }
   4.141  
   4.142  } // namespace ns3
   4.143  
   4.144 +
   4.145 +#ifdef RUN_SELF_TESTS
   4.146 +
   4.147 +#include "ns3/test.h"
   4.148 +
   4.149 +namespace ns3 {
   4.150 +
   4.151 +class HistogramTest : public ns3::Test {
   4.152 +private:
   4.153 +public:
   4.154 +  HistogramTest ();
   4.155 +  virtual bool RunTests (void);
   4.156 +
   4.157 +
   4.158 +};
   4.159 +
   4.160 +HistogramTest::HistogramTest ()
   4.161 +  : ns3::Test ("Histogram")
   4.162 +{}
   4.163 +
   4.164 +
   4.165 +bool 
   4.166 +HistogramTest::RunTests (void)
   4.167 +{
   4.168 +  bool result = true;
   4.169 +
   4.170 +  Histogram h0(3.5);
   4.171 +  // Testing floating-point bin widths
   4.172 +  {
   4.173 +    for (int i=1; i<= 10; i++) h0.AddHistogramValue(3.4);
   4.174 +    for (int i=1; i<= 5; i++) h0.AddHistogramValue(3.6);
   4.175 +
   4.176 +    NS_TEST_ASSERT_EQUAL (h0.GetBinWidth (0),  3.5);
   4.177 +    NS_TEST_ASSERT_EQUAL (h0.GetSize (),  10);
   4.178 +    NS_TEST_ASSERT_EQUAL (h0.GetNBins (),  2);
   4.179 +    NS_TEST_ASSERT_EQUAL (h0.GetBinStart(1),  3.5);
   4.180 +    NS_TEST_ASSERT_EQUAL (h0.GetBinEnd(1),  7.0);
   4.181 +    NS_TEST_ASSERT_EQUAL (h0.GetBinCount(0),  10);
   4.182 +    NS_TEST_ASSERT_EQUAL (h0.GetBinCount(1),  5);
   4.183 +  }
   4.184 +  
   4.185 +  {
   4.186 +  // Testing bin expansion
   4.187 +    h0.AddHistogramValue(74.3);
   4.188 +
   4.189 +    NS_TEST_ASSERT_EQUAL (h0.GetSize (),  (floor(74.3/(h0.GetBinWidth (0)*RESERVED_BINS_INC))+1)*RESERVED_BINS_INC);
   4.190 +    NS_TEST_ASSERT_EQUAL (h0.GetNBins (),  3);
   4.191 +
   4.192 +    /*for (uint32_t i=0; i < h0.GetSize () ; i++)
   4.193 +    {
   4.194 +      std::cout << i << ") BinStart:" << h0.GetBinStart (i) << " BinEnd:" << ((double) h0.GetBinStart (i) + h0.GetBinWidth (i)) << " BinCount: " << h0.GetBinCount (i) << std::endl;
   4.195 +    }*/
   4.196 +    
   4.197 +    NS_TEST_ASSERT_EQUAL (h0.GetBinCount (21),  1);
   4.198 +  }
   4.199 + 
   4.200 +  return result;
   4.201 +}
   4.202 +
   4.203 +static HistogramTest gHistogramTest;
   4.204 +
   4.205 +}; // namespace
   4.206 +
   4.207 +
   4.208 +#endif /* RUN_SELF_TESTS */
   4.209 +
     5.1 --- a/src/contrib/flow-monitor/histogram.h	Thu Apr 30 19:25:52 2009 +0100
     5.2 +++ b/src/contrib/flow-monitor/histogram.h	Tue May 05 18:16:31 2009 +0100
     5.3 @@ -15,46 +15,48 @@
     5.4  // along with this program; if not, write to the Free Software
     5.5  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     5.6  //
     5.7 -// Author: Gustavo J. A. M. Carneiro  <gjc@inescporto.pt> <gjcarneiro@gmail.com>
     5.8 +// Author: Pedro Fortuna  <pedro.fortuna@inescporto.pt> <pedro.fortuna@gmail.com>
     5.9  //
    5.10  
    5.11  #ifndef __HISTOGRAM_H__
    5.12  #define __HISTOGRAM_H__
    5.13  
    5.14  #include <vector>
    5.15 -//#include <map>
    5.16 +#include <stdint.h>
    5.17 +#include <stdio.h>
    5.18  
    5.19 -#include "ns3/ptr.h"
    5.20 -#include "ns3/object.h"
    5.21 -//#include "ns3/nstime.h"
    5.22 +#define DEFAULT_BIN_WIDTH	1
    5.23 +#define RESERVED_BINS_INC	10
    5.24  
    5.25  namespace ns3 {
    5.26  
    5.27 -class Histogram : public Object
    5.28 +class Histogram
    5.29  {
    5.30  public:
    5.31  
    5.32    // --- basic methods ---
    5.33 -  static TypeId GetTypeId ();
    5.34 -  TypeId GetInstanceTypeId () const;
    5.35 +  Histogram (double binWidth);
    5.36    Histogram ();
    5.37  
    5.38 -  // Methods for Configuring the Histogram
    5.39 -  void SetBinWidth(double binWidth);
    5.40 -
    5.41    // Methods for Getting the Histogram Results
    5.42 -  uint32_t GetNBins() const;
    5.43 -  double GetBinStart(uint32_t index);
    5.44 -  double GetBinWidth(uint32_t index) const;
    5.45 -  uint32_t GetBinCount(uint32_t index);
    5.46 +  uint32_t GetNBins () const;
    5.47 +  uint32_t GetSize () const;
    5.48 +  double GetBinStart (uint32_t index);
    5.49 +  double GetBinEnd (uint32_t index);
    5.50 +  double GetBinWidth (uint32_t index) const;
    5.51 +  void SetBinWidth (double binWidth);
    5.52 +  uint32_t GetBinCount (uint32_t index);
    5.53    
    5.54    // Method for adding values
    5.55 -  void AddHistogramValue(double value);
    5.56 +  void AddHistogramValue (double value);
    5.57    
    5.58  private:
    5.59 -  std::vector<uint32_t> histogram;
    5.60 -  double bin_width;
    5.61 -  uint32_t number_bins;
    5.62 +  std::vector<uint32_t> m_histogram;
    5.63 +  double m_binWidth;
    5.64 +  uint32_t m_numberBins;
    5.65 +
    5.66 +protected:
    5.67 +  bool m_valueAdded;
    5.68  
    5.69  };
    5.70