debug.h 13 KB

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