]> git.donarmstrong.com Git - mothur.git/blob - timing.h
10bd1f7705bed69363a08c9a2b3d2dd99cf3c054
[mothur.git] / timing.h
1 //uchime by Robert C. Edgar http://drive5.com/uchime This code is donated to the public domain.
2
3 #define TIMING 0
4 #ifndef timing_h
5 #define timing_h
6
7 #define BG_TIMING       0
8
9 #if !TIMING
10 #undef BG_TIMING
11 #define BG_TIMING       0
12 #endif
13
14 //#if   UCHIMES
15 #undef TIMING
16 #define TIMING  0
17 //#endif
18
19 #if TIMING
20
21 enum TIMER
22         {
23         TIMER_None,
24 #define T(x)    TIMER_##x,
25 #include "timers.h"
26 #undef T
27         };
28
29 const unsigned TimerCount =
30         1       // TIMER_None
31 #define T(x)    +1
32 #include "timers.h"
33 #undef T
34         ;
35
36 enum COUNTER
37         {
38 #define C(x)    COUNTER_##x,
39 #include "counters.h"
40 #undef C
41         };
42
43 enum ALLOCER
44         {
45 #define A(x)    ALLOCER_##x,
46 #include "allocs.h"
47 #undef A
48         };
49
50 const unsigned CounterCount =
51 #define C(x)    +1
52 #include "counters.h"
53 #undef C
54         ;
55
56 const unsigned AllocerCount =
57 #define A(x)    +1
58 #include "allocs.h"
59 #undef A
60         ;
61
62 #ifdef _MSC_VER
63
64 typedef unsigned __int64 TICKS;
65
66 #pragma warning(disable:4035)
67 inline TICKS GetClockTicks()
68         {
69         _asm
70                 {
71                 _emit   0x0f
72                 _emit   0x31
73                 }
74         }
75
76 #else   // ifdef _MSC_VER
77
78 typedef uint64_t TICKS;
79 __inline__ uint64_t GetClockTicks()
80         {
81         uint32_t lo, hi;
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;
85         }
86
87 #endif  // ifdef _MSC_VER
88
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))
92
93 const char *TimerToStr(TIMER t);
94
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;
107 #if     BG_TIMING
108 extern TIMER g_BackgroundTimer;
109 #endif
110
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__)
113
114 inline void *MyAlloc_(unsigned Bytes, unsigned a, const char *FileName, int Line)
115         {
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);
122         }
123
124 inline void MyFree_(void *p, unsigned Bytes, unsigned a, const char *FileName, int Line)
125         {
126         ++g_AllocFreeCount[a];
127         g_AllocFreeBytes[a] += Bytes;
128         g_AllocNetBytes[a] -= Bytes;
129         myfree2(p, Bytes);
130         }
131
132 #if     BG_TIMING
133 inline void SetBackgroundTimer_(TIMER Timer)
134         {
135         TICKS Now = GetClockTicks();
136         if (g_BeginTicks[g_BackgroundTimer] != 0)
137                 {
138                 ++g_TotalCounts[g_BackgroundTimer];
139                 g_TotalTicks[g_BackgroundTimer] += double(Now - g_BeginTicks[g_BackgroundTimer]);
140                 }
141         g_BackgroundTimer = Timer;
142         g_BeginTicks[Timer] = Now;
143         }
144 #else
145 #define SetBackgroundTimer_(Timer)      /* empty */
146 #endif
147
148 inline void StartTimer_(TIMER Timer)
149         {
150         if (g_CurrTimer != TIMER_None)
151                 Die("StartTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
152
153         TICKS Now = GetClockTicks();
154 #if     BG_TIMING
155         if (g_BeginTicks[g_BackgroundTimer] != 0)
156                 {
157                 ++g_TotalCounts[g_BackgroundTimer];
158                 g_TotalTicks[g_BackgroundTimer] += double(Now - g_BeginTicks[g_BackgroundTimer]);
159                 }
160 #endif
161         g_BeginTicks[Timer] = Now;
162         g_CurrTimer = Timer;
163         }
164
165 inline void PauseTimer_(TIMER Timer)
166         {
167         if (Timer != g_CurrTimer)
168                 Die("PauseTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
169
170         TICKS Now = GetClockTicks();
171         g_TotalTicks[Timer] += double(Now - g_BeginTicks[Timer]);
172         g_BeginTicks[Timer] = Now;
173         g_CurrTimer = TIMER_None;
174         }
175
176 inline void EndTimer_(TIMER Timer)
177         {
178         if (Timer != g_CurrTimer)
179                 Die("EndTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
180
181         TICKS Now = GetClockTicks();
182 #if     BG_TIMING
183         g_BeginTicks[g_BackgroundTimer] = Now;
184 #endif
185         g_TotalTicks[Timer] += double(Now - g_BeginTicks[Timer]);
186         ++g_TotalCounts[Timer];
187         g_CurrTimer = TIMER_None;
188         }
189
190 inline void StartTimer2_(TIMER Timer)
191         {
192         g_Timer2[Timer] = true;
193         g_BeginTicks[Timer] = GetClockTicks();
194         }
195
196 inline void EndTimer2_(TIMER Timer)
197         {
198         g_TotalTicks[Timer] += double(GetClockTicks() - g_BeginTicks[Timer]);
199         ++g_TotalCounts[Timer];
200         }
201
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)
209
210 #if     BG_TIMING
211 #define SetBackgroundTimer(x)   SetBackgroundTimer_(TIMER_##x)
212 #else
213 #define SetBackgroundTimer(x)   /* empty */
214 #endif
215
216 #else   // if TIMING
217
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)
229
230 #endif  // if TIMING
231
232 void LogMemStats();
233 void LogTickStats();
234 void LogStats();
235 void LogAllocs();
236
237 #define AddBytes(x, n)  /* empty */
238 #define SubBytes(x, n)  /* empty */
239
240 #endif  // if timing_h