Cleanup and simplifications
authorGustavo J. A. M. Carneiro <gjc@inescporto.pt>
Thu May 07 15:40:59 2009 +0100 (9 months ago)
changeset 3969a7a41ad7f52e
parent 3968 d767b88155a7
child 3970 5eadf28ea81b
Cleanup and simplifications
bindings/python/ns3_module_flow_monitor.py
examples/flowmon.py
src/contrib/flow-monitor/flow-monitor.cc
src/contrib/flow-monitor/histogram.cc
src/contrib/flow-monitor/histogram.h
     1.1 --- a/bindings/python/ns3_module_flow_monitor.py	Thu May 07 12:27:24 2009 +0100
     1.2 +++ b/bindings/python/ns3_module_flow_monitor.py	Thu May 07 15:40:59 2009 +0100
     1.3 @@ -92,11 +92,6 @@
     1.4                     'uint32_t', 
     1.5                     [], 
     1.6                     is_const=True)
     1.7 -    ## histogram.h: uint32_t ns3::Histogram::GetSize() const [member function]
     1.8 -    cls.add_method('GetSize', 
     1.9 -                   'uint32_t', 
    1.10 -                   [], 
    1.11 -                   is_const=True)
    1.12      ## histogram.h: double ns3::Histogram::GetBinStart(uint32_t index) [member function]
    1.13      cls.add_method('GetBinStart', 
    1.14                     'double', 
    1.15 @@ -110,16 +105,16 @@
    1.16                     'double', 
    1.17                     [param('uint32_t', 'index')], 
    1.18                     is_const=True)
    1.19 -    ## histogram.h: void ns3::Histogram::SetBinWidth(double binWidth) [member function]
    1.20 -    cls.add_method('SetBinWidth', 
    1.21 +    ## histogram.h: void ns3::Histogram::SetDefaultBinWidth(double binWidth) [member function]
    1.22 +    cls.add_method('SetDefaultBinWidth', 
    1.23                     'void', 
    1.24                     [param('double', 'binWidth')])
    1.25      ## histogram.h: uint32_t ns3::Histogram::GetBinCount(uint32_t index) [member function]
    1.26      cls.add_method('GetBinCount', 
    1.27                     'uint32_t', 
    1.28                     [param('uint32_t', 'index')])
    1.29 -    ## histogram.h: void ns3::Histogram::AddHistogramValue(double value) [member function]
    1.30 -    cls.add_method('AddHistogramValue', 
    1.31 +    ## histogram.h: void ns3::Histogram::AddValue(double value) [member function]
    1.32 +    cls.add_method('AddValue', 
    1.33                     'void', 
    1.34                     [param('double', 'value')])
    1.35      return
     2.1 --- a/examples/flowmon.py	Thu May 07 12:27:24 2009 +0100
     2.2 +++ b/examples/flowmon.py	Thu May 07 15:40:59 2009 +0100
     2.3 @@ -121,17 +121,17 @@
     2.4  	    print >> os, "  Mean{Jitter}: ", (st.jitterSum.GetSeconds() / (st.rxPackets-1))
     2.5              print >> os, "  Mean{Hop Count}: ", float(st.timesForwarded) / st.rxPackets + 1
     2.6  
     2.7 -        if 0:
     2.8 +        if 1:
     2.9              print >> os, "Delay Histogram"
    2.10 -            for i in range(st.delayHistogram.GetSize () ):
    2.11 +            for i in range(st.delayHistogram.GetNBins () ):
    2.12                print >> os, " ",i,"(", st.delayHistogram.GetBinStart (i), "-", \
    2.13                    st.delayHistogram.GetBinEnd (i), "): ", st.delayHistogram.GetBinCount (i)
    2.14              print >> os, "Jitter Histogram"
    2.15 -            for i in range(st.jitterHistogram.GetSize () ):
    2.16 +            for i in range(st.jitterHistogram.GetNBins () ):
    2.17                print >> os, " ",i,"(", st.jitterHistogram.GetBinStart (i), "-", \
    2.18                    st.jitterHistogram.GetBinEnd (i), "): ", st.jitterHistogram.GetBinCount (i)
    2.19              print >> os, "PacketSize Histogram"
    2.20 -            for i in range(st.packetSizeHistogram.GetSize () ):
    2.21 +            for i in range(st.packetSizeHistogram.GetNBins () ):
    2.22                print >> os, " ",i,"(", st.packetSizeHistogram.GetBinStart (i), "-", \
    2.23                    st.packetSizeHistogram.GetBinEnd (i), "): ", st.packetSizeHistogram.GetBinCount (i)
    2.24  
     3.1 --- a/src/contrib/flow-monitor/flow-monitor.cc	Thu May 07 12:27:24 2009 +0100
     3.2 +++ b/src/contrib/flow-monitor/flow-monitor.cc	Thu May 07 15:40:59 2009 +0100
     3.3 @@ -93,9 +93,9 @@
     3.4        ref.rxPackets = 0;
     3.5        ref.lostPackets = 0;
     3.6        ref.timesForwarded = 0;
     3.7 -      ref.delayHistogram.SetBinWidth(m_delayBinWidth);
     3.8 -      ref.jitterHistogram.SetBinWidth(m_jitterBinWidth);
     3.9 -      ref.packetSizeHistogram.SetBinWidth(m_packetSizeBinWidth);
    3.10 +      ref.delayHistogram.SetDefaultBinWidth (m_delayBinWidth);
    3.11 +      ref.jitterHistogram.SetDefaultBinWidth (m_jitterBinWidth);
    3.12 +      ref.packetSizeHistogram.SetDefaultBinWidth (m_packetSizeBinWidth);
    3.13        return ref;
    3.14      }
    3.15    else
    3.16 @@ -178,25 +178,25 @@
    3.17  
    3.18    FlowStats &stats = GetStatsForFlow (flowId);
    3.19    stats.delaySum += delay;
    3.20 -  stats.delayHistogram.AddHistogramValue ((double)delay.GetSeconds());
    3.21 +  stats.delayHistogram.AddValue (delay.GetSeconds ());
    3.22    if (stats.rxPackets > 0 )
    3.23      {
    3.24        Time jitter = stats.lastDelay - delay;
    3.25        if (jitter > Seconds (0))
    3.26  	{
    3.27        	  stats.jitterSum += jitter;
    3.28 -	  stats.jitterHistogram.AddHistogramValue ((double)jitter.GetSeconds());
    3.29 +	  stats.jitterHistogram.AddValue (jitter.GetSeconds ());
    3.30  	}
    3.31        else 
    3.32  	{
    3.33  	  stats.jitterSum -= jitter;
    3.34 -	  stats.jitterHistogram.AddHistogramValue (-(double)jitter.GetSeconds());
    3.35 +	  stats.jitterHistogram.AddValue (-jitter.GetSeconds());
    3.36  	}
    3.37      }
    3.38    stats.lastDelay = delay;
    3.39    
    3.40    stats.rxBytes += packetSize;
    3.41 -  stats.packetSizeHistogram.AddHistogramValue ((double)packetSize);
    3.42 +  stats.packetSizeHistogram.AddValue ((double) packetSize);
    3.43    stats.rxPackets++;
    3.44    if (stats.rxPackets == 1)
    3.45      {
     4.1 --- a/src/contrib/flow-monitor/histogram.cc	Thu May 07 12:27:24 2009 +0100
     4.2 +++ b/src/contrib/flow-monitor/histogram.cc	Thu May 07 15:40:59 2009 +0100
     4.3 @@ -18,36 +18,41 @@
     4.4  // Author: Pedro Fortuna  <pedro.fortuna@inescporto.pt> <pedro.fortuna@gmail.com>
     4.5  //
     4.6  
     4.7 +#include <math.h>
     4.8  #include "histogram.h"
     4.9  #include "ns3/simulator.h"
    4.10  #include "ns3/log.h"
    4.11  
    4.12 +#define DEFAULT_BIN_WIDTH	1
    4.13 +// #define RESERVED_BINS_INC	10
    4.14 +
    4.15 +
    4.16  namespace ns3 {
    4.17  
    4.18  NS_LOG_COMPONENT_DEFINE ("Histogram");
    4.19   
    4.20 -uint32_t 
    4.21 -Histogram::GetSize () const
    4.22 -{
    4.23 -  return m_histogram.size ();
    4.24 -}
    4.25 +// uint32_t 
    4.26 +// Histogram::GetSize () const
    4.27 +// {
    4.28 +//   return m_histogram.size ();
    4.29 +// }
    4.30  
    4.31  uint32_t 
    4.32  Histogram::GetNBins () const
    4.33  {
    4.34 -  return m_numberBins;
    4.35 +  return m_histogram.size ();
    4.36  }
    4.37    
    4.38  double 
    4.39  Histogram::GetBinStart (uint32_t index)
    4.40  {
    4.41 -  return (double)(index * m_binWidth);
    4.42 +  return index*m_binWidth;
    4.43  }
    4.44  
    4.45  double 
    4.46  Histogram::GetBinEnd (uint32_t index)
    4.47  {
    4.48 -  return (double)((index+1)* m_binWidth);
    4.49 +  return (index + 1) * m_binWidth;
    4.50  }
    4.51  
    4.52  double 
    4.53 @@ -57,44 +62,37 @@
    4.54  }
    4.55  
    4.56  void 
    4.57 -Histogram::SetBinWidth (double binWidth)
    4.58 +Histogram::SetDefaultBinWidth (double binWidth)
    4.59  {
    4.60 -  NS_ASSERT(!m_valueAdded); //we can only change the bin width if no values were added
    4.61 -  m_binWidth=binWidth;
    4.62 +  NS_ASSERT (m_histogram.size () == 0); //we can only change the bin width if no values were added
    4.63 +  m_binWidth = binWidth;
    4.64  }
    4.65  
    4.66  uint32_t 
    4.67  Histogram::GetBinCount (uint32_t index) 
    4.68  {
    4.69 +  NS_ASSERT (index < m_histogram.size ());
    4.70    return m_histogram[index];
    4.71  }
    4.72    
    4.73  void 
    4.74 -Histogram::AddHistogramValue (double value)
    4.75 +Histogram::AddValue (double value)
    4.76  {
    4.77 -  if (!m_valueAdded) m_valueAdded=true;
    4.78 -  uint32_t index = floor(value/m_binWidth);
    4.79 +  uint32_t index = floor (value/m_binWidth);
    4.80    
    4.81    //check if we need to resize the vector
    4.82 +  NS_LOG_DEBUG ("AddValue: index=" << index << ", m_histogram.size()=" << m_histogram.size ());
    4.83 +  
    4.84    if (index >= m_histogram.size ())
    4.85 -  {
    4.86 -    //calculate the number of RESERVED_BINS_INC increments 
    4.87 -    int bin_increments=(int)(floor((index-m_histogram.size ())/RESERVED_BINS_INC)+1);
    4.88 -    m_histogram.resize(m_histogram.size ()+bin_increments*RESERVED_BINS_INC);
    4.89 -  }
    4.90 -
    4.91 -  if (m_histogram[index] == 0) m_numberBins++;
    4.92 -  /*std::cout << "vou adicionar o valor " << value << " no index " << index << std::endl;
    4.93 -  std::cout << "o GetBinWidth= " << GetBinWidth(0) << std::endl;*/
    4.94 +    {
    4.95 +      m_histogram.resize (index + 1, 0);
    4.96 +    }
    4.97    m_histogram[index]++;
    4.98  }
    4.99    
   4.100  Histogram::Histogram (double binWidth)
   4.101  {
   4.102 -  m_histogram.resize (RESERVED_BINS_INC);
   4.103 -  m_binWidth=binWidth;
   4.104 -  m_numberBins=0;
   4.105 -  m_valueAdded=false;
   4.106 +  m_binWidth = binWidth;
   4.107  }
   4.108  
   4.109  Histogram::Histogram ()
   4.110 @@ -133,24 +131,21 @@
   4.111    Histogram h0(3.5);
   4.112    // Testing floating-point bin widths
   4.113    {
   4.114 -    for (int i=1; i<= 10; i++) h0.AddHistogramValue(3.4);
   4.115 -    for (int i=1; i<= 5; i++) h0.AddHistogramValue(3.6);
   4.116 +    for (int i=1; i<= 10; i++) h0.AddValue(3.4);
   4.117 +    for (int i=1; i<= 5; i++) h0.AddValue(3.6);
   4.118  
   4.119      NS_TEST_ASSERT_EQUAL (h0.GetBinWidth (0),  3.5);
   4.120 -    NS_TEST_ASSERT_EQUAL (h0.GetSize (),  10);
   4.121      NS_TEST_ASSERT_EQUAL (h0.GetNBins (),  2);
   4.122      NS_TEST_ASSERT_EQUAL (h0.GetBinStart(1),  3.5);
   4.123 -    NS_TEST_ASSERT_EQUAL (h0.GetBinEnd(1),  7.0);
   4.124      NS_TEST_ASSERT_EQUAL (h0.GetBinCount(0),  10);
   4.125      NS_TEST_ASSERT_EQUAL (h0.GetBinCount(1),  5);
   4.126    }
   4.127    
   4.128    {
   4.129    // Testing bin expansion
   4.130 -    h0.AddHistogramValue(74.3);
   4.131 +    h0.AddValue(74.3);
   4.132  
   4.133 -    NS_TEST_ASSERT_EQUAL (h0.GetSize (),  (floor(74.3/(h0.GetBinWidth (0)*RESERVED_BINS_INC))+1)*RESERVED_BINS_INC);
   4.134 -    NS_TEST_ASSERT_EQUAL (h0.GetNBins (),  3);
   4.135 +    NS_TEST_ASSERT_EQUAL (h0.GetNBins (),  22);
   4.136  
   4.137      /*for (uint32_t i=0; i < h0.GetSize () ; i++)
   4.138      {
     5.1 --- a/src/contrib/flow-monitor/histogram.h	Thu May 07 12:27:24 2009 +0100
     5.2 +++ b/src/contrib/flow-monitor/histogram.h	Thu May 07 15:40:59 2009 +0100
     5.3 @@ -18,15 +18,11 @@
     5.4  // Author: Pedro Fortuna  <pedro.fortuna@inescporto.pt> <pedro.fortuna@gmail.com>
     5.5  //
     5.6  
     5.7 -#ifndef __HISTOGRAM_H__
     5.8 -#define __HISTOGRAM_H__
     5.9 +#ifndef __NS3_HISTOGRAM_H__
    5.10 +#define __NS3_HISTOGRAM_H__
    5.11  
    5.12  #include <vector>
    5.13  #include <stdint.h>
    5.14 -#include <stdio.h>
    5.15 -
    5.16 -#define DEFAULT_BIN_WIDTH	1
    5.17 -#define RESERVED_BINS_INC	10
    5.18  
    5.19  namespace ns3 {
    5.20  
    5.21 @@ -40,24 +36,18 @@
    5.22  
    5.23    // Methods for Getting the Histogram Results
    5.24    uint32_t GetNBins () const;
    5.25 -  uint32_t GetSize () const;
    5.26    double GetBinStart (uint32_t index);
    5.27    double GetBinEnd (uint32_t index);
    5.28    double GetBinWidth (uint32_t index) const;
    5.29 -  void SetBinWidth (double binWidth);
    5.30 +  void SetDefaultBinWidth (double binWidth);
    5.31    uint32_t GetBinCount (uint32_t index);
    5.32    
    5.33    // Method for adding values
    5.34 -  void AddHistogramValue (double value);
    5.35 +  void AddValue (double value);
    5.36    
    5.37  private:
    5.38    std::vector<uint32_t> m_histogram;
    5.39    double m_binWidth;
    5.40 -  uint32_t m_numberBins;
    5.41 -
    5.42 -protected:
    5.43 -  bool m_valueAdded;
    5.44 -
    5.45  };
    5.46  
    5.47