nmsis_bench.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (c) 2019 Nuclei Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef __NMSIS_BENCH__
  19. #define __NMSIS_BENCH__
  20. /*!
  21. * @file nmsis_bench.h
  22. * @brief benchmark and helper related API for Nuclei N/NX Core
  23. */
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include "core_feature_base.h"
  28. #include <stdio.h>
  29. /**
  30. * \defgroup NMSIS_Core_Bench_Helpers NMSIS Bench and Test Related Helper Functions
  31. * \ingroup NMSIS_Core
  32. * \brief Functions that used to do benchmark and test suite.
  33. * \details
  34. *
  35. * NMSIS benchmark and test related helper functions are provided to help do benchmark
  36. * and test case pass/fail assertion.
  37. *
  38. * If you want to do calculate cpu cycle cost of a process, you can use BENCH_xxx macros
  39. * defined in this.
  40. *
  41. * In a single c source code file, you should include `nmsis_bench.h`, and then you should place `BENCH_DECLARE_VAR();`
  42. * before call other BENCH_xxx macros. If you want to start to do benchmark, you should only call `BENCH_INIT();`
  43. * once in your source code, and then place `BENCH_START(proc_name);` and `BENCH_END(proc_name)` before
  44. * and after the process you want to measure. You can refer to `<nuclei-sdk>/application/baremetal/demo_dsp`
  45. * for how to use it.
  46. *
  47. * If you want to disable the benchmark calculation, you can place `#define DISABLE_NMSIS_BENCH`
  48. * before include `nmsis_bench.h`
  49. *
  50. * If in your c test source code, you can add `NMSIS_TEST_PASS();` and `NMSIS_TEST_FAIL();` to mark c test
  51. * is pass or fail.
  52. *
  53. * @{
  54. */
  55. /**
  56. * \brief Prepare benchmark environment
  57. * \details
  58. * Prepare benchmark required environment, such as turn on necessary units
  59. * like vpu, cycle, instret counters, hpm counters
  60. */
  61. __STATIC_FORCEINLINE void __prepare_bench_env(void)
  62. {
  63. #ifdef __riscv_vector
  64. __RV_CSR_SET(CSR_MSTATUS, MSTATUS_VS);
  65. #endif
  66. __enable_all_counter();
  67. }
  68. #ifndef READ_CYCLE
  69. /** Read run cycle of cpu */
  70. #define READ_CYCLE __get_rv_cycle
  71. #endif
  72. #ifndef DISABLE_NMSIS_BENCH
  73. /** Declare benchmark required variables, need to be placed above all BENCH_xxx macros in each c source code if BENCH_xxx used */
  74. #define BENCH_DECLARE_VAR() static volatile uint64_t _bc_sttcyc, _bc_endcyc, _bc_usecyc, _bc_sumcyc, _bc_lpcnt, _bc_ercd;
  75. /** Initialize benchmark environment, need to called in before other BENCH_xxx macros are called */
  76. #define BENCH_INIT() printf("Benchmark initialized\n"); \
  77. __prepare_bench_env(); \
  78. _bc_ercd = 0; _bc_sumcyc = 0;
  79. /** Reset benchmark sum cycle and use cycle for proc */
  80. #define BENCH_RESET(proc) _bc_sumcyc = 0; _bc_usecyc = 0; _bc_lpcnt = 0; _bc_ercd = 0;
  81. /** Start to do benchmark for proc, and record start cycle, and reset error code */
  82. #define BENCH_START(proc) _bc_ercd = 0; \
  83. _bc_sttcyc = READ_CYCLE();
  84. /** Sample a benchmark for proc, and record this start -> sample cost cycle, and accumulate it to sum cycle */
  85. #define BENCH_SAMPLE(proc) _bc_endcyc = READ_CYCLE(); \
  86. _bc_usecyc = _bc_endcyc - _bc_sttcyc; \
  87. _bc_sumcyc += _bc_usecyc; _bc_lpcnt += 1;
  88. /** Mark end of benchmark for proc, and calc used cycle, and print it */
  89. #define BENCH_END(proc) BENCH_SAMPLE(proc); \
  90. printf("CSV, %s, %lu\n", #proc, (unsigned long)_bc_usecyc);
  91. /** Mark stop of benchmark, start -> sample -> sample -> stop, and print the sum cycle of a proc */
  92. #define BENCH_STOP(proc) printf("CSV, %s, %lu\n", #proc, (unsigned long)_bc_sumcyc);
  93. /** Show statistics of benchmark, format: STAT, proc, loopcnt, sumcyc */
  94. #define BENCH_STAT(proc) printf("STAT, %s, %lu, %lu\n", #proc, (unsigned long)_bc_lpcnt, (unsigned long)_bc_sumcyc);
  95. /** Get benchmark use cycle */
  96. #define BENCH_GET_USECYC() (_bc_usecyc)
  97. /** Get benchmark sum cycle */
  98. #define BENCH_GET_SUMCYC() (_bc_sumcyc)
  99. /** Get benchmark loop count */
  100. #define BENCH_GET_LPCNT() (_bc_lpcnt)
  101. /** Mark benchmark for proc is errored */
  102. #define BENCH_ERROR(proc) _bc_ercd = 1;
  103. /** Show the status of the benchmark */
  104. #define BENCH_STATUS(proc) if (_bc_ercd) { \
  105. printf("ERROR, %s\n", #proc); \
  106. } else { \
  107. printf("SUCCESS, %s\n", #proc); \
  108. }
  109. #else
  110. #define BENCH_DECLARE_VAR() static volatile uint64_t _bc_ercd, _bc_lpcnt;
  111. #define BENCH_INIT() _bc_ercd = 0; __prepare_bench_env();
  112. #define BENCH_RESET(proc)
  113. #define BENCH_START(proc) _bc_ercd = 0;
  114. #define BENCH_SAMPLE(proc) _bc_lpcnt += 1;
  115. #define BENCH_END(proc)
  116. #define BENCH_STOP(proc)
  117. #define BENCH_STAT(proc)
  118. #define BENCH_GET_USECYC() (0)
  119. #define BENCH_GET_SUMCYC() (0)
  120. #define BENCH_GET_LPCNT() (_bc_lpcnt)
  121. #define BENCH_ERROR(proc) _bc_ercd = 1;
  122. #define BENCH_STATUS(proc) if (_bc_ercd) { \
  123. printf("ERROR, %s\n", #proc); \
  124. } else { \
  125. printf("SUCCESS, %s\n", #proc); \
  126. }
  127. #endif
  128. // High performance monitor bench helpers
  129. #ifndef DISABLE_NMSIS_HPM
  130. /* Events type select */
  131. #define EVENT_SEL_INSTRUCTION_COMMIT 0
  132. #define EVENT_SEL_MEMORY_ACCESS 1
  133. /* Instruction commit events idx define*/
  134. #define EVENT_INSTRUCTION_COMMIT_CYCLE_COUNT 1
  135. #define EVENT_INSTRUCTION_COMMIT_RETIRED_COUNT 2
  136. /* Integer load instruction (includes LR) */
  137. #define EVENT_INSTRUCTION_COMMIT_INTEGER_LOAD 3
  138. /* Integer store instruction (includes SC) */
  139. #define EVENT_INSTRUCTION_COMMIT_INTEGER_STORE 4
  140. /* Atomic memory operation (do not include LR and SC) */
  141. #define EVENT_INSTRUCTION_COMMIT_ATOMIC_MEMORY_OPERATION 5
  142. /* System instruction */
  143. #define EVENT_INSTRUCTION_COMMIT_SYSTEM 6
  144. /* Integer computational instruction (excluding multiplication/division/remainder) */
  145. #define EVENT_INSTRUCTION_COMMIT_INTEGER_COMPUTATIONAL 7
  146. #define EVENT_INSTRUCTION_COMMIT_CONDITIONAL_BRANCH 8
  147. #define EVENT_INSTRUCTION_COMMIT_TAKEN_CONDITIONAL_BRANCH 9
  148. #define EVENT_INSTRUCTION_COMMIT_JAL 10
  149. #define EVENT_INSTRUCTION_COMMIT_JALR 11
  150. #define EVENT_INSTRUCTION_COMMIT_RETURN 12
  151. /* Control transfer instruction (CBR+JAL+JALR) */
  152. #define EVENT_INSTRUCTION_COMMIT_CONTROL_TRANSFER 13
  153. /* 14 Reseved */
  154. #define EVENT_INSTRUCTION_COMMIT_INTEGER_MULTIPLICATION 15
  155. /* Integer division/remainder instruction */
  156. #define EVENT_INSTRUCTION_COMMIT_INTEGER_DIVISION_REMAINDER 16
  157. #define EVENT_INSTRUCTION_COMMIT_FLOATING_POINT_LOAD 17
  158. #define EVENT_INSTRUCTION_COMMIT_FLOATING_POINT_STORE 18
  159. /* Floating-point addition/subtraction */
  160. #define EVENT_INSTRUCTION_COMMIT_FLOATING_POINT_ADDITION_SUBTRACTION 19
  161. #define EVENT_INSTRUCTION_COMMIT_FLOATING_POINT_MULTIPLICATION 20
  162. /* Floating-point fused multiply-add (FMADD, FMSUB, FNMSUB, FNMADD) */
  163. #define EVENT_INSTRUCTION_COMMIT_FLOATING_POINT_FUSED_MULTIPLY_ADD_SUB 21
  164. #define EVENT_INSTRUCTION_COMMIT_FLOATING_POINT_DIVISION_OR_SQUARE_ROOT 22
  165. #define EVENT_INSTRUCTION_COMMIT_OTHER_FLOATING_POINT_INSTRUCTION 23
  166. #define EVENT_INSTRUCTION_COMMIT_CONDITIONAL_BRANCH_PREDICTION_FAIL 24
  167. #define EVENT_INSTRUCTION_COMMIT_JAL_PREDICTION_FAIL 25
  168. #define EVENT_INSTRUCTION_COMMIT_JALR_PREDICTION_FAIL 26
  169. /* Memory access events idx define*/
  170. #define EVENT_MEMORY_ACCESS_ICACHE_MISS 1
  171. #define EVENT_MEMORY_ACCESS_DCACHE_MISS 2
  172. #define EVENT_MEMORY_ACCESS_ITLB_MISS 3
  173. #define EVENT_MEMORY_ACCESS_DTLB_MISS 4
  174. #define EVENT_MEMORY_ACCESS_MAIN_DTLB_MISS 5
  175. /* Enable the corresponding performance monitor counter increment for events in Machine/Supervisor/User Mode */
  176. #define MSU_EVENT_ENABLE 0x0F
  177. #define MEVENT_EN 0x08
  178. #define SEVENT_EN 0x02
  179. #define UEVENT_EN 0x01
  180. /** Declare high performance monitor counter idx benchmark required variables, need to be placed above all HPM_xxx macros in each c source code if HPM_xxx used */
  181. #define HPM_DECLARE_VAR(idx) static volatile uint64_t __hpm_sttcyc##idx, __hpm_endcyc##idx, __hpm_usecyc##idx, __hpm_sumcyc##idx, __hpm_lpcnt##idx, __hpm_val##idx;
  182. #define HPM_SEL_ENABLE(ena) (ena << 28)
  183. #define HPM_SEL_EVENT(sel, idx) ((sel) | (idx << 4))
  184. /** Construct a event variable to be set(sel -> event_sel, idx -> event_idx, ena -> m/s/u_enable) */
  185. #define HPM_EVENT(sel, idx, ena) (HPM_SEL_ENABLE(ena) | HPM_SEL_EVENT(sel, idx))
  186. /** Initialize high performance monitor environment, need to called in before other HPM_xxx macros are called */
  187. #define HPM_INIT() printf("High performance monitor initialized\n"); \
  188. __prepare_bench_env();
  189. /** Reset high performance benchmark for proc using counter which index is idx */
  190. #define HPM_RESET(idx, proc, event) __hpm_sumcyc##idx = 0; __hpm_lpcnt##idx = 0;
  191. /** Start to do high performance benchmark for proc, and record start hpm counter */
  192. #define HPM_START(idx, proc, event) \
  193. __hpm_val##idx = (event); \
  194. __set_hpm_event(idx, __hpm_val##idx); \
  195. __set_hpm_counter(idx, 0); \
  196. __hpm_sttcyc##idx = __get_hpm_counter(idx);
  197. /** Do high performance benchmark sample for proc, and sum it into sum counter */
  198. #define HPM_SAMPLE(idx, proc, event) \
  199. __hpm_endcyc##idx = __get_hpm_counter(idx); \
  200. __hpm_usecyc##idx = __hpm_endcyc##idx - __hpm_sttcyc##idx; \
  201. __hpm_sumcyc##idx += __hpm_usecyc##idx; \
  202. __hpm_lpcnt##idx += 1;
  203. /** Mark end of high performance benchmark for proc, and calc used hpm counter value */
  204. #define HPM_END(idx, proc, event) \
  205. HPM_SAMPLE(idx, proc, event); \
  206. printf("HPM%d:0x%x, %s, %lu\n", idx, event, #proc, (unsigned long)__hpm_usecyc##idx);
  207. /** Mark stop of hpm benchmark, start -> sample -> sample -> stop, and print the sum cycle of a proc */
  208. #define HPM_STOP(idx, proc, event) \
  209. printf("HPM%d:0x%x, %s, %lu\n", idx, event, #proc, (unsigned long)__hpm_sumcyc##idx);
  210. /** Show statistics of hpm benchmark, format: STATHPM#idx:event, proc, loopcnt, sumcyc */
  211. #define HPM_STAT(idx, proc, event) \
  212. printf("STATHPM%d:0x%x, %s, %lu, %lu\n", idx, event, #proc, (unsigned long)__hpm_lpcnt##idx, (unsigned long)__hpm_sumcyc##idx);
  213. /** Get hpm benchmark use cycle for counter idx */
  214. #define HPM_GET_USECYC(idx) (__hpm_usecyc##idx)
  215. /** Get hpm benchmark sum cycle for counter idx */
  216. #define HPM_GET_SUMCYC(idx) (__hpm_sumcyc##idx)
  217. /** Get hpm benchmark loop count for counter idx */
  218. #define HPM_GET_LPCNT(idx) (__hpm_lpcnt##idx)
  219. #else
  220. #define HPM_DECLARE_VAR(idx)
  221. #define HPM_EVENT(sel, idx, ena)
  222. #define HPM_INIT()
  223. #define HPM_RESET(idx, proc, event)
  224. #define HPM_START(idx, proc, event)
  225. #define HPM_SAMPLE(idx, proc, event)
  226. #define HPM_END(idx, proc, event)
  227. #define HPM_STOP(idx, proc, event)
  228. #define HPM_STAT(idx, proc, event)
  229. #define HPM_GET_USECYC(idx) (0)
  230. #define HPM_GET_SUMCYC(idx) (0)
  231. #define HPM_GET_LPCNT(idx) (1)
  232. #endif
  233. // NMSIS Helpers
  234. #ifndef DISABLE_NMSIS_HELPER
  235. /** Mark test or application passed */
  236. #define NMSIS_TEST_PASS() printf("\nNMSIS_TEST_PASS\n");
  237. /** Mark test or application failed */
  238. #define NMSIS_TEST_FAIL() printf("\nNMSIS_TEST_FAIL\n");
  239. #else
  240. #define NMSIS_TEST_PASS()
  241. #define NMSIS_TEST_FAIL()
  242. #endif
  243. /** @} */ /* End of Doxygen Group NMSIS_Core_Bench_Helpers */
  244. #ifdef __cplusplus
  245. }
  246. #endif
  247. #endif /* __NMSIS_BENCH__ */