logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

basic_profiling.h (4271B)


  1. // Copyright 2023 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. /*
  5. This API allows for basic profiling information to be printed out over console.
  6. Usage example:
  7. #include "basic_profiling.h"
  8. // Original code:
  9. matrix_task();
  10. // Delete the original, replace with the following (variant 1, automatic naming):
  11. PROFILE_CALL(1000, matrix_task());
  12. // Delete the original, replace with the following (variant 2, explicit naming):
  13. PROFILE_CALL_NAMED(1000, "matrix_task", {
  14. matrix_task();
  15. });
  16. */
  17. #if defined(PROTOCOL_LUFA) || defined(PROTOCOL_VUSB)
  18. # define TIMESTAMP_GETTER TCNT0
  19. #elif defined(PROTOCOL_CHIBIOS)
  20. # define TIMESTAMP_GETTER chSysGetRealtimeCounterX()
  21. #else
  22. # error Unknown protocol in use
  23. #endif
  24. #ifndef CONSOLE_ENABLE
  25. // Can't do anything if we don't have console output enabled.
  26. # define PROFILE_CALL_NAMED(count, name, call) \
  27. do { \
  28. } while (0)
  29. #else
  30. # define PROFILE_CALL_NAMED(count, name, call) \
  31. do { \
  32. static uint64_t inner_sum = 0; \
  33. static uint64_t outer_sum = 0; \
  34. uint32_t start_ts; \
  35. static uint32_t end_ts; \
  36. static uint32_t write_location = 0; \
  37. start_ts = TIMESTAMP_GETTER; \
  38. if (write_location > 0) { \
  39. outer_sum += start_ts - end_ts; \
  40. } \
  41. do { \
  42. call; \
  43. } while (0); \
  44. end_ts = TIMESTAMP_GETTER; \
  45. inner_sum += end_ts - start_ts; \
  46. ++write_location; \
  47. if (write_location >= ((uint32_t)count)) { \
  48. uint32_t inner_avg = inner_sum / (((uint32_t)count) - 1); \
  49. uint32_t outer_avg = outer_sum / (((uint32_t)count) - 1); \
  50. dprintf("%s -- Percentage time spent: %d%%\n", (name), (int)(inner_avg * 100 / (inner_avg + outer_avg))); \
  51. inner_sum = 0; \
  52. outer_sum = 0; \
  53. write_location = 0; \
  54. } \
  55. } while (0)
  56. #endif // CONSOLE_ENABLE
  57. #define PROFILE_CALL(count, call) PROFILE_CALL_NAMED(count, #call, call)