Skip to content

Commit cb8c89b

Browse files
committed
doc: subsys: logging: internal thread and thread wake up
Documenting new logger features: waking up processing thread and internal logger processing thread. Signed-off-by: Krzysztof Chruscinski <[email protected]>
1 parent 43f1317 commit cb8c89b

File tree

5 files changed

+82
-23
lines changed

5 files changed

+82
-23
lines changed

doc/subsystems/logging/logger.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ be processed by printk. Longer strings are trimmed.
109109
the log macro call. Note that it can lead to errors when logger is used in the
110110
interrupt context.
111111

112+
:option:`CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD`: When number of buffered log
113+
messages reaches the threshold dedicated thread (see :cpp:func:`log_thread_set`)
114+
is waken up. If :option:`CONFIG_LOG_PROCESS_THREAD` is enabled then this
115+
threshold is used by the internal thread.
116+
117+
:option:`CONFIG_LOG_PROCESS_THREAD`: When enabled, logger is creating own thread
118+
which handles log processing.
119+
112120
:option:`CONFIG_LOG_BUFFER_SIZE`: Number of bytes dedicated for the logger
113121
message pool. Single message capable of storing standard log with up to 3
114122
arguments or hexdump message with 12 bytes of data take 32 bytes.
@@ -221,6 +229,13 @@ be used to change maximal severity level for given module. Module is identified
221229
by source ID and domain ID. Source ID can be retrieved if source name is known
222230
by iterating through all registered sources.
223231

232+
If logger is processed from a thread then it is possible to enable a feature
233+
which will wake up processing thread when certain amount of log messages are
234+
buffered (see :option:`CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD`). It is also
235+
possible to enable internal logger thread (see
236+
:option:`CONFIG_LOG_PROCESS_THREAD`). In that case logger thread is initialized
237+
and log messages are processed implicitly.
238+
224239
.. _log_panic:
225240

226241
Logger panic

include/logging/log_ctrl.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ void log_init(void);
4747
*
4848
* See CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD.
4949
*
50-
* @param process_tid Process thread id. Used to wake up the thread
50+
* @note Function has asserts and has no effect when CONFIG_LOG_PROCESS is set.
51+
*
52+
* @param process_tid Process thread id. Used to wake up the thread.
5153
*/
5254
void log_thread_set(k_tid_t process_tid);
5355

@@ -81,6 +83,13 @@ void log_panic(void);
8183
*/
8284
bool log_process(bool bypass);
8385

86+
/**
87+
* @brief Return number of buffered log messages.
88+
*
89+
* @return Number of currently buffered log messages.
90+
*/
91+
u32_t log_buffered_cnt(void);
92+
8493
/** @brief Get number of independent logger sources (modules and instances)
8594
*
8695
* @param domain_id Domain ID.

samples/subsys/logging/logger/src/main.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,19 @@ static void external_log_system_showcase(void)
194194
ext_log_system_foo();
195195
}
196196

197+
static void wait_on_log_flushed(void)
198+
{
199+
while (log_buffered_cnt()) {
200+
k_sleep(5);
201+
}
202+
}
203+
197204
void log_demo_thread(void *dummy1, void *dummy2, void *dummy3)
198205
{
199-
const u32_t sleep_period = CONFIG_LOG_PROCESS_THREAD_SLEEP_MS + 100;
206+
k_sleep(100);
200207

201208
(void)log_set_timestamp_func(timestamp_get, timestamp_freq());
209+
202210
module_logging_showcase();
203211

204212
instance_logging_showcase();
@@ -218,20 +226,19 @@ void log_demo_thread(void *dummy1, void *dummy2, void *dummy3)
218226
log_source_id_get(INST2_NAME),
219227
CONFIG_LOG_DEFAULT_LEVEL);
220228

221-
/* Ensuring that log thread will process. */
222-
k_sleep(sleep_period);
229+
wait_on_log_flushed();
223230

224231
severity_levels_showcase();
225-
/* Ensuring that log thread will process. */
226-
k_sleep(sleep_period);
232+
233+
wait_on_log_flushed();
227234

228235
performance_showcase();
229-
/* Ensuring that log thread will process. */
230-
k_sleep(sleep_period);
236+
237+
wait_on_log_flushed();
231238

232239
external_log_system_showcase();
233-
/* Ensuring that log thread will process. */
234-
k_sleep(sleep_period);
240+
241+
wait_on_log_flushed();
235242
}
236243

237244
K_THREAD_DEFINE(log_demo_thread_id, STACKSIZE, log_demo_thread,

subsys/logging/Kconfig

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ config LOG_PROCESS_TRIGGER_THRESHOLD
214214
help
215215
When number of buffered messages reaches the threshold thread is waken
216216
up. Log processing thread ID is provided during log initialization.
217-
Set 0 to disable the feature.
217+
Set 0 to disable the feature. If LOG_PROCESS_THREAD is enabled then
218+
this threshold is used by the internal thread.
218219

219220
config LOG_PROCESS_THREAD
220221
bool "Enable internal thread for log processing"
@@ -225,14 +226,30 @@ config LOG_PROCESS_THREAD
225226
number of buffered messages exceeds the threshold (see
226227
LOG_PROCESS_TRIGGER_THR).
227228

229+
if LOG_PROCESS_THREAD
230+
228231
config LOG_PROCESS_THREAD_SLEEP_MS
229-
int "Set log processing thread sleep period"
230-
depends on LOG_PROCESS_THREAD
232+
int "Set internal log processing thread sleep period"
231233
default 1000
232234
help
233235
Log processing thread sleeps for requested period given in
234236
milliseconds. When waken up, thread process any buffered messages.
235237

238+
config LOG_PROCESS_THREAD_PRIO
239+
int "Priority of the log internal thread"
240+
default -2
241+
help
242+
Change with care since log processing can be time consuming thus it
243+
should be on low priority.
244+
245+
config LOG_PROCESS_THREAD_STACK_SIZE
246+
int "Stack size for the internal log processing thread"
247+
default 768
248+
help
249+
Set the internal stack size for log processing thread.
250+
251+
endif
252+
236253
config LOG_BUFFER_SIZE
237254
int "Number of bytes dedicated for the logger internal buffer."
238255
default 1024

subsys/logging/log_core.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ static inline void msg_finalize(struct log_msg *msg,
4646
msg->hdr.ids = src_level;
4747
msg->hdr.timestamp = timestamp_func();
4848

49+
atomic_inc(&buffered_cnt);
50+
4951
if (!IS_ENABLED(CONFIG_LOG_INPLACE_PROCESS) &&
5052
CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD) {
51-
atomic_inc(&buffered_cnt);
5253
if (buffered_cnt == CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD &&
5354
proc_tid) {
5455
k_wakeup(proc_tid);
@@ -224,7 +225,6 @@ void log_init(void)
224225
timestamp_func = timestamp_get;
225226
log_output_timestamp_freq_set(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC);
226227

227-
228228
/* Assign ids to backends. */
229229
for (i = 0; i < log_backend_count_get(); i++) {
230230
log_backend_id_set(log_backend_get(i),
@@ -239,7 +239,7 @@ void log_init(void)
239239
#endif
240240
}
241241

242-
void log_thread_set(k_tid_t process_tid)
242+
static void thread_set(k_tid_t process_tid)
243243
{
244244
proc_tid = process_tid;
245245

@@ -251,6 +251,15 @@ void log_thread_set(k_tid_t process_tid)
251251
}
252252
}
253253

254+
void log_thread_set(k_tid_t process_tid)
255+
{
256+
if (IS_ENABLED(CONFIG_LOG_PROCESS_THREAD)) {
257+
assert(0);
258+
} else {
259+
thread_set(process_tid);
260+
}
261+
}
262+
254263
int log_set_timestamp_func(timestamp_get_t timestamp_getter, u32_t freq)
255264
{
256265
if (!timestamp_getter) {
@@ -332,6 +341,11 @@ bool log_process(bool bypass)
332341
return (log_list_head_peek(&list) != NULL);
333342
}
334343

344+
u32_t log_buffered_cnt(void)
345+
{
346+
return buffered_cnt;
347+
}
348+
335349
u32_t log_src_cnt_get(u32_t domain_id)
336350
{
337351
return log_sources_count();
@@ -444,13 +458,10 @@ u32_t log_filter_get(struct log_backend const *const backend,
444458
}
445459

446460
#ifdef CONFIG_LOG_PROCESS_THREAD
447-
448-
#define STACKSIZE 768
449-
450461
static void log_process_thread_func(void *dummy1, void *dummy2, void *dummy3)
451462
{
452463
log_init();
453-
log_thread_set(k_current_get());
464+
thread_set(k_current_get());
454465

455466
while (1) {
456467
if (log_process(false) == false) {
@@ -459,7 +470,7 @@ static void log_process_thread_func(void *dummy1, void *dummy2, void *dummy3)
459470
}
460471
}
461472

462-
K_THREAD_DEFINE(log_process_thread, STACKSIZE, log_process_thread_func,
463-
NULL, NULL, NULL,
464-
K_LOWEST_APPLICATION_THREAD_PRIO, 0, K_NO_WAIT);
473+
K_THREAD_DEFINE(log_process_thread, CONFIG_LOG_PROCESS_THREAD_STACK_SIZE,
474+
log_process_thread_func, NULL, NULL, NULL,
475+
CONFIG_LOG_PROCESS_THREAD_PRIO, 0, K_NO_WAIT);
465476
#endif /* CONFIG_LOG_PROCESS_THREAD */

0 commit comments

Comments
 (0)