The Sparta Modeling Framework
Loading...
Searching...
No Matches
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
33#include "simdb/schema/DatabaseTypedefs.hpp"
34namespace simdb { class ObjectManager; }
35
36namespace sparta
37{
38
44{
45 std::vector<std::string> lines_;
46 std::stringstream stream_;
47
48public:
49
50 typedef std::ostream& (*manip_t)(std::ostream&);
51
53 LineStringStream() = default;
54
61 template <class T>
63 stream_ << t;
64 return *this;
65 }
66
71 if(f == static_cast<manip_t>(std::endl)){
72 addNewLine();
73 }else{
74 f(stream_);
75 }
76 return *this;
77 }
78
83 void addNewLine(){
84 lines_.push_back(stream_.str());
85 stream_.str(""); // Clear
86 }
87
93 std::vector<std::string> getLines() const {
94 std::vector<std::string> result = lines_; // Copy
95 result.push_back(stream_.str());
96 return result;
97 }
98}; // class LineStringStream
99
109{
110 std::string sim_name_;
111 std::string command_line_;
112 std::string working_dir_;
113 std::string executable_;
114 std::string simulator_version_;
115 std::string reproduction_info_;
116 std::string start_time_;
117 std::string sparta_version_;
118 std::vector<std::string> other_;
119
121 utils::ValidValue<std::string> db_elapsed_time_;
122
132 static std::stack<SimulationInfo*> sim_inst_stack_;
133
134 static SimulationInfo sim_inst_;
135
136public:
137
144 const std::string& sim_name;
145
149 const std::string& command_line;
150
154 const std::string& working_dir;
155
159 const std::string& executable;
160
164 const std::string& simulator_version;
165
169 static const char sparta_version[];
170
176 const std::string& reproduction_info;
177
181 const std::string& start_time;
182
186 const std::vector<std::string>& other;
187
192 if (sim_inst_stack_.empty()) {
193 return sim_inst_;
194 }
195 return *sim_inst_stack_.top();
196 }
197
202 if (!sim_inst_stack_.empty()) {
203 sim_inst_stack_.pop();
204 }
205 }
206
211 start_time_(TimeManager::getTimeManager().getLocalTime()),
212 sim_name(sim_name_),
213 command_line(command_line_),
214 working_dir(working_dir_),
215 executable(executable_),
216 simulator_version(simulator_version_),
217 reproduction_info(reproduction_info_),
218 start_time(start_time_),
219 other(other_)
220 {;}
221
227 {
228 sim_name_ = rhp.sim_name_;
229 command_line_ = rhp.command_line_;
230 working_dir_ = rhp.working_dir_;
231 executable_ = rhp.executable_;
232 simulator_version_ = rhp.simulator_version_;
233 reproduction_info_ = rhp.reproduction_info_;
234 other_ = rhp.other_;
235 }
236
241
246 sim_name_ = rhp.sim_name_;
247 command_line_ = rhp.command_line_;
248 working_dir_ = rhp.working_dir_;
249 executable_ = rhp.executable_;
250 simulator_version_ = rhp.simulator_version_;
251 reproduction_info_ = rhp.reproduction_info_;
252 start_time_ = rhp.start_time_;
253 other_ = rhp.other_;
254
255 return *this;
256 }
257
261 SimulationInfo(const std::string& _sim_name,
262 const std::string& _command_line,
263 const std::string& _working_dir,
264 const std::string& _executable,
265 const std::string& _simulator_version,
266 const std::string& _reproduction_info,
267 const std::vector<std::string> & _other) :
269 {
270 sim_name_ = _sim_name;
271 command_line_ = _command_line;
272 working_dir_ = _working_dir;
273 executable_ = _executable;
274 simulator_version_ = _simulator_version;
275 reproduction_info_ = _reproduction_info;
276 other_ = _other;
277 }
278
285 SimulationInfo(const std::string& _sim_name,
286 int argc,
287 char** argv,
288 const std::string& _simulator_version,
289 const std::string& _reproduction_info,
290 const std::vector<std::string> & _other) :
292 {
293 sim_name_ = _sim_name;
294 setCommandLine(argc, argv);
295 simulator_version_ = _simulator_version;
296 reproduction_info_ = _reproduction_info;
297 other_ = _other;
298
299 const uint32_t PATH_SIZE = 2048;
300 char tmp[PATH_SIZE];
301 if (getcwd(tmp, PATH_SIZE) != nullptr) {
302 working_dir_ = tmp;
303 } else {
304 std::stringstream ss;
305 ss << "<error " << errno << " determining working directory>";
306 working_dir_ = ss.str();
307 }
308 }
309
321 SimulationInfo(const simdb::ObjectManager & sim_db,
322 const simdb::DatabaseID obj_mgr_db_id,
323 const simdb::DatabaseID report_node_id = 0);
332 SimulationInfo(std::ifstream & json_fin,
333 std::map<std::string, std::string> * json_kvpairs = nullptr);
334
342 std::string getSpartaVersion() const {
343 if (sparta_version_.empty()) {
345 }
346 return sparta_version_;
347 }
348
353 void setCommandLine(int argc, char** argv) {
354 if(argc > 0){
355 executable_ = argv[0];
356 }
357 std::stringstream cmdln;
358 for(int i = 0; i < argc; ++i){
359 if(i > 0){
360 cmdln << " ";
361 }
362 size_t arglen = strlen(argv[i]);
363 if(arglen == 0
364 || memchr(argv[i], ' ', arglen) != nullptr
365 || memchr(argv[i], '\t', arglen) != nullptr
366 || memchr(argv[i], '\n', arglen) != nullptr){
367 cmdln << '"' << argv[i] << '"';
368 }else{
369 cmdln << argv[i];
370 }
371 }
372 command_line_ = cmdln.str();
373 }
374
378 void setCommandLine(const std::string& cmdline) {
379 command_line_ = cmdline;
380 }
381
385 void addOtherInfo(const std::string& _other){
386 other_.push_back(_other);
387 }
388
397 template <typename StreamType=std::ostream>
398 void write(StreamType& o,
399 const std::string& line_start="# ",
400 const std::string& line_end="\n",
401 bool show_field_names=true) const {
402
403 auto pairs = getHeaderPairs();
404 for(auto p : pairs){
405 o << line_start;
406 if(show_field_names){
407 o << std::left << std::setw(10) << (p.first + ":");
408 }
409 o << p.second << line_end;
410 }
411
412 if(other.size() > 0){
413 o << line_start << "Other:" << line_end;
414 for(const std::string& oi : other){
415 o << line_start << " " << oi << line_end;
416 }
417 }
418 }
419
424 template <typename ...Args>
425 std::string stringize(const Args& ...args) const {
426 std::stringstream ss;
427 write(ss, args...);
428 return ss.str();
429 }
430
437 template <typename ...Args>
438 std::vector<std::string> stringizeToLines(const Args& ...args) const {
440 write(ss, args...);
441 return ss.getLines();
442 }
443
447 std::vector<std::pair<std::string, std::string>> getHeaderPairs() const {
448 std::vector<std::pair<std::string, std::string>> result;
449 result.emplace_back("Name", sim_name);
450 result.emplace_back("Cmdline", command_line);
451 result.emplace_back("Exe", executable);
452 result.emplace_back("SimulatorVersion", simulator_version);
453 result.emplace_back("Repro", reproduction_info);
454 result.emplace_back("Start", start_time);
455
456 //SimDB-recreated SimulationInfo objects have their
457 //elapsed time values stored in the database directly.
458 if (db_elapsed_time_.isValid()) {
459 result.emplace_back("Elapsed", db_elapsed_time_.getValue());
460 }
461
462 //Normal use of the SimulationInfo::getInstance() singleton,
463 //which is used during live SPARTA simulations, computes the
464 //elapsed time value via the TimeManager.
465 else {
466 std::stringstream timestr;
468 result.emplace_back("Elapsed", timestr.str());
469 }
470
471 last_captured_elapsed_time_ = result.back().second;
472 return result;
473 }
474
481 return last_captured_elapsed_time_;
482 }
483
484private:
485 mutable utils::ValidValue<std::string> last_captured_elapsed_time_;
486
487}; // class SimulationInfo
488
492inline std::ostream& operator<<(std::ostream& o, const SimulationInfo& info) {
493 info.write(o);
494 return o;
495}
496
497} // 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::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)
std::ostream &(* manip_t)(std::ostream &)
Default Constructor.
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(const simdb::ObjectManager &sim_db, const simdb::DatabaseID obj_mgr_db_id, const simdb::DatabaseID report_node_id=0)
Recreate a SimulationInfo object from the provided SimInfo record with the given ObjMgrID.
SimulationInfo()
Default Constructor.
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.
const value_type & getValue() const
Get the value - const version.
bool isValid() const
Is this value valid.
Macros for handling exponential backoff.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo