debug.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #ifndef SILK_DEBUG_H
  28. #define SILK_DEBUG_H
  29. #ifdef _WIN32
  30. #define _CRT_SECURE_NO_DEPRECATE 1
  31. #endif
  32. #include "typedef.h"
  33. #include <stdio.h> /* file writing */
  34. #include <string.h> /* strcpy, strcmp */
  35. #ifdef __cplusplus
  36. extern "C"
  37. {
  38. #endif
  39. unsigned long GetHighResolutionTime(void); /* O time in usec*/
  40. /* make SILK_DEBUG dependent on compiler's _DEBUG */
  41. #if defined _WIN32
  42. #ifdef _DEBUG
  43. #define SILK_DEBUG 1
  44. #else
  45. #define SILK_DEBUG 0
  46. #endif
  47. /* overrule the above */
  48. #if 0
  49. /* #define NO_ASSERTS*/
  50. #undef SILK_DEBUG
  51. #define SILK_DEBUG 1
  52. #endif
  53. #else
  54. #define SILK_DEBUG 0
  55. #endif
  56. /* Flag for using timers */
  57. #define SILK_TIC_TOC 0
  58. #if SILK_TIC_TOC
  59. #if (defined(_WIN32) || defined(_WINCE))
  60. #include <windows.h> /* timer */
  61. #pragma warning( disable : 4996 ) /* stop bitching about strcpy in TIC()*/
  62. #else /* Linux or Mac*/
  63. #include <sys/time.h>
  64. #endif
  65. /*********************************/
  66. /* timer functions for profiling */
  67. /*********************************/
  68. /* example: */
  69. /* */
  70. /* TIC(LPC) */
  71. /* do_LPC(in_vec, order, acoef); // do LPC analysis */
  72. /* TOC(LPC) */
  73. /* */
  74. /* and call the following just before exiting (from main) */
  75. /* */
  76. /* silk_TimerSave("silk_TimingData.txt"); */
  77. /* */
  78. /* results are now in silk_TimingData.txt */
  79. void silk_TimerSave(char *file_name);
  80. /* max number of timers (in different locations) */
  81. #define silk_NUM_TIMERS_MAX 50
  82. /* max length of name tags in TIC(..), TOC(..) */
  83. #define silk_NUM_TIMERS_MAX_TAG_LEN 30
  84. extern int silk_Timer_nTimers;
  85. extern int silk_Timer_depth_ctr;
  86. extern char silk_Timer_tags[silk_NUM_TIMERS_MAX][silk_NUM_TIMERS_MAX_TAG_LEN];
  87. #ifdef _WIN32
  88. extern LARGE_INTEGER silk_Timer_start[silk_NUM_TIMERS_MAX];
  89. #else
  90. extern unsigned long silk_Timer_start[silk_NUM_TIMERS_MAX];
  91. #endif
  92. extern unsigned int silk_Timer_cnt[silk_NUM_TIMERS_MAX];
  93. extern opus_int64 silk_Timer_sum[silk_NUM_TIMERS_MAX];
  94. extern opus_int64 silk_Timer_max[silk_NUM_TIMERS_MAX];
  95. extern opus_int64 silk_Timer_min[silk_NUM_TIMERS_MAX];
  96. extern opus_int64 silk_Timer_depth[silk_NUM_TIMERS_MAX];
  97. /* WARNING: TIC()/TOC can measure only up to 0.1 seconds at a time */
  98. #ifdef _WIN32
  99. #define TIC(TAG_NAME) { \
  100. static int init = 0; \
  101. static int ID = -1; \
  102. if( init == 0 ) \
  103. { \
  104. int k; \
  105. init = 1; \
  106. for( k = 0; k < silk_Timer_nTimers; k++ ) { \
  107. if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) { \
  108. ID = k; \
  109. break; \
  110. } \
  111. } \
  112. if (ID == -1) { \
  113. ID = silk_Timer_nTimers; \
  114. silk_Timer_nTimers++; \
  115. silk_Timer_depth[ID] = silk_Timer_depth_ctr; \
  116. strcpy(silk_Timer_tags[ID], #TAG_NAME); \
  117. silk_Timer_cnt[ID] = 0; \
  118. silk_Timer_sum[ID] = 0; \
  119. silk_Timer_min[ID] = 0xFFFFFFFF; \
  120. silk_Timer_max[ID] = 0; \
  121. } \
  122. } \
  123. silk_Timer_depth_ctr++; \
  124. QueryPerformanceCounter(&silk_Timer_start[ID]); \
  125. }
  126. #else
  127. #define TIC(TAG_NAME) { \
  128. static int init = 0; \
  129. static int ID = -1; \
  130. if( init == 0 ) \
  131. { \
  132. int k; \
  133. init = 1; \
  134. for( k = 0; k < silk_Timer_nTimers; k++ ) { \
  135. if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) { \
  136. ID = k; \
  137. break; \
  138. } \
  139. } \
  140. if (ID == -1) { \
  141. ID = silk_Timer_nTimers; \
  142. silk_Timer_nTimers++; \
  143. silk_Timer_depth[ID] = silk_Timer_depth_ctr; \
  144. strcpy(silk_Timer_tags[ID], #TAG_NAME); \
  145. silk_Timer_cnt[ID] = 0; \
  146. silk_Timer_sum[ID] = 0; \
  147. silk_Timer_min[ID] = 0xFFFFFFFF; \
  148. silk_Timer_max[ID] = 0; \
  149. } \
  150. } \
  151. silk_Timer_depth_ctr++; \
  152. silk_Timer_start[ID] = GetHighResolutionTime(); \
  153. }
  154. #endif
  155. #ifdef _WIN32
  156. #define TOC(TAG_NAME) { \
  157. LARGE_INTEGER lpPerformanceCount; \
  158. static int init = 0; \
  159. static int ID = 0; \
  160. if( init == 0 ) \
  161. { \
  162. int k; \
  163. init = 1; \
  164. for( k = 0; k < silk_Timer_nTimers; k++ ) { \
  165. if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) { \
  166. ID = k; \
  167. break; \
  168. } \
  169. } \
  170. } \
  171. QueryPerformanceCounter(&lpPerformanceCount); \
  172. lpPerformanceCount.QuadPart -= silk_Timer_start[ID].QuadPart; \
  173. if((lpPerformanceCount.QuadPart < 100000000) && \
  174. (lpPerformanceCount.QuadPart >= 0)) { \
  175. silk_Timer_cnt[ID]++; \
  176. silk_Timer_sum[ID] += lpPerformanceCount.QuadPart; \
  177. if( lpPerformanceCount.QuadPart > silk_Timer_max[ID] ) \
  178. silk_Timer_max[ID] = lpPerformanceCount.QuadPart; \
  179. if( lpPerformanceCount.QuadPart < silk_Timer_min[ID] ) \
  180. silk_Timer_min[ID] = lpPerformanceCount.QuadPart; \
  181. } \
  182. silk_Timer_depth_ctr--; \
  183. }
  184. #else
  185. #define TOC(TAG_NAME) { \
  186. unsigned long endTime; \
  187. static int init = 0; \
  188. static int ID = 0; \
  189. if( init == 0 ) \
  190. { \
  191. int k; \
  192. init = 1; \
  193. for( k = 0; k < silk_Timer_nTimers; k++ ) { \
  194. if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) { \
  195. ID = k; \
  196. break; \
  197. } \
  198. } \
  199. } \
  200. endTime = GetHighResolutionTime(); \
  201. endTime -= silk_Timer_start[ID]; \
  202. if((endTime < 100000000) && \
  203. (endTime >= 0)) { \
  204. silk_Timer_cnt[ID]++; \
  205. silk_Timer_sum[ID] += endTime; \
  206. if( endTime > silk_Timer_max[ID] ) \
  207. silk_Timer_max[ID] = endTime; \
  208. if( endTime < silk_Timer_min[ID] ) \
  209. silk_Timer_min[ID] = endTime; \
  210. } \
  211. silk_Timer_depth_ctr--; \
  212. }
  213. #endif
  214. #else /* SILK_TIC_TOC */
  215. /* define macros as empty strings */
  216. #define TIC(TAG_NAME)
  217. #define TOC(TAG_NAME)
  218. #define silk_TimerSave(FILE_NAME)
  219. #endif /* SILK_TIC_TOC */
  220. #if SILK_DEBUG
  221. /************************************/
  222. /* write data to file for debugging */
  223. /************************************/
  224. /* Example: DEBUG_STORE_DATA(testfile.pcm, &RIN[0], 160*sizeof(opus_int16)); */
  225. #define silk_NUM_STORES_MAX 100
  226. extern FILE *silk_debug_store_fp[ silk_NUM_STORES_MAX ];
  227. extern int silk_debug_store_count;
  228. /* Faster way of storing the data */
  229. #define DEBUG_STORE_DATA( FILE_NAME, DATA_PTR, N_BYTES ) { \
  230. static opus_int init = 0, cnt = 0; \
  231. static FILE **fp; \
  232. if (init == 0) { \
  233. init = 1; \
  234. cnt = silk_debug_store_count++; \
  235. silk_debug_store_fp[ cnt ] = fopen(#FILE_NAME, "wb"); \
  236. } \
  237. fwrite((DATA_PTR), (N_BYTES), 1, silk_debug_store_fp[ cnt ]); \
  238. }
  239. /* Call this at the end of main() */
  240. #define SILK_DEBUG_STORE_CLOSE_FILES { \
  241. opus_int i; \
  242. for( i = 0; i < silk_debug_store_count; i++ ) { \
  243. fclose( silk_debug_store_fp[ i ] ); \
  244. } \
  245. }
  246. #else /* SILK_DEBUG */
  247. /* define macros as empty strings */
  248. #define DEBUG_STORE_DATA(FILE_NAME, DATA_PTR, N_BYTES)
  249. #define SILK_DEBUG_STORE_CLOSE_FILES
  250. #endif /* SILK_DEBUG */
  251. #ifdef __cplusplus
  252. }
  253. #endif
  254. #endif /* SILK_DEBUG_H */