test_perfmon_ansi.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "unity.h"
  8. #include "esp_log.h"
  9. #include <stdlib.h>
  10. #include "perfmon.h"
  11. // These includes required only for the tests
  12. #include "xtensa-debug-module.h"
  13. #include "eri.h"
  14. static const char *TAG = "perfmon";
  15. TEST_CASE("Perfomance counter dump", "[perfmon]")
  16. {
  17. xtensa_perfmon_dump();
  18. xtensa_perfmon_stop();
  19. xtensa_perfmon_dump();
  20. xtensa_perfmon_init(0, 0, 0xffff, 0, 6);
  21. xtensa_perfmon_dump();
  22. xtensa_perfmon_reset(0);
  23. xtensa_perfmon_start();
  24. int pm_data[10];
  25. for (int i = 0 ; i < 10 ; i++) {
  26. if (i == 4) {
  27. xtensa_perfmon_reset(0);
  28. xtensa_perfmon_start();
  29. }
  30. if (i == 6) {
  31. xtensa_perfmon_stop();
  32. }
  33. if (i == 8) {
  34. xtensa_perfmon_start();
  35. }
  36. pm_data[i] = eri_read(ERI_PERFMON_PM0);
  37. }
  38. for (int i = 0 ; i < 10 ; i++) {
  39. ESP_LOGI(TAG, "pm_data[%i]= %08x", i, pm_data[i]);
  40. }
  41. if (pm_data[4] > pm_data[3]) {
  42. ESP_LOGE(TAG, "The functions xtensa_perfmon_reset and xtensa_perfmon_start are not working correct.");
  43. ESP_LOGW(TAG, "pm_data[3]= %i, must be > pm_data[4]= %i", pm_data[3], pm_data[4]);
  44. TEST_ESP_OK(ESP_FAIL);
  45. }
  46. if ( pm_data[6] != pm_data[7]) {
  47. ESP_LOGE(TAG, "The xtensa_perfmon_stop functions is not working correct.");
  48. ESP_LOGW(TAG, "pm_data[6]= %i, must be == pm_data[7]= %i", pm_data[6], pm_data[7]);
  49. TEST_ESP_OK(ESP_FAIL);
  50. }
  51. if ( pm_data[7] == pm_data[8]) {
  52. ESP_LOGE(TAG, "The xtensa_perfmon_start functions is not working correct.");
  53. ESP_LOGW(TAG, "pm_data[7]= %i, must be < pm_data[8]= %i", pm_data[7], pm_data[8]);
  54. TEST_ESP_OK(ESP_FAIL);
  55. }
  56. xtensa_perfmon_stop();
  57. }
  58. static void test_call(void* params)
  59. {
  60. for (int i = 0 ; i < 1000 ; i++) {
  61. __asm__ __volatile__(" nop");
  62. }
  63. }
  64. static bool callback_called = false;
  65. static int callback_call_count = 0;
  66. static void test_callback(void *params, uint32_t select, uint32_t mask, uint32_t value)
  67. {
  68. ESP_LOGI("test", "test_callback select = %i, mask = %i, value = %i", select, mask, value);
  69. callback_called = true;
  70. callback_call_count++;
  71. }
  72. TEST_CASE("Performacnce test callback", "[perfmon]")
  73. {
  74. ESP_LOGI(TAG, "Initialize performance structure");
  75. xtensa_perfmon_config_t pm_config = {};
  76. pm_config.counters_size = sizeof(xtensa_perfmon_select_mask_all) / sizeof(uint32_t) / 2;
  77. pm_config.select_mask = xtensa_perfmon_select_mask_all;
  78. pm_config.repeat_count = 200;
  79. pm_config.max_deviation = 1;
  80. pm_config.call_function = test_call;
  81. pm_config.callback = test_callback;
  82. pm_config.callback_params = stdout;
  83. pm_config.tracelevel = -1; // Trace all events
  84. callback_called = false;
  85. callback_call_count = 0;
  86. xtensa_perfmon_exec(&pm_config);
  87. ESP_LOGI(TAG, "Callback count = %i", callback_call_count);
  88. if (callback_call_count != pm_config.counters_size) {
  89. ESP_LOGE(TAG, "The callback count is not correct.");
  90. ESP_LOGW(TAG, "callback_call_count= %i, must be == pm_config.counters_size= %i", callback_call_count, pm_config.counters_size);
  91. TEST_ESP_OK(ESP_FAIL);
  92. }
  93. if (ESP_OK != xtensa_perfmon_overflow(0))
  94. {
  95. ESP_LOGE(TAG, "Perfmon 0 overflow detected!");
  96. TEST_ESP_OK(ESP_FAIL);
  97. }
  98. if (ESP_OK != xtensa_perfmon_overflow(1))
  99. {
  100. ESP_LOGE(TAG, "Perfmon 1 overflow detected!");
  101. TEST_ESP_OK(ESP_FAIL);
  102. }
  103. if (false == callback_called) {
  104. TEST_ESP_OK(ESP_FAIL);
  105. }
  106. }
  107. static void exec_callback(void *params)
  108. {
  109. for (int i = 0 ; i < 100 ; i++) {
  110. __asm__ __volatile__(" nop");
  111. }
  112. }
  113. static const uint32_t test_dsp_table[] = {
  114. XTPERF_CNT_CYCLES, XTPERF_MASK_CYCLES, // total cycles
  115. XTPERF_CNT_INSN, XTPERF_MASK_INSN_ALL, // total instructions
  116. XTPERF_CNT_D_LOAD_U1, XTPERF_MASK_D_LOAD_LOCAL_MEM, // Mem read
  117. XTPERF_CNT_D_STORE_U1, XTPERF_MASK_D_STORE_LOCAL_MEM, // Mem write
  118. XTPERF_CNT_BUBBLES, XTPERF_MASK_BUBBLES_ALL &(~XTPERF_MASK_BUBBLES_R_HOLD_REG_DEP), // wait for other reasons
  119. XTPERF_CNT_BUBBLES, XTPERF_MASK_BUBBLES_R_HOLD_REG_DEP, // Wait for register dependency
  120. XTPERF_CNT_OVERFLOW, XTPERF_MASK_OVERFLOW, // Last test cycle
  121. };
  122. TEST_CASE("Performance test for Empty callback", "[perfmon]")
  123. {
  124. for (int i = 5 ; i < 10 ; i++) {
  125. exec_callback(NULL);
  126. ESP_LOGD(TAG, "Empty call passed.");
  127. }
  128. ESP_LOGI(TAG, "Start first test");
  129. xtensa_perfmon_config_t pm_config = {};
  130. pm_config.counters_size = sizeof(xtensa_perfmon_select_mask_all) / sizeof(uint32_t) / 2;
  131. pm_config.select_mask = xtensa_perfmon_select_mask_all;
  132. pm_config.repeat_count = 200;
  133. pm_config.max_deviation = 1;
  134. pm_config.call_function = exec_callback;
  135. pm_config.callback = xtensa_perfmon_view_cb;
  136. pm_config.callback_params = stdout;
  137. pm_config.tracelevel = -1;
  138. xtensa_perfmon_exec(&pm_config);
  139. callback_call_count = 0;
  140. ESP_LOGI(TAG, "Start second test");
  141. pm_config.counters_size = sizeof(test_dsp_table) / sizeof(uint32_t) / 2;
  142. pm_config.select_mask = test_dsp_table;
  143. pm_config.repeat_count = 200;
  144. pm_config.max_deviation = 1;
  145. pm_config.call_function = exec_callback;
  146. pm_config.callback = xtensa_perfmon_view_cb;
  147. pm_config.callback_params = stdout;
  148. pm_config.tracelevel = -1;
  149. xtensa_perfmon_exec(&pm_config);
  150. callback_call_count = 0;
  151. ESP_LOGI(TAG, "Start third test");
  152. pm_config.counters_size = sizeof(test_dsp_table) / sizeof(uint32_t) / 2;
  153. pm_config.select_mask = test_dsp_table;
  154. pm_config.repeat_count = 200;
  155. pm_config.max_deviation = 1;
  156. pm_config.call_function = exec_callback;
  157. pm_config.callback = test_callback;
  158. pm_config.callback_params = stdout;
  159. pm_config.tracelevel = -1;
  160. xtensa_perfmon_exec(&pm_config);
  161. if (callback_call_count != pm_config.counters_size) {
  162. TEST_ESP_OK(ESP_FAIL);
  163. }
  164. ESP_LOGI(TAG, "All tests passed.");
  165. }