A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
des-metrics.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 LLNL
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
7 */
8
9/**
10 * @file
11 * @ingroup simulator
12 * ns3::DesMetrics implementation.
13 */
14
15#include "des-metrics.h"
16
17#include "simulator.h"
18#include "system-path.h"
19
20#include <ctime> // time_t, time()
21#include <sstream>
22#include <string>
23
24namespace ns3
25{
26
27/* static */
28std::string DesMetrics::m_outputDir; // = "";
29
30void
31DesMetrics::Initialize(std::vector<std::string> args, std::string outDir /* = "" */)
32{
33 if (m_initialized)
34 {
35 // Running multiple tests, so close the previous output file
36 Close();
37 }
38
39 m_initialized = true;
40
41 std::string model_name("desTraceFile");
42 if (!args.empty())
43 {
44 std::string arg0 = args[0];
45 model_name = SystemPath::Split(arg0).back();
46 }
47 std::string jsonFile = model_name + ".json";
48 if (!outDir.empty())
49 {
51 }
52 if (!DesMetrics::m_outputDir.empty())
53 {
54 jsonFile = SystemPath::Append(DesMetrics::m_outputDir, jsonFile);
55 }
56
57 time_t current_time;
58 time(&current_time);
59 const char* date = ctime(&current_time);
60 std::string capture_date(date, 24); // discard trailing newline from ctime
61
62 m_os.open(jsonFile);
63 m_os << "{" << std::endl;
64 m_os << " \"simulator_name\" : \"ns-3\"," << std::endl;
65 m_os << " \"model_name\" : \"" << model_name << "\"," << std::endl;
66 m_os << " \"capture_date\" : \"" << capture_date << "\"," << std::endl;
67 m_os << " \"command_line_arguments\" : \"";
68 if (args.empty())
69 {
70 for (std::size_t i = 0; i < args.size(); ++i)
71 {
72 if (i > 0)
73 {
74 m_os << " ";
75 }
76 m_os << args[i];
77 }
78 }
79 else
80 {
81 m_os << "[argv empty or not available]";
82 }
83 m_os << "\"," << std::endl;
84 m_os << " \"events\" : [" << std::endl;
85
86 m_separator = ' ';
87}
88
89void
90DesMetrics::Trace(const Time& now, const Time& delay)
91{
93}
94
95void
96DesMetrics::TraceWithContext(uint32_t context, const Time& now, const Time& delay)
97{
98 if (!m_initialized)
99 {
100 std::vector<std::string> args;
101 Initialize(args);
102 }
103
104 std::ostringstream ss;
105 if (m_separator == ',')
106 {
107 ss << m_separator << std::endl;
108 }
109
111 // Force to signed so we can show NoContext as '-1'
112 int32_t send = (sendCtx != Simulator::NO_CONTEXT) ? (int32_t)sendCtx : -1;
113 int32_t recv = (context != Simulator::NO_CONTEXT) ? (int32_t)context : -1;
114
115 ss << " [\"" << send << "\",\"" << now.GetTimeStep() << "\",\"" << recv << "\",\""
116 << (now + delay).GetTimeStep() << "\"]";
117
118 {
119 std::unique_lock lock{m_mutex};
120 m_os << ss.str();
121 }
122
123 m_separator = ',';
124}
125
130
131void
133{
134 m_os << std::endl; // Finish the last event line
135
136 m_os << " ]" << std::endl;
137 m_os << "}" << std::endl;
138 m_os.close();
139
140 m_initialized = false;
141}
142
143} // namespace ns3
~DesMetrics() override
Destructor, closes the trace file.
bool m_initialized
Have we been initialized.
std::mutex m_mutex
Mutex to control access to the output file.
void Initialize(std::vector< std::string > args, std::string outDir="")
Open the DesMetrics trace file and print the header.
void Close()
Close the output file.
char m_separator
The separator between event records.
void Trace(const Time &now, const Time &delay)
Trace an event to self at the time it is scheduled.
void TraceWithContext(uint32_t context, const Time &now, const Time &delay)
Trace an event (with context) at the time it is scheduled.
std::ofstream m_os
The output JSON trace file stream.
static std::string m_outputDir
Cache the last-used output directory.
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition simulator.h:199
static uint32_t GetContext()
Get the current simulation context.
Definition simulator.cc:307
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetTimeStep() const
Get the raw time value, in the current resolution unit.
Definition nstime.h:434
ns3::DesMetrics declaration.
std::list< std::string > Split(std::string path)
Split a file system path into directories according to the local path separator.
std::string Append(std::string left, std::string right)
Join two file system path elements.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Simulator declaration.
ns3::SystemPath declarations.