Merge commit 'silk-repo/master'
[opus.git] / silk / SKP_debug.h
1 /***********************************************************************\r
2 Copyright (c) 2006-2011, Skype Limited. All rights reserved. \r
3 Redistribution and use in source and binary forms, with or without \r
4 modification, (subject to the limitations in the disclaimer below) \r
5 are permitted provided that the following conditions are met:\r
6 - Redistributions of source code must retain the above copyright notice,\r
7 this list of conditions and the following disclaimer.\r
8 - Redistributions in binary form must reproduce the above copyright \r
9 notice, this list of conditions and the following disclaimer in the \r
10 documentation and/or other materials provided with the distribution.\r
11 - Neither the name of Skype Limited, nor the names of specific \r
12 contributors, may be used to endorse or promote products derived from \r
13 this software without specific prior written permission.\r
14 NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED \r
15 BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND \r
16 CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND \r
18 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE \r
19 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, \r
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE \r
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
26 ***********************************************************************/\r
27 \r
28 #ifndef _SKP_DEBUG_H_\r
29 #define _SKP_DEBUG_H_\r
30 \r
31 #ifdef _WIN32\r
32 #define _CRT_SECURE_NO_DEPRECATE    1\r
33 #endif\r
34 \r
35 #include "SKP_Silk_typedef.h"\r
36 #include <stdio.h>      /* file writing */\r
37 #include <string.h>     /* strcpy, strcmp */\r
38 \r
39 #ifdef  __cplusplus\r
40 extern "C"\r
41 {\r
42 #endif\r
43 \r
44 unsigned long GetHighResolutionTime(void); /* O: time in usec*/\r
45 \r
46 /* make SKP_DEBUG dependent on compiler's _DEBUG */\r
47 #if defined _WIN32\r
48     #ifdef _DEBUG\r
49         #define SKP_DEBUG  1\r
50     #else\r
51         #define SKP_DEBUG  0\r
52     #endif\r
53 \r
54     /* overrule the above */\r
55     #if 0\r
56     //  #define NO_ASSERTS\r
57     #undef  SKP_DEBUG  \r
58     #define SKP_DEBUG  1\r
59     #endif\r
60 #else\r
61     #define SKP_DEBUG  0\r
62 #endif\r
63 \r
64 /* Flag for using timers */\r
65 #define SKP_TIC_TOC 0\r
66 \r
67 #if SKP_TIC_TOC\r
68 \r
69 #if (defined(_WIN32) || defined(_WINCE)) \r
70 #include <windows.h>    /* timer */\r
71 #pragma warning( disable : 4996 )       // stop bitching about strcpy in TIC()\r
72 #else   // Linux or Mac\r
73 #include <sys/time.h>\r
74 #endif\r
75 \r
76 /*********************************/\r
77 /* timer functions for profiling */\r
78 /*********************************/\r
79 /* example:                                                         */\r
80 /*                                                                  */\r
81 /* TIC(LPC)                                                         */\r
82 /* do_LPC(in_vec, order, acoef);    // do LPC analysis              */\r
83 /* TOC(LPC)                                                         */\r
84 /*                                                                  */\r
85 /* and call the following just before exiting (from main)           */\r
86 /*                                                                  */\r
87 /* SKP_TimerSave("SKP_TimingData.txt");                             */\r
88 /*                                                                  */\r
89 /* results are now in SKP_TimingData.txt                            */\r
90 \r
91 void SKP_TimerSave(char *file_name);\r
92 \r
93 /* max number of timers (in different locations) */\r
94 #define SKP_NUM_TIMERS_MAX                  50\r
95 /* max length of name tags in TIC(..), TOC(..) */\r
96 #define SKP_NUM_TIMERS_MAX_TAG_LEN          30\r
97 \r
98 extern int           SKP_Timer_nTimers;\r
99 extern int           SKP_Timer_depth_ctr;\r
100 extern char          SKP_Timer_tags[SKP_NUM_TIMERS_MAX][SKP_NUM_TIMERS_MAX_TAG_LEN];\r
101 #ifdef _WIN32\r
102 extern LARGE_INTEGER SKP_Timer_start[SKP_NUM_TIMERS_MAX];\r
103 #else\r
104 extern unsigned long SKP_Timer_start[SKP_NUM_TIMERS_MAX];\r
105 #endif  \r
106 extern unsigned int  SKP_Timer_cnt[SKP_NUM_TIMERS_MAX];\r
107 extern SKP_int64     SKP_Timer_sum[SKP_NUM_TIMERS_MAX];\r
108 extern SKP_int64     SKP_Timer_max[SKP_NUM_TIMERS_MAX];\r
109 extern SKP_int64     SKP_Timer_min[SKP_NUM_TIMERS_MAX];\r
110 extern SKP_int64     SKP_Timer_depth[SKP_NUM_TIMERS_MAX];\r
111 \r
112 /* WARNING: TIC()/TOC can measure only up to 0.1 seconds at a time */\r
113 #ifdef _WIN32\r
114 #define TIC(TAG_NAME) {                                     \\r
115     static int init = 0;                                    \\r
116     static int ID = -1;                                     \\r
117     if( init == 0 )                                         \\r
118     {                                                       \\r
119         int k;                                              \\r
120         init = 1;                                           \\r
121         for( k = 0; k < SKP_Timer_nTimers; k++ ) {          \\r
122             if( strcmp(SKP_Timer_tags[k], #TAG_NAME) == 0 ) {   \\r
123                 ID = k;                                     \\r
124                 break;                                      \\r
125             }                                               \\r
126         }                                                   \\r
127         if (ID == -1) {                                     \\r
128             ID = SKP_Timer_nTimers;                         \\r
129             SKP_Timer_nTimers++;                            \\r
130             SKP_Timer_depth[ID] = SKP_Timer_depth_ctr;      \\r
131             strcpy(SKP_Timer_tags[ID], #TAG_NAME);          \\r
132             SKP_Timer_cnt[ID] = 0;                          \\r
133             SKP_Timer_sum[ID] = 0;                          \\r
134             SKP_Timer_min[ID] = 0xFFFFFFFF;                 \\r
135             SKP_Timer_max[ID] = 0;                          \\r
136         }                                                   \\r
137     }                                                       \\r
138     SKP_Timer_depth_ctr++;                                  \\r
139     QueryPerformanceCounter(&SKP_Timer_start[ID]);          \\r
140 }\r
141 #else\r
142 #define TIC(TAG_NAME) {                                     \\r
143     static int init = 0;                                    \\r
144     static int ID = -1;                                     \\r
145     if( init == 0 )                                         \\r
146     {                                                       \\r
147         int k;                                              \\r
148         init = 1;                                           \\r
149         for( k = 0; k < SKP_Timer_nTimers; k++ ) {          \\r
150         if( strcmp(SKP_Timer_tags[k], #TAG_NAME) == 0 ) {   \\r
151                 ID = k;                                     \\r
152                 break;                                      \\r
153             }                                               \\r
154         }                                                   \\r
155         if (ID == -1) {                                     \\r
156             ID = SKP_Timer_nTimers;                         \\r
157             SKP_Timer_nTimers++;                            \\r
158             SKP_Timer_depth[ID] = SKP_Timer_depth_ctr;      \\r
159             strcpy(SKP_Timer_tags[ID], #TAG_NAME);          \\r
160             SKP_Timer_cnt[ID] = 0;                          \\r
161             SKP_Timer_sum[ID] = 0;                          \\r
162             SKP_Timer_min[ID] = 0xFFFFFFFF;                 \\r
163             SKP_Timer_max[ID] = 0;                          \\r
164         }                                                   \\r
165     }                                                       \\r
166     SKP_Timer_depth_ctr++;                                  \\r
167     SKP_Timer_start[ID] = GetHighResolutionTime();          \\r
168 }\r
169 #endif\r
170 \r
171 #ifdef _WIN32\r
172 #define TOC(TAG_NAME) {                                             \\r
173     LARGE_INTEGER lpPerformanceCount;                               \\r
174     static int init = 0;                                            \\r
175     static int ID = 0;                                              \\r
176     if( init == 0 )                                                 \\r
177     {                                                               \\r
178         int k;                                                      \\r
179         init = 1;                                                   \\r
180         for( k = 0; k < SKP_Timer_nTimers; k++ ) {                  \\r
181             if( strcmp(SKP_Timer_tags[k], #TAG_NAME) == 0 ) {       \\r
182                 ID = k;                                             \\r
183                 break;                                              \\r
184             }                                                       \\r
185         }                                                           \\r
186     }                                                               \\r
187     QueryPerformanceCounter(&lpPerformanceCount);                   \\r
188     lpPerformanceCount.QuadPart -= SKP_Timer_start[ID].QuadPart;    \\r
189     if((lpPerformanceCount.QuadPart < 100000000) &&                 \\r
190         (lpPerformanceCount.QuadPart >= 0)) {                       \\r
191         SKP_Timer_cnt[ID]++;                                        \\r
192         SKP_Timer_sum[ID] += lpPerformanceCount.QuadPart;           \\r
193         if( lpPerformanceCount.QuadPart > SKP_Timer_max[ID] )       \\r
194             SKP_Timer_max[ID] = lpPerformanceCount.QuadPart;        \\r
195         if( lpPerformanceCount.QuadPart < SKP_Timer_min[ID] )       \\r
196             SKP_Timer_min[ID] = lpPerformanceCount.QuadPart;        \\r
197     }                                                               \\r
198     SKP_Timer_depth_ctr--;                                          \\r
199 }\r
200 #else\r
201 #define TOC(TAG_NAME) {                                             \\r
202     unsigned long endTime;                                          \\r
203     static int init = 0;                                            \\r
204     static int ID = 0;                                              \\r
205     if( init == 0 )                                                 \\r
206     {                                                               \\r
207         int k;                                                      \\r
208         init = 1;                                                   \\r
209         for( k = 0; k < SKP_Timer_nTimers; k++ ) {                  \\r
210             if( strcmp(SKP_Timer_tags[k], #TAG_NAME) == 0 ) {       \\r
211                 ID = k;                                             \\r
212                 break;                                              \\r
213             }                                                       \\r
214         }                                                           \\r
215     }                                                               \\r
216     endTime = GetHighResolutionTime();                              \\r
217     endTime -= SKP_Timer_start[ID];                                 \\r
218     if((endTime < 100000000) &&                                     \\r
219         (endTime >= 0)) {                                           \\r
220         SKP_Timer_cnt[ID]++;                                        \\r
221         SKP_Timer_sum[ID] += endTime;                               \\r
222         if( endTime > SKP_Timer_max[ID] )                           \\r
223             SKP_Timer_max[ID] = endTime;                            \\r
224         if( endTime < SKP_Timer_min[ID] )                           \\r
225             SKP_Timer_min[ID] = endTime;                            \\r
226     }                                                               \\r
227         SKP_Timer_depth_ctr--;                                      \\r
228 }\r
229 #endif\r
230 \r
231 #else /* SKP_TIC_TOC */\r
232 \r
233 /* define macros as empty strings */\r
234 #define TIC(TAG_NAME)\r
235 #define TOC(TAG_NAME)\r
236 #define SKP_TimerSave(FILE_NAME)\r
237 \r
238 #endif /* SKP_TIC_TOC */\r
239 \r
240 \r
241 \r
242 #if SKP_DEBUG\r
243 /************************************/\r
244 /* write data to file for debugging */\r
245 /************************************/\r
246 /* opens an empty file if this file has not yet been open, then writes to the file and closes it            */\r
247 /* if file has been open previously it is opened again and the fwrite is appending, finally it is closed    */\r
248 #define SAVE_DATA( FILE_NAME, DATA_PTR, N_BYTES ) {                 \\r
249     static SKP_int32 init = 0;                                      \\r
250     FILE *fp;                                                       \\r
251     if (init == 0)      {                                               \\r
252         init = 1;                                                   \\r
253         fp = fopen(#FILE_NAME, "wb");                               \\r
254     } else {                                                        \\r
255         fp = fopen(#FILE_NAME, "ab+");                              \\r
256     }                                                               \\r
257     fwrite((DATA_PTR), (N_BYTES), 1, fp);                           \\r
258     fclose(fp);                                                     \\r
259 }       \r
260 \r
261 /* Example: DEBUG_STORE_DATA(testfile.pcm, &RIN[0], 160*sizeof(SKP_int16)); */\r
262 \r
263 #if 0\r
264 /* Ensure that everything is written to files when an assert breaks */\r
265 #define DEBUG_STORE_DATA(FILE_NAME, DATA_PTR, N_BYTES) SAVE_DATA(FILE_NAME, DATA_PTR, N_BYTES)\r
266 #define DEBUG_STORE_CLOSE_FILES\r
267 \r
268 #else\r
269 \r
270 #define SKP_NUM_STORES_MAX                                  100\r
271 extern FILE *SKP_debug_store_fp[ SKP_NUM_STORES_MAX ];\r
272 extern int SKP_debug_store_count;\r
273 \r
274 /* Faster way of storing the data */\r
275 #define DEBUG_STORE_DATA( FILE_NAME, DATA_PTR, N_BYTES ) {          \\r
276     static SKP_int init = 0, cnt = 0;                               \\r
277     static FILE **fp;                                               \\r
278     if (init == 0) {                                                \\r
279         init = 1;                                                                                               \\r
280         cnt = SKP_debug_store_count++;                              \\r
281         SKP_debug_store_fp[ cnt ] = fopen(#FILE_NAME, "wb");        \\r
282     }                                                               \\r
283     fwrite((DATA_PTR), (N_BYTES), 1, SKP_debug_store_fp[ cnt ]);    \\r
284 }\r
285 \r
286 /* Call this at the end of main() */\r
287 #define DEBUG_STORE_CLOSE_FILES {                                   \\r
288     SKP_int i;                                                      \\r
289     for( i = 0; i < SKP_debug_store_count; i++ ) {                  \\r
290         fclose( SKP_debug_store_fp[ i ] );                          \\r
291     }                                                               \\r
292 }\r
293 #endif\r
294 \r
295 /* micro sec */\r
296 #define SKP_GETTIME(void)       time = (SKP_int64) GetHighResolutionTime();     \r
297 \r
298 #else /* SKP_DEBUG */\r
299 \r
300 /* define macros as empty strings */\r
301 #define DEBUG_STORE_DATA(FILE_NAME, DATA_PTR, N_BYTES)\r
302 #define SAVE_DATA(FILE_NAME, DATA_PTR, N_BYTES)\r
303 #define DEBUG_STORE_CLOSE_FILES\r
304 \r
305 #endif /* SKP_DEBUG */\r
306 \r
307 #ifdef  __cplusplus\r
308 }\r
309 #endif\r
310 \r
311 #endif /* _SKP_DEBUG_H_ */\r