Browse Source

printload: use sched_lock to protect access to tcb

what could have gone wrong before? A scheduling switch during the printload
could have led to a task exit, rendering the tcb invalid. After switching
back, printload would access invalid memory.

This keeps the sched_lock() section as small as possible, just grabbing the
tcb variables we need.
sbg
Beat Küng 8 years ago committed by Lorenz Meier
parent
commit
c46274043f
  1. 95
      src/modules/systemlib/printload.c

95
src/modules/systemlib/printload.c

@ -140,10 +140,52 @@ void print_load(uint64_t t, int fd, struct print_load_s *print_state) @@ -140,10 +140,52 @@ void print_load(uint64_t t, int fd, struct print_load_s *print_state)
print_state->total_user_time = 0;
for (i = 0; i < CONFIG_MAX_TASKS; i++) {
sched_lock(); // need to lock the tcb access (but make it as short as possible)
if (!system_load.tasks[i].valid) {
sched_unlock();
continue;
}
uint64_t interval_runtime;
unsigned tcb_pid = system_load.tasks[i].tcb->pid;
size_t stack_size = system_load.tasks[i].tcb->adj_stack_size;
ssize_t stack_free = 0;
char tcb_name[CONFIG_TASK_NAME_SIZE];
strncpy(tcb_name, system_load.tasks[i].tcb->name, CONFIG_TASK_NAME_SIZE);
#if CONFIG_ARCH_INTERRUPTSTACK > 3
if (system_load.tasks[i].tcb->pid == 0) {
stack_size = (CONFIG_ARCH_INTERRUPTSTACK & ~3);
stack_free = up_check_intstack_remain();
} else {
#endif
stack_free = up_check_tcbstack_remain(system_load.tasks[i].tcb);
#if CONFIG_ARCH_INTERRUPTSTACK > 3
}
#endif
unsigned tcb_sched_priority = system_load.tasks[i].tcb->sched_priority;
#if CONFIG_ARCH_BOARD_SIM || !defined(CONFIG_PRIORITY_INHERITANCE)
#else
unsigned tcb_base_priority = system_load.tasks[i].tcb->base_priority;
#endif
#if CONFIG_RR_INTERVAL > 0
unsigned tcb_timeslice = system_load.tasks[i].tcb->timeslice);
#endif
unsigned tcb_task_state = system_load.tasks[i].tcb->task_state;
if (system_load.tasks[i].valid) {
switch (system_load.tasks[i].tcb->task_state) {
sched_unlock();
switch (tcb_task_state) {
// astyle formatting bug
case TSTATE_TASK_PENDING:
case TSTATE_TASK_READYTORUN:
case TSTATE_TASK_RUNNING:
@ -166,35 +208,38 @@ void print_load(uint64_t t, int fd, struct print_load_s *print_state) @@ -166,35 +208,38 @@ void print_load(uint64_t t, int fd, struct print_load_s *print_state)
print_state->blocked_count++;
break;
}
}
interval_runtime = (system_load.tasks[i].valid && print_state->last_times[i] > 0 &&
interval_runtime = (print_state->last_times[i] > 0 &&
system_load.tasks[i].total_runtime > print_state->last_times[i])
? (system_load.tasks[i].total_runtime - print_state->last_times[i]) / 1000
: 0;
print_state->last_times[i] = system_load.tasks[i].total_runtime;
if (system_load.tasks[i].valid && (print_state->new_time > print_state->interval_start_time)) {
if (print_state->new_time > print_state->interval_start_time) {
print_state->curr_loads[i] = interval_runtime * print_state->interval_time_ms_inv;
if (i > 0) {
if (tcb_pid != 0) {
print_state->total_user_time += interval_runtime;
}
} else {
print_state->curr_loads[i] = 0;
}
if (print_state->new_time <= print_state->interval_start_time) {
continue; // not enough data yet
}
for (i = 0; i < CONFIG_MAX_TASKS; i++) {
if (system_load.tasks[i].valid && (print_state->new_time > print_state->interval_start_time)) {
if (system_load.tasks[i].tcb->pid == 0) {
// print output
if (tcb_pid == 0) {
float idle;
float task_load;
float sched_load;
idle = print_state->curr_loads[0];
idle = print_state->curr_loads[i];
task_load = (float)(print_state->total_user_time) * print_state->interval_time_ms_inv;
/* this can happen if one tasks total runtime was not computed
@ -251,49 +296,31 @@ void print_load(uint64_t t, int fd, struct print_load_s *print_state) @@ -251,49 +296,31 @@ void print_load(uint64_t t, int fd, struct print_load_s *print_state)
);
}
size_t stack_size = system_load.tasks[i].tcb->adj_stack_size;
ssize_t stack_free = 0;
#if CONFIG_ARCH_INTERRUPTSTACK > 3
if (system_load.tasks[i].tcb->pid == 0) {
stack_size = (CONFIG_ARCH_INTERRUPTSTACK & ~3);
stack_free = up_check_intstack_remain();
} else {
#endif
stack_free = up_check_tcbstack_remain(system_load.tasks[i].tcb);
#if CONFIG_ARCH_INTERRUPTSTACK > 3
}
#endif
dprintf(fd, "%s%4d %*-s %8lld %2d.%03d %5u/%5u %3u (%3u) ",
clear_line,
system_load.tasks[i].tcb->pid,
CONFIG_TASK_NAME_SIZE, system_load.tasks[i].tcb->name,
tcb_pid,
CONFIG_TASK_NAME_SIZE, tcb_name,
(system_load.tasks[i].total_runtime / 1000),
(int)(print_state->curr_loads[i] * 100.0f),
(int)((print_state->curr_loads[i] * 100.0f - (int)(print_state->curr_loads[i] * 100.0f)) * 1000),
stack_size - stack_free,
stack_size,
system_load.tasks[i].tcb->sched_priority,
tcb_sched_priority,
#if CONFIG_ARCH_BOARD_SIM || !defined(CONFIG_PRIORITY_INHERITANCE)
0);
#else
system_load.tasks[i].tcb->base_priority);
tcb_base_priority);
#endif
#if CONFIG_RR_INTERVAL > 0
/* print scheduling info with RR time slice */
dprintf(fd, " %6d\n", system_load.tasks[i].tcb->timeslice);
dprintf(fd, " %6d\n", tcb_timeslice);
(void)tstate_name(TSTATE_TASK_INVALID); // Stop not used warning
#else
// print task state instead
dprintf(fd, " %-6s\n", tstate_name(system_load.tasks[i].tcb->task_state));
dprintf(fd, " %-6s\n", tstate_name(tcb_task_state));
#endif
}
}
print_state->interval_start_time = print_state->new_time;
}

Loading…
Cancel
Save