Skip to content

Commit

Permalink
cpuload: add nxsched_update_critmon() to handle thread busyloop
Browse files Browse the repository at this point in the history
Signed-off-by: ligd <[email protected]>
Signed-off-by: lipengfei28 <[email protected]>
  • Loading branch information
GUIDINGLI committed Oct 15, 2024
1 parent 5b14fb7 commit f0e12a9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions sched/sched/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ void nxsched_process_cpuload_ticks(clock_t ticks);
#ifdef CONFIG_SCHED_CRITMONITOR
void nxsched_resume_critmon(FAR struct tcb_s *tcb);
void nxsched_suspend_critmon(FAR struct tcb_s *tcb);
void nxsched_update_critmon(FAR struct tcb_s *tcb);
#endif

#if CONFIG_SCHED_CRITMONITOR_MAXTIME_PREEMPTION >= 0
Expand Down
6 changes: 6 additions & 0 deletions sched/sched/sched_cpuload.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ int clock_cpuload(int pid, FAR struct cpuload_s *cpuload)

DEBUGASSERT(cpuload);

#ifdef CONFIG_SCHED_CPULOAD_CRITMONITOR
/* Update critmon in case of the target thread busyloop */

nxsched_update_critmon(nxsched_get_tcb(pid));
#endif

/* Momentarily disable interrupts. We need (1) the task to stay valid
* while we are doing these operations and (2) the tick counts to be
* synchronized when read.
Expand Down
24 changes: 24 additions & 0 deletions sched/sched/sched_critmonitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,27 @@ void nxsched_suspend_critmon(FAR struct tcb_s *tcb)
}
#endif /* CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION */
}

void nxsched_update_critmon(FAR struct tcb_s *tcb)
{
clock_t current = perf_gettime();
clock_t elapsed = current - tcb->run_start;

if (tcb->task_state != TSTATE_TASK_RUNNING)
{
return;
}

#ifdef CONFIG_SCHED_CPULOAD_CRITMONITOR
clock_t tick = elapsed * CLOCKS_PER_SEC / perf_getfreq();
nxsched_process_taskload_ticks(tcb, tick);
#endif

tcb->run_start = current;
tcb->run_time += elapsed;
if (elapsed > tcb->run_max)
{
tcb->run_max = elapsed;
CHECK_THREAD(tcb->pid, elapsed);
}
}

0 comments on commit f0e12a9

Please sign in to comment.