Skip to content
Permalink
Browse files

subsys: logging: fix trigger threshold corner case

The CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD option can be used to wake up
the background log processing thread when a given number of messages
have been queued.

Currently, the msg_finalize() routine which is responsible for
queueing a log message for later handling appends messages to the
global list after performing the threshold check and waking up the
thread.

This leads to a race condition with undesirable behavior if the
threshold == 1:

- the msg_finalize() thread is scheduled out by calling k_wakeup()
- the log processing thread wakes up, notice that no messages are
  queued, and goes back to sleep
- the msg_finalize() thread is scheduled back in and the message is
  queued for processing

This defers the handling of the message until the processing thread
wakes up again after the CONFIG_LOG_PROCESS_THREAD_SLEEP_MS timeout,
which is not what the user wants.

Fix this by queueing the message before waking up the handler thread.
(This also may improve responsiveness for larger threshold values.)

Signed-off-by: Marti Bolivar <marti@foundries.io>
  • Loading branch information...
mbolivar authored and nashif committed Jul 24, 2018
1 parent e3bf535 commit 1bfcea244bcd9eec81f6a37929d5ac6fb6225c7c
Showing with 6 additions and 8 deletions.
  1. +6 −8 subsys/logging/log_core.c
@@ -48,14 +48,6 @@ static inline void msg_finalize(struct log_msg *msg,

atomic_inc(&buffered_cnt);

if (!IS_ENABLED(CONFIG_LOG_INPLACE_PROCESS) &&
CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD) {
if (buffered_cnt == CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD &&
proc_tid) {
k_wakeup(proc_tid);
}
}

key = irq_lock();

log_list_add_tail(&list, msg);
@@ -64,6 +56,12 @@ static inline void msg_finalize(struct log_msg *msg,

if (IS_ENABLED(CONFIG_LOG_INPLACE_PROCESS) || panic_mode) {
(void)log_process(false);
} else if (!IS_ENABLED(CONFIG_LOG_INPLACE_PROCESS) &&
CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD) {
if (buffered_cnt == CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD &&
proc_tid) {
k_wakeup(proc_tid);
}
}
}

0 comments on commit 1bfcea2

Please sign in to comment.
You can’t perform that action at this time.