test_perfmon.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stddef.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include "esp_log.h"
  11. #include "perfmon.h"
  12. #include "unity.h"
  13. #include "xtensa-debug-module.h"
  14. #include "eri.h"
  15. #include "xtensa_perfmon_access.h"
  16. static const char *TAG = "perfmon";
  17. static void delay(void)
  18. {
  19. for (int i = 0 ; i < 1000 ; i++) {
  20. __asm__ __volatile__("nop");
  21. }
  22. }
  23. TEST_CASE("Start/stop/reset sanity check", "[perfmon]")
  24. {
  25. xtensa_perfmon_stop();
  26. xtensa_perfmon_init(0, 0, 0xffff, 0, 6);
  27. xtensa_perfmon_reset(0);
  28. delay();
  29. uint32_t count_0 = eri_read(ERI_PERFMON_PM0);
  30. TEST_ASSERT_EQUAL_UINT32_MESSAGE(0, count_0, "Counter should be 0 after reset");
  31. xtensa_perfmon_start();
  32. delay();
  33. uint32_t count_1 = eri_read(ERI_PERFMON_PM0);
  34. TEST_ASSERT_GREATER_THAN_UINT32_MESSAGE(0, count_1, "Counter should not be 0 after start");
  35. xtensa_perfmon_stop();
  36. uint32_t count_2 = eri_read(ERI_PERFMON_PM0);
  37. delay();
  38. uint32_t count_3 = eri_read(ERI_PERFMON_PM0);
  39. TEST_ASSERT_EQUAL_UINT32_MESSAGE(count_2, count_3, "Counter should not change after stop");
  40. xtensa_perfmon_reset(0);
  41. xtensa_perfmon_start();
  42. delay();
  43. delay();
  44. uint32_t count_4 = eri_read(ERI_PERFMON_PM0);
  45. TEST_ASSERT_GREATER_THAN_UINT32_MESSAGE(count_1, count_4, "Counter should be greater when delay is longer");
  46. xtensa_perfmon_stop();
  47. }
  48. static void test_call(void *params)
  49. {
  50. delay();
  51. }
  52. static void test_callback(void *params, uint32_t select, uint32_t mask, uint32_t value)
  53. {
  54. int *call_count = (int *)params;
  55. ESP_LOGI(TAG, "%s: select=%" PRIu32 ", mask=%" PRIx32 ", value=%" PRIu32,
  56. __func__, select, mask, value);
  57. (*call_count)++;
  58. }
  59. TEST_CASE("xtensa_perfmon_exec custom callback", "[perfmon]")
  60. {
  61. int num_counters = sizeof(xtensa_perfmon_select_mask_all) / sizeof(xtensa_perfmon_select_mask_all[0]) / 2;
  62. int callback_call_count = 0;
  63. xtensa_perfmon_config_t pm_config = {
  64. .counters_size = num_counters,
  65. .select_mask = xtensa_perfmon_select_mask_all,
  66. .repeat_count = 200,
  67. .max_deviation = 1,
  68. .call_function = test_call,
  69. .call_params = NULL,
  70. .callback = test_callback,
  71. .callback_params = &callback_call_count,
  72. .tracelevel = -1
  73. };
  74. TEST_ESP_OK(xtensa_perfmon_exec(&pm_config));
  75. TEST_ASSERT_NOT_EQUAL_MESSAGE(0, callback_call_count, "Callback should be called at least once");
  76. // Performance counters should not have overflow status set
  77. TEST_ESP_OK(xtensa_perfmon_overflow(0));
  78. TEST_ESP_OK(xtensa_perfmon_overflow(1));
  79. TEST_ASSERT_EQUAL_MESSAGE(num_counters, callback_call_count,
  80. "Callback should be called once for every counter");
  81. }
  82. TEST_CASE("xtensa_perfmon_view_cb test", "[perfmon]")
  83. {
  84. const uint32_t test_table[] = {
  85. XTPERF_CNT_CYCLES, XTPERF_MASK_CYCLES, // total cycles
  86. XTPERF_CNT_INSN, XTPERF_MASK_INSN_ALL, // total instructions
  87. XTPERF_CNT_D_LOAD_U1, XTPERF_MASK_D_LOAD_LOCAL_MEM, // Mem read
  88. XTPERF_CNT_D_STORE_U1, XTPERF_MASK_D_STORE_LOCAL_MEM, // Mem write
  89. XTPERF_CNT_BUBBLES, XTPERF_MASK_BUBBLES_ALL &(~XTPERF_MASK_BUBBLES_R_HOLD_REG_DEP), // wait for other reasons
  90. XTPERF_CNT_BUBBLES, XTPERF_MASK_BUBBLES_R_HOLD_REG_DEP, // Wait for register dependency
  91. XTPERF_CNT_OVERFLOW, XTPERF_MASK_OVERFLOW, // Last test cycle
  92. };
  93. int num_counters = sizeof(test_table) / sizeof(test_table[0]) / 2;
  94. // We will collect the output of xtensa_perfmon_view_cb in a string
  95. // and check that the output matches the counters table above.
  96. char *out_str = NULL;
  97. size_t out_len = 0;
  98. FILE *out_stream = open_memstream(&out_str, &out_len);
  99. xtensa_perfmon_config_t pm_config = {
  100. .counters_size = num_counters,
  101. .select_mask = test_table,
  102. .repeat_count = 200,
  103. .max_deviation = 1,
  104. .call_function = test_call,
  105. .call_params = NULL,
  106. .callback = xtensa_perfmon_view_cb,
  107. .callback_params = out_stream,
  108. .tracelevel = -1,
  109. };
  110. TEST_ESP_OK(xtensa_perfmon_exec(&pm_config));
  111. fclose(out_stream);
  112. TEST_ASSERT_MESSAGE(strlen(out_str) > 0, "xtensa_perfmon_view_cb should print something");
  113. // Check that performance counters defined in test_table are present in the output:
  114. const char *p = out_str;
  115. // 1. XTPERF_CNT_CYCLES
  116. p = strstr(p, "Value =");
  117. TEST_ASSERT_NOT_NULL(p);
  118. p = strstr(p, "Counts cycles.");
  119. TEST_ASSERT_NOT_NULL(p);
  120. // 2. XTPERF_CNT_INSN
  121. p = strstr(p, "Value =");
  122. TEST_ASSERT_NOT_NULL(p);
  123. p = strstr(p, "Successfully Retired Instructions.");
  124. // 3. XTPERF_CNT_D_LOAD_U1
  125. p = strstr(p, "Value =");
  126. TEST_ASSERT_NOT_NULL(p);
  127. p = strstr(p, "Load Instruction (Data Memory).");
  128. // 4. XTPERF_CNT_D_STORE_U1
  129. p = strstr(p, "Value =");
  130. TEST_ASSERT_NOT_NULL(p);
  131. p = strstr(p, "Store Instruction (Data Memory).");
  132. TEST_ASSERT_NOT_NULL(p);
  133. // 5. XTPERF_CNT_BUBBLES
  134. p = strstr(p, "Value =");
  135. TEST_ASSERT_NOT_NULL(p);
  136. p = strstr(p, "Hold and Other Bubble cycles.");
  137. TEST_ASSERT_NOT_NULL(p);
  138. p = strstr(p, "CTI bubble (e.g. branch delay slot)");
  139. // 6. XTPERF_CNT_BUBBLES (with a different mask)
  140. p = strstr(p, "Value =");
  141. TEST_ASSERT_NOT_NULL(p);
  142. p = strstr(p, "Hold and Other Bubble cycles.");
  143. TEST_ASSERT_NOT_NULL(p);
  144. p = strstr(p, "R hold caused by register dependency");
  145. // 7. XTPERF_CNT_OVERFLOW
  146. p = strstr(p, "Value =");
  147. TEST_ASSERT_NOT_NULL(p);
  148. p = strstr(p, "Overflow counter");
  149. TEST_ASSERT_NOT_NULL(p);
  150. free(out_str);
  151. }