Skip to content

Commit 0015af0

Browse files
committed
调整代码,以支持cpu usage
1 parent e933c1f commit 0015af0

File tree

7 files changed

+39
-1
lines changed

7 files changed

+39
-1
lines changed

include/rtdef.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,10 @@ struct rt_thread
663663
rt_ubase_t init_tick; /**< thread's initialized tick */
664664
rt_ubase_t remaining_tick; /**< remaining tick */
665665

666+
#ifdef RT_USING_CPU_USAGE
667+
rt_uint64_t duration_tick; /**< cpu usage tick */
668+
#endif
669+
666670
struct rt_timer thread_timer; /**< built-in thread timer */
667671

668672
void (*cleanup)(struct rt_thread *tid); /**< cleanup function when thread exit */

include/rtthread.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ rt_uint16_t rt_critical_level(void);
186186

187187
#ifdef RT_USING_HOOK
188188
void rt_scheduler_sethook(void (*hook)(rt_thread_t from, rt_thread_t to));
189+
void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid));
189190
#endif
190191

191192
#ifdef RT_USING_SMP

libcpu/arm/cortex-a/interrupt.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ struct rt_irq_desc isr_table[MAX_HANDLERS];
2727
rt_uint32_t rt_interrupt_from_thread = 0;
2828
rt_uint32_t rt_interrupt_to_thread = 0;
2929
rt_uint32_t rt_thread_switch_interrupt_flag = 0;
30+
31+
#ifdef RT_USING_HOOK
32+
static void (*rt_interrupt_switch_hook)(void);
33+
34+
void rt_interrupt_switch_sethook(void (*hook)(void))
35+
{
36+
rt_interrupt_switch_hook = hook;
37+
}
38+
#endif
39+
40+
void rt_interrupt_hook(void)
41+
{
42+
RT_OBJECT_HOOK_CALL(rt_interrupt_switch_hook, ());
43+
}
3044
#endif
3145

3246
const unsigned int VECTOR_BASE = 0x00;

libcpu/arm/cortex-a/start_gcc.S

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ rt_hw_context_switch_interrupt_do:
341341
ldr r6, [r6]
342342
ldr sp, [r6] @ get new task's stack pointer
343343

344+
bl rt_interrupt_hook
345+
344346
#ifdef RT_USING_FPU
345347
/* fpu context */
346348
ldmfd sp!, {r6}

src/irq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ void rt_interrupt_leave(void)
8989
rt_interrupt_nest));
9090

9191
level = rt_hw_interrupt_disable();
92-
rt_interrupt_nest --;
9392
RT_OBJECT_HOOK_CALL(rt_interrupt_leave_hook,());
93+
rt_interrupt_nest --;
9494
rt_hw_interrupt_enable(level);
9595
}
9696
RTM_EXPORT(rt_interrupt_leave);

src/scheduler.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ rt_uint8_t rt_current_priority;
4949

5050
#ifdef RT_USING_HOOK
5151
static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
52+
static void (*rt_scheduler_switch_hook)(struct rt_thread *tid);
5253

5354
/**
5455
* @addtogroup Hook
@@ -68,6 +69,12 @@ rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
6869
rt_scheduler_hook = hook;
6970
}
7071

72+
void
73+
rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid))
74+
{
75+
rt_scheduler_switch_hook = hook;
76+
}
77+
7178
/**@}*/
7279
#endif /* RT_USING_HOOK */
7380

@@ -364,6 +371,8 @@ void rt_schedule(void)
364371
_rt_scheduler_stack_check(to_thread);
365372
#endif /* RT_USING_OVERFLOW_CHECK */
366373

374+
RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread));
375+
367376
rt_hw_context_switch((rt_ubase_t)&current_thread->sp,
368377
(rt_ubase_t)&to_thread->sp, to_thread);
369378
}
@@ -473,6 +482,8 @@ void rt_schedule(void)
473482
{
474483
extern void rt_thread_handle_sig(rt_bool_t clean_state);
475484

485+
RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread));
486+
476487
rt_hw_context_switch((rt_ubase_t)&from_thread->sp,
477488
(rt_ubase_t)&to_thread->sp);
478489

@@ -609,6 +620,8 @@ void rt_scheduler_do_irq_switch(void *context)
609620
current_thread->cpus_lock_nest--;
610621
current_thread->scheduler_lock_nest--;
611622

623+
RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread));
624+
612625
rt_hw_context_switch_interrupt(context, (rt_ubase_t)&current_thread->sp,
613626
(rt_ubase_t)&to_thread->sp, to_thread);
614627
}

src/thread.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
228228
thread->lwp = RT_NULL;
229229
#endif /* RT_USING_LWP */
230230

231+
#ifdef RT_USING_CPU_USAGE
232+
thread->duration_tick = 0;
233+
#endif
234+
231235
RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));
232236

233237
return RT_EOK;

0 commit comments

Comments
 (0)