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 =
62 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
63 typedef uint64_t TICKS;
64 __inline__ uint64_t GetClockTicks()
67 /* We cannot use "=A", since this would use %rax on x86_64 */
68 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
69 return (uint64_t)hi << 32 | lo;
73 #else // ifdef _MSC_VER
74 typedef unsigned __int64 TICKS;
76 #pragma warning(disable:4035)
77 inline TICKS GetClockTicks()
86 #endif // ifdef _MSC_VER
88 //void AddTicks(const string &Name, TICKS Ticks1, TICKS Ticks2);
89 //void AddBytes(const string &Name, double Bytes);
90 //#define SubBytes(Name, Bytes) AddBytes(Name, -double(Bytes))
92 const char *TimerToStr(TIMER t);
94 extern TICKS g_BeginTicks[TimerCount];
95 extern double g_TotalTicks[TimerCount];
96 extern double g_TotalCounts[TimerCount];
97 extern double g_Counters[CounterCount];
98 extern unsigned g_AllocNewCount[AllocerCount];
99 extern unsigned g_AllocFreeCount[AllocerCount];
100 extern double g_AllocNewBytes[AllocerCount];
101 extern double g_AllocFreeBytes[AllocerCount];
102 extern double g_AllocNetBytes[AllocerCount];
103 extern double g_AllocPeakBytes[AllocerCount];
104 extern bool g_Timer2[TimerCount];
105 extern TIMER g_CurrTimer;
107 extern TIMER g_BackgroundTimer;
110 #define MYALLOC(Type, N, Name) (Type *) MyAlloc_((N)*sizeof(Type), ALLOCER_##Name, __FILE__, __LINE__)
111 #define MYFREE(Array, N, Name) MyFree_(Array, N*sizeof(Array[0]), ALLOCER_##Name, __FILE__, __LINE__)
113 inline void *MyAlloc_(unsigned Bytes, unsigned a, const char *FileName, int Line)
115 ++g_AllocNewCount[a];
116 g_AllocNewBytes[a] += Bytes;
117 g_AllocNetBytes[a] += Bytes;
118 if (g_AllocNetBytes[a] > g_AllocPeakBytes[a])
119 g_AllocPeakBytes[a] = g_AllocNetBytes[a];
120 return mymalloc(Bytes);
123 inline void MyFree_(void *p, unsigned Bytes, unsigned a, const char *FileName, int Line)
125 ++g_AllocFreeCount[a];
126 g_AllocFreeBytes[a] += Bytes;
127 g_AllocNetBytes[a] -= Bytes;
132 inline void SetBackgroundTimer_(TIMER Timer)
134 TICKS Now = GetClockTicks();
135 if (g_BeginTicks[g_BackgroundTimer] != 0)
137 ++g_TotalCounts[g_BackgroundTimer];
138 g_TotalTicks[g_BackgroundTimer] += double(Now - g_BeginTicks[g_BackgroundTimer]);
140 g_BackgroundTimer = Timer;
141 g_BeginTicks[Timer] = Now;
144 #define SetBackgroundTimer_(Timer) /* empty */
147 inline void StartTimer_(TIMER Timer)
149 if (g_CurrTimer != TIMER_None)
150 Die("StartTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
152 TICKS Now = GetClockTicks();
154 if (g_BeginTicks[g_BackgroundTimer] != 0)
156 ++g_TotalCounts[g_BackgroundTimer];
157 g_TotalTicks[g_BackgroundTimer] += double(Now - g_BeginTicks[g_BackgroundTimer]);
160 g_BeginTicks[Timer] = Now;
164 inline void PauseTimer_(TIMER Timer)
166 if (Timer != g_CurrTimer)
167 Die("PauseTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
169 TICKS Now = GetClockTicks();
170 g_TotalTicks[Timer] += double(Now - g_BeginTicks[Timer]);
171 g_BeginTicks[Timer] = Now;
172 g_CurrTimer = TIMER_None;
175 inline void EndTimer_(TIMER Timer)
177 if (Timer != g_CurrTimer)
178 Die("EndTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
180 TICKS Now = GetClockTicks();
182 g_BeginTicks[g_BackgroundTimer] = Now;
184 g_TotalTicks[Timer] += double(Now - g_BeginTicks[Timer]);
185 ++g_TotalCounts[Timer];
186 g_CurrTimer = TIMER_None;
189 inline void StartTimer2_(TIMER Timer)
191 g_Timer2[Timer] = true;
192 g_BeginTicks[Timer] = GetClockTicks();
195 inline void EndTimer2_(TIMER Timer)
197 g_TotalTicks[Timer] += double(GetClockTicks() - g_BeginTicks[Timer]);
198 ++g_TotalCounts[Timer];
201 #define AddCounter(x, N) g_Counters[COUNTER_##x] += N
202 #define IncCounter(x) ++(g_Counters[COUNTER_##x])
203 #define StartTimer(x) StartTimer_(TIMER_##x)
204 #define PauseTimer(x) PauseTimer_(TIMER_##x)
205 #define EndTimer(x) EndTimer_(TIMER_##x)
206 #define StartTimer2(x) StartTimer2_(TIMER_##x)
207 #define EndTimer2(x) EndTimer2_(TIMER_##x)
210 #define SetBackgroundTimer(x) SetBackgroundTimer_(TIMER_##x)
212 #define SetBackgroundTimer(x) /* empty */
217 #define AddCounter(x, N) /* empty */
218 #define IncCounter(x) /* empty */
219 #define StartTimer(x) /* empty */
220 #define PauseTimer(x) /* empty */
221 #define EndTimer(x) /* empty */
222 #define StartTimer2(x) /* empty */
223 #define PauseTimer2(x) /* empty */
224 #define EndTimer2(x) /* empty */
225 #define SetBackgroundTimer(x) /* empty */
226 #define MYALLOC(Type, N, Name) myalloc(Type, N)
227 #define MYFREE(Array, N, Name) myfree(Array)
236 #define AddBytes(x, n) /* empty */
237 #define SubBytes(x, n) /* empty */
239 #endif // if timing_h