A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
des-metrics.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 LLNL
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
7 */
8
9#ifndef DESMETRICS_H
10#define DESMETRICS_H
11
12/**
13 * @file
14 * @ingroup simulator
15 * ns3::DesMetrics declaration.
16 */
17
18#include "nstime.h"
19#include "singleton.h"
20
21#include <fstream>
22#include <mutex>
23#include <stdint.h> // uint32_t
24#include <string>
25#include <vector>
26
27namespace ns3
28{
29
30/**
31 * @ingroup simulator
32 *
33 * @brief Event trace data collector for the DES Metrics project.
34 *
35 * This feature generates a JSON file with event trace data,
36 * including the source and destination context for each
37 * event, and the (virtual) times when the event was scheduled and
38 * when it will execute.
39 *
40 * See the DES Metrics Project page: https://github.com/wilseypa/desMetrics
41 * for more information and analysis tools.
42 *
43 * If enabled (see below), ns-3 scripts should use CommandLine to
44 * parse arguments, which will open the JSON file with the same name
45 * as the script, and write the JSON header. Failure to use CommandLine when
46 * DES Metrics is enabled will put the trace data in the file
47 * \c desTraceFile.json instead. All examples accessible from \c test.py
48 * use CommandLine, and so generate JSON files.
49 *
50 * Output from scripts ends up in the current working directory (normally the
51 * top level directory). When \c test.py is used to run tests or examples
52 * the trace files are generated in a time-stamped subdirectory of
53 * \c testpy-output/, which \c test.py normally deletes.
54 * To keep the output of examples, use the \c --retain argument to \c test.py.
55 *
56 * The output file has the following form:
57 * \verbatim
58{
59 "simulator_name" : "ns-3",
60 "model_name" : "ipv4-raw",
61 "capture_date" : "Fri May 27 00:34:27 2016",
62 "command_line_arguments" : "ipv4-raw [ns3-dev-test-runner-debug] --test-name=ipv4-raw
63--stop-on-failure --fullness=QUICK --xml --tempdir=testpy-output/2016-05-27-04-33-35-CUT
64--out=testpy-output/2016-05-27-04-33-35-CUT/ipv4-raw.xml", "events" : [
65 ["0",0,"0",0],
66 ["1",0,"0",0],
67 ["0",0,"0",0],
68 ...
69 ["0",0,"0",0]
70 ]
71} \endverbatim
72 * The first few fields are self-explanatory. The \c event record consists of
73 * the source context, the event send time, the destination context,
74 * and the event execution time. Times are given in the
75 * current Time resolution.
76 *
77 * <b> Enabling DES Metrics </b>
78 *
79 * Enable DES Metrics at configure time with
80 * \verbatim
81 $ ns3 configure ... --enable-des-metrics \endverbatim
82 *
83 * <b> Working with DES Metrics </b>
84 *
85 * Some useful shell pipelines:
86 *
87 * \li Run everything, retaining the results directory: <br/>
88 * \code ./test.py --nobuild --retain \endcode
89 * \li Example traces end up in \c testpy-output/, so move there: <br/>
90 * \code cd testpy-output/$(date +"%F")*_/ \endcode
91 * (Remove the `_', which is to work around a Doxygen limitation.)
92 * \li Remove the traces with less than 10 events: <br/>
93 * \code wc -l *.json | sort -nr | grep "^ *[789] " | cut -d ' ' -f 9 | xargs rm -f \endcode
94 * \li Show the largest file, and total number of trace files: <br/>
95 * \code wc -l *.json | sort -n | tail -2 \endcode
96 *
97 */
98class DesMetrics : public Singleton<DesMetrics>
99{
100 public:
101 /**
102 * Open the DesMetrics trace file and print the header.
103 *
104 * The trace file will have the same base name as the main program,
105 * '.json' as the extension.
106 *
107 * \param args [in] Command line arguments.
108 * \param outDir [in] Directory where the trace file should be written.
109 */
110 void Initialize(std::vector<std::string> args, std::string outDir = "");
111
112 /**
113 * Trace an event to self at the time it is scheduled.
114 *
115 * \param now [in] The local simulation time.
116 * \param delay [in] The delay to the event.
117 */
118 void Trace(const Time& now, const Time& delay);
119
120 /**
121 * Trace an event (with context) at the time it is scheduled.
122 *
123 * \param context [in] The context (NodeId) which will receive the event.
124 * \param now [in] The local simulation time.
125 * \param delay [in] The delay to the event.
126 */
127 void TraceWithContext(uint32_t context, const Time& now, const Time& delay);
128
129 /**
130 * Destructor, closes the trace file.
131 */
132 ~DesMetrics() override;
133
134 private:
135 /** Close the output file. */
136 void Close();
137
138 /**
139 * Cache the last-used output directory.
140 *
141 * This is enables repeated/re-entrant use of CommandLine, for example
142 * in \c command-line-test-suite.cc
143 */
144 static std::string m_outputDir;
145
146 bool m_initialized; //!< Have we been initialized.
147 std::ofstream m_os; //!< The output JSON trace file stream.
148 char m_separator; //!< The separator between event records.
149
150 /** Mutex to control access to the output file. */
151 std::mutex m_mutex;
152
153}; // class DesMetrics
154
155} // namespace ns3
156
157#endif /* DESMETRICS_H */
Event trace data collector for the DES Metrics project.
Definition des-metrics.h:99
~DesMetrics() override
Destructor, closes the trace file.
bool m_initialized
Have we been initialized.
std::mutex m_mutex
Mutex to control access to the output file.
void Initialize(std::vector< std::string > args, std::string outDir="")
Open the DesMetrics trace file and print the header.
void Close()
Close the output file.
char m_separator
The separator between event records.
void Trace(const Time &now, const Time &delay)
Trace an event to self at the time it is scheduled.
void TraceWithContext(uint32_t context, const Time &now, const Time &delay)
Trace an event (with context) at the time it is scheduled.
std::ofstream m_os
The output JSON trace file stream.
static std::string m_outputDir
Cache the last-used output directory.
A template singleton.
Definition singleton.h:57
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::Singleton declaration and template implementation.