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 (int argc, char * argv[], 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 (argc)
54  {
55  std::string arg0 = argv[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 (argc)
80  {
81  for (int i = 0; i < argc; ++i)
82  {
83  if (i > 0) m_os << " ";
84  m_os << argv[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  Initialize (0, 0);
110  }
111 
112  std::ostringstream ss;
113  if (m_separator == ',')
114  {
115  ss << m_separator << std::endl;
116  }
117 
118  uint32_t sendCtx = Simulator::GetContext ();
119  // Force to signed so we can show NoContext as '-1'
120  int32_t send = (sendCtx != Simulator::NO_CONTEXT) ? (int32_t)sendCtx : -1;
121  int32_t recv = (context != Simulator::NO_CONTEXT) ? (int32_t)context : -1;
122 
123  ss << " [\""
124  << send << "\",\""
125  << now.GetTimeStep () << "\",\""
126  << recv << "\",\""
127  << (now + delay).GetTimeStep () << "\"]";
128 
129  {
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 
155 
156 } // namespace ns3
157 
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void Initialize(int argc, char *argv[], std::string outDir="")
Open the DesMetrics trace file and print the header.
Definition: des-metrics.cc:42
static uint32_t GetContext(void)
Get the current simulation context.
Definition: simulator.cc:353
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:143
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:204
System-independent file and directory function declarations.
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:137
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
int64_t GetTimeStep(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:377
bool m_initialized
Have we been initialized.
Definition: des-metrics.h:158
Flag for events not associated with any particular context.
Definition: simulator.h:192
std::string Append(std::string left, std::string right)
Join two file system path elements.
Definition: system-path.cc:187
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