Skip to content

Trace - make clear that trace context is unused with Zephyr - #11102

Open
lyakh wants to merge 3 commits into
thesofproject:mainfrom
lyakh:trace
Open

Trace - make clear that trace context is unused with Zephyr#11102
lyakh wants to merge 3 commits into
thesofproject:mainfrom
lyakh:trace

Conversation

@lyakh

@lyakh lyakh commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

We have a lot of code that uses trace context like tr_dbg() / tr_err() calls. Changing all those locations would be a very big change. This PR takes a middle ground: it adds comments to trace context declarations where they are harmless and makes harmless those, that end up generating needless code under Zephyr. Yes, made with AI.

lyakh added 3 commits August 17, 2026 15:55
Under Zephyr with CONFIG_ZEPHYR_LOG, tr_err()/tr_warn()/tr_info()/tr_dbg()
expand directly to LOG_ERR()/LOG_WRN()/LOG_INF()/LOG_DBG() and completely
ignore their first (tr_ctx) argument. A plain, otherwise-unreferenced
DECLARE_TR_CTX() static is therefore dead and the linker discards it.

However, a `struct comp_driver`/`struct dai_driver` instance whose `.tctx`
field points at such a context keeps it alive, because the driver struct
itself is always referenced (component/DAI driver registration). This adds
a few bytes of otherwise-dead .data per affected driver and forces the
firmware to retain values that can never be read on Zephyr.

Guard the `.tctx = &foo_tr,` initializers (and the equivalent runtime
`drv->tctx = &lib_manager_tr;` assignment) with #ifndef __ZEPHYR__ so the
now fully-unreferenced trace contexts can be garbage-collected by the
linker, same as any other unused DECLARE_TR_CTX(). Fix the shared
DECLARE_MODULE_ADAPTER() macro once, which covers ~40 IPC4 module_adapter
components: MODULE_ADAPTER_TCTX_INIT(tr) resolves to &(tr) normally and to
NULL under Zephyr, so `.tctx = MODULE_ADAPTER_TCTX_INIT(tr),` still parses
as a valid (harmless) initializer either way.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
pipeline_new() and buffer_new() unconditionally memcpy_s() the static
pipe_tr/buffer_tr DECLARE_TR_CTX() objects into the new pipeline's/buffer's
embedded tr_ctx field. On Zephyr this copy is pure overhead: comp_init()
in component.h already skips the equivalent copy for comp_dev with
#ifndef __ZEPHYR__, but these two sites were missed.

buffer_new()'s copy was already conditional on \!CONFIG_SOF_USERSPACE_LL;
simplify that to \!__ZEPHYR__ since CONFIG_SOF_USERSPACE_LL can only be
defined in Zephyr builds.

Also, since &pipe_tr/&buffer_tr were the only references to those
objects, guarding the copy lets the linker garbage-collect them on
Zephyr, same as ipc_tr and friends.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
Under Zephyr with CONFIG_ZEPHYR_LOG, tr_err()/tr_warn()/tr_info()/tr_dbg()
expand directly to LOG_ERR()/LOG_WRN()/LOG_INF()/LOG_DBG() and never look
at their tr_ctx argument. None of these remaining DECLARE_TR_CTX()
instances are referenced by anything other than tr_*() calls in the same
file (verified: no .tctx/->tctx assignment picks up their address), so
they are already dead code eliminated by the linker on Zephyr builds -
this commit only documents that fact, it changes no generated code.

(dai-legacy.c, host-legacy.c and the platform/library/schedule/*.c files
are excluded: they are only ever built for non-Zephyr, non-hardware
configurations - testbench/cmocka/legacy XTOS - so the comment would not
apply to them.)

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@kv2019i kv2019i left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling, but I think this needs iteration still...

Comment thread src/audio/base_fw.c
@@ -41,6 +41,7 @@
LOG_MODULE_REGISTER(basefw, CONFIG_SOF_LOG_LEVEL);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"srop trace context"?

Comment thread src/audio/base_fw.c

static const struct comp_driver comp_basefw = {
.uid = SOF_RT_UUID(basefw_uuid),
#ifndef __ZEPHYR__

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be reversed? I mean these are only needed in LIBRARY builds now (or are they even needed there)? Or maybe that's not accurate enough, but maybe "#ifdef CONFIG_TRACE" (i.e. a build really using sof-logger with dictionaries).

And for Zephyr, this doesn't cover the case where you build with Zephyr but disable CONFIG_ZEPHYR_LOG. I think these cases should be prevented somehow.

@@ -35,6 +35,7 @@ LOG_MODULE_REGISTER(pipe, CONFIG_SOF_LOG_LEVEL);

SOF_DEFINE_REG_UUID(pipe);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's with the "!CONFIG_SOF_USERSPACE_LL" in the commit message?

Comment thread src/audio/aria/aria.c

#else

/* unused with Zephyr, generates no output */

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused if not CONFIG_TRACE, not really related to Zephyr...

@abonislawski

Copy link
Copy Markdown
Member

This adds a lot of duplicated comments for relatively little value (and wasted tokens for future agents). Is it worth repeating the same comment everywhere? Could we add it once to the definition of DECLARE_TR_CTX instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants