22 #define T(x) TIMER_##x,
27 const unsigned TimerCount =
36 #define C(x) COUNTER_##x,
43 #define A(x) ALLOCER_##x,
48 const unsigned CounterCount =
54 const unsigned AllocerCount =
62 typedef unsigned __int64 TICKS;
64 #pragma warning(disable:4035)
65 inline TICKS GetClockTicks()
74 #else // ifdef _MSC_VER
76 typedef uint64_t TICKS;
77 __inline__ uint64_t GetClockTicks()
80 /* We cannot use "=A", since this would use %rax on x86_64 */
81 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
82 return (uint64_t)hi << 32 | lo;
85 #endif // ifdef _MSC_VER
87 //void AddTicks(const string &Name, TICKS Ticks1, TICKS Ticks2);
88 //void AddBytes(const string &Name, double Bytes);
89 //#define SubBytes(Name, Bytes) AddBytes(Name, -double(Bytes))
91 const char *TimerToStr(TIMER t);
93 extern TICKS g_BeginTicks[TimerCount];
94 extern double g_TotalTicks[TimerCount];
95 extern double g_TotalCounts[TimerCount];
96 extern double g_Counters[CounterCount];
97 extern unsigned g_AllocNewCount[AllocerCount];
98 extern unsigned g_AllocFreeCount[AllocerCount];
99 extern double g_AllocNewBytes[AllocerCount];
100 extern double g_AllocFreeBytes[AllocerCount];
101 extern double g_AllocNetBytes[AllocerCount];
102 extern double g_AllocPeakBytes[AllocerCount];
103 extern bool g_Timer2[TimerCount];
104 extern TIMER g_CurrTimer;
106 extern TIMER g_BackgroundTimer;
109 #define MYALLOC(Type, N, Name) (Type *) MyAlloc_((N)*sizeof(Type), ALLOCER_##Name, __FILE__, __LINE__)
110 #define MYFREE(Array, N, Name) MyFree_(Array, N*sizeof(Array[0]), ALLOCER_##Name, __FILE__, __LINE__)
112 inline void *MyAlloc_(unsigned Bytes, unsigned a, const char *FileName, int Line)
114 ++g_AllocNewCount[a];
115 g_AllocNewBytes[a] += Bytes;
116 g_AllocNetBytes[a] += Bytes;
117 if (g_AllocNetBytes[a] > g_AllocPeakBytes[a])
118 g_AllocPeakBytes[a] = g_AllocNetBytes[a];
119 return mymalloc(Bytes);
122 inline void MyFree_(void *p, unsigned Bytes, unsigned a, const char *FileName, int Line)
124 ++g_AllocFreeCount[a];
125 g_AllocFreeBytes[a] += Bytes;
126 g_AllocNetBytes[a] -= Bytes;
131 inline void SetBackgroundTimer_(TIMER Timer)
133 TICKS Now = GetClockTicks();
134 if (g_BeginTicks[g_BackgroundTimer] != 0)
136 ++g_TotalCounts[g_BackgroundTimer];
137 g_TotalTicks[g_BackgroundTimer] += double(Now - g_BeginTicks[g_BackgroundTimer]);
139 g_BackgroundTimer = Timer;
140 g_BeginTicks[Timer] = Now;
143 #define SetBackgroundTimer_(Timer) /* empty */
146 inline void StartTimer_(TIMER Timer)
148 if (g_CurrTimer != TIMER_None)
149 Die("StartTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
151 TICKS Now = GetClockTicks();
153 if (g_BeginTicks[g_BackgroundTimer] != 0)
155 ++g_TotalCounts[g_BackgroundTimer];
156 g_TotalTicks[g_BackgroundTimer] += double(Now - g_BeginTicks[g_BackgroundTimer]);
159 g_BeginTicks[Timer] = Now;
163 inline void PauseTimer_(TIMER Timer)
165 if (Timer != g_CurrTimer)
166 Die("PauseTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
168 TICKS Now = GetClockTicks();
169 g_TotalTicks[Timer] += double(Now - g_BeginTicks[Timer]);
170 g_BeginTicks[Timer] = Now;
171 g_CurrTimer = TIMER_None;
174 inline void EndTimer_(TIMER Timer)
176 if (Timer != g_CurrTimer)
177 Die("EndTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
179 TICKS Now = GetClockTicks();
181 g_BeginTicks[g_BackgroundTimer] = Now;
183 g_TotalTicks[Timer] += double(Now - g_BeginTicks[Timer]);
184 ++g_TotalCounts[Timer];
185 g_CurrTimer = TIMER_None;
188 inline void StartTimer2_(TIMER Timer)
190 g_Timer2[Timer] = true;
191 g_BeginTicks[Timer] = GetClockTicks();
194 inline void EndTimer2_(TIMER Timer)
196 g_TotalTicks[Timer] += double(GetClockTicks() - g_BeginTicks[Timer]);
197 ++g_TotalCounts[Timer];
200 #define AddCounter(x, N) g_Counters[COUNTER_##x] += N
201 #define IncCounter(x) ++(g_Counters[COUNTER_##x])
202 #define StartTimer(x) StartTimer_(TIMER_##x)
203 #define PauseTimer(x) PauseTimer_(TIMER_##x)
204 #define EndTimer(x) EndTimer_(TIMER_##x)
205 #define StartTimer2(x) StartTimer2_(TIMER_##x)
206 #define EndTimer2(x) EndTimer2_(TIMER_##x)
209 #define SetBackgroundTimer(x) SetBackgroundTimer_(TIMER_##x)
211 #define SetBackgroundTimer(x) /* empty */
216 #define AddCounter(x, N) /* empty */
217 #define IncCounter(x) /* empty */
218 #define StartTimer(x) /* empty */
219 #define PauseTimer(x) /* empty */
220 #define EndTimer(x) /* empty */
221 #define StartTimer2(x) /* empty */
222 #define PauseTimer2(x) /* empty */
223 #define EndTimer2(x) /* empty */
224 #define SetBackgroundTimer(x) /* empty */
225 #define MYALLOC(Type, N, Name) myalloc(Type, N)
226 #define MYFREE(Array, N, Name) myfree(Array)
235 #define AddBytes(x, n) /* empty */
236 #define SubBytes(x, n) /* empty */
238 #endif // if timing_h