A Discrete-Event Network Simulator
API
des-metrics.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 LLNL
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
18  */
19 
26 #include "des-metrics.h"
27 
28 #include "simulator.h"
29 #include "system-path.h"
30 
31 #include <ctime> // time_t, time()
32 #include <sstream>
33 #include <string>
34 
35 namespace ns3
36 {
37 
38 /* static */
39 std::string DesMetrics::m_outputDir; // = "";
40 
41 void
42 DesMetrics::Initialize(std::vector<std::string> args, std::string outDir /* = "" */)
43 {
44  if (m_initialized)
45  {
46  // Running multiple tests, so close the previous output file
47  Close();
48  }
49 
50  m_initialized = true;
51 
52  std::string model_name("desTraceFile");
53  if (!args.empty())
54  {
55  std::string arg0 = args[0];
56  model_name = SystemPath::Split(arg0).back();
57  }
58  std::string jsonFile = model_name + ".json";
59  if (!outDir.empty())
60  {
61  DesMetrics::m_outputDir = outDir;
62  }
63  if (!DesMetrics::m_outputDir.empty())
64  {
65  jsonFile = SystemPath::Append(DesMetrics::m_outputDir, jsonFile);
66  }
67 
68  time_t current_time;
69  time(&current_time);
70  const char* date = ctime(&current_time);
71  std::string capture_date(date, 24); // discard trailing newline from ctime
72 
73  m_os.open(jsonFile);
74  m_os << "{" << std::endl;
75  m_os << " \"simulator_name\" : \"ns-3\"," << std::endl;
76  m_os << " \"model_name\" : \"" << model_name << "\"," << std::endl;
77  m_os << " \"capture_date\" : \"" << capture_date << "\"," << std::endl;
78  m_os << " \"command_line_arguments\" : \"";
79  if (args.empty())
80  {
81  for (std::size_t i = 0; i < args.size(); ++i)
82  {
83  if (i > 0)
84  {
85  m_os << " ";
86  }
87  m_os << args[i];
88  }
89  }
90  else
91  {
92  m_os << "[argv empty or not available]";
93  }
94  m_os << "\"," << std::endl;
95  m_os << " \"events\" : [" << std::endl;
96 
97  m_separator = ' ';
98 }
99 
100 void
101 DesMetrics::Trace(const Time& now, const Time& delay)
102 {
104 }
105 
106 void
107 DesMetrics::TraceWithContext(uint32_t context, const Time& now, const Time& delay)
108 {
109  if (!m_initialized)
110  {
111  std::vector<std::string> args;
112  Initialize(args);
113  }
114 
115  std::ostringstream ss;
116  if (m_separator == ',')
117  {
118  ss << m_separator << std::endl;
119  }
120 
121  uint32_t sendCtx = Simulator::GetContext();
122  // Force to signed so we can show NoContext as '-1'
123  int32_t send = (sendCtx != Simulator::NO_CONTEXT) ? (int32_t)sendCtx : -1;
124  int32_t recv = (context != Simulator::NO_CONTEXT) ? (int32_t)context : -1;
125 
126  ss << " [\"" << send << "\",\"" << now.GetTimeStep() << "\",\"" << recv << "\",\""
127  << (now + delay).GetTimeStep() << "\"]";
128 
129  {
130  std::unique_lock lock{m_mutex};
131  m_os << ss.str();
132  }
133 
134  m_separator = ',';
135 }
136 
138 {
139  Close();
140 }
141 
142 void
144 {
145  m_os << std::endl; // Finish the last event line
146 
147  m_os << " ]" << std::endl;
148  m_os << "}" << std::endl;
149  m_os.close();
150 
151  m_initialized = false;
152 }
153 
154 } // namespace ns3
~DesMetrics() override
Destructor, closes the trace file.
Definition: des-metrics.cc:137
bool m_initialized
Have we been initialized.
Definition: des-metrics.h:157
std::mutex m_mutex
Mutex to control access to the output file.
Definition: des-metrics.h:162
void Initialize(std::vector< std::string > args, std::string outDir="")
Open the DesMetrics trace file and print the header.
Definition: des-metrics.cc:42
void Close()
Close the output file.
Definition: des-metrics.cc:143
char m_separator
The separator between event records.
Definition: des-metrics.h:159
void Trace(const Time &now, const Time &delay)
Trace an event to self at the time it is scheduled.
Definition: des-metrics.cc:101
void TraceWithContext(uint32_t context, const Time &now, const Time &delay)
Trace an event (with context) at the time it is scheduled.
Definition: des-metrics.cc:107
std::ofstream m_os
The output JSON trace file stream.
Definition: des-metrics.h:158
static std::string m_outputDir
Cache the last-used output directory.
Definition: des-metrics.h:155
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition: simulator.h:210
static uint32_t GetContext()
Get the current simulation context.
Definition: simulator.cc:318
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetTimeStep() const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:445
ns3::DesMetrics declaration.
std::list< std::string > Split(std::string path)
Split a file system path into directories according to the local path separator.
Definition: system-path.cc:258
std::string Append(std::string left, std::string right)
Join two file system path elements.
Definition: system-path.cc:240
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Simulator declaration.
ns3::SystemPath declarations.