A Discrete-Event Network Simulator
API
des-metrics.cc
Go to the documentation of this file.
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 
28 #include "des-metrics.h"
29 #include "simulator.h"
30 #include "system-path.h"
31 
32 #include <ctime> // time_t, time()
33 #include <sstream>
34 #include <string>
35 
36 namespace ns3 {
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.size () > 0)
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 != "")
60  {
61  DesMetrics::m_outputDir = outDir;
62  }
63  if (DesMetrics::m_outputDir != "")
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.c_str ());
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.size () == 0)
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 
101 void
102 DesMetrics::Trace (const Time & now, const Time & delay)
103 {
104  TraceWithContext (Simulator::GetContext (), now, delay);
105 }
106 
107 void
108 DesMetrics::TraceWithContext (uint32_t context, const Time & now, const Time & delay)
109 {
110  if (!m_initialized)
111  {
112  std::vector<std::string> args;
113  Initialize (args);
114  }
115 
116  std::ostringstream ss;
117  if (m_separator == ',')
118  {
119  ss << m_separator << std::endl;
120  }
121 
122  uint32_t sendCtx = Simulator::GetContext ();
123  // Force to signed so we can show NoContext as '-1'
124  int32_t send = (sendCtx != Simulator::NO_CONTEXT) ? (int32_t)sendCtx : -1;
125  int32_t recv = (context != Simulator::NO_CONTEXT) ? (int32_t)context : -1;
126 
127  ss << " [\""
128  << send << "\",\""
129  << now.GetTimeStep () << "\",\""
130  << recv << "\",\""
131  << (now + delay).GetTimeStep () << "\"]";
132 
133  {
134  std::unique_lock lock {m_mutex};
135  m_os << ss.str ();
136  }
137 
138  m_separator = ',';
139 }
140 
142 {
143  Close ();
144 }
145 
146 void
148 {
149  m_os << std::endl; // Finish the last event line
150 
151  m_os << " ]" << std::endl;
152  m_os << "}" << std::endl;
153  m_os.close ();
154 
155  m_initialized = false;
156 }
157 
158 
159 
160 } // namespace ns3
161 
void Close(void)
Close the output file.
Definition: des-metrics.cc:147
bool m_initialized
Have we been initialized.
Definition: des-metrics.h:158
std::mutex m_mutex
Mutex to control access to the output file.
Definition: des-metrics.h:163
void Initialize(std::vector< std::string > args, std::string outDir="")
Open the DesMetrics trace file and print the header.
Definition: des-metrics.cc:42
char m_separator
The separator between event records.
Definition: des-metrics.h:160
void Trace(const Time &now, const Time &delay)
Trace an event to self at the time it is scheduled.
Definition: des-metrics.cc:102
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:108
std::ofstream m_os
The output JSON trace file stream.
Definition: des-metrics.h:159
~DesMetrics(void)
Destructor, closes the trace file.
Definition: des-metrics.cc:141
static std::string m_outputDir
Cache the last-used output directory.
Definition: des-metrics.h:156
static uint32_t GetContext(void)
Get the current simulation context.
Definition: simulator.cc:300
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition: simulator.h:199
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
int64_t GetTimeStep(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:415
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:241
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Simulator declaration.
ns3::SystemPath declarations.