<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Custom Tracing Implementation</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/115823/custom-tracing-implementation</link><description>Hi, we are working on an application that makes heavy use of work queues. We&amp;#39;d like some way to monitor work queue events to optimize timings/priorities etc. I was looking into user tracing configurable via: https://docs.zephyrproject.org/latest/services</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 28 Oct 2024 07:56:28 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/115823/custom-tracing-implementation" /><item><title>RE: Custom Tracing Implementation</title><link>https://devzone.nordicsemi.com/thread/508076?ContentTypeID=1</link><pubDate>Mon, 28 Oct 2024 07:56:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4071c44a-1c4b-46fb-b38c-5cc81c1ae1e4</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;I have not tried to for sometime but to&amp;nbsp;achieve detailed tracing on work queues in the &lt;code&gt;nrf5340&lt;/code&gt; application using Zephyr, there are some ways to extend the current limited tracing capabilities in the Zephyr RTOS and Nordic SDK without needing to directly patch the SDK.&lt;/p&gt;
&lt;p&gt;Try to redefine the tracing macros,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The documentation suggests applications can redefine tracing macros, though examples are sparse. By creating custom definitions for work queue functions (such as &lt;code&gt;k_work_submit&lt;/code&gt; and &lt;code&gt;k_work_handler&lt;/code&gt;), you can add logging hooks and trace events. Define custom tracing macros like this in your application:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define sys_trace_k_work_submit(work) custom_trace_k_work_submit(work)
#define sys_trace_k_work_handler_entry(work) custom_trace_k_work_handler_entry(work)
#define sys_trace_k_work_handler_exit(work) custom_trace_k_work_handler_exit(work)
&lt;/pre&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Then, implement &lt;code&gt;custom_trace_k_work_submit&lt;/code&gt;, &lt;code&gt;custom_trace_k_work_handler_entry&lt;/code&gt;, and &lt;code&gt;custom_trace_k_work_handler_exit&lt;/code&gt; functions to add logging, timestamps, or even direct telemetry outputs. This approach lets you track when work items are submitted and handled without modifying the SDK source.&amp;nbsp;Then, implement &lt;code&gt;custom_trace_k_work_submit&lt;/code&gt;, &lt;code&gt;custom_trace_k_work_handler_entry&lt;/code&gt;, and &lt;code&gt;custom_trace_k_work_handler_exit&lt;/code&gt; functions to add logging, timestamps, or even direct telemetry outputs. This approach lets you track when work items are submitted and handled without modifying the SDK source.&lt;/p&gt;
&lt;p&gt;You can, use the&amp;nbsp;&amp;nbsp;&lt;code&gt;CONFIG_TRACING_USER&lt;/code&gt; macro can be configured to enable user-defined trace events. Implementing &lt;code&gt;sys_trace&lt;/code&gt; functions directly for specific work queue operations can give you detailed control over individual queue item handling. This method requires configuring your build environment with &lt;code&gt;CONFIG_TRACING_USER&lt;/code&gt; enabled and defining custom &lt;code&gt;sys_trace&lt;/code&gt; calls within the user space application&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#ifdef CONFIG_TRACING_USER
void sys_trace_k_work_queue_user_defined_event(const k_work *work) {
    // Log or output trace for custom events, e.g., enqueue/dequeue times.
}
#endif
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Also, by enabling and configuring &lt;code&gt;CONFIG_TIMESLICING&lt;/code&gt;, you can also better understand CPU usage patterns across tasks. Combined with work queue tracing, this profiling can help you optimize task priorities and timings based on real-world CPU load patterns observed during testing.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Custom Tracing Implementation</title><link>https://devzone.nordicsemi.com/thread/508000?ContentTypeID=1</link><pubDate>Fri, 25 Oct 2024 20:10:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:46194b7f-6654-4e5a-9d29-8c87a08f8275</guid><dc:creator>timl2415</dc:creator><description>&lt;p&gt;In addition to the above it seems like there&amp;#39;s no tracing around the actual handling of work items in the work queue, only around the APIs that feed into the work queue externally (k_work_submit, k_work_cancel etc.). So even with a fully implemented tracing backend it would be difficult to see which work items are getting processed.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>