How to properly use the user-defined tracing API? #75406
Replies: 1 comment
-
UpdatesEver since the original post I made some kernel investigation and a few tweaks on the base example, making it even simpler. Just one application thread is necessary now, with the #include <zephyr/logging/log.h>
#include <zephyr/kernel.h>
#include <string.h>
LOG_MODULE_REGISTER(tracing);
static void thread_function(void *a1, void *a2, void *a3){ // main thread: prints "hello" every second
for(;;){
printf("\n%s\n\n", CONFIG_BOARD_TARGET);
k_sleep(K_SECONDS(1));
}
}
K_THREAD_DEFINE(
main_thread, 1024, thread_function,
NULL, NULL, NULL, 5, 0, 0
);
void sys_trace_thread_switched_in_user(){
if(k_sched_current_thread_query() == main_thread) LOG_INF("SWT_TO");
}
void sys_trace_thread_switched_out_user(){
if(k_sched_current_thread_query() == main_thread) LOG_INF("SWT_AY");
} Alternatively it's possible to use the following functions, which will yield similar results while keeping the safeguards of the sample code: void sys_trace_thread_switched_in_user(){
unsigned int key = irq_lock();
k_tid_t thread = k_sched_current_thread_query();
if(thread == main_thread) LOG_INF("SWT_TO");
irq_unlock(key);
}
void sys_trace_thread_switched_out_user(){
unsigned int key = irq_lock();
k_tid_t thread = k_sched_current_thread_query();
if(thread == main_thread) LOG_INF("SWT_AY");
irq_unlock(key);
} ResultsThe following targets were tested:
OutputThe actual output observed for each target can be seen below. target: target: target: target target |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
EDIT: more comprehensive testing has been carried out with more targets.
Check the comments below for updates.
Original problem
Hey everyone! Hope you're all doing great.
Let's say I have a very basic project as given below (one can just paste it and run to see in action). We have two threads, the
main
and thelog
. The first simply prints "hello" every second and the other simply prints log events, waiting for incoming events on a message queue:Now what I want is that every time the
main
thread enters/leaves the CPU for execution, theSWITCH_TO
andSWITCH_AWAY
events are fired, respectively. And this is what I came up for the tracing, based on the provided sample code:The whole point of my question is that although this implementation seems correct to my unexperienced Zephyr developer eyes, the actual log output features way more
SWITCH_AWAY
events thanSWITCH_TO
events, when in fact I was expecting more of a balanced outcome really:I presume possible comments might warn me my method of checking for the thread is wrong, but just removing it doesn't really solve the issue. In fact, for these tracing functions:
The output still showcases a mismatch of
SWITCH_TO
andSWITCH_AWAY
events:So the obvious question is: Is this a bug I should report or is it just me not really getting what are these functions made for?
If the latter holds, what should I do instead to enable the proper function tracing?
Thank you all very much and sorry for the long message, I tried to keep it as simple and clear as possible.
Beta Was this translation helpful? Give feedback.
All reactions