Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions components/finsh/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ long list_thread(void)
rt_list_t *next = (rt_list_t *)RT_NULL;
const char *item_title = "thread";
const size_t tcb_strlen = sizeof(void *) * 2 + 2;
#ifdef RT_USING_CPU_USAGE_TRACER
const size_t usage_strlen = sizeof(void *) + 1;
#endif
int maxlen;

list_find_init(&find_arg, RT_Object_Class_Thread, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
Expand All @@ -230,22 +232,40 @@ long list_thread(void)
maxlen = RT_NAME_MAX;

#ifdef RT_USING_SMP
rt_kprintf("%-*.*s cpu bind pri status sp stack size max used left tick error tcb addr usage\n", maxlen, maxlen, item_title);
rt_kprintf("%-*.*s cpu bind pri status sp stack size max used left tick error tcb addr ", maxlen, maxlen, item_title);
#ifdef RT_USING_CPU_USAGE_TRACER
rt_kprintf("usage count last time");
#endif
rt_kprintf("\n");
object_split(maxlen);
rt_kprintf(" --- ---- --- ------- ---------- ---------- ------ ---------- -------");
rt_kprintf(" ");
rt_kprintf(" --- ---- --- ------- ---------- ---------- ------ ---------- ------- ");
object_split(tcb_strlen);
rt_kprintf(" ");
#ifdef RT_USING_CPU_USAGE_TRACER
object_split(usage_strlen);
rt_kprintf(" ");
object_split(8);
rt_kprintf(" ");
object_split(10);
#endif
rt_kprintf("\n");
#else
rt_kprintf("%-*.*s pri status sp stack size max used left tick error tcb addr usage\n", maxlen, maxlen, item_title);
rt_kprintf("%-*.*s pri status sp stack size max used left tick error tcb addr ", maxlen, maxlen, item_title);
#ifdef RT_USING_CPU_USAGE_TRACER
rt_kputs("usage count last time");
#endif
rt_kprintf("\n");
object_split(maxlen);
rt_kprintf(" --- ------- ---------- ---------- ------ ---------- -------");
rt_kprintf(" ");
rt_kprintf(" --- ------- ---------- ---------- ------ ---------- ------- ");
object_split(tcb_strlen);
rt_kprintf(" ");
#ifdef RT_USING_CPU_USAGE_TRACER
object_split(usage_strlen);
rt_kprintf(" ");
object_split(8);
rt_kprintf(" ");
object_split(10);
#endif
rt_kprintf("\n");
#endif /*RT_USING_SMP*/

Expand Down Expand Up @@ -310,6 +330,11 @@ long list_thread(void)
thread->remaining_tick,
rt_strerror(thread->error),
thread);
#ifdef RT_USING_CPU_USAGE_TRACER
uint16_t usage = rt_thread_get_usage(thread);
rt_kprintf(" %2hu.%2hu%%%8d%10d", usage / 100, usage % 100, thread->ctx_count, thread->ctx_last_time);
#endif
rt_kprintf(" \n");
#else
ptr = (rt_uint8_t *)thread->stack_addr;
while (*ptr == '#') ptr ++;
Expand All @@ -323,9 +348,10 @@ long list_thread(void)
thread);
#endif
#ifdef RT_USING_CPU_USAGE_TRACER
rt_kprintf(" %3d%%\n", rt_thread_get_usage(thread));
uint16_t usage = rt_thread_get_usage(thread);
rt_kprintf(" %2hu.%2hu%%%8d%10d", usage / 100, usage % 100, thread->ctx_count, thread->ctx_last_time);
#else
rt_kprintf(" N/A\n");
rt_kprintf("\n");
#endif
}
}
Expand Down
4 changes: 3 additions & 1 deletion include/rtdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,9 @@ struct rt_thread
rt_ubase_t user_time; /**< Ticks on user */
rt_ubase_t system_time; /**< Ticks on system */
rt_ubase_t total_time_prev; /**< Previous total ticks snapshot */
rt_uint8_t cpu_usage; /**< Recent CPU usage in percent */
rt_ubase_t cpu_usage; /**< Recent CPU usage in percent */
rt_ubase_t ctx_last_time; /**< Last context switch time */
rt_ubase_t ctx_count; /**< Context switch count */
#endif /* RT_USING_CPU_USAGE_TRACER */

#ifdef RT_USING_MEM_PROTECTION
Expand Down
2 changes: 1 addition & 1 deletion include/rtthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void rt_thread_wakeup_set(struct rt_thread *thread, rt_wakeup_func_t func, void*
#endif /* RT_USING_SMART */
rt_err_t rt_thread_get_name(rt_thread_t thread, char *name, rt_uint8_t name_size);
#ifdef RT_USING_CPU_USAGE_TRACER
rt_uint8_t rt_thread_get_usage(rt_thread_t thread);
rt_uint16_t rt_thread_get_usage(rt_thread_t thread);
#endif /* RT_USING_CPU_USAGE_TRACER */
#ifdef RT_USING_SIGNALS
void rt_thread_alloc_sig(rt_thread_t tid);
Expand Down
8 changes: 4 additions & 4 deletions src/kservice.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,8 @@ static void _cpu_usage_refresh_threads(rt_uint64_t total_delta)

if (total_delta > 0U)
{
rt_uint64_t usage = (thread_delta * 100U) / total_delta;
t->cpu_usage = (rt_uint8_t)(usage > 100U ? 100U : usage);
rt_uint64_t usage = (thread_delta * 10000U) / total_delta;
t->cpu_usage = (rt_uint16_t)(usage > 10000U ? 10000U : usage);
}
else
{
Expand Down Expand Up @@ -731,9 +731,9 @@ static void _cpu_usage_update(void)
* @note Sampling interval can be tuned with RT_CPU_USAGE_CALC_INTERVAL_MS.
* @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_uint16_t rt_thread_get_usage(rt_thread_t thread)
{
rt_uint8_t usage;
rt_uint16_t usage;

RT_ASSERT(thread != RT_NULL);

Expand Down
15 changes: 12 additions & 3 deletions src/scheduler_mp.c
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,10 @@ rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level)
cpu_id, RT_SCHED_PRIV(to_thread).current_priority,
RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
RT_NAME_MAX, current_thread->parent.name, current_thread->sp);

#ifdef RT_USING_CPU_USAGE_TRACER
to_thread->ctx_count++;
to_thread->ctx_last_time = rt_tick_get();
#endif
rt_hw_context_switch((rt_ubase_t)&current_thread->sp,
(rt_ubase_t)&to_thread->sp, to_thread);
}
Expand Down Expand Up @@ -980,7 +983,10 @@ void rt_schedule(void)
cpu_id, RT_SCHED_PRIV(to_thread).current_priority,
RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
RT_NAME_MAX, current_thread->parent.name, current_thread->sp);

#ifdef RT_USING_CPU_USAGE_TRACER
to_thread->ctx_count++;
to_thread->ctx_last_time = rt_tick_get();
#endif
rt_hw_context_switch((rt_ubase_t)&current_thread->sp,
(rt_ubase_t)&to_thread->sp, to_thread);
}
Expand Down Expand Up @@ -1065,7 +1071,10 @@ void rt_scheduler_do_irq_switch(void *context)
cpu_id, RT_SCHED_PRIV(to_thread).current_priority,
RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
RT_NAME_MAX, current_thread->parent.name, current_thread->sp);

#ifdef RT_USING_CPU_USAGE_TRACER
to_thread->ctx_count++;
to_thread->ctx_last_time = rt_tick_get();
#endif
rt_hw_context_switch_interrupt(context, (rt_ubase_t)&current_thread->sp,
(rt_ubase_t)&to_thread->sp, to_thread);
}
Expand Down
11 changes: 8 additions & 3 deletions src/scheduler_up.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,12 @@ void rt_schedule(void)
extern void rt_thread_handle_sig(rt_bool_t clean_state);

RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread));

#ifdef RT_USING_CPU_USAGE_TRACER
to_thread->ctx_count++;
to_thread->ctx_last_time = rt_tick_get();
#endif
rt_hw_context_switch((rt_uintptr_t)&from_thread->sp,
(rt_uintptr_t)&to_thread->sp);

/* enable interrupt */
rt_hw_interrupt_enable(level);

Expand Down Expand Up @@ -390,7 +392,10 @@ void rt_schedule(void)
else
{
LOG_D("switch in interrupt");

#ifdef RT_USING_CPU_USAGE_TRACER
to_thread->ctx_count++;
to_thread->ctx_last_time = rt_tick_get();
#endif
rt_hw_context_switch_interrupt((rt_uintptr_t)&from_thread->sp,
(rt_uintptr_t)&to_thread->sp, from_thread, to_thread);
}
Expand Down
Loading