View | Details | Raw Unified | Return to bug 1998
Collapse All | Expand All

(-)a/doc/tutorial/source/data-collection.rst (+2 lines)
 Lines 359-364    Link Here 
359
  +------------------+-------------------+------------------------------------+
359
  +------------------+-------------------+------------------------------------+
360
  | bool             | BooleanProbe      | stats/model/uinteger-16-probe.h    |
360
  | bool             | BooleanProbe      | stats/model/uinteger-16-probe.h    |
361
  +------------------+-------------------+------------------------------------+
361
  +------------------+-------------------+------------------------------------+
362
  | ns3::Time        | TimeProbe         | stats/model/time-probe.h           |
363
  +------------------+-------------------+------------------------------------+
362
364
363
The following TraceSource types are supported by Probes as of this writing:
365
The following TraceSource types are supported by Probes as of this writing:
364
366
(-)a/src/stats/doc/data-collection-helpers.rst (+1 lines)
 Lines 605-610    Link Here 
605
- Uinteger8Probe
605
- Uinteger8Probe
606
- Uinteger16Probe
606
- Uinteger16Probe
607
- Uinteger32Probe
607
- Uinteger32Probe
608
- TimeProbe
608
- PacketProbe
609
- PacketProbe
609
- ApplicationPacketProbe
610
- ApplicationPacketProbe
610
- Ipv4PacketProbe
611
- Ipv4PacketProbe
(-)a/src/stats/doc/scope-and-limitations.rst (+1 lines)
 Lines 18-23    Link Here 
18
- Uinteger8Probe
18
- Uinteger8Probe
19
- Uinteger16Probe
19
- Uinteger16Probe
20
- Uinteger32Probe
20
- Uinteger32Probe
21
- TimeProbe
21
- PacketProbe
22
- PacketProbe
22
- ApplicationPacketProbe
23
- ApplicationPacketProbe
23
- Ipv4PacketProbe
24
- Ipv4PacketProbe
(-)24bd6ec8ad51 (+247 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2014 University of Washington
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
19
//
20
// This example is designed to show the main features of an ns3::TimeProbe.
21
// A test object is used to emit values through a trace source.  The 
22
// example shows three ways to use a ns3::TimeProbe to hook the output
23
// of this trace source (in addition to hooking the raw trace source).
24
//
25
// It produces two types of output.  By default, it will generate a
26
// gnuplot of interarrival times.  If the '--verbose=1' argument is passed,
27
// it will also generate debugging output of the form (for example):
28
//
29
//     Emitting at 96.5378 seconds
30
//     context: raw trace source old 0.293343 new 0.00760254
31
//     context: probe1 old 0.293343 new 0.00760254
32
//     context: probe2 old 0.293343 new 0.00760254
33
//     context: probe3 old 0.293343 new 0.00760254
34
//
35
// The stopTime defaults to 100 seconds but can be changed by an argument.
36
//
37
38
#include <string>
39
40
#include "ns3/core-module.h"
41
#include "ns3/time-probe.h"
42
#include "ns3/gnuplot-helper.h"
43
44
using namespace ns3;
45
46
NS_LOG_COMPONENT_DEFINE ("TimeProbeExample");
47
48
//
49
// This is our test object, an object that emits values according to
50
// a Poisson arrival process.   It emits a traced Time value as a 
51
// trace source; this takes the value of interarrival time
52
//
53
class Emitter : public Object
54
{
55
public:
56
  static TypeId GetTypeId (void);
57
  Emitter ();
58
private:
59
  void DoInitialize (void);
60
//  void Emit (void);
61
  void Emit (void);
62
63
  TracedValue<Time> m_interval;  
64
  Time m_last;  
65
  Ptr<ExponentialRandomVariable> m_var;
66
};
67
68
NS_OBJECT_ENSURE_REGISTERED (Emitter);
69
70
TypeId
71
Emitter::GetTypeId (void)
72
{
73
  static TypeId tid = TypeId ("ns3::Emitter")
74
    .AddConstructor<Emitter> ()
75
    .SetParent<Object> ()
76
    .AddTraceSource ("Interval",
77
                     "Trace source",
78
                     MakeTraceSourceAccessor (&Emitter::m_interval))
79
  ;
80
  return tid;
81
}
82
83
Emitter::Emitter (void)
84
  : m_interval (Seconds (0)),
85
    m_last (Seconds (0))
86
{
87
  m_var = CreateObject<ExponentialRandomVariable> ();
88
}
89
90
void
91
Emitter::DoInitialize (void)
92
{
93
  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
94
}
95
96
void
97
Emitter::Emit (void)
98
{
99
  NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().GetSeconds () << " seconds");
100
  m_interval = Simulator::Now () - m_last;
101
  m_last = Simulator::Now ();
102
  TimeProbe::SetValueByPath ("/Names/probe3", m_interval);
103
  Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
104
}
105
106
// This is a function to test hooking a raw function to the trace source
107
void
108
NotifyViaTraceSource (std::string context, Time oldVal, Time newVal)
109
{
110
  BooleanValue verbose;
111
  GlobalValue::GetValueByName ("verbose", verbose);
112
  if (verbose.Get ())
113
    {
114
      std::cout << "context: " << context << " old " << oldVal.GetSeconds () << " new " << newVal.GetSeconds () << std::endl;
115
    }
116
}
117
118
// This is a function to test hooking it to the probe output
119
void
120
NotifyViaProbe (std::string context, double oldVal, double newVal)
121
{
122
  BooleanValue verbose;
123
  GlobalValue::GetValueByName ("verbose", verbose);
124
  if (verbose.Get ())
125
    {
126
      std::cout << "context: " << context << " old " << oldVal << " new " << newVal << std::endl;
127
    }
128
}
129
130
static ns3::GlobalValue g_verbose ("verbose",
131
                                   "Whether to enable verbose output",
132
                                   ns3::BooleanValue (false),
133
                                   ns3::MakeBooleanChecker ());
134
135
int main (int argc, char *argv[])
136
{
137
  double stopTime = 100.0;
138
  bool verbose = false;
139
140
  CommandLine cmd;
141
  cmd.AddValue ("stopTime", "Time (seconds) to terminate simulation", stopTime);
142
  cmd.AddValue ("verbose", "Whether to enable verbose output", verbose);
143
  cmd.Parse (argc, argv);
144
  bool connected;
145
146
  // Set a global value, so that the callbacks can access it
147
  if (verbose)
148
    {
149
      GlobalValue::Bind ("verbose", BooleanValue (true));
150
      LogComponentEnable ("TimeProbeExample", LOG_LEVEL_ALL);
151
    }
152
153
  Ptr<Emitter> emitter = CreateObject<Emitter> ();
154
  Names::Add ("/Names/Emitter", emitter);
155
156
  //
157
  // The below shows typical functionality without a probe
158
  // (connect a sink function to a trace source)
159
  //
160
  connected = emitter->TraceConnect ("Interval", "raw trace source", MakeCallback (&NotifyViaTraceSource));
161
  NS_ASSERT_MSG (connected, "Trace source not connected");
162
163
  //
164
  // Next, we'll show several use cases of using a Probe to access and
165
  // filter the values of the underlying trace source
166
  //
167
168
  //
169
  // Probe1 will be hooked directly to the Emitter trace source object
170
  //
171
172
  // probe1 will be hooked to the Emitter trace source
173
  Ptr<TimeProbe> probe1 = CreateObject<TimeProbe> ();
174
  // the probe's name can serve as its context in the tracing
175
  probe1->SetName ("probe1");
176
177
  // Connect the probe to the emitter's Interval
178
  connected = probe1->ConnectByObject ("Interval", emitter);
179
  NS_ASSERT_MSG (connected, "Trace source not connected to probe1");
180
181
  // The probe itself should generate output.  The context that we provide
182
  // to this probe (in this case, the probe name) will help to disambiguate
183
  // the source of the trace
184
  connected = probe1->TraceConnect ("Output", probe1->GetName (), MakeCallback (&NotifyViaProbe));
185
  NS_ASSERT_MSG (connected, "Trace source not connected to probe1 Output");
186
187
  //
188
  // Probe2 will be hooked to the Emitter trace source object by
189
  // accessing it by path name in the Config database
190
  //
191
192
  // Create another similar probe; this will hook up via a Config path
193
  Ptr<TimeProbe> probe2 = CreateObject<TimeProbe> ();
194
  probe2->SetName ("probe2");
195
196
  // Note, no return value is checked here
197
  probe2->ConnectByPath ("/Names/Emitter/Interval");
198
199
  // The probe itself should generate output.  The context that we provide
200
  // to this probe (in this case, the probe name) will help to disambiguate
201
  // the source of the trace
202
  connected = probe2->TraceConnect ("Output", "probe2", MakeCallback (&NotifyViaProbe));
203
  NS_ASSERT_MSG (connected, "Trace source not connected to probe2 Output");
204
205
  //
206
  // Probe3 will be called by the emitter directly through the
207
  // static method SetValueByPath().
208
  //
209
  Ptr<TimeProbe> probe3 = CreateObject<TimeProbe> ();
210
  probe3->SetName ("probe3");
211
212
  // By adding to the config database, we can access it later
213
  Names::Add ("/Names/probe3", probe3);
214
215
  // The probe itself should generate output.  The context that we provide
216
  // to this probe (in this case, the probe name) will help to disambiguate
217
  // the source of the trace
218
  connected = probe3->TraceConnect ("Output", "probe3", MakeCallback (&NotifyViaProbe));
219
  NS_ASSERT_MSG (connected, "Trace source not connected to probe3 Output");
220
221
  // Plot the interval values
222
  GnuplotHelper plotHelper;
223
  plotHelper.ConfigurePlot ("time-probe-example",
224
                            "Emitter interarrivals vs. Time",
225
                            "Simulation time (Seconds)",
226
                            "Interarrival time (Seconds)",
227
                            "png");
228
229
  // Helper creates a TimeProbe and hooks it to the /Names/Emitter/Interval
230
  // source.  Helper also takes the Output of the TimeProbe and plots it
231
  // as a dataset labeled 'Emitter Interarrival Time'
232
  plotHelper.PlotProbe ("ns3::TimeProbe",
233
                        "/Names/Emitter/Interval",
234
                        "Output",
235
                        "Emitter Interarrival Time",
236
                        GnuplotAggregator::KEY_INSIDE);
237
238
  // The Emitter object is not associated with an ns-3 node, so
239
  // it won't get started automatically, so we need to do this ourselves
240
  Simulator::Schedule (Seconds (0.0), &Emitter::Initialize, emitter);
241
242
  Simulator::Stop (Seconds (stopTime));
243
  Simulator::Run ();
244
  Simulator::Destroy ();
245
246
  return 0;
247
}
(-)a/src/stats/examples/wscript (+3 lines)
 Lines 10-15    Link Here 
10
    program = bld.create_ns3_program('double-probe-example', ['network', 'stats'])
10
    program = bld.create_ns3_program('double-probe-example', ['network', 'stats'])
11
    program.source = 'double-probe-example.cc'
11
    program.source = 'double-probe-example.cc'
12
12
13
    program = bld.create_ns3_program('time-probe-example', ['stats'])
14
    program.source = 'time-probe-example.cc'
15
13
    program = bld.create_ns3_program('gnuplot-aggregator-example', ['network', 'stats'])
16
    program = bld.create_ns3_program('gnuplot-aggregator-example', ['network', 'stats'])
14
    program.source = 'gnuplot-aggregator-example.cc'
17
    program.source = 'gnuplot-aggregator-example.cc'
15
18
(-)a/src/stats/helper/file-helper.cc (+7 lines)
 Lines 543-548    Link Here 
543
        MakeCallback (&TimeSeriesAdaptor::TraceSinkUinteger32,
543
        MakeCallback (&TimeSeriesAdaptor::TraceSinkUinteger32,
544
                      m_timeSeriesAdaptorMap[probeContext]));
544
                      m_timeSeriesAdaptorMap[probeContext]));
545
    }
545
    }
546
  else if (m_probeMap[probeName].second == "ns3::TimeProbe")
547
    {
548
      m_probeMap[probeName].first->TraceConnectWithoutContext
549
        (probeTraceSource,
550
        MakeCallback (&TimeSeriesAdaptor::TraceSinkDouble,
551
                      m_timeSeriesAdaptorMap[probeContext]));
552
    }
546
  else
553
  else
547
    {
554
    {
548
      NS_FATAL_ERROR ("Unknown probe type " << m_probeMap[probeName].second << "; need to add support in the helper for this");
555
      NS_FATAL_ERROR ("Unknown probe type " << m_probeMap[probeName].second << "; need to add support in the helper for this");
(-)a/src/stats/helper/gnuplot-helper.cc (+7 lines)
 Lines 403-408    Link Here 
403
        MakeCallback (&TimeSeriesAdaptor::TraceSinkUinteger32,
403
        MakeCallback (&TimeSeriesAdaptor::TraceSinkUinteger32,
404
                      m_timeSeriesAdaptorMap[probeContext]));
404
                      m_timeSeriesAdaptorMap[probeContext]));
405
    }
405
    }
406
  else if (m_probeMap[probeName].second == "ns3::TimeProbe")
407
    {
408
      m_probeMap[probeName].first->TraceConnectWithoutContext
409
        (probeTraceSource,
410
        MakeCallback (&TimeSeriesAdaptor::TraceSinkDouble,
411
                      m_timeSeriesAdaptorMap[probeContext]));
412
    }
406
  else
413
  else
407
    {
414
    {
408
      NS_FATAL_ERROR ("Unknown probe type " << m_probeMap[probeName].second << "; need to add support in the helper for this");
415
      NS_FATAL_ERROR ("Unknown probe type " << m_probeMap[probeName].second << "; need to add support in the helper for this");
(-)24bd6ec8ad51 (+111 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2011 Bucknell University
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
 * Authors: L. Felipe Perrone (perrone@bucknell.edu)
19
 *          Tiago G. Rodrigues (tgr002@bucknell.edu)
20
 *
21
 * Modified by: Mitch Watrous (watrous@u.washington.edu)
22
 *
23
 */
24
25
#include "ns3/time-probe.h"
26
#include "ns3/object.h"
27
#include "ns3/log.h"
28
#include "ns3/names.h"
29
#include "ns3/config.h"
30
#include "ns3/trace-source-accessor.h"
31
32
namespace ns3 {
33
34
NS_LOG_COMPONENT_DEFINE ("TimeProbe");
35
36
NS_OBJECT_ENSURE_REGISTERED (TimeProbe);
37
38
TypeId
39
TimeProbe::GetTypeId ()
40
{
41
  static TypeId tid = TypeId ("ns3::TimeProbe")
42
    .SetParent<Probe> ()
43
    .AddConstructor<TimeProbe> ()
44
    .AddTraceSource ("Output",
45
                     "The double valued (units of seconds) probe output",
46
                     MakeTraceSourceAccessor (&TimeProbe::m_output))
47
  ;
48
  return tid;
49
}
50
51
TimeProbe::TimeProbe ()
52
{
53
  NS_LOG_FUNCTION (this);
54
  m_output = 0;
55
}
56
57
TimeProbe::~TimeProbe ()
58
{
59
  NS_LOG_FUNCTION (this);
60
}
61
62
double
63
TimeProbe::GetValue (void) const
64
{
65
  NS_LOG_FUNCTION (this);
66
  return m_output;
67
}
68
void
69
TimeProbe::SetValue (Time newVal)
70
{
71
  NS_LOG_FUNCTION (this << newVal.GetSeconds ());
72
  m_output = newVal.GetSeconds ();
73
}
74
75
void
76
TimeProbe::SetValueByPath (std::string path, Time newVal)
77
{
78
  NS_LOG_FUNCTION (path << newVal.GetSeconds ());
79
  Ptr<TimeProbe> probe = Names::Find<TimeProbe> (path);
80
  NS_ASSERT_MSG (probe, "Error:  Can't find probe for path " << path);
81
  probe->SetValue (newVal);
82
}
83
84
bool
85
TimeProbe::ConnectByObject (std::string traceSource, Ptr<Object> obj)
86
{
87
  NS_LOG_FUNCTION (this << traceSource << obj);
88
  NS_LOG_DEBUG ("Name of trace source (if any) in names database: " << Names::FindPath (obj));
89
  bool connected = obj->TraceConnectWithoutContext (traceSource, MakeCallback (&ns3::TimeProbe::TraceSink, this));
90
  return connected;
91
}
92
93
void
94
TimeProbe::ConnectByPath (std::string path)
95
{
96
  NS_LOG_FUNCTION (this << path);
97
  NS_LOG_DEBUG ("Name of trace source to search for in config database: " << path);
98
  Config::ConnectWithoutContext (path, MakeCallback (&ns3::TimeProbe::TraceSink, this));
99
}
100
101
void
102
TimeProbe::TraceSink (Time oldData, Time newData)
103
{
104
  NS_LOG_FUNCTION (this << oldData.GetSeconds () << newData.GetSeconds ());
105
  if (IsEnabled ())
106
    {
107
      m_output = newData.GetSeconds ();
108
    }
109
}
110
111
} // namespace ns3
(-)24bd6ec8ad51 (+110 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2011 Bucknell University
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
 * Authors: L. Felipe Perrone (perrone@bucknell.edu)
19
 *          Tiago G. Rodrigues (tgr002@bucknell.edu)
20
 *
21
 * Modified by: Mitch Watrous (watrous@u.washington.edu)
22
 */
23
24
#ifndef TIME_PROBE_H
25
#define TIME_PROBE_H
26
27
#include "ns3/probe.h"
28
#include "ns3/object.h"
29
#include "ns3/callback.h"
30
#include "ns3/boolean.h"
31
#include "ns3/traced-value.h"
32
#include "ns3/simulator.h"
33
#include "ns3/nstime.h"
34
35
namespace ns3 {
36
37
/**
38
 * \ingroup probes
39
 *
40
 * This class is designed to probe an underlying ns3 TraceSource exporting
41
 * an ns3::Time.  This probe exports a trace source "Output" of type 
42
 * double, in units of seconds. The Output trace source emits a value when 
43
 * either the trace source emits a new value, or when SetValue () is called.
44
 *
45
 * The current value of the probe can be polled with the GetValue ()
46
 * method.
47
 */
48
class TimeProbe : public Probe
49
{
50
public:
51
  /**
52
   * \brief Get the type ID.
53
   * \return the object TypeId
54
   */
55
  static TypeId GetTypeId ();
56
  TimeProbe ();
57
  virtual ~TimeProbe ();
58
59
  /**
60
   * \return the most recent value (units of seconds)
61
   */
62
  double GetValue (void) const;
63
64
  /**
65
   * \param value set the traced Time to a new value
66
   */
67
  void SetValue (Time value);
68
69
  /**
70
   * \brief Set a probe value by its name in the Config system
71
   *
72
   * \param path Config path to access the probe
73
   * \param value set the traced Time to a new value
74
   */
75
  static void SetValueByPath (std::string path, Time value);
76
77
  /**
78
   * \brief connect to a trace source attribute provided by a given object
79
   *
80
   * \param traceSource the name of the attribute TraceSource to connect to
81
   * \param obj ns3::Object to connect to
82
   * \return true if the trace source was successfully connected
83
   */
84
  virtual bool ConnectByObject (std::string traceSource, Ptr<Object> obj);
85
86
  /**
87
   * \brief connect to a trace source provided by a config path
88
   *
89
   * \param path Config path to bind to
90
   *
91
   * Note, if an invalid path is provided, the probe will not be connected
92
   * to anything.
93
   */
94
  virtual void ConnectByPath (std::string path);
95
96
private:
97
  /**
98
   * \brief Method to connect to an underlying ns3::TraceSource of type Time 
99
   *
100
   * \param oldData previous value of the Time 
101
   * \param newData new value of the Time 
102
   */
103
  void TraceSink (Time oldData, Time newData);
104
105
  TracedValue<double> m_output; //!< Output trace source.
106
};
107
108
} // namespace ns3
109
110
#endif // TIME_PROBE_H
(-)a/src/stats/wscript (+2 lines)
 Lines 25-30    Link Here 
25
        'model/probe.cc',
25
        'model/probe.cc',
26
        'model/boolean-probe.cc',
26
        'model/boolean-probe.cc',
27
        'model/double-probe.cc',
27
        'model/double-probe.cc',
28
        'model/time-probe.cc',
28
        'model/uinteger-8-probe.cc',
29
        'model/uinteger-8-probe.cc',
29
        'model/uinteger-16-probe.cc',
30
        'model/uinteger-16-probe.cc',
30
        'model/uinteger-32-probe.cc',
31
        'model/uinteger-32-probe.cc',
 Lines 58-63    Link Here 
58
        'model/probe.h',
59
        'model/probe.h',
59
        'model/boolean-probe.h',
60
        'model/boolean-probe.h',
60
        'model/double-probe.h',
61
        'model/double-probe.h',
62
        'model/time-probe.h',
61
        'model/uinteger-8-probe.h',
63
        'model/uinteger-8-probe.h',
62
        'model/uinteger-16-probe.h',
64
        'model/uinteger-16-probe.h',
63
        'model/uinteger-32-probe.h',
65
        'model/uinteger-32-probe.h',

Return to bug 1998