From 9f3043f9cad0346aa82b17cec2736ab270910a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Wed, 15 Mar 2017 14:05:23 +0100 Subject: [PATCH] printload: store total_runtime in a separate loop Because during the printf, the total_runtime could be changed by another task. This is still not entirely race-free though. --- src/modules/systemlib/printload.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/modules/systemlib/printload.c b/src/modules/systemlib/printload.c index d7dd027703..62a27dcf19 100644 --- a/src/modules/systemlib/printload.c +++ b/src/modules/systemlib/printload.c @@ -139,6 +139,13 @@ void print_load(uint64_t t, int fd, struct print_load_s *print_state) print_state->blocked_count = 0; print_state->total_user_time = 0; + // create a copy of the runtimes because this could be updated during the print output + uint32_t total_runtime[CONFIG_MAX_TASKS]; + + for (i = 0; i < CONFIG_MAX_TASKS; i++) { + total_runtime[i] = (uint32_t)(system_load.tasks[i].total_runtime / 1000); + } + for (i = 0; i < CONFIG_MAX_TASKS; i++) { sched_lock(); // need to lock the tcb access (but make it as short as possible) @@ -209,12 +216,10 @@ void print_load(uint64_t t, int fd, struct print_load_s *print_state) break; } - interval_runtime = (print_state->last_times[i] > 0 && - system_load.tasks[i].total_runtime / 1000 > print_state->last_times[i]) - ? (system_load.tasks[i].total_runtime / 1000 - print_state->last_times[i]) - : 0; + interval_runtime = (print_state->last_times[i] > 0 && total_runtime[i] > print_state->last_times[i]) + ? (total_runtime[i] - print_state->last_times[i]) : 0; - print_state->last_times[i] = system_load.tasks[i].total_runtime / 1000; + print_state->last_times[i] = total_runtime[i]; float current_load = 0.f; @@ -296,11 +301,11 @@ void print_load(uint64_t t, int fd, struct print_load_s *print_state) ); } - dprintf(fd, "%s%4d %*-s %8lld %2d.%03d %5u/%5u %3u (%3u) ", + dprintf(fd, "%s%4d %*-s %8d %2d.%03d %5u/%5u %3u (%3u) ", clear_line, tcb_pid, CONFIG_TASK_NAME_SIZE, tcb_name, - (system_load.tasks[i].total_runtime / 1000), + total_runtime[i], (int)(current_load * 100.0f), (int)((current_load * 100.0f - (int)(current_load * 100.0f)) * 1000), stack_size - stack_free,