debug.h 13 KB

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