diff --git a/include/zephyr/kernel.h b/include/zephyr/kernel.h index c2d5cf1ef90..d722eb72c4b 100644 --- a/include/zephyr/kernel.h +++ b/include/zephyr/kernel.h @@ -6193,6 +6193,15 @@ int k_thread_runtime_stats_get(k_tid_t thread, */ int k_thread_runtime_stats_all_get(k_thread_runtime_stats_t *stats); +/** + * @brief Get the runtime statistics of all threads on specified cpu + * + * @param cpu The cpu number + * @param stats Pointer to struct to copy statistics into. + * @return -EINVAL if null pointers, otherwise 0 + */ +int k_thread_runtime_stats_cpu_get(int cpu, k_thread_runtime_stats_t *stats); + /** * @brief Enable gathering of runtime statistics for specified thread * diff --git a/kernel/thread.c b/kernel/thread.c index 212a08085f1..25bf9a9e55d 100644 --- a/kernel/thread.c +++ b/kernel/thread.c @@ -998,6 +998,27 @@ int k_thread_runtime_stats_all_get(k_thread_runtime_stats_t *stats) return 0; } +int k_thread_runtime_stats_cpu_get(int cpu, k_thread_runtime_stats_t *stats) +{ + if (stats == NULL) { + return -EINVAL; + } + + *stats = (k_thread_runtime_stats_t) {}; + +#ifdef CONFIG_SCHED_THREAD_USAGE_ALL +#ifdef CONFIG_SMP + z_sched_cpu_usage(cpu, stats); +#else + __ASSERT(cpu == 0, "cpu filter out of bounds"); + ARG_UNUSED(cpu); + z_sched_cpu_usage(0, stats); +#endif +#endif + + return 0; +} + #ifdef CONFIG_THREAD_ABORT_NEED_CLEANUP /** Pointer to thread which needs to be cleaned up. */ static struct k_thread *thread_to_cleanup;