Skip to content

Commit 6ca327d

Browse files
authored
[smart] add CPU/thread usage tracing config (#8947)
* [smart] add CPU usage tracing config This patch introduces following features: - Added CPU usage tracing functionality, enabled by default, for applications like 'top' - update time as smart independent Signed-off-by: Shell <smokewood@qq.com> * fixup: add ump idle thread --------- Signed-off-by: Shell <smokewood@qq.com>
1 parent e03342f commit 6ca327d

6 files changed

Lines changed: 76 additions & 52 deletions

File tree

components/lwp/Kconfig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ if RT_USING_LWP
1212

1313
if LWP_DEBUG
1414
config LWP_DEBUG_INIT
15-
select RT_USING_HOOKLIST
16-
bool "Enable debug mode of init process"
17-
default n
15+
select RT_USING_HOOKLIST
16+
bool "Enable debug mode of init process"
17+
default n
1818
endif
1919

2020
config RT_LWP_MAX_NR

components/lwp/lwp.c

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -686,37 +686,3 @@ rt_err_t lwp_backtrace_frame(rt_thread_t uthread, struct rt_hw_backtrace_frame *
686686
}
687687
return rc;
688688
}
689-
690-
void rt_update_process_times(void)
691-
{
692-
struct rt_thread *thread;
693-
#ifdef RT_USING_SMP
694-
struct rt_cpu* pcpu;
695-
696-
pcpu = rt_cpu_self();
697-
#endif
698-
699-
thread = rt_thread_self();
700-
701-
if (!IS_USER_MODE(thread))
702-
{
703-
thread->user_time += 1;
704-
#ifdef RT_USING_SMP
705-
pcpu->cpu_stat.user += 1;
706-
#endif
707-
}
708-
else
709-
{
710-
thread->system_time += 1;
711-
#ifdef RT_USING_SMP
712-
if (thread == pcpu->idle_thread)
713-
{
714-
pcpu->cpu_stat.idle += 1;
715-
}
716-
else
717-
{
718-
pcpu->cpu_stat.system += 1;
719-
}
720-
#endif
721-
}
722-
}

include/rtdef.h

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,18 @@ enum
701701
#define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */
702702
#define RT_THREAD_CTRL_BIND_CPU 0x04 /**< Set thread bind cpu. */
703703

704+
/**
705+
* CPU usage statistics data
706+
*/
707+
struct rt_cpu_usage_stats
708+
{
709+
rt_ubase_t user;
710+
rt_ubase_t system;
711+
rt_ubase_t irq;
712+
rt_ubase_t idle;
713+
};
714+
typedef struct rt_cpu_usage_stats *rt_cpu_usage_stats_t;
715+
704716
#ifdef RT_USING_SMP
705717

706718
#define RT_CPU_DETACHED RT_CPUS_NR /**< The thread not running on cpu. */
@@ -714,15 +726,6 @@ enum
714726
#define RT_STOP_IPI 1
715727
#endif /* RT_STOP_IPI */
716728

717-
struct rt_cpu_usage_stats
718-
{
719-
rt_uint64_t user;
720-
rt_uint64_t system;
721-
rt_uint64_t irq;
722-
rt_uint64_t idle;
723-
};
724-
typedef struct rt_cpu_usage_stats *rt_cpu_usage_stats_t;
725-
726729
#define _SCHEDULER_CONTEXT(fileds) fileds
727730

728731
/**
@@ -762,14 +765,21 @@ struct rt_cpu
762765

763766
#ifdef RT_USING_SMART
764767
struct rt_spinlock spinlock;
768+
#endif /* RT_USING_SMART */
769+
#ifdef RT_USING_CPU_USAGE_TRACER
765770
struct rt_cpu_usage_stats cpu_stat;
766-
#endif
771+
#endif /* RT_USING_CPU_USAGE_TRACER */
767772
};
768773

769774
#else /* !RT_USING_SMP */
770775
struct rt_cpu
771776
{
772777
struct rt_thread *current_thread;
778+
struct rt_thread *idle_thread;
779+
780+
#ifdef RT_USING_CPU_USAGE_TRACER
781+
struct rt_cpu_usage_stats cpu_stat;
782+
#endif /* RT_USING_CPU_USAGE_TRACER */
773783
};
774784

775785
#endif /* RT_USING_SMP */
@@ -947,9 +957,6 @@ struct rt_thread
947957
void *susp_recycler; /**< suspended recycler on this thread */
948958
void *robust_list; /**< pi lock, very carefully, it's a userspace list!*/
949959

950-
rt_uint64_t user_time;
951-
rt_uint64_t system_time;
952-
953960
#ifndef ARCH_MM_MMU
954961
lwp_sighandler_t signal_handler[32];
955962
#else
@@ -963,6 +970,11 @@ struct rt_thread
963970
#endif /* ARCH_MM_MMU */
964971
#endif /* RT_USING_SMART */
965972

973+
#ifdef RT_USING_CPU_USAGE_TRACER
974+
rt_ubase_t user_time; /**< Ticks on user */
975+
rt_ubase_t system_time; /**< Ticks on system */
976+
#endif /* RT_USING_CPU_USAGE_TRACER */
977+
966978
#ifdef RT_USING_MEM_PROTECTION
967979
void *mem_regions;
968980
#ifdef RT_USING_HW_STACK_GUARD
@@ -976,7 +988,9 @@ struct rt_thread
976988
typedef struct rt_thread *rt_thread_t;
977989

978990
#ifdef RT_USING_SMART
979-
#define IS_USER_MODE(t) ((t)->user_ctx.ctx == RT_NULL)
991+
#define LWP_IS_USER_MODE(t) ((t)->user_ctx.ctx == RT_NULL)
992+
#else
993+
#define LWP_IS_USER_MODE(t) (0)
980994
#endif /* RT_USING_SMART */
981995

982996
/**@}*/

src/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ if RT_USING_TIMER_SOFT
183183
default 512
184184
endif
185185

186+
config RT_USING_CPU_USAGE_TRACER
187+
select RT_USING_HOOK
188+
bool "Enable cpu usage tracing"
189+
help
190+
Enable cpu usage tracer for application like top.
191+
default y if RT_USING_SMART
192+
default n
193+
186194
menu "kservice optimization"
187195
config RT_USING_TINY_FFS
188196
bool "Enable kservice to use tiny finding first bit set method"

src/clock.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,36 @@ void rt_tick_set(rt_tick_t tick)
7979
rt_atomic_store(&(rt_tick), tick);
8080
}
8181

82+
#ifdef RT_USING_CPU_USAGE_TRACER
83+
static void _update_process_times(void)
84+
{
85+
struct rt_thread *thread = rt_thread_self();
86+
struct rt_cpu *pcpu = rt_cpu_self();
87+
88+
if (!LWP_IS_USER_MODE(thread))
89+
{
90+
thread->user_time += 1;
91+
pcpu->cpu_stat.user += 1;
92+
}
93+
else
94+
{
95+
thread->system_time += 1;
96+
if (thread == pcpu->idle_thread)
97+
{
98+
pcpu->cpu_stat.idle += 1;
99+
}
100+
else
101+
{
102+
pcpu->cpu_stat.system += 1;
103+
}
104+
}
105+
}
106+
107+
#else
108+
109+
#define _update_process_times()
110+
#endif /* RT_USING_CPU_USAGE_TRACER */
111+
82112
/**
83113
* @brief This function will notify kernel there is one tick passed.
84114
* Normally, this function is invoked by clock ISR.
@@ -88,6 +118,10 @@ void rt_tick_increase(void)
88118
RT_ASSERT(rt_interrupt_get_nest() > 0);
89119

90120
RT_OBJECT_HOOK_CALL(rt_tick_hook, ());
121+
122+
/* tracing cpu usage */
123+
_update_process_times();
124+
91125
/* increase the global tick */
92126
#ifdef RT_USING_SMP
93127
/* get percpu and increase the tick */

src/idle.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,11 @@ void rt_thread_idle_init(void)
346346
32);
347347
#ifdef RT_USING_SMP
348348
rt_thread_control(&idle_thread[i], RT_THREAD_CTRL_BIND_CPU, (void*)i);
349+
#endif /* RT_USING_SMP */
349350

351+
/* update */
350352
rt_cpu_index(i)->idle_thread = &idle_thread[i];
351-
#endif /* RT_USING_SMP */
353+
352354
/* startup */
353355
rt_thread_startup(&idle_thread[i]);
354356
}

0 commit comments

Comments
 (0)