diff --git a/include/kernel.h b/include/kernel.h index 340a87142c1..c7e370ae301 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -21,10 +21,6 @@ #include #include -#ifdef CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS -#include -#endif - #ifdef __cplusplus extern "C" { #endif @@ -5874,8 +5870,6 @@ __syscall int k_float_disable(struct k_thread *thread); */ __syscall int k_float_enable(struct k_thread *thread, unsigned int options); -#ifdef CONFIG_THREAD_RUNTIME_STATS - /** * @brief Get the runtime statistics of a thread * @@ -5894,8 +5888,6 @@ int k_thread_runtime_stats_get(k_tid_t thread, */ int k_thread_runtime_stats_all_get(k_thread_runtime_stats_t *stats); -#endif - #ifdef __cplusplus } #endif diff --git a/include/kernel/thread.h b/include/kernel/thread.h index ae0930b70e0..b25bdbb8e12 100644 --- a/include/kernel/thread.h +++ b/include/kernel/thread.h @@ -169,29 +169,11 @@ struct _thread_userspace_local_data { }; #endif -#ifdef CONFIG_THREAD_RUNTIME_STATS -struct k_thread_runtime_stats { - /* Thread execution cycles */ -#ifdef CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS - timing_t execution_cycles; -#else +typedef struct k_thread_runtime_stats { +#ifdef CONFIG_SCHED_THREAD_USAGE uint64_t execution_cycles; #endif -}; - -typedef struct k_thread_runtime_stats k_thread_runtime_stats_t; - -struct _thread_runtime_stats { - /* Timestamp when last switched in */ -#ifdef CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS - timing_t last_switched_in; -#else - uint32_t last_switched_in; -#endif - - k_thread_runtime_stats_t stats; -}; -#endif +} k_thread_runtime_stats_t; struct z_poller { bool is_polling; @@ -289,11 +271,6 @@ struct k_thread { uintptr_t tls; #endif /* CONFIG_THREAD_LOCAL_STORAGE */ -#ifdef CONFIG_THREAD_RUNTIME_STATS - /** Runtime statistics */ - struct _thread_runtime_stats rt_stats; -#endif - #ifdef CONFIG_DEMAND_PAGING_THREAD_STATS /** Paging statistics */ struct k_mem_paging_stats_t paging_stats; diff --git a/kernel/Kconfig b/kernel/Kconfig index 84cd5cb6779..d10da55974b 100644 --- a/kernel/Kconfig +++ b/kernel/Kconfig @@ -377,26 +377,14 @@ config THREAD_MAX_NAME_LEN config INSTRUMENT_THREAD_SWITCHING bool -config SCHED_THREAD_USAGE - bool "Collect thread runtime usage" - select INSTRUMENT_THREAD_SWITCHING if !USE_SWITCH - help - Alternate implementation of thread runtime cycle usage - -config SCHED_THREAD_USAGE_ALL - bool "Collect total system runtime usage" - default y if SCHED_THREAD_USAGE - help - Maintain a sum of all non-idle thread cycle usage. - menuconfig THREAD_RUNTIME_STATS bool "Thread runtime statistics" - select INSTRUMENT_THREAD_SWITCHING help Gather thread runtime statistics. For example: - Thread total execution cycles + - System total execution cycles if THREAD_RUNTIME_STATS @@ -409,6 +397,20 @@ config THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS Note that timing functions may use a different timer than the default timer for OS timekeeping. +config SCHED_THREAD_USAGE + bool "Collect thread runtime usage" + default y + select INSTRUMENT_THREAD_SWITCHING if !USE_SWITCH + help + Collect thread runtime info at context switch time + +config SCHED_THREAD_USAGE_ALL + bool "Collect total system runtime usage" + default y if SCHED_THREAD_USAGE + depends on SCHED_THREAD_USAGE + help + Maintain a sum of all non-idle thread cycle usage. + endif # THREAD_RUNTIME_STATS endmenu diff --git a/kernel/include/ksched.h b/kernel/include/ksched.h index f29e6c41669..9127113555c 100644 --- a/kernel/include/ksched.h +++ b/kernel/include/ksched.h @@ -385,6 +385,8 @@ void z_sched_usage_stop(void); void z_sched_usage_start(struct k_thread *thread); +uint64_t z_sched_thread_usage(struct k_thread *thread); + static inline void z_sched_usage_switch(struct k_thread *thread) { ARG_UNUSED(thread); diff --git a/kernel/thread.c b/kernel/thread.c index fa651b9a0f7..f9239495c7e 100644 --- a/kernel/thread.c +++ b/kernel/thread.c @@ -31,10 +31,6 @@ #include LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL); -#ifdef CONFIG_THREAD_RUNTIME_STATS -k_thread_runtime_stats_t threads_runtime_stats; -#endif - #ifdef CONFIG_THREAD_MONITOR /* This lock protects the linked list of active threads; i.e. the * initial _kernel.threads pointer and the linked list made up of @@ -607,10 +603,6 @@ char *z_setup_new_thread(struct k_thread *new_thread, SYS_PORT_TRACING_OBJ_FUNC(k_thread, create, new_thread); -#ifdef CONFIG_THREAD_RUNTIME_STATS - memset(&new_thread->rt_stats, 0, sizeof(new_thread->rt_stats)); -#endif - return stack_ptr; } @@ -1013,18 +1005,6 @@ void z_thread_mark_switched_in(void) #ifdef CONFIG_TRACING SYS_PORT_TRACING_FUNC(k_thread, switched_in); #endif - -#ifdef CONFIG_THREAD_RUNTIME_STATS - struct k_thread *thread; - - thread = k_current_get(); -#ifdef CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS - thread->rt_stats.last_switched_in = timing_counter_get(); -#else - thread->rt_stats.last_switched_in = k_cycle_get_32(); -#endif /* CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS */ - -#endif /* CONFIG_THREAD_RUNTIME_STATS */ } void z_thread_mark_switched_out(void) @@ -1033,48 +1013,12 @@ void z_thread_mark_switched_out(void) z_sched_usage_stop(); #endif -#ifdef CONFIG_THREAD_RUNTIME_STATS -#ifdef CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS - timing_t now; -#else - uint32_t now; -#endif /* CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS */ - - uint64_t diff; - struct k_thread *thread; - - thread = k_current_get(); - - if (unlikely(thread->rt_stats.last_switched_in == 0)) { - /* Has not run before */ - return; - } - - if (unlikely(thread->base.thread_state == _THREAD_DUMMY)) { - /* dummy thread has no stat struct */ - return; - } - -#ifdef CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS - now = timing_counter_get(); - diff = timing_cycles_get(&thread->rt_stats.last_switched_in, &now); -#else - now = k_cycle_get_32(); - diff = (uint64_t)(now - thread->rt_stats.last_switched_in); - thread->rt_stats.last_switched_in = 0; -#endif /* CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS */ - - thread->rt_stats.stats.execution_cycles += diff; - - threads_runtime_stats.execution_cycles += diff; -#endif /* CONFIG_THREAD_RUNTIME_STATS */ - #ifdef CONFIG_TRACING SYS_PORT_TRACING_FUNC(k_thread, switched_out); #endif } +#endif /* CONFIG_INSTRUMENT_THREAD_SWITCHING */ -#ifdef CONFIG_THREAD_RUNTIME_STATS int k_thread_runtime_stats_get(k_tid_t thread, k_thread_runtime_stats_t *stats) { @@ -1082,8 +1026,11 @@ int k_thread_runtime_stats_get(k_tid_t thread, return -EINVAL; } - (void)memcpy(stats, &thread->rt_stats.stats, - sizeof(thread->rt_stats.stats)); + *stats = (k_thread_runtime_stats_t) {}; + +#ifdef CONFIG_SCHED_THREAD_USAGE + stats->execution_cycles = z_sched_thread_usage(thread); +#endif return 0; } @@ -1094,11 +1041,11 @@ int k_thread_runtime_stats_all_get(k_thread_runtime_stats_t *stats) return -EINVAL; } - (void)memcpy(stats, &threads_runtime_stats, - sizeof(threads_runtime_stats)); + *stats = (k_thread_runtime_stats_t) {}; + +#ifdef CONFIG_SCHED_THREAD_USAGE_ALL + stats->execution_cycles = _kernel.all_thread_usage; +#endif return 0; } -#endif /* CONFIG_THREAD_RUNTIME_STATS */ - -#endif /* CONFIG_INSTRUMENT_THREAD_SWITCHING */