]> git.donarmstrong.com Git - mothur.git/blob - timing.h
4db7847346356b41a8a12a9e226413f2e81abecb
[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 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
63 typedef uint64_t TICKS;
64 __inline__ uint64_t GetClockTicks()
65 {
66         uint32_t lo, hi;
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;
70 }
71
72
73 #else   // ifdef _MSC_VER
74 typedef unsigned __int64 TICKS;
75
76 #pragma warning(disable:4035)
77 inline TICKS GetClockTicks()
78 {
79         _asm
80         {
81                 _emit   0x0f
82                 _emit   0x31
83         }
84 }
85
86 #endif  // ifdef _MSC_VER
87
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))
91
92 const char *TimerToStr(TIMER t);
93
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;
106 #if     BG_TIMING
107 extern TIMER g_BackgroundTimer;
108 #endif
109
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__)
112
113 inline void *MyAlloc_(unsigned Bytes, unsigned a, const char *FileName, int Line)
114         {
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);
121         }
122
123 inline void MyFree_(void *p, unsigned Bytes, unsigned a, const char *FileName, int Line)
124         {
125         ++g_AllocFreeCount[a];
126         g_AllocFreeBytes[a] += Bytes;
127         g_AllocNetBytes[a] -= Bytes;
128         myfree2(p, Bytes);
129         }
130
131 #if     BG_TIMING
132 inline void SetBackgroundTimer_(TIMER Timer)
133         {
134         TICKS Now = GetClockTicks();
135         if (g_BeginTicks[g_BackgroundTimer] != 0)
136                 {
137                 ++g_TotalCounts[g_BackgroundTimer];
138                 g_TotalTicks[g_BackgroundTimer] += double(Now - g_BeginTicks[g_BackgroundTimer]);
139                 }
140         g_BackgroundTimer = Timer;
141         g_BeginTicks[Timer] = Now;
142         }
143 #else
144 #define SetBackgroundTimer_(Timer)      /* empty */
145 #endif
146
147 inline void StartTimer_(TIMER Timer)
148         {
149         if (g_CurrTimer != TIMER_None)
150                 Die("StartTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
151
152         TICKS Now = GetClockTicks();
153 #if     BG_TIMING
154         if (g_BeginTicks[g_BackgroundTimer] != 0)
155                 {
156                 ++g_TotalCounts[g_BackgroundTimer];
157                 g_TotalTicks[g_BackgroundTimer] += double(Now - g_BeginTicks[g_BackgroundTimer]);
158                 }
159 #endif
160         g_BeginTicks[Timer] = Now;
161         g_CurrTimer = Timer;
162         }
163
164 inline void PauseTimer_(TIMER Timer)
165         {
166         if (Timer != g_CurrTimer)
167                 Die("PauseTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
168
169         TICKS Now = GetClockTicks();
170         g_TotalTicks[Timer] += double(Now - g_BeginTicks[Timer]);
171         g_BeginTicks[Timer] = Now;
172         g_CurrTimer = TIMER_None;
173         }
174
175 inline void EndTimer_(TIMER Timer)
176         {
177         if (Timer != g_CurrTimer)
178                 Die("EndTimer(%s), curr=%s", TimerToStr(Timer), TimerToStr(g_CurrTimer));
179
180         TICKS Now = GetClockTicks();
181 #if     BG_TIMING
182         g_BeginTicks[g_BackgroundTimer] = Now;
183 #endif
184         g_TotalTicks[Timer] += double(Now - g_BeginTicks[Timer]);
185         ++g_TotalCounts[Timer];
186         g_CurrTimer = TIMER_None;
187         }
188
189 inline void StartTimer2_(TIMER Timer)
190         {
191         g_Timer2[Timer] = true;
192         g_BeginTicks[Timer] = GetClockTicks();
193         }
194
195 inline void EndTimer2_(TIMER Timer)
196         {
197         g_TotalTicks[Timer] += double(GetClockTicks() - g_BeginTicks[Timer]);
198         ++g_TotalCounts[Timer];
199         }
200
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)
208
209 #if     BG_TIMING
210 #define SetBackgroundTimer(x)   SetBackgroundTimer_(TIMER_##x)
211 #else
212 #define SetBackgroundTimer(x)   /* empty */
213 #endif
214
215 #else   // if TIMING
216
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)
228
229 #endif  // if TIMING
230
231 void LogMemStats();
232 void LogTickStats();
233 void LogStats();
234 void LogAllocs();
235
236 #define AddBytes(x, n)  /* empty */
237 #define SubBytes(x, n)  /* empty */
238
239 #endif  // if timing_h