From baca419934d568ae58a343e78f484d565a93e6cf Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Tue, 16 Jun 2020 13:18:53 +0200 Subject: [PATCH] sys/schedstatistics: handle if PIDs are KERNEL_PID_UNDEF Previously, sched_statistics_cb() was always called with two valid PIDs. Now it is possible (when the idle thread is not used) that one of the two might be KERNEL_PID_UNDEF, as the callback might be called when descheduling the last thread, or scheduling the first. --- sys/schedstatistics/schedstatistics.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/sys/schedstatistics/schedstatistics.c b/sys/schedstatistics/schedstatistics.c index 0f5c78d0dd..92743ca6ba 100644 --- a/sys/schedstatistics/schedstatistics.c +++ b/sys/schedstatistics/schedstatistics.c @@ -31,15 +31,18 @@ void sched_statistics_cb(kernel_pid_t active_thread, kernel_pid_t next_thread) { uint32_t now = xtimer_now().ticks32; - /* Update active thread runtime, there is always an active thread since - first sched_run happens when main_trampoline gets scheduled */ - schedstat_t *active_stat = &sched_pidlist[active_thread]; - active_stat->runtime_ticks += now - active_stat->laststart; + /* Update active thread stats */ + if (active_thread != KERNEL_PID_UNDEF) { + schedstat_t *active_stat = &sched_pidlist[active_thread]; + active_stat->runtime_ticks += now - active_stat->laststart; + } /* Update next_thread stats */ - schedstat_t *next_stat = &sched_pidlist[next_thread]; - next_stat->laststart = now; - next_stat->schedules++; + if (next_thread != KERNEL_PID_UNDEF) { + schedstat_t *next_stat = &sched_pidlist[next_thread]; + next_stat->laststart = now; + next_stat->schedules++; + } } void init_schedstatistics(void)