You.i Engine
YiProfileData.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_PROFILE_DATA_H_
3 #define _YI_PROFILE_DATA_H_
4 
5 #include "framework/YiPredef.h"
6 #include "utility/YiString.h"
7 
8 #include <glm/gtc/constants.hpp>
9 #include <glm/gtc/epsilon.hpp>
10 
16 
18 // Hold on to the last 'n' values
19 // Useful when real values need to be analyzed on top
20 // min / max / avg
21 static const uint32_t gHistoryLen = 1000;
22 
34 {
35 public:
36  enum class Accuracy
37  {
38  Seconds,
41  };
42 
43  class Average
44  {
45  public:
46  Average();
47  Average(uint32_t window);
48 
52  void Init(uint32_t window);
53 
54  void Add(uint64_t data);
55 
56  float Avg() const;
57 
58  uint64_t GetLast() const;
59 
60  private:
61  uint64_t m_total;
62  uint32_t m_window;
63 
64  std::unique_ptr<uint64_t[]> m_pDataN; // Data of N entries where matches
65  // the window size
66  uint32_t m_dataCnt;
67  uint32_t m_writeIdx;
68 
69  bool m_isFirstDataSkipped;
70 
72  };
73 
75 
79  void Reset();
80  void Init(const CYIString &name);
81  void ConfigureAvgWindow(uint32_t window);
82 
83  // Data Collection APIs:
84  /*
85  Either use MarkBegin() - MarkEnd() to mark
86  the beginning and end of an activity and let the PerfTimer
87  do the calculation, or simply pass a delta that you have calculated
88  with MarkDelta().
89  */
90  void MarkBegin();
91  void MarkEnd();
92  void MarkDelta(uint64_t delta);
93 
94  // Reporting APIs:
95  uint64_t Min() const;
96  uint64_t Max() const;
97  float Avg() const;
98  uint64_t Total() const;
99  uint64_t GetLastSample() const;
100  uint32_t GetSampleCnt() const;
101 
102  uint64_t GetLastPeriodLength() const;
103 
104  /* Creates a single line summary of collected statistics
105  reported in millisecond accuracy. Regular APIs report
106  results in microsecond accuracy.
107  */
109 
110  /* Creates a single line summary of collected statistics
111  reported in millisecond accuracy. Regular APIs report
112  results in microsecond accuracy. The format is XML as follows:
113  <profile name=...>
114  <min>...</min>
115  <max>...</max>
116  <avg>...</avg>
117  <fps>...</fps>
118  </profile>
119  */
121 
122  // This is the time spent between the end of cycle and the
123  // beginning of the other cycle.
124  float AvgDutyCycle() const;
125  float AvgPeriod() const;
126  float CalculateFPS() const;
127 
128 private:
129  uint64_t m_history[gHistoryLen];
130  uint32_t m_writeIdx;
131  uint32_t m_dataSampleCnt;
132 
133  uint64_t m_beginTime;
134  uint64_t m_delta;
135 
136  uint64_t m_dataTotal;
137  uint64_t m_min;
138  uint64_t m_max;
139 
140  Average m_avgSample;
141  Average m_avgPeriod;
142 
143  bool m_profiling;
144 
145  CYIString m_Name;
146 };
147 
150 inline uint64_t CYIProfileData::Min() const
151 {
152  return (m_dataSampleCnt > 1) ? m_min : 0;
153 }
154 
155 inline uint64_t CYIProfileData::Max() const
156 {
157  return (m_dataSampleCnt > 1) ? m_max : 0;
158 }
159 
160 inline float CYIProfileData::Avg() const
161 {
162  return (m_dataSampleCnt > 1) ? m_avgSample.Avg() : 0.0f;
163 }
164 
165 inline uint64_t CYIProfileData::Total() const
166 {
167  return (m_dataSampleCnt > 1) ? m_dataTotal : 0;
168 }
169 
170 inline uint64_t CYIProfileData::GetLastSample() const
171 {
172  return m_delta;
173 }
174 inline uint32_t CYIProfileData::GetSampleCnt() const
175 {
176  return (m_dataSampleCnt > 1) ? (m_dataSampleCnt - 1) : 0;
177 }
178 
179 inline float CYIProfileData::AvgDutyCycle() const
180 {
181  float period = m_avgPeriod.Avg();
182 
183  return (!glm::epsilonEqual(period, 0.0f, glm::epsilon<float>())) ? Avg() * 100 / period : 0;
184 }
185 inline float CYIProfileData::AvgPeriod() const
186 {
187  return (float)m_avgPeriod.Avg();
188 }
189 
190 inline uint64_t CYIProfileData::GetLastPeriodLength() const
191 {
192  return m_avgPeriod.GetLast();
193 }
194 
195 #endif // _YI_PROFILE_DATA_H_
uint32_t GetSampleCnt() const
Definition: YiProfileData.h:174
#define YI_DISALLOW_COPY_AND_ASSIGN(TypeName)
Delete the copy constructor and assignment operator (and consequently the move constructor as well) ...
Definition: YiPredef.h:122
uint64_t Max() const
Definition: YiProfileData.h:155
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:36
void Init(const CYIString &name)
Accuracy
Definition: YiProfileData.h:36
float AvgDutyCycle() const
Definition: YiProfileData.h:179
float Avg() const
Definition: YiProfileData.h:160
uint64_t Total() const
Definition: YiProfileData.h:165
uint64_t GetLastSample() const
Definition: YiProfileData.h:170
void MarkDelta(uint64_t delta)
uint64_t GetLastPeriodLength() const
Definition: YiProfileData.h:190
CYIString Report(Accuracy accuracy=Accuracy::Milliseconds) const
void ConfigureAvgWindow(uint32_t window)
CYIString ReportXML(Accuracy accuracy=Accuracy::Milliseconds) const
Definition: YiProfileData.h:43
This class is used as a profiling or a measurement tool for recurring activities. By marking the begi...
Definition: YiProfileData.h:33
float AvgPeriod() const
Definition: YiProfileData.h:185
float CalculateFPS() const
uint64_t Min() const
Definition: YiProfileData.h:150