OLD | NEW |
(Empty) | |
| 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 |
| 22 /** |
| 23 * @file |
| 24 * @ingroup simulator |
| 25 * ns3::DesMetrics implementation. |
| 26 */ |
| 27 |
| 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 (¤t_time); |
| 70 const char * date = ctime (¤t_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 & d
elay) |
| 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 { |
| 130 CriticalSection cs (m_mutex); |
| 131 m_os << ss.str (); |
| 132 } |
| 133 |
| 134 m_separator = ','; |
| 135 } |
| 136 |
| 137 DesMetrics::~DesMetrics (void) |
| 138 { |
| 139 Close (); |
| 140 } |
| 141 |
| 142 void |
| 143 DesMetrics::Close (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 |
OLD | NEW |