From 0ee1278729a71f473c55ba7e269f29d14a1b7dfd Mon Sep 17 00:00:00 2001 From: Serhiy Katsyuba Date: Wed, 8 Jun 2022 18:43:32 +0200 Subject: [PATCH] Fix performance counter regression A regression bug was introduced by gh-5393 pull request. As a source of high precision CPU timestamp (aka "system" timer) this fix uses arch_timing_counter_get() for builds with Zephyr and timer_get_system(cpu_timer_get()) for SOF only builds. Note: arch_timing_counter_get() might not be implemented for your Zephyr platform. In this case k_cycle_get_64() is used instead. This will result in both "platform" and "cpu" timestamps to be equal. Signed-off-by: Serhiy Katsyuba --- src/include/sof/lib/perf_cnt.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/include/sof/lib/perf_cnt.h b/src/include/sof/lib/perf_cnt.h index f5bb2e632da4..697b7c9f681f 100644 --- a/src/include/sof/lib/perf_cnt.h +++ b/src/include/sof/lib/perf_cnt.h @@ -39,10 +39,25 @@ struct perf_cnt_data { /** \brief Clears performance counters data. */ #define perf_cnt_clear(pcd) memset((pcd), 0, sizeof(struct perf_cnt_data)) +/* NOTE: Zephyr's arch_timing_counter_get() might not be implemented + * for a particular platform. In this case let's fallback to use + * Zephyr's k_cycle_get_64(). This will result in both "platform" and + * "cpu" timestamps to be equal. + */ +#ifdef __ZEPHYR__ + #ifdef CONFIG_TIMING_FUNCTIONS + #define perf_cnt_get_cpu_ts arch_timing_counter_get + #else + #define perf_cnt_get_cpu_ts k_cycle_get_64 + #endif /* CONFIG_TIMING_FUNCTIONS */ +#else + #define perf_cnt_get_cpu_ts() timer_get_system(cpu_timer_get()) +#endif /* __ZEPHYR__ */ + /** \brief Initializes timestamps with current timer values. */ #define perf_cnt_init(pcd) do { \ (pcd)->plat_ts = k_cycle_get_64(); \ - (pcd)->cpu_ts = k_cycle_get_64(); \ + (pcd)->cpu_ts = perf_cnt_get_cpu_ts(); \ } while (0) /* Trace macros that can be used as trace_m argument of the perf_cnt_stamp() @@ -99,7 +114,7 @@ struct perf_cnt_data { uint32_t plat_ts = \ (uint32_t)k_cycle_get_64(); \ uint32_t cpu_ts = \ - (uint32_t)k_cycle_get_64(); \ + (uint32_t)perf_cnt_get_cpu_ts(); \ if ((pcd)->plat_ts) { \ (pcd)->plat_delta_last = plat_ts - (pcd)->plat_ts; \ (pcd)->cpu_delta_last = cpu_ts - (pcd)->cpu_ts; \