Skip to content

Commit

Permalink
sched: replace sync pause with async pause for sched_backtrace
Browse files Browse the repository at this point in the history
Signed-off-by: hujun5 <[email protected]>
  • Loading branch information
hujun260 committed Oct 12, 2024
1 parent 0bea0a0 commit 7155929
Showing 1 changed file with 79 additions and 9 deletions.
88 changes: 79 additions & 9 deletions sched/sched/sched_backtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,59 @@

#include "sched.h"

#ifdef CONFIG_ARCH_HAVE_BACKTRACE

/****************************************************************************
* Private Type Declarations
****************************************************************************/

#ifdef CONFIG_SMP
struct backtrace_arg_s
{
pid_t pid;
FAR void **buffer;
int size;
int skip;
cpu_set_t saved_affinity;
uint16_t saved_flags;
bool need_restore;
};

/****************************************************************************
* Private Functions
****************************************************************************/

static int sched_backtrace_handler(FAR void *cookie)
{
FAR struct backtrace_arg_s *arg = cookie;
FAR struct tcb_s *tcb;
irqstate_t flags;

flags = enter_critical_section();

tcb = nxsched_get_tcb(arg->pid);

if (!tcb || tcb->task_state == TSTATE_TASK_INVALID ||
(tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0)
{
/* There is no TCB with this pid or, if there is, it is not a task. */

leave_critical_section(flags);
return -ESRCH;
}

if (arg->need_restore)
{
tcb->affinity = arg->saved_affinity;
tcb->flags = arg->saved_flags;
}

leave_critical_section(flags);

return up_backtrace(tcb, arg->buffer, arg->size, arg->skip);
}
#endif

/****************************************************************************
* Public Functions
****************************************************************************/
Expand All @@ -44,7 +97,6 @@
*
****************************************************************************/

#ifdef CONFIG_ARCH_HAVE_BACKTRACE
int sched_backtrace(pid_t tid, FAR void **buffer, int size, int skip)
{
FAR struct tcb_s *tcb = this_task();
Expand All @@ -65,18 +117,36 @@ int sched_backtrace(pid_t tid, FAR void **buffer, int size, int skip)
if (tcb->task_state == TSTATE_TASK_RUNNING &&
g_nx_initstate != OSINIT_PANIC)
{
up_cpu_pause(tcb->cpu);
struct backtrace_arg_s arg;

if ((tcb->flags & TCB_FLAG_CPU_LOCKED) != 0)
{
arg.pid = tcb->pid;
arg.need_restore = false;
}
else
{
arg.pid = tcb->pid;
arg.saved_flags = tcb->flags;
arg.saved_affinity = tcb->affinity;
arg.need_restore = true;

tcb->flags |= TCB_FLAG_CPU_LOCKED;
CPU_SET(tcb->cpu, &tcb->affinity);
}

arg.buffer = buffer;
arg.size = size;
arg.skip = skip;
ret = nxsched_smp_call_single(tcb->cpu,
sched_backtrace_handler,
&arg, true);
}
else
#endif

ret = up_backtrace(tcb, buffer, size, skip);
#ifdef CONFIG_SMP
if (tcb->task_state == TSTATE_TASK_RUNNING &&
g_nx_initstate != OSINIT_PANIC)
{
up_cpu_resume(tcb->cpu);
ret = up_backtrace(tcb, buffer, size, skip);
}
#endif
}

leave_critical_section(flags);
Expand Down

0 comments on commit 7155929

Please sign in to comment.