Skip to content

Commit bc4c036

Browse files
committed
[Add][kservice]Add thread usage support.
1 parent 1e34830 commit bc4c036

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

components/finsh/cmd.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ long list_thread(void)
165165
rt_list_t *next = (rt_list_t *)RT_NULL;
166166
const char *item_title = "thread";
167167
const size_t tcb_strlen = sizeof(void *) * 2 + 2;
168+
const size_t usage_strlen = sizeof(void *) + 1;
168169
int maxlen;
169170

170171
list_find_init(&find_arg, RT_Object_Class_Thread, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
@@ -173,18 +174,22 @@ long list_thread(void)
173174
maxlen = RT_NAME_MAX;
174175

175176
#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);
177+
rt_kprintf("%-*.*s cpu bind pri status sp stack size max used left tick error tcb addr usage\n", maxlen, maxlen, item_title);
177178
object_split(maxlen);
178179
rt_kprintf(" --- ---- --- ------- ---------- ---------- ------ ---------- -------");
179180
rt_kprintf(" ");
180181
object_split(tcb_strlen);
182+
rt_kprintf(" ");
183+
object_split(usage_strlen);
181184
rt_kprintf("\n");
182185
#else
183-
rt_kprintf("%-*.*s pri status sp stack size max used left tick error tcb addr\n", maxlen, maxlen, item_title);
186+
rt_kprintf("%-*.*s pri status sp stack size max used left tick error tcb addr usage\n", maxlen, maxlen, item_title);
184187
object_split(maxlen);
185188
rt_kprintf(" --- ------- ---------- ---------- ------ ---------- -------");
186189
rt_kprintf(" ");
187190
object_split(tcb_strlen);
191+
rt_kprintf(" ");
192+
object_split(usage_strlen);
188193
rt_kprintf("\n");
189194
#endif /*RT_USING_SMP*/
190195

@@ -243,24 +248,34 @@ long list_thread(void)
243248
ptr = (rt_uint8_t *)thread->stack_addr + thread->stack_size - 1;
244249
while (*ptr == '#')ptr --;
245250

246-
rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %s %p\n",
251+
rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %s %p",
247252
((rt_ubase_t)thread->sp - (rt_ubase_t)thread->stack_addr),
248253
thread->stack_size,
249254
((rt_ubase_t)ptr - (rt_ubase_t)thread->stack_addr) * 100 / thread->stack_size,
250255
thread->remaining_tick,
251256
rt_strerror(thread->error),
252257
thread);
258+
#ifdef RT_USING_CPU_USAGE_TRACER
259+
rt_kprintf(" %3d%%\n", rt_thread_get_usage(thread));
260+
#else
261+
rt_kprintf(" N/A\n");
262+
#endif
253263
#else
254264
ptr = (rt_uint8_t *)thread->stack_addr;
255265
while (*ptr == '#') ptr ++;
256-
rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %s %p\n",
266+
rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %s %p",
257267
thread->stack_size + ((rt_ubase_t)thread->stack_addr - (rt_ubase_t)thread->sp),
258268
thread->stack_size,
259269
(thread->stack_size - ((rt_ubase_t) ptr - (rt_ubase_t) thread->stack_addr)) * 100
260270
/ thread->stack_size,
261271
RT_SCHED_PRIV(thread).remaining_tick,
262272
rt_strerror(thread->error),
263273
thread);
274+
#ifdef RT_USING_CPU_USAGE_TRACER
275+
rt_kprintf(" %3d%%\n", rt_thread_get_usage(thread));
276+
#else
277+
rt_kprintf(" N/A\n");
278+
#endif
264279
#endif
265280
}
266281
}

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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,12 @@ endif
192192
config RT_USING_CPU_USAGE_TRACER
193193
select RT_USING_HOOK
194194
bool "Enable cpu usage tracing"
195-
help
196-
Enable cpu usage tracer for application like top.
197-
default y if RT_USING_SMART
198195
default n
196+
help
197+
Enable thread CPU usage statistics and monitoring.
198+
This feature tracks CPU usage for each thread and provides
199+
percentage information through the list thread command.
200+
It will automatically integrate with the scheduler to track thread execution time.
199201

200202
menu "kservice options"
201203
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)