SimulationTrace.h
1 
10 #ifndef WRENCH_SIMULATIONTRACE_H
11 #define WRENCH_SIMULATIONTRACE_H
12 
13 #include <vector>
14 #include <map>
15 #include <cmath>
16 #include <cfloat>
17 #include <algorithm>
18 
19 #include "wrench/simulation/SimulationTimestamp.h"
20 
21 namespace wrench {
22 
23 
24  /***********************/
26  /***********************/
27 
29  class GenericSimulationTrace {
30 
31  public:
32  virtual ~GenericSimulationTrace() {}
33 
34  };
35  /***********************/
37  /***********************/
38 
39  /***********************/
41  /***********************/
42 
48  template <class T> class SimulationTrace : public GenericSimulationTrace {
49 
50  public:
51 
59  this->trace.push_back(timestamp);
60  }
61 
62 
69  std::vector<SimulationTimestamp<T> *> getTrace() {
70  return this->trace;
71  }
72 
77  for (auto &timestamp : this->trace) {
78  delete timestamp;
79  }
80  }
81 
82 
83  private:
84  std::vector<SimulationTimestamp<T> *> trace;
85  };
86 
87 
91  template <>
92  class SimulationTrace<SimulationTimestampPstateSet> : public GenericSimulationTrace {
93  public:
94 
100  auto hostname = new_timestamp->getContent()->getHostname();
101 
102  auto hostname_search = latest_timestamps_by_host.find(hostname);
103  if (hostname_search == latest_timestamps_by_host.end()) {
104  // no pstate timestamps associated to this hostname have been added yet
105  this->trace.push_back(new_timestamp);
106  this->latest_timestamps_by_host[hostname] = this->trace.size() - 1;
107  } else {
108  // a pstate timestamp associated to this host already exists
109  SimulationTimestamp<SimulationTimestampPstateSet> *& latest_timestamp = this->trace[this->latest_timestamps_by_host[hostname]];
110 
111  // if the new_timestamp has the same date as the latest_timestamp, then the new_timestamp replaces
112  // the latest_timestamp in the trace, else the new time_stamp is added to the trace and the map of latest
113  // timestamps is updated to reflect this change
114  if (std::fabs(new_timestamp->getDate() - latest_timestamp->getDate()) < DBL_EPSILON) {
115  std::swap(new_timestamp, latest_timestamp);
116 
117  // now, latest_timestamp points to the contents of new_timestamp
118  // and new_timestamp points to the contents of latest_timestamp so
119  // we need to delete new_timestamp
120  delete new_timestamp;
121  } else {
122  if (new_timestamp->getDate() > latest_timestamp->getDate()) {
123  this->trace.push_back(new_timestamp);
124  this->latest_timestamps_by_host[hostname] = this->trace.size() - 1;
125  } else {
126  throw std::runtime_error(
127  "SimulationTrace<SimulationTimestampPstateSet>::addTimestamp() timestamps out of order");
128  }
129  }
130  }
131  }
132 
137  std::vector<SimulationTimestamp<SimulationTimestampPstateSet> *> getTrace() {
138  return this->trace;
139  }
140 
145  for (auto &timestamp: this->trace) {
146  delete timestamp;
147  }
148  }
149 
150  private:
151  std::map<std::string, size_t> latest_timestamps_by_host;
152  std::vector<SimulationTimestamp<SimulationTimestampPstateSet> *> trace;
153  };
154 
155  /***********************/
157  /***********************/
158 
159 };
160 
161 
162 #endif //WRENCH_SIMULATIONTRACE_H
std::vector< SimulationTimestamp< T > * > getTrace()
Retrieve the trace as a vector of timestamps.
Definition: SimulationTrace.h:69
A template class to represent a trace of timestamps.
Definition: SimulationTrace.h:48
A time-stamped simulation event stored in SimulationOutput.
Definition: SimulationTimestamp.h:26
void addTimestamp(SimulationTimestamp< SimulationTimestampPstateSet > *new_timestamp)
Append a SimulationTimestampPstateSet timestamp to the trace.
Definition: SimulationTrace.h:99
Definition: Alarm.cpp:20
std::vector< SimulationTimestamp< SimulationTimestampPstateSet > * > getTrace()
Retrieve the trace as a vector of SimulationTimestamp<SimulationTimestampPstateSet> timestamps.
Definition: SimulationTrace.h:137
A specialized class to represent a trace of SimulationTimestampPstateSet timestamps.
Definition: SimulationTrace.h:92
void addTimestamp(SimulationTimestamp< T > *timestamp)
Append a timestamp to the trace.
Definition: SimulationTrace.h:58
double getDate()
Definition: SimulationTimestamp.h:44
A simulation timestamp class for changes in a host's pstate.
Definition: SimulationTimestampTypes.h:569
T *const getContent()
Definition: SimulationTimestamp.h:35