Skip to content

Commit e830c51

Browse files
committed
优化线程列表输出,增加CPU使用率跟踪功能,调整输出格式
1 parent 603ccde commit e830c51

File tree

4 files changed

+57
-23
lines changed

4 files changed

+57
-23
lines changed

components/finsh/cmd.c

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ MSH_CMD_EXPORT(version, show RT-Thread version information);
6262

6363
rt_inline void object_split(int len)
6464
{
65-
while (len--) rt_kprintf("-");
65+
while (len--) rt_kputs("-");
6666
}
6767

6868
typedef struct
@@ -165,7 +165,9 @@ 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+
#ifdef RT_USING_CPU_USAGE_TRACER
168169
const size_t usage_strlen = sizeof(void *) + 1;
170+
#endif
169171
int maxlen;
170172

171173
list_find_init(&find_arg, RT_Object_Class_Thread, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
@@ -174,23 +176,41 @@ long list_thread(void)
174176
maxlen = RT_NAME_MAX;
175177

176178
#ifdef RT_USING_SMP
177-
rt_kprintf("%-*.*s cpu bind pri status sp stack size max used left tick error tcb addr usage\n", maxlen, maxlen, item_title);
179+
rt_kprintf("%-*.*s cpu bind pri status sp stack size max used left tick error tcb addr ", maxlen, maxlen, item_title);
180+
#ifdef RT_USING_CPU_USAGE_TRACER
181+
rt_kputs("usage count last time");
182+
#endif
183+
rt_kputs("\n");
178184
object_split(maxlen);
179-
rt_kprintf(" --- ---- --- ------- ---------- ---------- ------ ---------- -------");
180-
rt_kprintf(" ");
185+
rt_kputs(" --- ---- --- ------- ---------- ---------- ------ ---------- ------- ");
181186
object_split(tcb_strlen);
182-
rt_kprintf(" ");
187+
rt_kputs(" ");
188+
#ifdef RT_USING_CPU_USAGE_TRACER
183189
object_split(usage_strlen);
184-
rt_kprintf("\n");
190+
rt_kputs(" ");
191+
object_split(8);
192+
rt_kputs(" ");
193+
object_split(10);
194+
#endif
195+
rt_kputs("\n");
185196
#else
186-
rt_kprintf("%-*.*s pri status sp stack size max used left tick error tcb addr usage\n", maxlen, maxlen, item_title);
197+
rt_kprintf("%-*.*s pri status sp stack size max used left tick error tcb addr ", maxlen, maxlen, item_title);
198+
#ifdef RT_USING_CPU_USAGE_TRACER
199+
rt_kputs("usage count last time");
200+
#endif
201+
rt_kputs("\n");
187202
object_split(maxlen);
188-
rt_kprintf(" --- ------- ---------- ---------- ------ ---------- -------");
189-
rt_kprintf(" ");
203+
rt_kputs(" --- ------- ---------- ---------- ------ ---------- ------- ");
190204
object_split(tcb_strlen);
191-
rt_kprintf(" ");
205+
rt_kputs(" ");
206+
#ifdef RT_USING_CPU_USAGE_TRACER
192207
object_split(usage_strlen);
193-
rt_kprintf("\n");
208+
rt_kputs(" ");
209+
object_split(8);
210+
rt_kputs(" ");
211+
object_split(10);
212+
#endif
213+
rt_kputs("\n");
194214
#endif /*RT_USING_SMP*/
195215

196216
do
@@ -256,10 +276,9 @@ long list_thread(void)
256276
rt_strerror(thread->error),
257277
thread);
258278
#ifdef RT_USING_CPU_USAGE_TRACER
259-
rt_kprintf(" %3d%%\n", rt_thread_get_usage(thread));
260-
#else
261-
rt_kprintf(" N/A\n");
279+
rt_kprintf(" %3d%%%8d%10d", rt_thread_get_usage(thread), thread->ctx_count, thread->ctx_last_time);
262280
#endif
281+
rt_kprintf(" \n");
263282
#else
264283
ptr = (rt_uint8_t *)thread->stack_addr;
265284
while (*ptr == '#') ptr ++;
@@ -272,10 +291,9 @@ long list_thread(void)
272291
rt_strerror(thread->error),
273292
thread);
274293
#ifdef RT_USING_CPU_USAGE_TRACER
275-
rt_kprintf(" %3d%%\n", rt_thread_get_usage(thread));
276-
#else
277-
rt_kprintf(" N/A\n");
294+
rt_kprintf(" %3d%%%8d%10d", rt_thread_get_usage(thread), thread->ctx_count, thread->ctx_last_time);
278295
#endif
296+
rt_kputs("\n");
279297
#endif
280298
}
281299
}

include/rtdef.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,8 @@ struct rt_thread
940940
#ifdef RT_USING_CPU_USAGE_TRACER
941941
rt_ubase_t user_time; /**< Ticks on user */
942942
rt_ubase_t system_time; /**< Ticks on system */
943+
rt_ubase_t ctx_last_time; /**< Last context switch time */
944+
rt_ubase_t ctx_count; /**< Context switch count */
943945
#endif /* RT_USING_CPU_USAGE_TRACER */
944946

945947
#ifdef RT_USING_MEM_PROTECTION

src/scheduler_mp.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,10 @@ rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level)
879879
cpu_id, RT_SCHED_PRIV(to_thread).current_priority,
880880
RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
881881
RT_NAME_MAX, current_thread->parent.name, current_thread->sp);
882-
882+
#ifdef RT_USING_CPU_USAGE_TRACER
883+
to_thread->ctx_count++;
884+
to_thread->ctx_last_time = rt_tick_get();
885+
#endif
883886
rt_hw_context_switch((rt_ubase_t)&current_thread->sp,
884887
(rt_ubase_t)&to_thread->sp, to_thread);
885888
}
@@ -980,7 +983,10 @@ void rt_schedule(void)
980983
cpu_id, RT_SCHED_PRIV(to_thread).current_priority,
981984
RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
982985
RT_NAME_MAX, current_thread->parent.name, current_thread->sp);
983-
986+
#ifdef RT_USING_CPU_USAGE_TRACER
987+
to_thread->ctx_count++;
988+
to_thread->ctx_last_time = rt_tick_get();
989+
#endif
984990
rt_hw_context_switch((rt_ubase_t)&current_thread->sp,
985991
(rt_ubase_t)&to_thread->sp, to_thread);
986992
}
@@ -1065,7 +1071,10 @@ void rt_scheduler_do_irq_switch(void *context)
10651071
cpu_id, RT_SCHED_PRIV(to_thread).current_priority,
10661072
RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
10671073
RT_NAME_MAX, current_thread->parent.name, current_thread->sp);
1068-
1074+
#ifdef RT_USING_CPU_USAGE_TRACER
1075+
to_thread->ctx_count++;
1076+
to_thread->ctx_last_time = rt_tick_get();
1077+
#endif
10691078
rt_hw_context_switch_interrupt(context, (rt_ubase_t)&current_thread->sp,
10701079
(rt_ubase_t)&to_thread->sp, to_thread);
10711080
}

src/scheduler_up.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,12 @@ void rt_schedule(void)
355355
extern void rt_thread_handle_sig(rt_bool_t clean_state);
356356

357357
RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread));
358-
358+
#ifdef RT_USING_CPU_USAGE_TRACER
359+
to_thread->ctx_count++;
360+
to_thread->ctx_last_time = rt_tick_get();
361+
#endif
359362
rt_hw_context_switch((rt_uintptr_t)&from_thread->sp,
360363
(rt_uintptr_t)&to_thread->sp);
361-
362364
/* enable interrupt */
363365
rt_hw_interrupt_enable(level);
364366

@@ -386,7 +388,10 @@ void rt_schedule(void)
386388
else
387389
{
388390
LOG_D("switch in interrupt");
389-
391+
#ifdef RT_USING_CPU_USAGE_TRACER
392+
to_thread->ctx_count++;
393+
to_thread->ctx_last_time = rt_tick_get();
394+
#endif
390395
rt_hw_context_switch_interrupt((rt_uintptr_t)&from_thread->sp,
391396
(rt_uintptr_t)&to_thread->sp, from_thread, to_thread);
392397
}

0 commit comments

Comments
 (0)