debug.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #ifdef HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32. #include "debug.h"
  33. #include "SigProc_FIX.h"
  34. #if SILK_TIC_TOC
  35. #ifdef _WIN32
  36. #if (defined(_WIN32) || defined(_WINCE))
  37. #include <windows.h> /* timer */
  38. #else /* Linux or Mac*/
  39. #include <sys/time.h>
  40. #endif
  41. unsigned long silk_GetHighResolutionTime(void) /* O time in usec*/
  42. {
  43. /* Returns a time counter in microsec */
  44. /* the resolution is platform dependent */
  45. /* but is typically 1.62 us resolution */
  46. LARGE_INTEGER lpPerformanceCount;
  47. LARGE_INTEGER lpFrequency;
  48. QueryPerformanceCounter(&lpPerformanceCount);
  49. QueryPerformanceFrequency(&lpFrequency);
  50. return (unsigned long)((1000000*(lpPerformanceCount.QuadPart)) / lpFrequency.QuadPart);
  51. }
  52. #else /* Linux or Mac*/
  53. unsigned long GetHighResolutionTime(void) /* O time in usec*/
  54. {
  55. struct timeval tv;
  56. gettimeofday(&tv, 0);
  57. return((tv.tv_sec*1000000)+(tv.tv_usec));
  58. }
  59. #endif
  60. int silk_Timer_nTimers = 0;
  61. int silk_Timer_depth_ctr = 0;
  62. char silk_Timer_tags[silk_NUM_TIMERS_MAX][silk_NUM_TIMERS_MAX_TAG_LEN];
  63. #ifdef WIN32
  64. LARGE_INTEGER silk_Timer_start[silk_NUM_TIMERS_MAX];
  65. #else
  66. unsigned long silk_Timer_start[silk_NUM_TIMERS_MAX];
  67. #endif
  68. unsigned int silk_Timer_cnt[silk_NUM_TIMERS_MAX];
  69. opus_int64 silk_Timer_min[silk_NUM_TIMERS_MAX];
  70. opus_int64 silk_Timer_sum[silk_NUM_TIMERS_MAX];
  71. opus_int64 silk_Timer_max[silk_NUM_TIMERS_MAX];
  72. opus_int64 silk_Timer_depth[silk_NUM_TIMERS_MAX];
  73. #ifdef WIN32
  74. void silk_TimerSave(char *file_name)
  75. {
  76. if( silk_Timer_nTimers > 0 )
  77. {
  78. int k;
  79. FILE *fp;
  80. LARGE_INTEGER lpFrequency;
  81. LARGE_INTEGER lpPerformanceCount1, lpPerformanceCount2;
  82. int del = 0x7FFFFFFF;
  83. double avg, sum_avg;
  84. /* estimate overhead of calling performance counters */
  85. for( k = 0; k < 1000; k++ ) {
  86. QueryPerformanceCounter(&lpPerformanceCount1);
  87. QueryPerformanceCounter(&lpPerformanceCount2);
  88. lpPerformanceCount2.QuadPart -= lpPerformanceCount1.QuadPart;
  89. if( (int)lpPerformanceCount2.LowPart < del )
  90. del = lpPerformanceCount2.LowPart;
  91. }
  92. QueryPerformanceFrequency(&lpFrequency);
  93. /* print results to file */
  94. sum_avg = 0.0f;
  95. for( k = 0; k < silk_Timer_nTimers; k++ ) {
  96. if (silk_Timer_depth[k] == 0) {
  97. sum_avg += (1e6 * silk_Timer_sum[k] / silk_Timer_cnt[k] - del) / lpFrequency.QuadPart * silk_Timer_cnt[k];
  98. }
  99. }
  100. fp = fopen(file_name, "w");
  101. fprintf(fp, " min avg %% max count\n");
  102. for( k = 0; k < silk_Timer_nTimers; k++ ) {
  103. if (silk_Timer_depth[k] == 0) {
  104. fprintf(fp, "%-28s", silk_Timer_tags[k]);
  105. } else if (silk_Timer_depth[k] == 1) {
  106. fprintf(fp, " %-27s", silk_Timer_tags[k]);
  107. } else if (silk_Timer_depth[k] == 2) {
  108. fprintf(fp, " %-26s", silk_Timer_tags[k]);
  109. } else if (silk_Timer_depth[k] == 3) {
  110. fprintf(fp, " %-25s", silk_Timer_tags[k]);
  111. } else {
  112. fprintf(fp, " %-24s", silk_Timer_tags[k]);
  113. }
  114. avg = (1e6 * silk_Timer_sum[k] / silk_Timer_cnt[k] - del) / lpFrequency.QuadPart;
  115. fprintf(fp, "%8.2f", (1e6 * (silk_max_64(silk_Timer_min[k] - del, 0))) / lpFrequency.QuadPart);
  116. fprintf(fp, "%12.2f %6.2f", avg, 100.0 * avg / sum_avg * silk_Timer_cnt[k]);
  117. fprintf(fp, "%12.2f", (1e6 * (silk_max_64(silk_Timer_max[k] - del, 0))) / lpFrequency.QuadPart);
  118. fprintf(fp, "%10d\n", silk_Timer_cnt[k]);
  119. }
  120. fprintf(fp, " microseconds\n");
  121. fclose(fp);
  122. }
  123. }
  124. #else
  125. void silk_TimerSave(char *file_name)
  126. {
  127. if( silk_Timer_nTimers > 0 )
  128. {
  129. int k;
  130. FILE *fp;
  131. /* print results to file */
  132. fp = fopen(file_name, "w");
  133. fprintf(fp, " min avg max count\n");
  134. for( k = 0; k < silk_Timer_nTimers; k++ )
  135. {
  136. if (silk_Timer_depth[k] == 0) {
  137. fprintf(fp, "%-28s", silk_Timer_tags[k]);
  138. } else if (silk_Timer_depth[k] == 1) {
  139. fprintf(fp, " %-27s", silk_Timer_tags[k]);
  140. } else if (silk_Timer_depth[k] == 2) {
  141. fprintf(fp, " %-26s", silk_Timer_tags[k]);
  142. } else if (silk_Timer_depth[k] == 3) {
  143. fprintf(fp, " %-25s", silk_Timer_tags[k]);
  144. } else {
  145. fprintf(fp, " %-24s", silk_Timer_tags[k]);
  146. }
  147. fprintf(fp, "%d ", silk_Timer_min[k]);
  148. fprintf(fp, "%f ", (double)silk_Timer_sum[k] / (double)silk_Timer_cnt[k]);
  149. fprintf(fp, "%d ", silk_Timer_max[k]);
  150. fprintf(fp, "%10d\n", silk_Timer_cnt[k]);
  151. }
  152. fprintf(fp, " microseconds\n");
  153. fclose(fp);
  154. }
  155. }
  156. #endif
  157. #endif /* SILK_TIC_TOC */
  158. #if SILK_DEBUG
  159. FILE *silk_debug_store_fp[ silk_NUM_STORES_MAX ];
  160. int silk_debug_store_count = 0;
  161. #endif /* SILK_DEBUG */