debug.h 13 KB

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