HEBench
hebench_report_cpp.h
Go to the documentation of this file.
1 
2 // Copyright (C) 2021 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef _HEBench_Harness_Report_CPP_H_0596d40a3cce4b108a81595c50eb286d
6 #define _HEBench_Harness_Report_CPP_H_0596d40a3cce4b108a81595c50eb286d
7 
8 #include <ratio>
9 #include <stdexcept>
10 #include <string>
11 
12 #include "hebench_report_types.h"
13 
14 namespace hebench {
15 namespace ReportGen {
16 namespace cpp {
17 
18 //class ReportSummary;
19 
21 {
22 public:
23  enum class TimeUnit
24  {
25  Default,
26  Seconds,
30  };
31 
32  static constexpr TimeUnit getPrefix(char ch_prefix)
33  {
34  switch (ch_prefix)
35  {
36  case 0:
37  return TimeUnit::Default;
38  break;
39  case 's':
40  return TimeUnit::Seconds;
41  break;
42  case 'm':
44  break;
45  case 'u':
47  break;
48  case 'n':
49  return TimeUnit::Nanoseconds;
50  break;
51  default:
52  throw std::invalid_argument("Unknown prefix.");
53  break;
54  } // end switch
55  }
56 
57  static constexpr char getPrefix(TimeUnit unit)
58  {
59  switch (unit)
60  {
61  case TimeUnit::Default:
62  return 0;
63  break;
64  case TimeUnit::Seconds:
65  return 's';
66  break;
68  return 'm';
69  break;
71  return 'u';
72  break;
74  return 'n';
75  break;
76  default:
77  throw std::invalid_argument("Unknown prefix.");
78  break;
79  } // end switch
80  }
81 
110  static void setTimingPrefix(TimingPrefixedSeconds &prefix, double seconds, char ch_prefix);
111  static void setTimingPrefix(TimingPrefixedSeconds &prefix, double seconds, TimeUnit unit)
112  {
113  setTimingPrefix(prefix, seconds, getPrefix(unit));
114  }
135  static void computeTimingPrefix(TimingPrefixedSeconds &prefix, double seconds);
136 };
137 
139 {
140  friend class ReportSummary;
141 
142 private:
143  static constexpr const char *m_private_class_name = "TimingReport";
144 
145 public:
146  TimingReport(const TimingReport &) = delete;
147 
148 public:
149  TimingReport(const std::string &header = std::string());
151  virtual ~TimingReport();
152 
154 
155  void setHeader(const std::string &new_header);
156  void appendHeader(const std::string &new_header, bool new_line = true);
157  void prependHeader(const std::string &new_header, bool new_line = true);
158  std::string getHeader() const;
159 
160  void setFooter(const std::string &new_footer);
161  void appendFooter(const std::string &new_header, bool new_line = true);
162  void prependFooter(const std::string &new_header, bool new_line = true);
163  std::string getFooter() const;
164 
179  void addEventType(uint32_t event_type_id, const std::string &event_type_header, bool is_main_event = false);
180  bool hasEventType(uint32_t event_type_id) const;
181  std::string getEventTypeHeader(uint32_t event_type_id) const;
182  uint64_t getEventTypeCount() const;
191  uint32_t getEventType(uint64_t index) const;
192  uint32_t getMainEventType() const;
193 
194  // events management
195 
196  void addEvent(const TimingReportEventC &p_event);
197  void getEvent(TimingReportEventC &p_event, uint64_t index) const;
198  uint64_t getEventCount() const;
199  uint64_t getEventCapacity() const;
200  void setEventCapacity(uint64_t new_capacity);
201  void clear();
202 
203  // CSV
204 
205  void save2CSV(const std::string &filename);
206  std::string convert2CSV();
207 
208  static TimingReport loadReportFromCSV(const std::string &s_csv_content);
209  static TimingReport loadReportFromCSVFile(const std::string &filename);
210 
211  template <class TimeInterval = std::ratio<1, 1>>
212  static double computeElapsedWallTime(const TimingReportEventC &event);
213  template <class TimeInterval = std::ratio<1, 1>>
214  static double computeElapsedCPUTime(const TimingReportEventC &event);
215 
216 private:
217  void *m_lib_handle;
218 };
219 
220 template <class TimeInterval>
222 {
223  return (std::abs(event.wall_time_end - event.wall_time_start)
224  * (event.time_interval_ratio_num * TimeInterval::den))
225  / (event.time_interval_ratio_den * TimeInterval::num);
226 }
227 
228 template <class TimeInterval>
230 {
231  return (std::abs(event.cpu_time_end - event.cpu_time_start)
232  * (event.time_interval_ratio_num * TimeInterval::den))
233  / (event.time_interval_ratio_den * TimeInterval::num);
234 }
235 
236 //class ReportSummary
237 //{
238 //private:
239 // static constexpr const char *m_private_class_name = "ReportSummary";
240 
241 //public:
242 // ReportSummary(const TimingReport &report);
243 //};
244 
245 } // namespace cpp
246 } // namespace ReportGen
247 } // namespace hebench
248 
249 #endif // defined _HEBench_Harness_Report_CPP_H_0596d40a3cce4b108a81595c50eb286d
static void setTimingPrefix(TimingPrefixedSeconds &prefix, double seconds, TimeUnit unit)
static constexpr TimeUnit getPrefix(char ch_prefix)
static void setTimingPrefix(TimingPrefixedSeconds &prefix, double seconds, char ch_prefix)
Converts the time in seconds to the specified time unit.
static constexpr char getPrefix(TimeUnit unit)
static void computeTimingPrefix(TimingPrefixedSeconds &prefix, double seconds)
Given a time interval in seconds, computes the timing prefix.
TimingReport & operator=(TimingReport &&)
bool hasEventType(uint32_t event_type_id) const
static TimingReport loadReportFromCSVFile(const std::string &filename)
void save2CSV(const std::string &filename)
static TimingReport loadReportFromCSV(const std::string &s_csv_content)
void setFooter(const std::string &new_footer)
static double computeElapsedWallTime(const TimingReportEventC &event)
void addEvent(const TimingReportEventC &p_event)
void prependFooter(const std::string &new_header, bool new_line=true)
void setEventCapacity(uint64_t new_capacity)
void getEvent(TimingReportEventC &p_event, uint64_t index) const
void prependHeader(const std::string &new_header, bool new_line=true)
void addEventType(uint32_t event_type_id, const std::string &event_type_header, bool is_main_event=false)
Adds a new event type to the types of events.
std::string getEventTypeHeader(uint32_t event_type_id) const
void appendFooter(const std::string &new_header, bool new_line=true)
static double computeElapsedCPUTime(const TimingReportEventC &event)
uint32_t getEventType(uint64_t index) const
Retrieve an event type ID.
TimingReport(const TimingReport &)=delete
void setHeader(const std::string &new_header)
void appendHeader(const std::string &new_header, bool new_line=true)
int64_t time_interval_ratio_num
Scale of time interval used for this event.