VPTissue Reference Manual
CumulativeRecords.h
Go to the documentation of this file.
1 #ifndef UTIL_CLOCK_MAN_CUMULATIVE_RECORDS_H_INCLUDED
2 #define UTIL_CLOCK_MAN_CUMULATIVE_RECORDS_H_INCLUDED
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
22 #include "Utils.h"
23 
24 #include <chrono>
25 #include <cmath>
26 #include <iostream>
27 #include <iomanip>
28 #include <list>
29 #include <map>
30 #include <numeric>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 namespace SimPT_Sim {
36 namespace ClockMan {
37 
43 template<typename T = std::chrono::seconds>
45 {
46 public:
47  using Duration = T;
48 
50  void Clear()
51  {
52  m_map.clear();
53  }
54 
56  unsigned int GetCount(const std::string& name) const
57  {
58  return IsPresent(name) ? m_map.at(name).first : 0U;
59  }
60 
62  Duration GetCumulative(const std::string& name) const
63  {
64  return IsPresent(name) ? m_map.at(name).second : T::duration::zero();
65  }
66 
68  Duration GetMean(const std::string& name) const
69  {
70  const auto count = GetCount(name);
71  return (count > 0U) ? GetCumulative(name) / count : T::duration::zero();
72  }
73 
75  std::list<std::string> GetNames() const
76  {
77  std::list<std::string> l;
78  for (auto const& p : m_map) {
79  l.push_back(p.first);
80  }
81  return l;
82  }
83 
85  template <typename U = std::chrono::seconds>
87  {
89  for (auto const& p : m_map) {
90  rec.Record(p.first, GetCumulative(p.first));
91  }
92  return rec;
93  }
94 
97  {
98  return *this;
99  }
100 
102  bool IsPresent(const std::string& name) const
103  {
104  return (m_map.find(name) != m_map.end());
105  }
106 
108  template<typename U>
109  void Merge(const CumulativeRecords<U>& extra)
110  {
111  Merge(extra.template GetRecords<Duration>());
112  }
113 
116  {
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;
122  } else {
123  m_map[p.first] = p.second;
124  }
125  }
126  }
127 
129  template<typename R, typename P>
130  void Record(const std::string& name, const std::chrono::duration<R, P>& duration)
131  {
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);
136  } else {
137  m_map[name] = make_pair(1U, std::chrono::duration_cast<T>(duration));
138  }
139  }
140 
142  void Record(const std::string& name, const Duration& duration)
143  {
144  const auto it = m_map.find(name);
145  if (it != m_map.end()) {
146  (it->second).first++;
147  (it->second).second += duration;
148  } else {
149  m_map[name] = make_pair(1U, duration);
150  }
151  }
152 
153 private:
154  std::map<std::string, std::pair<unsigned int, Duration>> m_map;
155 };
156 
160 inline std::ostream& operator<<(std::ostream& os,
162 {
163  using namespace std;
164  using namespace std::chrono;
165 
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;
172 
173  auto name_list = dr.GetNames();
174  name_list.sort();
175  for (auto const& name : name_list) {
176  auto const count = dr.GetCount(name);
177  auto const cumul_val = dr.GetCumulative(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);
181 
182  os << right
183  << setw(14) << name << " | "
184  << setw(23) << Utils::ToColonString(cumul) << " | "
185  << setw(8) << scientific << setprecision(4) << cumul.count()/1000000 << " | "
186  << setw(7) << count << " | "
187  << setw(23) << Utils::ToColonString(avg) << " | "
188  << setw(8) << scientific << setprecision(4) << avg.count()/1000000 << " | "<< endl;
189  }
190  return os;
191 }
192 
196 inline std::ostream& operator<<(std::ostream& os,
198 {
199  using namespace std;
200  using namespace std::chrono;
201 
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;
208 
209  auto name_list = dr.GetNames();
210  name_list.sort();
211  for (auto const& name : name_list) {
212  auto const count = dr.GetCount(name);
213  auto const cumul_val = dr.GetCumulative(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);
217 
218  os << right
219  << setw(14) << name << " | "
220  << setw(19) << Utils::ToColonString(cumul) << " | "
221  << setw(8) << scientific << setprecision(4) << cumul.count()/1000000 << " | "
222  << setw(7) << count << " | "
223  << setw(19) << Utils::ToColonString(avg) << " | "
224  << setw(8) << scientific << setprecision(4) << avg.count()/1000000 << " | "<< endl;
225  }
226  return os;
227 }
228 
232 inline std::ostream& operator<<(std::ostream& os,
234 {
235  using namespace std;
236  using namespace std::chrono;
237 
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;
244 
245  auto name_list = dr.GetNames();
246  name_list.sort();
247  for (auto const& name : name_list) {
248  auto const count = dr.GetCount(name);
249  auto const cumul_val = dr.GetCumulative(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);
253 
254  os << right
255  << setw(14) << name << " | "
256  << setw(13) << Utils::ToColonString(cumul) << " | "
257  << setw(8) << scientific << setprecision(4) << cumul.count() << " | "
258  << setw(7) << count << " | "
259  << setw(13) << Utils::ToColonString(avg) << " | "
260  << setw(8) << scientific << setprecision(4) << avg.count() << " | "<< endl;
261  }
262  return os;
263 }
264 
268 inline std::ostream& operator<<(std::ostream& os,
270 {
271  using namespace std;
272  using namespace std::chrono;
273 
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;
280 
281  auto name_list = dr.GetNames();
282  name_list.sort();
283  for (auto const& name : name_list) {
284  auto const count = dr.GetCount(name);
285  auto const cumul_val = dr.GetCumulative(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);
289 
290  os << right
291  << setw(14) << name << " | "
292  << setw(12) << Utils::ToColonString(cumul) << " | "
293  << setw(7) << scientific << setprecision(4) << cumul.count() << " | "
294  << setw(7) << count << " | "
295  << setw(12) << Utils::ToColonString(avg) << " | "
296  << setw(7) << scientific << setprecision(4) << avg.count() << " | "<< endl;
297  }
298  return os;
299 }
300 
304 template<typename T>
305 inline std::ostream& operator<<(std::ostream& os, CumulativeRecords<T> const& dr)
306 {
307  using namespace std;
308 
309  os << right << "timings: name | total | count | mean |" << endl;
310  os << right << "-----------------------------------------------------------------------------" << endl;
311 
312  auto name_list = dr.GetNames();
313  name_list.sort();
314  for (auto const& name : name_list) {
315  os << right << setw(14) << name << " | "
316  << setw(20) << Utils::ToColonString(dr.GetCumulative(name)) << " | "
317  << setw(10) << dr.GetCount(name) << " | "
318  << setw(20) << Utils::ToColonString(dr.GetMean(name)) << " | "
319  << endl;
320  }
321  return os;
322 }
323 
324 } // namespace
325 } // namespace
326 
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.
STL namespace.
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.
Definition: Utils.h:42
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.