1 //uchime by Robert C. Edgar http://drive5.com/uchime This code is donated to the public domain.
24 #define T(x) TIMER_##x,
29 const unsigned TimerCount =
38 #define C(x) COUNTER_##x,
45 #define A(x) ALLOCER_##x,
50 const unsigned CounterCount =
56 const unsigned AllocerCount =
64 typedef unsigned __int64 TICKS;
66 #pragma warning(disable:4035)
67 inline TICKS GetClockTicks()
76 #else // ifdef _MSC_VER
78 typedef uint64_t TICKS;
79 __inline__ uint64_t GetClockTicks()
82 /* We cannot use "=A", since this would use %rax on x86_64 */
83 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
84 return (uint64_t)hi << 32 | lo;
87 #endif // ifdef _MSC_VER
89 //void AddTicks(const string &Name, TICKS Ticks1, TICKS Ticks2);
90 //void AddBytes(const string &Name, double Bytes);
91 //#define SubBytes(Name, Bytes) AddBytes(Name, -double(Bytes))
93 const char *TimerToStr(TIMER t);
95 extern TICKS g_BeginTicks[TimerCount];
96 extern double g_TotalTicks[TimerCount];
97 extern double g_TotalCounts[TimerCount];
98 extern double g_Counters[CounterCount];
99 extern unsigned g_AllocNewCount[AllocerCount];
100 extern unsigned g_AllocFreeCount[AllocerCount];
101 extern double g_AllocNewBytes[AllocerCount];
102 extern double g_AllocFreeBytes[AllocerCount];
103 extern double g_AllocNetBytes[AllocerCount];
104 extern double g_AllocPeakBytes[AllocerCount];
105 extern bool g_Timer2[TimerCount];
106 extern TIMER g_CurrTimer;
108 extern TIMER g_BackgroundTimer;
111 #define MYALLOC(Type, N, Name) (Type *) MyAlloc_((N)*sizeof(Type), ALLOCER_##Name, __FILE__, __LINE__)
112 #define MYFREE(Array, N, Name) MyFree_(Array, N*sizeof(Array[0]), ALLOCER_##Name, __FILE__, __LINE__)
114 inline void *MyAlloc_(unsigned Bytes, unsigned a, const char *FileName, int Line)
116 ++g_AllocNewCount[a];
117 g_AllocNewBytes[a] += Bytes;
118 g_AllocNetBytes[a] += Bytes;
119 if (g_AllocNetBytes[a] > g_AllocPeakBytes[a])
120 g_AllocPeakBytes[a] = g_AllocNetBytes[a];
121 return mymalloc(Bytes);
124 inline void MyFree_(void *p, unsigned Bytes, unsigned a, const char *FileName, int Line)
126 ++g_AllocFreeCount[a];
127 g_AllocFreeBytes[a] += Bytes;
128 g_AllocNetBytes[a] -= Bytes;
133 inline void SetBackgroundTimer_(TIMER Timer)
135 TICKS Now = GetClockTicks();
136 if (g_BeginTicks[g_BackgroundTimer] != 0)
138 ++g_TotalCounts[g_BackgroundTimer];
139 g_TotalTicks[g_BackgroundTimer] += double(Now - g_BeginTicks[g_BackgroundTimer]);
141 g_BackgroundTimer = Timer;
142 g_BeginTicks[Timer] = Now;
145 #define SetBackgroundTimer_(Timer) /* empty */
148 inline void StartTimer_(TIMER Timer)
150 if (g_CurrTimer != TIMER_None)
151 Die("StartTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
153 TICKS Now = GetClockTicks();
155 if (g_BeginTicks[g_BackgroundTimer] != 0)
157 ++g_TotalCounts[g_BackgroundTimer];
158 g_TotalTicks[g_BackgroundTimer] += double(Now - g_BeginTicks[g_BackgroundTimer]);
161 g_BeginTicks[Timer] = Now;
165 inline void PauseTimer_(TIMER Timer)
167 if (Timer != g_CurrTimer)
168 Die("PauseTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
170 TICKS Now = GetClockTicks();
171 g_TotalTicks[Timer] += double(Now - g_BeginTicks[Timer]);
172 g_BeginTicks[Timer] = Now;
173 g_CurrTimer = TIMER_None;
176 inline void EndTimer_(TIMER Timer)
178 if (Timer != g_CurrTimer)
179 Die("EndTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
181 TICKS Now = GetClockTicks();
183 g_BeginTicks[g_BackgroundTimer] = Now;
185 g_TotalTicks[Timer] += double(Now - g_BeginTicks[Timer]);
186 ++g_TotalCounts[Timer];
187 g_CurrTimer = TIMER_None;
190 inline void StartTimer2_(TIMER Timer)
192 g_Timer2[Timer] = true;
193 g_BeginTicks[Timer] = GetClockTicks();
196 inline void EndTimer2_(TIMER Timer)
198 g_TotalTicks[Timer] += double(GetClockTicks() - g_BeginTicks[Timer]);
199 ++g_TotalCounts[Timer];
202 #define AddCounter(x, N) g_Counters[COUNTER_##x] += N
203 #define IncCounter(x) ++(g_Counters[COUNTER_##x])
204 #define StartTimer(x) StartTimer_(TIMER_##x)
205 #define PauseTimer(x) PauseTimer_(TIMER_##x)
206 #define EndTimer(x) EndTimer_(TIMER_##x)
207 #define StartTimer2(x) StartTimer2_(TIMER_##x)
208 #define EndTimer2(x) EndTimer2_(TIMER_##x)
211 #define SetBackgroundTimer(x) SetBackgroundTimer_(TIMER_##x)
213 #define SetBackgroundTimer(x) /* empty */
218 #define AddCounter(x, N) /* empty */
219 #define IncCounter(x) /* empty */
220 #define StartTimer(x) /* empty */
221 #define PauseTimer(x) /* empty */
222 #define EndTimer(x) /* empty */
223 #define StartTimer2(x) /* empty */
224 #define PauseTimer2(x) /* empty */
225 #define EndTimer2(x) /* empty */
226 #define SetBackgroundTimer(x) /* empty */
227 #define MYALLOC(Type, N, Name) myalloc(Type, N)
228 #define MYFREE(Array, N, Name) myfree(Array)
237 #define AddBytes(x, n) /* empty */
238 #define SubBytes(x, n) /* empty */
240 #endif // if timing_h