The Sparta Modeling Framework
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SimulationInfo.hpp
Go to the documentation of this file.
1// <SimulationInfo.h> -*- C++ -*-
2
3
10#pragma once
11
12#include <errno.h>
13#include <cstdint>
14#include <unistd.h>
15#include <iostream>
16#include <iomanip>
17#include <string>
18#include <vector>
19#include <sstream>
20#include <cstring>
21#include <stack>
22#include <fstream>
23#include <map>
24#include <memory>
25#include <utility>
26
27#include "sparta/utils/TimeManager.hpp"
32
33namespace sparta
34{
35
41{
42 std::vector<std::string> lines_;
43 std::stringstream stream_;
44
45public:
46
47 typedef std::ostream& (*manip_t)(std::ostream&);
48
50 LineStringStream() = default;
51
58 template <class T>
60 stream_ << t;
61 return *this;
62 }
63
68 if(f == static_cast<manip_t>(std::endl)){
69 addNewLine();
70 }else{
71 f(stream_);
72 }
73 return *this;
74 }
75
80 void addNewLine(){
81 lines_.push_back(stream_.str());
82 stream_.str(""); // Clear
83 }
84
90 std::vector<std::string> getLines() const {
91 std::vector<std::string> result = lines_; // Copy
92 result.push_back(stream_.str());
93 return result;
94 }
95}; // class LineStringStream
96
106{
107 std::string sim_name_;
108 std::string command_line_;
109 std::string working_dir_;
110 std::string executable_;
111 std::string simulator_version_;
112 std::string reproduction_info_;
113 std::string start_time_;
114 std::string sparta_version_;
115 std::vector<std::string> other_;
116
117 static SimulationInfo sim_inst_;
118
119public:
120
127 const std::string& sim_name;
128
132 const std::string& command_line;
133
137 const std::string& working_dir;
138
142 const std::string& executable;
143
147 const std::string& simulator_version;
148
152 static const char sparta_version[];
153
159 const std::string& reproduction_info;
160
164 const std::string& start_time;
165
169 const std::vector<std::string>& other;
170
175 return sim_inst_;
176 }
177
181 ~SimulationInfo() = default;
182
187 start_time_(TimeManager::getTimeManager().getLocalTime()),
188 sim_name(sim_name_),
189 command_line(command_line_),
190 working_dir(working_dir_),
191 executable(executable_),
192 simulator_version(simulator_version_),
193 reproduction_info(reproduction_info_),
194 start_time(start_time_),
195 other(other_)
196 {;}
197
203 {
204 sim_name_ = rhp.sim_name_;
205 command_line_ = rhp.command_line_;
206 working_dir_ = rhp.working_dir_;
207 executable_ = rhp.executable_;
208 simulator_version_ = rhp.simulator_version_;
209 reproduction_info_ = rhp.reproduction_info_;
210 other_ = rhp.other_;
211 }
212
217
222 sim_name_ = rhp.sim_name_;
223 command_line_ = rhp.command_line_;
224 working_dir_ = rhp.working_dir_;
225 executable_ = rhp.executable_;
226 simulator_version_ = rhp.simulator_version_;
227 reproduction_info_ = rhp.reproduction_info_;
228 start_time_ = rhp.start_time_;
229 other_ = rhp.other_;
230
231 return *this;
232 }
233
237 SimulationInfo(const std::string& _sim_name,
238 const std::string& _command_line,
239 const std::string& _working_dir,
240 const std::string& _executable,
241 const std::string& _simulator_version,
242 const std::string& _reproduction_info,
243 const std::vector<std::string> & _other) :
245 {
246 sim_name_ = _sim_name;
247 command_line_ = _command_line;
248 working_dir_ = _working_dir;
249 executable_ = _executable;
250 simulator_version_ = _simulator_version;
251 reproduction_info_ = _reproduction_info;
252 other_ = _other;
253 }
254
261 SimulationInfo(const std::string& _sim_name,
262 int argc,
263 char** argv,
264 const std::string& _simulator_version,
265 const std::string& _reproduction_info,
266 const std::vector<std::string> & _other) :
268 {
269 sim_name_ = _sim_name;
270 setCommandLine(argc, argv);
271 simulator_version_ = _simulator_version;
272 reproduction_info_ = _reproduction_info;
273 other_ = _other;
274
275 const uint32_t PATH_SIZE = 2048;
276 char tmp[PATH_SIZE];
277 if (getcwd(tmp, PATH_SIZE) != nullptr) {
278 working_dir_ = tmp;
279 } else {
280 std::stringstream ss;
281 ss << "<error " << errno << " determining working directory>";
282 working_dir_ = ss.str();
283 }
284 }
285
294 SimulationInfo(std::ifstream & json_fin,
295 std::map<std::string, std::string> * json_kvpairs = nullptr);
296
302 std::string getSpartaVersion() const {
303 if (sparta_version_.empty()) {
305 }
306 return sparta_version_;
307 }
308
313 void setCommandLine(int argc, char** argv) {
314 if(argc > 0){
315 executable_ = argv[0];
316 }
317 std::stringstream cmdln;
318 for(int i = 0; i < argc; ++i){
319 if(i > 0){
320 cmdln << " ";
321 }
322 size_t arglen = strlen(argv[i]);
323 if(arglen == 0
324 || memchr(argv[i], ' ', arglen) != nullptr
325 || memchr(argv[i], '\t', arglen) != nullptr
326 || memchr(argv[i], '\n', arglen) != nullptr){
327 cmdln << '"' << argv[i] << '"';
328 }else{
329 cmdln << argv[i];
330 }
331 }
332 command_line_ = cmdln.str();
333 }
334
338 void setCommandLine(const std::string& cmdline) {
339 command_line_ = cmdline;
340 }
341
345 void addOtherInfo(const std::string& _other){
346 other_.push_back(_other);
347 }
348
357 template <typename StreamType=std::ostream>
358 void write(StreamType& o,
359 const std::string& line_start="# ",
360 const std::string& line_end="\n",
361 bool show_field_names=true) const {
362
363 auto pairs = getHeaderPairs();
364 for(auto p : pairs){
365 o << line_start;
366 if(show_field_names){
367 o << std::left << std::setw(10) << (p.first + ":");
368 }
369 o << p.second << line_end;
370 }
371
372 if(other.size() > 0){
373 o << line_start << "Other:" << line_end;
374 for(const std::string& oi : other){
375 o << line_start << " " << oi << line_end;
376 }
377 }
378 }
379
384 template <typename ...Args>
385 std::string stringize(const Args& ...args) const {
386 std::stringstream ss;
387 write(ss, args...);
388 return ss.str();
389 }
390
397 template <typename ...Args>
398 std::vector<std::string> stringizeToLines(const Args& ...args) const {
400 write(ss, args...);
401 return ss.getLines();
402 }
403
407 std::vector<std::pair<std::string, std::string>> getHeaderPairs() const {
408 std::vector<std::pair<std::string, std::string>> result;
409 result.emplace_back("Name", sim_name);
410 result.emplace_back("Cmdline", command_line);
411 result.emplace_back("Exe", executable);
412 result.emplace_back("SimulatorVersion", simulator_version);
413 result.emplace_back("Repro", reproduction_info);
414 result.emplace_back("Start", start_time);
415
416 std::stringstream timestr;
418 result.emplace_back("Elapsed", timestr.str());
419
420 last_captured_elapsed_time_ = result.back().second;
421 return result;
422 }
423
430 return last_captured_elapsed_time_;
431 }
432
433private:
434 mutable utils::ValidValue<std::string> last_captured_elapsed_time_;
435
436}; // class SimulationInfo
437
441inline std::ostream& operator<<(std::ostream& o, const SimulationInfo& info) {
442 info.write(o);
443 return o;
444}
445
446} // namespace sparta
Set of macros for Sparta assertions. Caught by the framework.
Exception class for all of Sparta.
Cool string utilities.
File that defines a ValidValue.
Helper class for building a set of lines through an ostream-like interface.
LineStringStream & operator<<(const T &t)
Insertion operator on this LogObject.
void addNewLine()
Add a new line to the list. This is also called when std::endl is inserted into this stream.
std::ostream &(*) manip_t(std::ostream &)
Default Constructor.
std::vector< std::string > getLines() const
Gets the lines including whathever line is currently being built.
LineStringStream & operator<<(manip_t f)
Handler for stream modifiers (e.g. endl)
Contains information describing the simulation instance for the purpose of identifying the simulation...
std::vector< std::string > stringizeToLines(const Args &...args) const
Generate a vector of lines.
void addOtherInfo(const std::string &_other)
Add other information to the simulation.
const std::string & command_line
Simulator application instance command-line.
SimulationInfo(const std::string &_sim_name, const std::string &_command_line, const std::string &_working_dir, const std::string &_executable, const std::string &_simulator_version, const std::string &_reproduction_info, const std::vector< std::string > &_other)
SimulationInfo constructor.
void setCommandLine(int argc, char **argv)
Assign command_line_ and executable_ from args.
const std::string & start_time
Time at which the simulation started (roughly)
const utils::ValidValue< std::string > & getLastCapturedElapsedTime() const
Return the very last 'Elapsed' time that this object got from the TimeManager. This is reset with eac...
SimulationInfo(const std::string &_sim_name, int argc, char **argv, const std::string &_simulator_version, const std::string &_reproduction_info, const std::vector< std::string > &_other)
SimulationInfo constructor with automatic command-line reconstruction.
const std::vector< std::string > & other
other simulator information
SimulationInfo & operator=(const SimulationInfo &rhp)
Assignable.
SimulationInfo(const SimulationInfo &rhp)
Copy-constructor.
const std::string & executable
Executable being run.
const std::string & working_dir
Simulator application instance working dir.
SimulationInfo(SimulationInfo &&)=delete
Not move-constructable.
std::vector< std::pair< std::string, std::string > > getHeaderPairs() const
Gets (name, value) pairs for each header entry.
void write(StreamType &o, const std::string &line_start="# ", const std::string &line_end="\n", bool show_field_names=true) const
Write this information to an ostream.
const std::string & reproduction_info
versions/buildnums/tags of simulator and dependencies necessary for reproducing the build from a vers...
std::string stringize(const Args &...args) const
Generate a string using the write function without needing to construct a temporary stringstream.
SimulationInfo(std::ifstream &json_fin, std::map< std::string, std::string > *json_kvpairs=nullptr)
Instantiate a SimulationInfo object from a json, json_reduced, json_detail, or js_json report file.
static SimulationInfo & getInstance()
Gets the SimulationInfo singleton instance.
const std::string & sim_name
Simulator application name. Note that multiple simulators could exist in the same process space,...
std::string getSpartaVersion() const
Get the SPARTA version string for this SimulationInfo object. Most of the time, this will be Simulati...
static const char sparta_version[]
The version of SPARTA.
void setCommandLine(const std::string &cmdline)
Assign command line from string.
SimulationInfo()
Default Constructor.
~SimulationInfo()=default
Destruction.
const std::string & simulator_version
Simulator Version of the simulator itself.
Singleton which manages wall-clock time for simulations in SPARTA This is not a "timer" manager,...
double getSecondsElapsed() const
Gets the number of seconds elapsed since the instantiation of SPARTA static and global vars.
static TimeManager & getTimeManager()
Returns the TimeManager singleton.
Provides a wrapper around a value to ensure that the value is assigned.
Macros for handling exponential backoff.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo