OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Stat.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2013-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // statistics
17 #ifndef INC_C4Stat
18 #define INC_C4Stat
19 
20 class C4Stat;
21 
22 // *** main statistic class
23 // should only been constructed once per application
25 {
26  friend class C4Stat;
27 
28 public:
29  C4MainStat();
30  ~C4MainStat();
31 
32  void Show();
33  void ShowPart(int FrameCounter);
34 
35  void Reset();
36  void ResetPart();
37 
38 protected:
40 
41  void RegisterStat(C4Stat* pStat);
42  void UnRegStat(C4Stat* pStat);
43 };
44 
45 
46 // *** one statistic.
47 // Holds the data about one "checkpoint" in code
48 // registers himself to C4MainStat when Start() is called the first time
49 class C4Stat
50 {
51  friend class C4MainStat;
52 
53 public:
54  C4Stat(const char* strName);
55  ~C4Stat();
56 
57  inline void Start()
58  {
59  if (!iStartCalled)
61  iCount ++;
62  iCountPart ++;
63  iStartCalled ++;
64  }
65 
66  inline void Stop()
67  {
68  assert(iStartCalled);
69  iStartCalled --;
70  if (!iStartCalled && iCount >= 100)
71  {
72  uint32_t tTime = C4TimeMilliseconds::Now() - tStartTime;
73 
74  tTimeSum += tTime;
75  tTimeSumPart += tTime;
76  }
77  }
78 
79  void Reset();
80  void ResetPart();
81 
82  static C4MainStat *getMainStat();
83 
84 protected:
85 
86  // used by C4MainStat
89 
91 
92  // start-call depth
93  unsigned int iStartCalled;
94 
95 
96  // ** statistic data
97 
98  // sum of times
99  uint32_t tTimeSum;
100 
101  // number of starts called
102  unsigned int iCount;
103 
104  // ** statistic data (partial stat)
105 
106  // sum of times
107  uint32_t tTimeSumPart;
108 
109  // number of starts called
110  unsigned int iCountPart;
111 
112 
113  // name of statistic
114  const char* strName;
115 
116  // registred?
118 
119 };
120 
121 // *** some directives
122 #ifdef STAT
123 
124 // used to create and start a new C4Stat object
125 #define C4ST_STARTNEW(StatName, strName) static C4Stat StatName(strName); StatName.Start();
126 
127 // used to create a new C4Stat object
128 #define C4ST_NEW(StatName, strName) C4Stat StatName(strName);
129 
130 // used to start an existing C4Stat object
131 #define C4ST_START(StatName) StatName.Start();
132 
133 // used to stop an existing C4Stat object
134 #define C4ST_STOP(StatName) StatName.Stop();
135 
136 // shows the statistic (to log)
137 #define C4ST_SHOWSTAT C4Stat::getMainStat()->Show();
138 
139 // shows the statistic (to log)
140 #define C4ST_SHOWPARTSTAT(FrameCounter) C4Stat::getMainStat()->ShowPart(FrameCounter);
141 
142 // resets the whole statistic
143 #define C4ST_RESET C4Stat::getMainStat()->Reset();
144 
145 // resets the partial statistic
146 #define C4ST_RESETPART C4Stat::getMainStat()->ResetPart();
147 
148 #else
149 
150 #define C4ST_STARTNEW(StatName, strName)
151 #define C4ST_NEW(StatName, strName)
152 #define C4ST_START(StatName)
153 #define C4ST_STOP(StatName)
154 #define C4ST_SHOWSTAT
155 #define C4ST_SHOWPARTSTAT(FrameCounter)
156 #define C4ST_RESET
157 #define C4ST_RESETPART
158 
159 #endif
160 
161 #endif // INC_C4Stat
void RegisterStat(C4Stat *pStat)
Definition: C4Stat.cpp:32
C4Stat * pPrev
Definition: C4Stat.h:88
C4Stat * pNext
Definition: C4Stat.h:87
void Reset()
Definition: C4Stat.cpp:181
void ShowPart(int FrameCounter)
Definition: C4Stat.cpp:152
uint32_t tTimeSumPart
Definition: C4Stat.h:107
void Reset()
Definition: C4Stat.cpp:73
unsigned int iStartCalled
Definition: C4Stat.h:93
void Stop()
Definition: C4Stat.h:66
Definition: C4Stat.h:49
void ResetPart()
Definition: C4Stat.cpp:191
C4Stat(const char *strName)
Definition: C4Stat.cpp:169
C4Stat * pFirst
Definition: C4Stat.h:39
void Show()
Definition: C4Stat.cpp:86
const char * strName
Definition: C4Stat.h:114
uint32_t tTimeSum
Definition: C4Stat.h:99
C4TimeMilliseconds tStartTime
Definition: C4Stat.h:90
unsigned int iCount
Definition: C4Stat.h:102
void UnRegStat(C4Stat *pStat)
Definition: C4Stat.cpp:50
void ResetPart()
Definition: C4Stat.cpp:79
C4MainStat()
Definition: C4Stat.cpp:23
unsigned int iCountPart
Definition: C4Stat.h:110
~C4MainStat()
Definition: C4Stat.cpp:28
~C4Stat()
Definition: C4Stat.cpp:176
static C4MainStat * getMainStat()
Definition: C4Stat.cpp:197
static C4TimeMilliseconds Now()
bool bRegistred
Definition: C4Stat.h:117
void Start()
Definition: C4Stat.h:57