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) m_os << " ";
84  m_os << args[i];
85  }
86  }
87  else
88  {
89  m_os << "[argv empty or not available]";
90  }
91  m_os << "\"," << std::endl;
92  m_os << " \"events\" : [" << std::endl;
93 
94  m_separator = ' ';
95 
96 }
97 
98 void
99 DesMetrics::Trace (const Time & now, const Time & delay)
100 {
101  TraceWithContext (Simulator::GetContext (), now, delay);
102 }
103 
104 void
105 DesMetrics::TraceWithContext (uint32_t context, const Time & now, const Time & delay)
106 {
107  if (!m_initialized)
108  {
109  std::vector<std::string> args;
110  Initialize (args);
111  }
112 
113  std::ostringstream ss;
114  if (m_separator == ',')
115  {
116  ss << m_separator << std::endl;
117  }
118 
119  uint32_t sendCtx = Simulator::GetContext ();
120  // Force to signed so we can show NoContext as '-1'
121  int32_t send = (sendCtx != Simulator::NO_CONTEXT) ? (int32_t)sendCtx : -1;
122  int32_t recv = (context != Simulator::NO_CONTEXT) ? (int32_t)context : -1;
123 
124  ss << " [\""
125  << send << "\",\""
126  << now.GetTimeStep () << "\",\""
127  << recv << "\",\""
128  << (now + delay).GetTimeStep () << "\"]";
129 
130  {
132  m_os << ss.str ();
133  }
134 
135  m_separator = ',';
136 }
137 
139 {
140  Close ();
141 }
142 
143 void
145 {
146  m_os << std::endl; // Finish the last event line
147 
148  m_os << " ]" << std::endl;
149  m_os << "}" << std::endl;
150  m_os.close ();
151 
152  m_initialized = false;
153 }
154 
155 
156 
157 } // namespace ns3
158 
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
static uint32_t GetContext(void)
Get the current simulation context.
Definition: simulator.cc:378
std::ofstream m_os
The output JSON trace file stream.
Definition: des-metrics.h:159
void Close(void)
Close the output file.
Definition: des-metrics.cc:144
ns3::Simulator declaration.
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:205
ns3::SystemPath declarations.
void Initialize(std::vector< std::string > args, std::string outDir="")
Open the DesMetrics trace file and print the header.
Definition: des-metrics.cc:42
A class which provides a simple way to implement a Critical Section.
Definition: system-mutex.h:118
~DesMetrics(void)
Destructor, closes the trace file.
Definition: des-metrics.cc:138
char m_separator
The separator between event records.
Definition: des-metrics.h:160
Every class exported by the ns3 library is enclosed in the ns3 namespace.
SystemMutex m_mutex
Mutex to control access to the output file.
Definition: des-metrics.h:163
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:105
bool m_initialized
Have we been initialized.
Definition: des-metrics.h:158
std::string Append(std::string left, std::string right)
Join two file system path elements.
Definition: system-path.cc:188
Flag for events not associated with any particular context.
Definition: simulator.h:198
void Trace(const Time &now, const Time &delay)
Trace an event to self at the time it is scheduled.
Definition: des-metrics.cc:99
static std::string m_outputDir
Cache the last-used output directory.
Definition: des-metrics.h:156
int64_t GetTimeStep(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:391