From bc4c0369420be58a8b0d5378f3238a14df7ff57e Mon Sep 17 00:00:00 2001 From: Rbb666 Date: Thu, 28 Aug 2025 19:10:16 +0800 Subject: [Add][kservice]Add thread usage support. --- src/Kconfig | 8 +++++--- src/kservice.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/Kconfig b/src/Kconfig index 099f95e9cf..4ac6cdd8aa 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -192,10 +192,12 @@ endif config RT_USING_CPU_USAGE_TRACER select RT_USING_HOOK bool "Enable cpu usage tracing" - help - Enable cpu usage tracer for application like top. - default y if RT_USING_SMART default n + help + Enable thread CPU usage statistics and monitoring. + This feature tracks CPU usage for each thread and provides + percentage information through the list thread command. + It will automatically integrate with the scheduler to track thread execution time. menu "kservice options" config RT_USING_TINY_FFS diff --git a/src/kservice.c b/src/kservice.c index 880e8f377d..259eac9a70 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -535,6 +535,53 @@ rt_err_t rt_backtrace_thread(rt_thread_t thread) return rc; } +#ifdef RT_USING_CPU_USAGE_TRACER +/** + * @brief Get thread usage percentage relative to total system CPU time + * + * This function calculates the CPU usage percentage of a specific thread + * relative to the total CPU time consumed by all threads in the system. + * + * @param thread Pointer to the thread object. Must not be NULL. + * + * @return The CPU usage percentage as an integer value (0-100). + * Returns 0 if total system time is 0 or if CPU usage tracing is not enabled. + * + * @note This function requires RT_USING_CPU_USAGE_TRACER to be enabled. + * @note The percentage is calculated as: (thread_time * 100) / total_system_time + * @note Due to integer arithmetic, the result is truncated and may not sum + * to exactly 100% across all threads due to rounding. + * @note If thread is NULL, an assertion will be triggered in debug builds. + */ +rt_uint8_t rt_thread_get_usage(rt_thread_t thread) +{ + rt_ubase_t thread_time; + rt_ubase_t total_time = 0U; + int i; + rt_cpu_t pcpu; + + RT_ASSERT(thread != RT_NULL); + + thread_time = thread->user_time + thread->system_time; + + /* Calculate total system time by summing all CPUs' time */ + for (i = 0; i < RT_CPUS_NR; i++) + { + pcpu = rt_cpu_index(i); + total_time += pcpu->cpu_stat.user + pcpu->cpu_stat.system + pcpu->cpu_stat.idle; + } + + if (total_time > 0U) + { + /* Calculate thread usage percentage: (thread_time * 100) / total_time */ + rt_ubase_t usage = (thread_time * 100U) / total_time; + return (rt_uint8_t)(usage > 100U ? 100U : usage); + } + + return 0U; +} +#endif /* RT_USING_CPU_USAGE_TRACER */ + #if defined(RT_USING_LIBC) && defined(RT_USING_FINSH) #include /* for string service */ -- cgit v1.3.1