test_perfmon_ansi.c 6.5 KB

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