Skip to content

dax: add an instance manager#10688

Open
checkupup wants to merge 1 commit intothesofproject:mainfrom
checkupup:main_dax_user_priority
Open

dax: add an instance manager#10688
checkupup wants to merge 1 commit intothesofproject:mainfrom
checkupup:main_dax_user_priority

Conversation

@checkupup
Copy link
Copy Markdown
Contributor

@checkupup checkupup commented Apr 8, 2026

When multiple DAX components are running simultaneously, instance resources may be insufficient. In this case, DAX instance manager ensures that higher-priority DAX components are always granted instance resources. For DAX components that cannot obtain instance resources in a timely manner, dax_process will operate in pass-through mode.

@sofci
Copy link
Copy Markdown
Collaborator

sofci commented Apr 8, 2026

Can one of the admins verify this patch?

reply test this please to run this test once

@lgirdwood
Copy link
Copy Markdown
Member

test this please

Copy link
Copy Markdown
Member

@lgirdwood lgirdwood left a comment

Choose a reason for hiding this comment

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

@checkupup are you able to add more inline comments describing the flow/mechanism and log when we have to fallback to pass through mode. How would priority be set by users, i.e. using topology tuples would mean same dax/sof binaries could be used but with different priorities depending on target.

@checkupup checkupup force-pushed the main_dax_user_priority branch from 5af4619 to a50b365 Compare April 14, 2026 03:19
@checkupup
Copy link
Copy Markdown
Contributor Author

@checkupup are you able to add more inline comments describing the flow/mechanism and log when we have to fallback to pass through mode.

Sure, see my latest commit.

When multiple DAX components are running simultaneously, instance
resources may be insufficient. In this case, DAX instance manager
ensures that higher-priority DAX components are always granted
instance resources. For DAX components that cannot obtain instance
resources in a timely manner, `dax_process` will operate in
pass-through mode.

Signed-off-by: Jun Lai <jun.lai@dolby.com>
@checkupup checkupup force-pushed the main_dax_user_priority branch from a50b365 to 2b0800b Compare April 15, 2026 07:12
@checkupup checkupup changed the title dax: add a user priority mechanism dax: add an instance manager Apr 15, 2026
@checkupup
Copy link
Copy Markdown
Contributor Author

To maintain code readability, I have extracted the entire instance resource handling logic into the file dax_instance_manager.c

@checkupup checkupup marked this pull request as ready for review April 20, 2026 03:32
@checkupup
Copy link
Copy Markdown
Contributor Author

Code Logic:

Assume that instance 1 is created first.

While Instance 1 is running, Instance 2 requests to be created. If Instance 2 has a higher priority than Instance 1, then during Instance 1's process loop, check_priority_l evaluates to false, causing Instance 1 to voluntarily release its held memory resources. Instance 2 checks for available memory on each iteration of its process loop; if memory is available, it proceeds to create the instance. Since Instance 1 has already released its memory, Instance 2 is successfully created. At this point, Instance 1 operates in passthrough mode.

Once Instance 2 finishes its work and releases the memory, if Instance 1 is still active, check_priority_l in its process loop evaluates to true. Instance 1 then re-requests the memory resources. Upon successfully acquiring them, it exits passthrough mode and resumes actual audio effect processing.

Note: The speaker instance has a higher priority than the headphone instance.

Copy link
Copy Markdown
Collaborator

@kv2019i kv2019i left a comment

Choose a reason for hiding this comment

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

Some comments inline.

if (inst_mgr.users[i].id != DAX_USER_ID_INVALID &&
!inst_mgr.users[i].allocated) {
inst_mgr.available_inst_cnt++;
}
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.

Hmm, is this a bit racy? Consider: A single DAX component registers, its first establish_instance call hits ENOMEM (e.g., tight memory). The code sets available_inst_cnt = 1 (one non-allocated user). On the next process loop, check_priority_l sees allocated_cnt=0 < available_inst_cnt=1, returns true, and the allocation is retried — on every call.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, thanks, @kv2019i , the condition shoud be:

if (inst_mgr.users[i].id != DAX_USER_ID_INVALID && inst_mgr.users[i].allocated) {...}

That's my bad.

*
* @return 0 on success, negative error code on failure.
*/
int register_user(struct processing_module *mod);
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.

Consider adding a "dax_" prefix. This is added to global namespace and "register_user" could coflict with other software modules.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, good ideas

*
* @return 0 on success, negative error code on failure.
*/
int unregister_user(struct processing_module *mod);
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.

Ditto here.

*
* @return 0 on success, negative error code on failure.
*/
int check_and_update_instance(struct processing_module *mod);
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.

And here.

*/
int32_t available_inst_cnt;

struct k_spinlock lock;
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.

This is a more future-proofing type of comment and not blocking for the PR. We have multiple efforts ongoing to support running some of the audio modules in Zephyr user-space threads on future SOF platforms. We don't yet have documentation on the requirements to audio modules on this. But, but, spinlocks won't be available to user threads and currently the audio code is almost free of spinlock use (except for KPB and now potentially DAX). One practical alternative is mutexes (rtos/lock.h , wraps native Zephyr locks). But just a consideration, as said, this is purely a future-oriented comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since a lock is acquired on every loop process, the context-switching overhead introduced by a mutex may not be efficient.

ret = establish_instance(mod);
else
ret = destroy_instance(mod);

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.

As the lock is dropped here, it would be good to add a comment how the code handles the scenario where priority circumstances have changed. E.g. while running the blocking "establisb_instance()", the "has_priority" priority has become stale. Not necessarily a problem, but good to add a comment you've considered this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The comment will be added:

/* This section of logic do not need lock protection because
 * it does not involve access to any shared resources. `has_priority`
 * may be stale when using in if condition but it is OK, `update_allocation_l`
 * will synchronize instance state which will be used in the next check phase.
 */

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