You.i Engine
YiProfileData.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2017. 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 
37  enum ACCURACY {
41  };
42 
43  class Average
44  {
45  public:
46  Average();
47  Average(uint32_t uWindow);
48  ~Average();
49 
53  void Init(uint32_t uWindow);
54 
55  void Add(uint64_t uData);
56 
57  float Avg() const;
58 
59  private:
60  uint64_t m_uTotal;
61  uint32_t m_uWindow;
62 
63  uint64_t *m_pDataN; // Data of N entries where matches
64  // the window size
65  uint32_t m_uDataCnt;
66  uint32_t m_uWriteIdx;
67 
68  bool m_bIsFirstDataSkipped;
69 
71  };
72 
74 
78  void Reset();
79  void Init(const CYIString &name);
80  void ConfigureAvgWindow(uint32_t uWindow);
81 
82  // Data Collection APIs:
83  /*
84  Either use MarkBegin() - MarkEnd() to mark
85  the beginning and end of an activity and let the PerfTimer
86  do the calculation, or simply pass a delta that you have calculated
87  with MarkDelta().
88  */
89  void MarkBegin();
90  void MarkEnd();
91  void MarkDelta(uint64_t uDelta);
92 
93  // Reporting APIs:
94  uint64_t Min() const;
95  uint64_t Max() const;
96  float Avg() const;
97  uint64_t Total() const;
98  uint64_t GetLastSample() const;
99  uint32_t GetSampleCnt() const;
100 
101  /* Creates a single line summary of collected statistics
102  reported in millisecond accuracy. Regular APIs report
103  results in microsecond accuracy.
104  */
105  CYIString Report(ACCURACY eAccuracy = MILLISECONDS) const;
106 
107  /* Creates a single line summary of collected statistics
108  reported in millisecond accuracy. Regular APIs report
109  results in microsecond accuracy. The format is XML as follows:
110  <profile name=...>
111  <min>...</min>
112  <max>...</max>
113  <avg>...</avg>
114  <fps>...</fps>
115  </profile>
116  */
117  CYIString ReportXML(ACCURACY eAccuracy = MILLISECONDS) const;
118 
119  // This is the time spent between the end of cycle and the
120  // beginning of the other cycle.
121  float AvgDutyCycle() const;
122  float AvgPeriod() const;
123  float CalculateFPS() const;
124 
125 private:
126  uint64_t m_uHistory[gHistoryLen];
127  uint32_t m_uWriteIdx;
128  uint32_t m_uDataSampleCnt;
129 
130  uint64_t m_uBeginTime;
131  uint64_t m_uDelta;
132 
133  uint64_t m_uDataTotal;
134  uint64_t m_uMin;
135  uint64_t m_uMax;
136 
137  Average m_nAvgSample;
138  Average m_nAvgPeriod;
139 
140  bool m_bProfiling;
141 
142  CYIString m_Name;
143 };
144 
147 inline uint64_t CYIProfileData::Min() const
148 {
149  return (m_uDataSampleCnt > 1) ? m_uMin : 0;
150 }
151 
152 inline uint64_t CYIProfileData::Max() const
153 {
154  return (m_uDataSampleCnt > 1) ? m_uMax : 0;
155 }
156 
157 inline float CYIProfileData::Avg() const
158 {
159  return (m_uDataSampleCnt > 1) ? m_nAvgSample.Avg() : 0.0f;
160 }
161 
162 inline uint64_t CYIProfileData::Total() const
163 {
164  return (m_uDataSampleCnt > 1) ? m_uDataTotal : 0;
165 }
166 
167 inline uint64_t CYIProfileData::GetLastSample() const
168 {
169  return m_uDelta;
170 }
171 inline uint32_t CYIProfileData::GetSampleCnt() const
172 {
173  return (m_uDataSampleCnt > 1) ? (m_uDataSampleCnt-1) : 0;
174 }
175 
176 inline float CYIProfileData::AvgDutyCycle() const
177 {
178  float period = m_nAvgPeriod.Avg();
179 
180  return (!glm::epsilonEqual(period, 0.0f, glm::epsilon<float>())) ? Avg() * 100 / period : 0;
181 }
182 inline float CYIProfileData::AvgPeriod() const
183 {
184  return (float)m_nAvgPeriod.Avg();
185 }
186 
187 #endif // _YI_PROFILE_DATA_H_
Definition: YiProfileData.h:40
#define YI_DISALLOW_COPY_AND_ASSIGN(TypeName)
Delete the copy constructor and assignment operator (and consequently the move constructor as well) ...
Definition: YiPredef.h:114
ACCURACY
Definition: YiProfileData.h:37
void MarkDelta(uint64_t uDelta)
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:35
CYIString Report(ACCURACY eAccuracy=MILLISECONDS) const
void Add(uint64_t uData)
uint32_t GetSampleCnt() const
Definition: YiProfileData.h:171
void ConfigureAvgWindow(uint32_t uWindow)
uint64_t Total() const
Definition: YiProfileData.h:162
float CalculateFPS() const
float Avg() const
Definition: YiProfileData.h:157
float AvgDutyCycle() const
Definition: YiProfileData.h:176
Definition: YiProfileData.h:38
uint64_t Max() const
Definition: YiProfileData.h:152
void Init(uint32_t uWindow)
CYIString ReportXML(ACCURACY eAccuracy=MILLISECONDS) const
uint64_t GetLastSample() const
Definition: YiProfileData.h:167
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:182
Definition: YiProfileData.h:39
uint64_t Min() const
Definition: YiProfileData.h:147