OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Stat.cpp
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) 2009-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 
18 #include "C4Include.h"
19 #include "lib/C4Stat.h"
20 
21 // ** implemetation of C4MainStat
22 
24  : pFirst(0)
25 {
26 }
27 
29 {
30 }
31 
33 {
34  // add to list
35  if (!pFirst)
36  {
37  pFirst = pStat;
38  pStat->pNext = 0;
39  pStat->pPrev = 0;
40  }
41  else
42  {
43  pStat->pNext = pFirst;
44  pFirst->pPrev = pStat;
45  pStat->pPrev = 0;
46  pFirst = pStat;
47  }
48 }
49 
51 {
52  // first item?
53  if (!pStat->pPrev)
54  {
55  pFirst = pStat->pNext;
56  pStat->pNext = 0;
57  }
58  // last item?
59  else if (!pStat->pNext)
60  {
61  pStat->pPrev->pNext = 0;
62  pStat->pPrev = 0;
63  }
64  else
65  {
66  pStat->pNext->pPrev = pStat->pPrev;
67  pStat->pPrev->pNext = pStat->pNext;
68  pStat->pNext = 0;
69  pStat->pPrev = 0;
70  }
71 }
72 
74 {
75  for (C4Stat* pAkt = pFirst; pAkt; pAkt = pAkt->pNext)
76  pAkt->Reset();
77 }
78 
80 {
81  for (C4Stat* pAkt = pFirst; pAkt; pAkt = pAkt->pNext)
82  pAkt->ResetPart();
83 }
84 
85 
87 {
88 
89  // count stats
90  unsigned int iCnt = 0;
91  C4Stat* pAkt;
92  for (pAkt = pFirst; pAkt; pAkt = pAkt->pNext)
93  iCnt++;
94 
95  // create array
96  C4Stat** StatArray = new C4Stat*[iCnt];
97  bool* bHS = new bool[iCnt];
98 
99  // sort it
100  unsigned int i,ii;
101  for (ii=0; ii<iCnt; ii++) bHS[ii] = false;
102  for (i=0; i<iCnt; i++)
103  {
104  C4Stat* pBestStat = nullptr;
105  unsigned int iBestNr = ~0;
106 
107  for (ii=0, pAkt = pFirst; ii<iCnt; ii++, pAkt = pAkt->pNext)
108  if (!bHS[ii])
109  {
110  if (iBestNr == ~0u)
111  {
112  iBestNr = ii;
113  pBestStat = pAkt;
114  }
115  else if (stricmp(pBestStat->strName, pAkt->strName) > 0)
116  {
117  iBestNr = ii;
118  pBestStat = pAkt;
119  }
120  }
121 
122  if (iBestNr == (unsigned int) -1)
123  break;
124  bHS[iBestNr] = true;
125 
126  StatArray[i] = pBestStat;
127  }
128 
129  delete [] bHS;
130 
131  LogSilent("** Stat");
132 
133  // output in order
134  for (i=0; i<iCnt; i++)
135  {
136  pAkt = StatArray[i];
137 
138  // output it!
139  if (pAkt->iCount)
140  LogSilentF("%s: n = %u, t = %u, td = %.2f",
141  pAkt->strName, pAkt->iCount, pAkt->tTimeSum,
142  double(pAkt->tTimeSum) / pAkt->iCount * 1000);
143  }
144 
145  // delete...
146  delete[] StatArray;
147 
148  // ok. job done
149  LogSilent("** Stat end");
150 }
151 
152 void C4MainStat::ShowPart(int FrameCounter)
153 {
154  C4Stat* pAkt;
155 
156  // insert tick nr
157  LogSilentF("** PartStat begin %d", FrameCounter);
158 
159  // insert all stats
160  for (pAkt = pFirst; pAkt; pAkt = pAkt->pNext)
161  LogSilentF("%s: n=%u, t=%u", pAkt->strName, pAkt->iCountPart, pAkt->tTimeSumPart);
162 
163  // insert part stat end idtf
164  LogSilentF("** PartStat end\n");
165 }
166 
167 // ** implemetation of C4Stat
168 
169 C4Stat::C4Stat(const char* strnName)
170  : strName(strnName)
171 {
172  Reset();
173  getMainStat()->RegisterStat(this);
174 }
175 
177 {
178  getMainStat()->UnRegStat(this);
179 }
180 
182 {
183  iStartCalled = 0;
184 
185  tTimeSum = 0;
186  iCount = 0;
187 
188  ResetPart();
189 }
190 
192 {
193  tTimeSumPart = 0;
194  iCountPart = 0;
195 }
196 
198 {
199  static C4MainStat *pMainStat = new C4MainStat();
200  return pMainStat;
201 }
void RegisterStat(C4Stat *pStat)
Definition: C4Stat.cpp:32
C4Stat * pPrev
Definition: C4Stat.h:88
bool LogSilent(const char *szMessage, bool fConsole)
Definition: C4Log.cpp:117
C4Stat * pNext
Definition: C4Stat.h:87
int stricmp(const char *s1, const char *s2)
void Reset()
Definition: C4Stat.cpp:181
void ShowPart(int FrameCounter)
Definition: C4Stat.cpp:152
uint32_t tTimeSumPart
Definition: C4Stat.h:107
friend class C4MainStat
Definition: C4Stat.h:51
void Reset()
Definition: C4Stat.cpp:73
unsigned int iStartCalled
Definition: C4Stat.h:93
Definition: C4Stat.h:49
bool LogSilentF(const char *strMessage,...)
Definition: C4Log.cpp:263
void ResetPart()
Definition: C4Stat.cpp:191
C4Stat(const char *strName)
Definition: C4Stat.cpp:169
C4Stat * pFirst
Definition: C4Stat.h:39
int iCnt
Definition: TstC4NetIO.cpp:35
void Show()
Definition: C4Stat.cpp:86
const char * strName
Definition: C4Stat.h:114
uint32_t tTimeSum
Definition: C4Stat.h:99
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