2019-07-30 13:50:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Freie Universität Berlin
|
|
|
|
* 2019 Inria
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup sys
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Scheduler statistics implementation
|
|
|
|
*
|
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
* @author René Kijewski <rene.kijewski@fu-berlin.de>
|
|
|
|
* @author Francisco Molina <francois-xavier.molina@inria.fr>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sched.h"
|
|
|
|
#include "schedstatistics.h"
|
2020-08-23 21:25:54 +02:00
|
|
|
#include "thread.h"
|
2021-11-16 15:50:37 +01:00
|
|
|
#include "ztimer.h"
|
2019-07-30 13:50:10 +02:00
|
|
|
|
2020-12-15 16:38:56 +01:00
|
|
|
/**
|
|
|
|
* When core_idle_thread is not active, the KERNEL_PID_UNDEF is used to track
|
|
|
|
* the idle time
|
|
|
|
*/
|
2019-07-30 13:50:10 +02:00
|
|
|
schedstat_t sched_pidlist[KERNEL_PID_LAST + 1];
|
|
|
|
|
|
|
|
void sched_statistics_cb(kernel_pid_t active_thread, kernel_pid_t next_thread)
|
|
|
|
{
|
2021-11-16 15:50:37 +01:00
|
|
|
uint32_t now = ztimer_now(ZTIMER_USEC);
|
2019-07-30 13:50:10 +02:00
|
|
|
|
2020-06-16 13:18:53 +02:00
|
|
|
/* Update active thread stats */
|
2020-12-15 16:38:56 +01:00
|
|
|
if (!IS_USED(MODULE_CORE_IDLE_THREAD) || active_thread != KERNEL_PID_UNDEF) {
|
2020-06-16 13:18:53 +02:00
|
|
|
schedstat_t *active_stat = &sched_pidlist[active_thread];
|
2021-11-16 15:50:37 +01:00
|
|
|
active_stat->runtime_us += now - active_stat->laststart;
|
2020-06-16 13:18:53 +02:00
|
|
|
}
|
2019-07-30 13:50:10 +02:00
|
|
|
|
|
|
|
/* Update next_thread stats */
|
2020-12-15 16:38:56 +01:00
|
|
|
if (!IS_USED(MODULE_CORE_IDLE_THREAD) || next_thread != KERNEL_PID_UNDEF) {
|
2020-06-16 13:18:53 +02:00
|
|
|
schedstat_t *next_stat = &sched_pidlist[next_thread];
|
|
|
|
next_stat->laststart = now;
|
|
|
|
next_stat->schedules++;
|
|
|
|
}
|
2019-07-30 13:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_schedstatistics(void)
|
|
|
|
{
|
|
|
|
/* Init laststart for the thread starting schedstatistics since the callback
|
|
|
|
wasn't registered when it was first scheduled */
|
2020-08-23 21:25:54 +02:00
|
|
|
schedstat_t *active_stat = &sched_pidlist[thread_getpid()];
|
2021-11-16 15:50:37 +01:00
|
|
|
active_stat->laststart = ztimer_now(ZTIMER_USEC);
|
2019-07-30 13:50:10 +02:00
|
|
|
active_stat->schedules = 1;
|
|
|
|
sched_register_cb(sched_statistics_cb);
|
|
|
|
}
|