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