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