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