From 90c90747b88b2eea8ca60be9b0b68f9d0d57215d Mon Sep 17 00:00:00 2001 From: Gabriel Wang Date: Wed, 28 Feb 2024 20:02:30 +0000 Subject: [PATCH] add __cpu_perf__ --- perf_counter.h | 6 ++++-- perfc_port_pmu.h | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/perf_counter.h b/perf_counter.h index 6904ec0..a3801b1 100644 --- a/perf_counter.h +++ b/perf_counter.h @@ -380,14 +380,14 @@ __asm(".global __ensure_systick_wrapper\n\t"); E.g. \code while (1) { - __cpu_time__(100) { + __cpu_usage__(100) { delay_us(5000); } delay_us(5000); } \endcode */ -#define __cpu_time__(__CNT, ...) \ +#define __cpu_usage__(__CNT, ...) \ static int64_t SAFE_NAME(s_lTimestamp) = 0, SAFE_NAME(s_lTotal) = 0; \ static uint32_t s_wLoopCounter = (__CNT); \ using(float __usage__ = 0, ({ \ @@ -412,6 +412,8 @@ __asm(".global __ensure_systick_wrapper\n\t"); ({SAFE_NAME(s_lTotal) += stop_task_cycle_counter(); \ s_wLoopCounter--;})) +#define __cpu_time__ __cpu_usage__ + /*! * \addtogroup gBasicTimerService 1.2 Timer Service * \ingroup gBasic diff --git a/perfc_port_pmu.h b/perfc_port_pmu.h index fbb47d1..0ce7b68 100644 --- a/perfc_port_pmu.h +++ b/perfc_port_pmu.h @@ -23,6 +23,43 @@ /*============================ MACROS ========================================*/ /*============================ MACROFIED FUNCTIONS ===========================*/ +#define __cpu_perf__(__str, ...) \ + using( \ + struct { \ + uint64_t dwNoInstr; \ + int64_t lCycles; \ + uint32_t wCalib; \ + float fCPI; \ + } __PERF_INFO__ = {0}, \ + ({ \ + __PERF_INFO__.dwNoInstr = perfc_pmu_get_instruction_count(); \ + __PERF_INFO__.wCalib = perfc_pmu_get_instruction_count() \ + - __PERF_INFO__.dwNoInstr; \ + __PERF_INFO__.dwNoInstr = perfc_pmu_get_instruction_count(); \ + }), \ + ({ \ + __PERF_INFO__.dwNoInstr = perfc_pmu_get_instruction_count() \ + - __PERF_INFO__.dwNoInstr \ + - __PERF_INFO__.wCalib; \ + __PERF_INFO__.fCPI = (float)( (double)__PERF_INFO__.lCycles \ + / (double)__PERF_INFO__.dwNoInstr); \ + if (__PLOOC_VA_NUM_ARGS(__VA_ARGS__) == 0) { \ + printf( "\r\n" \ + "[Report for " __str "]\r\n" \ + "-----------------------------------------\r\n" \ + "Instruction executed: %lld\r\n" \ + "Cycle Used: %lld\r\n" \ + "Cycles per Instructions: %3.3f \r\n", \ + __PERF_INFO__.dwNoInstr, \ + __PERF_INFO__.lCycles, \ + __PERF_INFO__.fCPI); \ + } else { \ + __VA_ARGS__ \ + } \ + })) \ + __cycleof__("", { __PERF_INFO__.lCycles = __cycle_count__; }) + + /*============================ TYPES =========================================*/ typedef uint32_t perfc_global_interrupt_status_t;