Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/schedstatistics: fix call to uninitialized xtimer #11781

Merged
merged 2 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ endif

ifneq (,$(filter schedstatistics,$(USEMODULE)))
USEMODULE += xtimer
USEMODULE += sched_cb
endif

ifneq (,$(filter arduino,$(USEMODULE)))
Expand Down
12 changes: 10 additions & 2 deletions core/include/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,21 @@ 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);
#endif /* MODULE_SCHEDSTATISTICS */

#ifdef MODULE_SCHED_CB
/**
* @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));
#endif /* MODULE_SCHEDSTATISTICS */
void sched_register_cb(void (*callback)(kernel_pid_t, kernel_pid_t));
#endif /* MODULE_SCHED_CB */

#ifdef __cplusplus
}
Expand Down
9 changes: 0 additions & 9 deletions core/kernel_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@

#include "periph/pm.h"

#ifdef MODULE_SCHEDSTATISTICS
#include "sched.h"
#endif

#define ENABLE_DEBUG (0)
#include "debug.h"

Expand All @@ -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();
Expand Down
66 changes: 42 additions & 24 deletions core/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ FORCE_USED_SECTION
const uint8_t _tcb_name_offset = offsetof(thread_t, name);
#endif

#ifdef MODULE_SCHED_CB
static void (*sched_cb) (kernel_pid_t active_thread, kernel_pid_t next_thread) = NULL;
#endif
#ifdef MODULE_SCHEDSTATISTICS
static void (*sched_cb) (uint32_t timestamp, uint32_t value) = NULL;
schedstat_t sched_pidlist[KERNEL_PID_LAST + 1];
#endif

Expand All @@ -100,10 +102,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;
Expand All @@ -114,21 +112,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++;
#ifdef MODULE_SCHED_CB
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

Expand All @@ -151,13 +142,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) {
Expand Down Expand Up @@ -221,3 +205,37 @@ NORETURN void sched_task_exit(void)
sched_active_thread = NULL;
cpu_switch_context_exit();
}

#ifdef MODULE_SCHED_CB
fjmolinas marked this conversation as resolved.
Show resolved Hide resolved
void sched_register_cb(void (*callback)(kernel_pid_t, kernel_pid_t))
{
sched_cb = callback;
}
#endif

#ifdef MODULE_SCHEDSTATISTICS
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
1 change: 1 addition & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ PSEUDOMODULES += saul_gpio
PSEUDOMODULES += saul_nrf_temperature
PSEUDOMODULES += scanf_float
PSEUDOMODULES += schedstatistics
PSEUDOMODULES += sched_cb
PSEUDOMODULES += semtech_loramac_rx
PSEUDOMODULES += sock
PSEUDOMODULES += sock_ip
Expand Down
7 changes: 7 additions & 0 deletions sys/auto_init/auto_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
#include "net/sock/dtls.h"
#endif

#ifdef MODULE_SCHEDSTATISTICS
#include "sched.h"
#endif

#define ENABLE_DEBUG (0)
#include "debug.h"

Expand All @@ -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();
Expand Down