Skip to content

Commit a0d314e

Browse files
committed
[Add][kservice]Add thread usage support.
1 parent 6a9d218 commit a0d314e

File tree

4 files changed

+71
-11
lines changed

4 files changed

+71
-11
lines changed

components/finsh/cmd.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,16 @@ long list_thread(void)
173173
maxlen = RT_NAME_MAX;
174174

175175
#ifdef RT_USING_SMP
176-
rt_kprintf("%-*.*s cpu bind pri status sp stack size max used left tick error tcb addr\n", maxlen, maxlen, item_title);
176+
rt_kprintf("%-*.*s cpu bind pri status sp stack size max used left tick error tcb addr usage\n", maxlen, maxlen, item_title);
177177
object_split(maxlen);
178-
rt_kprintf(" --- ---- --- ------- ---------- ---------- ------ ---------- -------");
178+
rt_kprintf(" --- ---- --- ------- ---------- ---------- ------ ---------- ------- ------");
179179
rt_kprintf(" ");
180180
object_split(tcb_strlen);
181181
rt_kprintf("\n");
182182
#else
183-
rt_kprintf("%-*.*s pri status sp stack size max used left tick error tcb addr\n", maxlen, maxlen, item_title);
183+
rt_kprintf("%-*.*s pri status sp stack size max used left tick error tcb addr usage\n", maxlen, maxlen, item_title);
184184
object_split(maxlen);
185-
rt_kprintf(" --- ------- ---------- ---------- ------ ---------- -------");
185+
rt_kprintf(" --- ------- ---------- ---------- ------ ---------- ------- ------");
186186
rt_kprintf(" ");
187187
object_split(tcb_strlen);
188188
rt_kprintf("\n");
@@ -243,24 +243,34 @@ long list_thread(void)
243243
ptr = (rt_uint8_t *)thread->stack_addr + thread->stack_size - 1;
244244
while (*ptr == '#')ptr --;
245245

246-
rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %s %p\n",
246+
rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %s %p",
247247
((rt_ubase_t)thread->sp - (rt_ubase_t)thread->stack_addr),
248248
thread->stack_size,
249249
((rt_ubase_t)ptr - (rt_ubase_t)thread->stack_addr) * 100 / thread->stack_size,
250250
thread->remaining_tick,
251251
rt_strerror(thread->error),
252252
thread);
253+
#ifdef RT_USING_CPU_USAGE_TRACER
254+
rt_kprintf(" %3d%%\n", rt_thread_get_usage(thread));
255+
#else
256+
rt_kprintf(" N/A\n");
257+
#endif
253258
#else
254259
ptr = (rt_uint8_t *)thread->stack_addr;
255260
while (*ptr == '#') ptr ++;
256-
rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %s %p\n",
261+
rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %s %p",
257262
thread->stack_size + ((rt_ubase_t)thread->stack_addr - (rt_ubase_t)thread->sp),
258263
thread->stack_size,
259264
(thread->stack_size - ((rt_ubase_t) ptr - (rt_ubase_t) thread->stack_addr)) * 100
260265
/ thread->stack_size,
261266
RT_SCHED_PRIV(thread).remaining_tick,
262267
rt_strerror(thread->error),
263268
thread);
269+
#ifdef RT_USING_CPU_USAGE_TRACER
270+
rt_kprintf(" %3d%%\n", rt_thread_get_usage(thread));
271+
#else
272+
rt_kprintf(" N/A\n");
273+
#endif
264274
#endif
265275
}
266276
}

include/rtthread.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ rt_err_t rt_thread_wakeup(rt_thread_t thread);
181181
void rt_thread_wakeup_set(struct rt_thread *thread, rt_wakeup_func_t func, void* user_data);
182182
#endif /* RT_USING_SMART */
183183
rt_err_t rt_thread_get_name(rt_thread_t thread, char *name, rt_uint8_t name_size);
184+
#ifdef RT_USING_CPU_USAGE_TRACER
185+
rt_uint8_t rt_thread_get_usage(rt_thread_t thread);
186+
#endif /* RT_USING_CPU_USAGE_TRACER */
184187
#ifdef RT_USING_SIGNALS
185188
void rt_thread_alloc_sig(rt_thread_t tid);
186189
void rt_thread_free_sig(rt_thread_t tid);

src/Kconfig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ config RT_USING_SMART
4444
select RT_USING_KTIME
4545
select RT_USING_STDC_ATOMIC
4646
select RT_USING_SYSTEM_WORKQUEUE
47-
select RT_USING_CPU_USAGE_TRACER
4847
select RT_USING_SCHED_THREAD_CTX
4948
depends on ARCH_ARM_CORTEX_M || ARCH_ARM_ARM9 || ARCH_ARM_CORTEX_A || ARCH_ARMV8 || ARCH_RISCV64
5049
depends on !RT_USING_NANO
@@ -190,12 +189,13 @@ if RT_USING_TIMER_SOFT
190189
endif
191190

192191
config RT_USING_CPU_USAGE_TRACER
193-
select RT_USING_HOOK
194192
bool "Enable cpu usage tracing"
193+
default y
195194
help
196-
Enable cpu usage tracer for application like top.
197-
default y if RT_USING_SMART
198-
default n
195+
Enable thread CPU usage statistics and monitoring.
196+
This feature tracks CPU usage for each thread and provides
197+
percentage information through the list thread command.
198+
It will automatically integrate with the scheduler to track thread execution time.
199199

200200
menu "kservice options"
201201
config RT_USING_TINY_FFS

src/kservice.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,53 @@ rt_err_t rt_backtrace_thread(rt_thread_t thread)
535535
return rc;
536536
}
537537

538+
#ifdef RT_USING_CPU_USAGE_TRACER
539+
/**
540+
* @brief Get thread usage percentage relative to total system CPU time
541+
*
542+
* This function calculates the CPU usage percentage of a specific thread
543+
* relative to the total CPU time consumed by all threads in the system.
544+
*
545+
* @param thread Pointer to the thread object. Must not be NULL.
546+
*
547+
* @return The CPU usage percentage as an integer value (0-100).
548+
* Returns 0 if total system time is 0 or if CPU usage tracing is not enabled.
549+
*
550+
* @note This function requires RT_USING_CPU_USAGE_TRACER to be enabled.
551+
* @note The percentage is calculated as: (thread_time * 100) / total_system_time
552+
* @note Due to integer arithmetic, the result is truncated and may not sum
553+
* to exactly 100% across all threads due to rounding.
554+
* @note If thread is NULL, an assertion will be triggered in debug builds.
555+
*/
556+
rt_uint8_t rt_thread_get_usage(rt_thread_t thread)
557+
{
558+
rt_ubase_t thread_time;
559+
rt_ubase_t total_time = 0U;
560+
int i;
561+
rt_cpu_t pcpu;
562+
563+
RT_ASSERT(thread != RT_NULL);
564+
565+
thread_time = thread->user_time + thread->system_time;
566+
567+
/* Calculate total system time by summing all CPUs' time */
568+
for (i = 0; i < RT_CPUS_NR; i++)
569+
{
570+
pcpu = rt_cpu_index(i);
571+
total_time += pcpu->cpu_stat.user + pcpu->cpu_stat.system + pcpu->cpu_stat.idle;
572+
}
573+
574+
if (total_time > 0U)
575+
{
576+
/* Calculate thread usage percentage: (thread_time * 100) / total_time */
577+
rt_ubase_t usage = (thread_time * 100U) / total_time;
578+
return (rt_uint8_t)(usage > 100U ? 100U : usage);
579+
}
580+
581+
return 0U;
582+
}
583+
#endif /* RT_USING_CPU_USAGE_TRACER */
584+
538585
#if defined(RT_USING_LIBC) && defined(RT_USING_FINSH)
539586
#include <stdlib.h> /* for string service */
540587

0 commit comments

Comments
 (0)