From 8698d22e6738a278eb9639ba9c8860456550771f Mon Sep 17 00:00:00 2001 From: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com> Date: Thu, 28 Nov 2024 11:49:55 +0800 Subject: [PATCH] add thread cpu time for zephyr (#3937) --- core/shared/platform/zephyr/zephyr_time.c | 15 +++++++++++++-- product-mini/platforms/zephyr/simple/prj.conf | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/core/shared/platform/zephyr/zephyr_time.c b/core/shared/platform/zephyr/zephyr_time.c index 78bc3e0761..59b720e414 100644 --- a/core/shared/platform/zephyr/zephyr_time.c +++ b/core/shared/platform/zephyr/zephyr_time.c @@ -14,6 +14,17 @@ os_time_get_boot_us() uint64 os_time_thread_cputime_us(void) { - /* FIXME if u know the right api */ - return os_time_get_boot_us(); + k_tid_t tid; + struct k_thread_runtime_stats stats; + uint32 clock_freq; + uint64 cpu_cycles, time_in_us = 0; + + tid = k_current_get(); + if (k_thread_runtime_stats_get(tid, &stats) == 0) { + cpu_cycles = stats.execution_cycles; + clock_freq = CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC; + time_in_us = (cpu_cycles * 1000000) / clock_freq; + } + + return time_in_us; } diff --git a/product-mini/platforms/zephyr/simple/prj.conf b/product-mini/platforms/zephyr/simple/prj.conf index c269b8ab42..8ab3c52f8c 100644 --- a/product-mini/platforms/zephyr/simple/prj.conf +++ b/product-mini/platforms/zephyr/simple/prj.conf @@ -5,3 +5,4 @@ CONFIG_STACK_SENTINEL=y CONFIG_PRINTK=y CONFIG_LOG=y CONFIG_LOG_BUFFER_SIZE=4096 +CONFIG_THREAD_RUNTIME_STATS=y