1 #ifndef UTIL_CLOCK_MAN_CUMULATIVE_RECORDS_H_INCLUDED
2 #define UTIL_CLOCK_MAN_CUMULATIVE_RECORDS_H_INCLUDED
43 template<
typename T = std::chrono::seconds>
56 unsigned int GetCount(
const std::string& name)
const
58 return IsPresent(name) ? m_map.at(name).first : 0U;
64 return IsPresent(name) ? m_map.at(name).second : T::duration::zero();
68 Duration
GetMean(
const std::string& name)
const
71 return (count > 0U) ?
GetCumulative(name) / count : T::duration::zero();
77 std::list<std::string> l;
78 for (
auto const& p : m_map) {
85 template <
typename U = std::chrono::seconds>
89 for (
auto const& p : m_map) {
104 return (m_map.find(name) != m_map.end());
111 Merge(extra.template GetRecords<Duration>());
117 for (
const auto& p : extra.m_map) {
118 const auto it = m_map.find(p.first);
119 if (it != m_map.end()) {
120 (it->second).first += (p.second).first;
121 (it->second).second += (p.second).second;
123 m_map[p.first] = p.second;
129 template<
typename R,
typename P>
130 void Record(
const std::string& name,
const std::chrono::duration<R, P>& duration)
132 const auto it = m_map.find(name);
133 if (it != m_map.end()) {
134 (it->second).first++;
135 (it->second).second += std::chrono::duration_cast<T>(duration);
137 m_map[name] = make_pair(1U, std::chrono::duration_cast<T>(duration));
142 void Record(
const std::string& name,
const Duration& duration)
144 const auto it = m_map.find(name);
145 if (it != m_map.end()) {
146 (it->second).first++;
147 (it->second).second += duration;
149 m_map[name] = make_pair(1U, duration);
154 std::map<std::string, std::pair<unsigned int, Duration>> m_map;
166 os << right <<
"timings: " << endl
167 << setw(14) <<
"name" <<
" | "
168 << setw(36) <<
" (hh:mm:ss: ms: us: ns) | (ms) | "
169 << setw(7) <<
"count" <<
" | "
170 << setw(36) <<
"(hh:mm:ss:ms:us:ns) | (ms) | " << endl
171 << string(100,
'-') << endl;
175 for (
auto const& name : name_list) {
176 auto const count = dr.
GetCount(name);
178 auto const cumul = duration_cast<nanoseconds>(cumul_val);
179 auto const avg_val = (count != 0) ? (cumul_val / count) : nanoseconds::zero();
180 auto const avg = duration_cast<nanoseconds>(avg_val);
183 << setw(14) << name <<
" | "
185 << setw(8) << scientific << setprecision(4) << cumul.count()/1000000 <<
" | "
186 << setw(7) << count <<
" | "
188 << setw(8) << scientific << setprecision(4) << avg.count()/1000000 <<
" | "<< endl;
202 os << right <<
"timings: " << endl
203 << setw(14) <<
"name" <<
" | "
204 << setw(32) <<
" (hh:mm:ss: ms: us) | (ms) | "
205 << setw(7) <<
"count" <<
" | "
206 << setw(32) <<
" (hh:mm:ss: ms: us) | (ms) | " << endl
207 << string(100,
'-') << endl;
211 for (
auto const& name : name_list) {
212 auto const count = dr.
GetCount(name);
214 auto const cumul = duration_cast<microseconds>(cumul_val);
215 auto const avg_val = (count != 0) ? (cumul_val / count) : microseconds::zero();
216 auto const avg = duration_cast<microseconds>(avg_val);
219 << setw(14) << name <<
" | "
221 << setw(8) << scientific << setprecision(4) << cumul.count()/1000000 <<
" | "
222 << setw(7) << count <<
" | "
224 << setw(8) << scientific << setprecision(4) << avg.count()/1000000 <<
" | "<< endl;
238 os << right <<
"timings: " << endl
239 << setw(14) <<
"name" <<
" | "
240 << setw(21) <<
"(hh:mm:ss:ms) | (ms) | "
241 << setw(7) <<
"count" <<
" | "
242 << setw(21) <<
"(hh:mm:ss:ms) | (ms) | " << endl
243 << string(80,
'-') << endl;
247 for (
auto const& name : name_list) {
248 auto const count = dr.
GetCount(name);
250 auto const cumul = duration_cast<milliseconds>(cumul_val);
251 auto const avg_val = (count != 0) ? (cumul_val / count) : milliseconds::zero();
252 auto const avg = duration_cast<milliseconds>(avg_val);
255 << setw(14) << name <<
" | "
257 << setw(8) << scientific << setprecision(4) << cumul.count() <<
" | "
258 << setw(7) << count <<
" | "
260 << setw(8) << scientific << setprecision(4) << avg.count() <<
" | "<< endl;
274 os << right <<
"timings: " << endl
275 << setw(14) <<
"name" <<
" | "
276 << setw(20) <<
" (hh:mm:ss) | (s) | "
277 << setw(7) <<
"count" <<
" | "
278 << setw(20) <<
" (hh:mm:ss) | (s) | " << endl
279 << string(76,
'-') << endl;
283 for (
auto const& name : name_list) {
284 auto const count = dr.
GetCount(name);
286 auto const cumul = duration_cast<seconds>(cumul_val);
287 auto const avg_val = (count != 0) ? (cumul_val / count) : seconds::zero();
288 auto const avg = duration_cast<seconds>(avg_val);
291 << setw(14) << name <<
" | "
293 << setw(7) << scientific << setprecision(4) << cumul.count() <<
" | "
294 << setw(7) << count <<
" | "
296 << setw(7) << scientific << setprecision(4) << avg.count() <<
" | "<< endl;
305 inline std::ostream& operator<<(std::ostream& os, CumulativeRecords<T>
const& dr)
309 os << right <<
"timings: name | total | count | mean |" << endl;
310 os << right <<
"-----------------------------------------------------------------------------" << endl;
312 auto name_list = dr.GetNames();
314 for (
auto const& name : name_list) {
315 os << right << setw(14) << name <<
" | "
317 << setw(10) << dr.GetCount(name) <<
" | "
327 #endif // end-of-include-guard
void Clear()
Clear the entire set of records.
bool IsPresent(const std::string &name) const
Return whether there are records associated with a given name.
void Merge(const CumulativeRecords< Duration > &extra)
Merge an extra set of records of same Duration (no casting).
Interface for TimeKeeper::Utils.
static std::string ToColonString(std::chrono::seconds d)
Procude string in hh:mm:ss format.
std::list< std::string > GetNames() const
Return list of names.
Utility class to record durations in a cumulative manner.
Namespace for the core simulator.
CumulativeRecords< Duration > GetRecords() const
Return records for all names in durations Duration (so no casting).
Duration GetCumulative(const std::string &name) const
Return cumulative time for records with name.
Duration GetMean(const std::string &name) const
Return cumulative time for records with name.
unsigned int GetCount(const std::string &name) const
Return count for records with name.
void Merge(const CumulativeRecords< U > &extra)
Merge an extra set of records (casting durations if required).
void Record(const std::string &name, const Duration &duration)
Record the duration for the given name.
std::ostream & operator<<(std::ostream &os, CumulativeRecords< std::chrono::nanoseconds > const &dr)
Nicely formated output with nanoseconds.
CumulativeRecords< U > GetRecords() const
Return records for all names in duration unit U.
void Record(const std::string &name, const std::chrono::duration< R, P > &duration)
Record the duration for the given name.