tracing: roll thread switch in/out into thread stats functions

Since the tracing of thread being switched in/out has the same
instrumentation points, we can roll the tracing function calls
into the one for thread stats gathering functions.
This avoids duplicating code to call another function.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
Daniel Leung 2020-08-27 16:12:01 -07:00 committed by Anas Nashif
parent fc577c4bd1
commit 11e6b43090
17 changed files with 85 additions and 56 deletions

View file

@ -273,10 +273,10 @@ _firq_switch_from_coop:
pop_s r0 /* status32 into r0 */
sr r0, [_ARC_V2_STATUS32_P0]
#ifdef CONFIG_TRACING
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
push_s blink
bl sys_trace_thread_switched_in
bl z_thread_mark_switched_in
pop_s blink
#endif
@ -294,10 +294,10 @@ _firq_switch_from_firq:
sr ilink, [_ARC_V2_STATUS32_P0]
ld ilink, [sp, -8] /* pc into ilink */
#ifdef CONFIG_TRACING
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
push_s blink
bl sys_trace_thread_switched_in
bl z_thread_mark_switched_in
pop_s blink
#endif

View file

@ -309,10 +309,10 @@ _rirq_switch_from_coop:
*/
st_s r13, [sp, ___isf_t_r13_OFFSET]
#ifdef CONFIG_TRACING
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
push_s blink
bl sys_trace_thread_switched_in
bl z_thread_mark_switched_in
pop_s blink
#endif
@ -326,10 +326,10 @@ _rirq_switch_from_rirq:
_set_misc_regs_irq_switch_from_irq
#ifdef CONFIG_TRACING
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
push_s blink
bl sys_trace_thread_switched_in
bl z_thread_mark_switched_in
pop_s blink
#endif

View file

@ -114,10 +114,10 @@ _switch_return_from_coop:
pop_s r3 /* status32 into r3 */
kflag r3 /* write status32 */
#ifdef CONFIG_TRACING
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
push_s blink
bl sys_trace_thread_switched_in
bl z_thread_mark_switched_in
pop_s blink
#endif
@ -150,10 +150,10 @@ _switch_return_from_firq:
#else
sr r3, [_ARC_V2_AUX_IRQ_ACT]
#endif
#ifdef CONFIG_TRACING
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
push_s blink
bl sys_trace_thread_switched_in
bl z_thread_mark_switched_in
pop_s blink
#endif

View file

@ -55,10 +55,10 @@ GDATA(z_arm_tls_ptr)
SECTION_FUNC(TEXT, z_arm_pendsv)
#ifdef CONFIG_TRACING
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
/* Register the context switch */
push {r0, lr}
bl sys_trace_thread_switched_out
bl z_thread_mark_switched_out
#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
pop {r0, r1}
mov lr, r1
@ -366,10 +366,10 @@ _thread_irq_disabled:
pop {r2, lr}
#endif /* CONFIG_BUILTIN_STACK_GUARD */
#ifdef CONFIG_TRACING
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
/* Register the context switch */
push {r0, lr}
bl sys_trace_thread_switched_in
bl z_thread_mark_switched_in
#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
pop {r0, r1}
mov lr, r1

View file

@ -391,8 +391,8 @@ void arch_switch_to_main_thread(struct k_thread *main_thread, char *stack_ptr,
z_arm_prepare_switch_to_main();
_current = main_thread;
#ifdef CONFIG_TRACING
sys_trace_thread_switched_in();
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
z_thread_mark_switched_in();
#endif
/* the ready queue cache already contains the main thread */

View file

@ -75,9 +75,9 @@ SECTION_FUNC(TEXT, z_arm64_context_switch)
ldr x1, [x2]
mov sp, x1
#ifdef CONFIG_TRACING
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
stp xzr, x30, [sp, #-16]!
bl sys_trace_thread_switched_in
bl z_thread_mark_switched_in
ldp xzr, x30, [sp], #16
#endif

View file

@ -21,7 +21,7 @@ GTEXT(_k_neg_eagain)
*/
SECTION_FUNC(exception.other, arch_swap)
#if defined(CONFIG_TRACING)
#if defined(CONFIG_INSTRUMENT_THREAD_SWITCHING)
/* Get a reference to _kernel in r10 */
movhi r10, %hi(_kernel)
ori r10, r10, %lo(_kernel)
@ -34,7 +34,7 @@ SECTION_FUNC(exception.other, arch_swap)
stw ra, _thread_offset_to_ra(r11)
stw sp, _thread_offset_to_sp(r11)
call sys_trace_thread_switched_out
call z_thread_mark_switched_out
/* Get a reference to _kernel in r10 */
movhi r10, %hi(_kernel)
ori r10, r10, %lo(_kernel)
@ -135,7 +135,7 @@ no_unlock:
wrctl status, r3
#endif
#if defined(CONFIG_TRACING)
#if defined(CONFIG_INSTRUMENT_THREAD_SWITCHING)
/* Get a reference to _kernel in r10 */
movhi r10, %hi(_kernel)
ori r10, r10, %lo(_kernel)
@ -147,7 +147,7 @@ no_unlock:
stw ra, _thread_offset_to_ra(r11)
stw sp, _thread_offset_to_sp(r11)
call sys_trace_thread_switched_in
call z_thread_mark_switched_in
/* Get a reference to _kernel in r10 */
movhi r10, %hi(_kernel)

View file

@ -30,8 +30,8 @@ int arch_swap(unsigned int key)
* and so forth. But we do not need to do so because we use posix
* threads => those are all nicely kept by the native OS kernel
*/
#if CONFIG_TRACING
sys_trace_thread_switched_out();
#if CONFIG_INSTRUMENT_THREAD_SWITCHING
z_thread_mark_switched_out();
#endif
_current->callee_saved.key = key;
_current->callee_saved.retval = -EAGAIN;
@ -50,8 +50,8 @@ int arch_swap(unsigned int key)
_current = _kernel.ready_q.cache;
#if CONFIG_TRACING
sys_trace_thread_switched_in();
#if CONFIG_INSTRUMENT_THREAD_SWITCHING
z_thread_mark_switched_in();
#endif
/*
@ -89,11 +89,15 @@ void arch_switch_to_main_thread(struct k_thread *main_thread, char *stack_ptr,
(posix_thread_status_t *)
_kernel.ready_q.cache->callee_saved.thread_status;
sys_trace_thread_switched_out();
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
z_thread_mark_switched_out();
#endif
_current = _kernel.ready_q.cache;
sys_trace_thread_switched_in();
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
z_thread_mark_switched_in();
#endif
posix_main_thread_start(ready_thread_ptr->thread_idx);
} /* LCOV_EXCL_LINE */

View file

@ -233,10 +233,13 @@ GTEXT(_k_neg_eagain)
GTEXT(_is_next_thread_current)
GTEXT(z_get_next_ready_thread)
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
GTEXT(z_thread_mark_switched_in)
GTEXT(z_thread_mark_switched_out)
#ifdef CONFIG_TRACING
GTEXT(sys_trace_thread_switched_in)
GTEXT(sys_trace_isr_enter)
#endif
#endif
#ifdef CONFIG_IRQ_OFFLOAD
GTEXT(_offload_routine)
@ -812,8 +815,8 @@ skip_fp_move_irq:
reschedule:
#if CONFIG_TRACING
call sys_trace_thread_switched_out
#if CONFIG_INSTRUMENT_THREAD_SWITCHING
call z_thread_mark_switched_out
#endif
/* Get reference to _kernel */
la t0, _kernel
@ -932,8 +935,8 @@ skip_load_fp_callee_saved:
kernel_swap:
#endif /* CONFIG_USERSPACE */
#if CONFIG_TRACING
call sys_trace_thread_switched_in
#if CONFIG_INSTRUMENT_THREAD_SWITCHING
call z_thread_mark_switched_in
#endif
#ifdef CONFIG_RISCV_SOC_CONTEXT_SAVE

View file

@ -66,9 +66,9 @@
*/
SECTION_FUNC(TEXT, arch_swap)
#if defined(CONFIG_TRACING)
#if defined(CONFIG_INSTRUMENT_THREAD_SWITCHING)
pushl %eax
call sys_trace_thread_switched_out
call z_thread_mark_switched_out
popl %eax
#endif
/*
@ -344,9 +344,9 @@ CROHandlingDone:
pushl 4(%esp)
popfl
#if defined(CONFIG_TRACING)
#if defined(CONFIG_INSTRUMENT_THREAD_SWITCHING)
pushl %eax
call sys_trace_thread_switched_in
call z_thread_mark_switched_in
popl %eax
#endif
ret

View file

@ -325,7 +325,7 @@ z_x86_switch:
__resume:
#if (!defined(CONFIG_X86_KPTI) && defined(CONFIG_USERSPACE)) \
|| defined(CONFIG_TRACING)
|| defined(CONFIG_INSTRUMENT_THREAD_SWITCHING)
pushq %rdi /* Caller-saved, stash it */
#if !defined(CONFIG_X86_KPTI) && defined(CONFIG_USERSPACE)
/* If KPTI is enabled we're always on the kernel's page tables in
@ -334,11 +334,12 @@ __resume:
*/
call z_x86_swap_update_page_tables
#endif
#ifdef CONFIG_TRACING
call sys_trace_thread_switched_in
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
call z_thread_mark_switched_in
#endif
popq %rdi
#endif /* (!CONFIG_X86_KPTI && CONFIG_USERSPACE) || CONFIG_TRACING */
#endif /* (!CONFIG_X86_KPTI && CONFIG_USERSPACE) || \
CONFIG_INSTRUMENT_THREAD_SWITCHING */
#ifdef CONFIG_USERSPACE
/* Set up exception return stack frame */

View file

@ -241,8 +241,8 @@ flushloop:
*/
l32i a1, a2, BSA_A2_OFF
#ifdef CONFIG_TRACING
call4 sys_trace_thread_switched_in
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
call4 z_thread_mark_switched_in
#endif
j _restore_context
_switch_restore_pc:

View file

@ -155,18 +155,23 @@ struct gdb_ctx;
extern int z_gdb_main_loop(struct gdb_ctx *ctx, bool start);
#endif
#ifdef CONFIG_THREAD_RUNTIME_STATS
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
void z_thread_mark_switched_in(void);
void z_thread_mark_switched_out(void);
#else
static inline void z_thread_mark_switched_in(void)
{
}
static inline void z_thread_mark_switched_out(void)
{
}
#endif
/**
* @brief Called after a thread has been selected to run
*/
#define z_thread_mark_switched_in()
/**
* @brief Called before a thread has been selected to run
*/
#define z_thread_mark_switched_out()
#endif /* CONFIG_INSTRUMENT_THREAD_SWITCHING */
#ifdef __cplusplus
}

View file

@ -106,7 +106,7 @@ static ALWAYS_INLINE unsigned int do_swap(unsigned int key,
z_smp_release_global_lock(new_thread);
}
#endif
sys_trace_thread_switched_out();
z_thread_mark_switched_out();
wait_for_switch(new_thread);
arch_cohere_stacks(old_thread, NULL, new_thread);
_current_cpu->current = new_thread;

View file

@ -914,7 +914,7 @@ struct k_thread *z_get_next_ready_thread(void)
/* Just a wrapper around _current = xxx with tracing */
static inline void set_current(struct k_thread *new_thread)
{
sys_trace_thread_switched_out();
z_thread_mark_switched_out();
_current_cpu->current = new_thread;
}

View file

@ -1025,17 +1025,24 @@ static inline k_ticks_t z_vrfy_k_thread_timeout_expires_ticks(
#include <syscalls/k_thread_timeout_expires_ticks_mrsh.c>
#endif
#ifdef CONFIG_THREAD_RUNTIME_STATS
#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
void z_thread_mark_switched_in(void)
{
#ifdef CONFIG_TRACING
sys_trace_thread_switched_in();
#endif
#ifdef CONFIG_THREAD_RUNTIME_STATS
struct k_thread *thread;
thread = k_current_get();
thread->rt_stats.last_switched_in = k_cycle_get_32();
#endif /* CONFIG_THREAD_RUNTIME_STATS */
}
void z_thread_mark_switched_out(void)
{
#ifdef CONFIG_THREAD_RUNTIME_STATS
uint32_t now;
uint64_t diff;
struct k_thread *thread;
@ -1058,8 +1065,14 @@ void z_thread_mark_switched_out(void)
thread->rt_stats.last_switched_in = 0;
threads_runtime_stats.execution_cycles += diff;
#endif /* CONFIG_THREAD_RUNTIME_STATS */
#ifdef CONFIG_TRACING
sys_trace_thread_switched_out();
#endif
}
#ifdef CONFIG_THREAD_RUNTIME_STATS
int k_thread_runtime_stats_get(k_tid_t thread,
k_thread_runtime_stats_t *stats)
{
@ -1085,3 +1098,5 @@ int k_thread_runtime_stats_all_get(k_thread_runtime_stats_t *stats)
return 0;
}
#endif /* CONFIG_THREAD_RUNTIME_STATS */
#endif /* CONFIG_INSTRUMENT_THREAD_SWITCHING */

View file

@ -10,6 +10,7 @@ config TRACING
imply THREAD_NAME
imply THREAD_STACK_INFO
imply THREAD_MONITOR
select INSTRUMENT_THREAD_SWITCHING
help
Enable system tracing. This requires a backend such as SEGGER
Systemview to be enabled as well.