debug.h 13 KB

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