1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 10:12:45 +01:00

core/schedstatistics: refactor

- add init_schedstatistics function to be called after
  auto_init, that way xtimer_is init is called before
  the first call to xtimer_now
- register schedstatics code as a callback to be executed
  on each sched_run()
This commit is contained in:
francisco 2019-07-03 15:12:03 +02:00 committed by Francisco Molina
parent 999fffdc59
commit 3d62fa05b8
4 changed files with 51 additions and 33 deletions

View File

@ -219,12 +219,18 @@ typedef struct {
*/
extern schedstat_t sched_pidlist[KERNEL_PID_LAST + 1];
/**
* @brief Registers the sched statistics callback and sets laststart for
* caller thread
*/
void init_schedstatistics(void);
/**
* @brief Register a callback that will be called on every scheduler run
*
* @param[in] callback The callback functions the will be called
*/
void sched_register_cb(void (*callback)(uint32_t, uint32_t));
void sched_register_cb(void (*callback)(kernel_pid_t, kernel_pid_t));
#endif /* MODULE_SCHEDSTATISTICS */
#ifdef __cplusplus

View File

@ -29,10 +29,6 @@
#include "periph/pm.h"
#ifdef MODULE_SCHEDSTATISTICS
#include "sched.h"
#endif
#define ENABLE_DEBUG (0)
#include "debug.h"
@ -49,11 +45,6 @@ static void *main_trampoline(void *arg)
auto_init();
#endif
#ifdef MODULE_SCHEDSTATISTICS
schedstat_t *ss = &sched_pidlist[thread_getpid()];
ss->laststart = 0;
#endif
LOG_INFO("main(): This is RIOT! (Version: " RIOT_VERSION ")\n");
main();

View File

@ -75,7 +75,7 @@ const uint8_t _tcb_name_offset = offsetof(thread_t, name);
#endif
#ifdef MODULE_SCHEDSTATISTICS
static void (*sched_cb) (uint32_t timestamp, uint32_t value) = NULL;
static void (*sched_cb) (kernel_pid_t active_thread, kernel_pid_t next_thread) = NULL;
schedstat_t sched_pidlist[KERNEL_PID_LAST + 1];
#endif
@ -100,10 +100,6 @@ int __attribute__((used)) sched_run(void)
return 0;
}
#ifdef MODULE_SCHEDSTATISTICS
uint32_t now = xtimer_now().ticks32;
#endif
if (active_thread) {
if (active_thread->status == STATUS_RUNNING) {
active_thread->status = STATUS_PENDING;
@ -114,21 +110,14 @@ int __attribute__((used)) sched_run(void)
LOG_WARNING("scheduler(): stack overflow detected, pid=%" PRIkernel_pid "\n", active_thread->pid);
}
#endif
#ifdef MODULE_SCHEDSTATISTICS
schedstat_t *active_stat = &sched_pidlist[active_thread->pid];
if (active_stat->laststart) {
active_stat->runtime_ticks += now - active_stat->laststart;
}
#endif
}
#ifdef MODULE_SCHEDSTATISTICS
schedstat_t *next_stat = &sched_pidlist[next_thread->pid];
next_stat->laststart = now;
next_stat->schedules++;
if (sched_cb) {
sched_cb(now, next_thread->pid);
/* Use `sched_active_pid` instead of `active_thread` since after `sched_task_exit()` is
called `active_thread` is set to NULL while `sched_active_thread` isn't updated until
`next_thread` is scheduled*/
sched_cb(sched_active_pid, next_thread->pid);
}
#endif
@ -151,13 +140,6 @@ int __attribute__((used)) sched_run(void)
return 1;
}
#ifdef MODULE_SCHEDSTATISTICS
void sched_register_cb(void (*callback)(uint32_t, uint32_t))
{
sched_cb = callback;
}
#endif
void sched_set_status(thread_t *process, thread_status_t status)
{
if (status >= STATUS_ON_RUNQUEUE) {
@ -221,3 +203,35 @@ NORETURN void sched_task_exit(void)
sched_active_thread = NULL;
cpu_switch_context_exit();
}
#ifdef MODULE_SCHEDSTATISTICS
void sched_register_cb(void (*callback)(kernel_pid_t, kernel_pid_t))
{
sched_cb = callback;
}
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 next_thread stats */
schedstat_t *next_stat = &sched_pidlist[next_thread];
next_stat->laststart = now;
next_stat->schedules++;
}
void init_schedstatistics(void)
{
/* Init laststart for the thread starting schedstatistics since the callback
wasn't registered when it was first scheduled */
schedstat_t *active_stat = &sched_pidlist[sched_active_pid];
active_stat->laststart = xtimer_now().ticks32;
active_stat->schedules = 1;
sched_register_cb(sched_statistics_cb);
}
#endif

View File

@ -92,6 +92,10 @@
#include "net/sock/dtls.h"
#endif
#ifdef MODULE_SCHEDSTATISTICS
#include "sched.h"
#endif
#define ENABLE_DEBUG (0)
#include "debug.h"
@ -105,6 +109,9 @@ void auto_init(void)
DEBUG("Auto init xtimer module.\n");
xtimer_init();
#endif
#ifdef MODULE_SCHEDSTATISTICS
init_schedstatistics();
#endif
#ifdef MODULE_MCI
DEBUG("Auto init mci module.\n");
mci_initialize();