perf_trace.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This software is released under the MIT License.
  8. * https://opensource.org/licenses/MIT
  9. *
  10. */
  11. #pragma once
  12. #include "sys/time.h"
  13. #define PERF_MAX LONG_MAX
  14. #define MIN_MAX_VAL(x) x==PERF_MAX?0:x
  15. #define CURR_SAMPLE_RATE output.current_sample_rate>0?output.current_sample_rate:1
  16. #define FRAMES_TO_MS(f) (uint32_t)f*(uint32_t)1000/(uint32_t)(CURR_SAMPLE_RATE)
  17. #ifdef BYTES_TO_FRAME
  18. #define BYTES_TO_MS(b) FRAMES_TO_MS(BYTES_TO_FRAME(b))
  19. #else
  20. #define BYTES_TO_MS(b) FRAMES_TO_MS(b/BYTES_PER_FRAME)
  21. #endif
  22. #define SET_MIN_MAX(val,var) var=val; if(var<min_##var) min_##var=var; if(var>max_##var) max_##var=var; count_##var++; avgtot_##var+= var
  23. #define SET_MIN_MAX_SIZED(val,var,siz) var=val; if(var<min_##var) min_##var=var; if(var>max_##var) max_##var=var; count_##var++; avgtot_##var+= var;size_##var=siz
  24. #define RESET_MIN_MAX(var) min_##var=PERF_MAX; max_##var=0; avgtot_##var=0;count_##var=0;var=0;size_##var=0
  25. #define RESET_MIN_MAX_DURATION(var) min_##var=PERF_MAX; max_##var=0; avgtot_##var=0;count_##var=0;var=0
  26. #define DECLARE_MIN_MAX(var) static uint32_t min_##var = PERF_MAX, max_##var = 0, size_##var = 0, count_##var=0;static uint64_t avgtot_##var = 0; static uint32_t var=0
  27. #define DECLARE_MIN_MAX_DURATION(var) static uint32_t min_##var = PERF_MAX, max_##var = 0, count_##var=0; uint64_t avgtot_##var = 0; uint32_t var=0
  28. #define LINE_MIN_MAX_AVG(var) (uint32_t)(count_##var>0?avgtot_##var/count_##var:0)
  29. #define LINE_MIN_MAX_FORMAT_HEAD1 " +----------+----------+----------------+-----+----------------+"
  30. #define LINE_MIN_MAX_FORMAT_HEAD2 " | max | min | average | | count |"
  31. #define LINE_MIN_MAX_FORMAT_HEAD3 " | (bytes) | (bytes) | (bytes) | | |"
  32. #define LINE_MIN_MAX_FORMAT_HEAD4 " +----------+----------+----------------+-----+----------------+"
  33. #define LINE_MIN_MAX_FORMAT_FOOTER " +----------+----------+----------------+-----+----------------+"
  34. #define LINE_MIN_MAX_FORMAT "%14s|%10u|%10u|%16u|%5u|%16u|"
  35. #define LINE_MIN_MAX(name,var) name,\
  36. MIN_MAX_VAL(max_##var),\
  37. MIN_MAX_VAL(min_##var),\
  38. LINE_MIN_MAX_AVG(var),\
  39. size_##var!=0?100*LINE_MIN_MAX_AVG(var)/size_##var:0,\
  40. count_##var
  41. #define LINE_MIN_MAX_FORMAT_STREAM "%14s|%10u|%10u|%16u|%5u|%16u|"
  42. #define LINE_MIN_MAX_STREAM(name,var) name,\
  43. MIN_MAX_VAL(max_##var),\
  44. MIN_MAX_VAL(min_##var),\
  45. LINE_MIN_MAX_AVG(var),\
  46. size_##var!=0?100*LINE_MIN_MAX_AVG(var)/size_##var:0,\
  47. count_##var
  48. #define LINE_MIN_MAX_DURATION_FORMAT "%14s%10u|%10u|%11u|%11u|"
  49. #define LINE_MIN_MAX_DURATION(name,var) name,MIN_MAX_VAL(max_##var),MIN_MAX_VAL(min_##var), LINE_MIN_MAX_AVG(var), count_##var
  50. #define TIME_MEASUREMENT_START(x) x=esp_timer_get_time()
  51. #define TIME_MEASUREMENT_GET(x) (esp_timer_get_time()-x)
  52. #define TIMED_SECTION_START_MS_FORCE(x,force) if(hasTimeElapsed(x,force)) {
  53. #define TIMED_SECTION_START_MS(x) if(hasTimeElapsed(x,false)){
  54. #define TIMED_SECTION_START_FORCE(x,force) TIMED_SECTION_START_MS(x * 1000UL,force)
  55. #define TIMED_SECTION_START(x) TIMED_SECTION_START_MS(x * 1000UL)
  56. #define TIMED_SECTION_END }
  57. static inline bool hasTimeElapsed(time_t delayMS, bool bforce)
  58. {
  59. static time_t lastTime=0;
  60. struct timeval tv;
  61. gettimeofday(&tv, NULL);
  62. if (lastTime <= tv.tv_sec * 1000 + tv.tv_usec / 1000 ||bforce)
  63. {
  64. lastTime = tv.tv_sec * 1000 + tv.tv_usec / 1000 + delayMS;
  65. return true;
  66. }
  67. else
  68. return false;
  69. }