| 1 | /* |
| 2 | * LegacyClonk |
| 3 | * |
| 4 | * Copyright (c) RedWolf Design |
| 5 | * Copyright (c) 2017-2020, The LegacyClonk 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 | |
| 17 | // statistics |
| 18 | // by peter |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #include "Standard.h" |
| 23 | |
| 24 | #include <cassert> |
| 25 | #include <cstdio> |
| 26 | |
| 27 | class C4Stat; |
| 28 | |
| 29 | // *** main statistic class |
| 30 | // should only been constructed once per application |
| 31 | class C4MainStat |
| 32 | { |
| 33 | friend class C4Stat; |
| 34 | |
| 35 | public: |
| 36 | C4MainStat(); |
| 37 | ~C4MainStat(); |
| 38 | |
| 39 | void Show(); |
| 40 | void ShowPart(); |
| 41 | |
| 42 | void Reset(); |
| 43 | void ResetPart(); |
| 44 | |
| 45 | void OpenStatFile(); |
| 46 | void CloseStatFile(); |
| 47 | |
| 48 | FILE *StatFile; |
| 49 | bool bStatFileOpen; |
| 50 | |
| 51 | protected: |
| 52 | C4Stat *pFirst; |
| 53 | |
| 54 | void RegisterStat(C4Stat *pStat); |
| 55 | void UnRegStat(C4Stat *pStat); |
| 56 | }; |
| 57 | |
| 58 | // *** one statistic. |
| 59 | // Holds the data about one "checkpoint" in code |
| 60 | // registers himself to C4MainStat when Start() is called the first time |
| 61 | class C4Stat |
| 62 | { |
| 63 | friend class C4MainStat; |
| 64 | |
| 65 | public: |
| 66 | C4Stat(const char *strName); |
| 67 | ~C4Stat(); |
| 68 | |
| 69 | inline void Start() |
| 70 | { |
| 71 | if (!iStartCalled) |
| 72 | iStartTick = timeGetTime(); |
| 73 | iCount++; |
| 74 | iCountPart++; |
| 75 | iStartCalled++; |
| 76 | } |
| 77 | |
| 78 | inline void Stop() |
| 79 | { |
| 80 | assert(iStartCalled); |
| 81 | iStartCalled--; |
| 82 | if (!iStartCalled && iCount >= 100) |
| 83 | { |
| 84 | unsigned int iTime = timeGetTime() - iStartTick; |
| 85 | |
| 86 | iTimeSum += iTime; |
| 87 | iTimeSumPart += iTime; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void Reset(); |
| 92 | void ResetPart(); |
| 93 | |
| 94 | static C4MainStat *getMainStat(); |
| 95 | |
| 96 | protected: |
| 97 | // used by C4MainStat |
| 98 | C4Stat *pNext; |
| 99 | C4Stat *pPrev; |
| 100 | |
| 101 | // start tick |
| 102 | unsigned int iStartTick; |
| 103 | |
| 104 | // start-call depth |
| 105 | unsigned int iStartCalled; |
| 106 | |
| 107 | // ** statistic data |
| 108 | |
| 109 | // sum of times |
| 110 | unsigned int iTimeSum; |
| 111 | |
| 112 | // number of starts called |
| 113 | unsigned int iCount; |
| 114 | |
| 115 | // ** statistic data (partial stat) |
| 116 | |
| 117 | // sum of times |
| 118 | unsigned int iTimeSumPart; |
| 119 | |
| 120 | // number of starts called |
| 121 | unsigned int iCountPart; |
| 122 | |
| 123 | // name of statistic |
| 124 | const char *strName; |
| 125 | }; |
| 126 | |
| 127 | // *** some directives |
| 128 | #ifdef USE_STAT |
| 129 | |
| 130 | // used to create and start a new C4Stat object |
| 131 | #define C4ST_STARTNEW(StatName, strName) static C4Stat StatName(strName); StatName.Start(); |
| 132 | |
| 133 | // used to create a new C4Stat object |
| 134 | #define C4ST_NEW(StatName, strName) C4Stat StatName(strName); |
| 135 | |
| 136 | // used to start an existing C4Stat object |
| 137 | #define C4ST_START(StatName) StatName.Start(); |
| 138 | |
| 139 | // used to stop an existing C4Stat object |
| 140 | #define C4ST_STOP(StatName) StatName.Stop(); |
| 141 | |
| 142 | // shows the statistic (to log) |
| 143 | #define C4ST_SHOWSTAT C4Stat::getMainStat()->Show(); |
| 144 | |
| 145 | // shows the statistic (to log) |
| 146 | #define C4ST_SHOWPARTSTAT C4Stat::getMainStat()->ShowPart(); |
| 147 | |
| 148 | // resets the whole statistic |
| 149 | #define C4ST_RESET C4Stat::getMainStat()->Reset(); |
| 150 | |
| 151 | // resets the partial statistic |
| 152 | #define C4ST_RESETPART C4Stat::getMainStat()->ResetPart(); |
| 153 | |
| 154 | #else |
| 155 | |
| 156 | #define C4ST_STARTNEW(StatName, strName) |
| 157 | #define C4ST_NEW(StatName, strName) |
| 158 | #define C4ST_START(StatName) |
| 159 | #define C4ST_STOP(StatName) |
| 160 | #define C4ST_SHOWSTAT |
| 161 | #define C4ST_SHOWPARTSTAT |
| 162 | #define C4ST_RESET |
| 163 | #define C4ST_RESETPART |
| 164 | |
| 165 | #endif |
| 166 | |