Audiokinetic's Community Q&A is the forum where users can ask and answer questions within the Wwise and Strata communities. If you would like to get an answer from Audiokinetic's Technical support team, make sure you use the Support Tickets page.

Unity PS4 how to force thread affinity

0 votes
I'm working on PS4 and during profiling it's noted that the wwise threads occupy core #0, I want to prevent this happening.

Finding documentation is sparse, to say the least and it's not documented really at all in the wwise documentation I've been able to find. So far, I've got this:

        AkPlatformInitSettings platformSettings = new AkPlatformInitSettings();
        AkSoundEngine.GetDefaultPlatformInitSettings(platformSettings);
        uint allCoresExcept0 = 0xfffffffe;
        platformSettings.threadLEngine.dwAffinityMask = allCoresExcept0;
        platformSettings.threadMonitor.dwAffinityMask = allCoresExcept0;
        platformSettings.threadOutputMgr.dwAffinityMask = allCoresExcept0;
        platformSettings.threadBankManager.dwAffinityMask = allCoresExcept0;

The whole platformSettings parameter is only read from the engine, how do I get it to be applied before initialization?
asked Oct 8, 2019 in General Discussion by Brian W. (100 points)

1 Answer

0 votes
> Finding documentation is sparse, to say the least and it's not documented really at all in the wwise documentation

The documentation is fine any further detail would only serve to teach people how thread affinity works in addition to where to specify the mask for each thread and is therefore outside the scope of the Wwise SDK documentation.

> I've been able to find. So far, I've got this:

That code looks fine and the mask is correct - although as a matter of taste you might consider ~0x1 to be clearer - what you are missing is to specify these settings in the call to AkSoundEngine.Init.

Also note that the stream manager's devices create threads. If you wish to ensure that all the Wwise created threads do not run on cpu0 specify the affinity mask here also.
answered Oct 9, 2019 by Dan M. (2,660 points)
Actually, it would have to be ~(uint)0x01 :) I picked the bitmask, just for preference right now.
"any further detail would only serve to teach people how thread affinity works". Isn't that the point of documentation?


For future reference, here is my fix.

AkWwiseInitializationSettings.cs, inside InitializeSoundEngine()

    public static bool InitializeSoundEngine()
    {
#if UNITY_EDITOR
        Instance.ActiveSettingsHash = GetHashOfActiveSettings();
        Instance.ActiveSettingsHaveChanged = true;
#endif

        AkInitializationSettings settings = ActivePlatformSettings.AkInitializationSettings;
        uint allCoresExcept0 = ~(uint)1;
        settings.platformSettings.threadLEngine.dwAffinityMask &= allCoresExcept0;
        settings.platformSettings.threadMonitor.dwAffinityMask &= allCoresExcept0;
        settings.platformSettings.threadOutputMgr.dwAffinityMask &= allCoresExcept0;
        settings.platformSettings.threadBankManager.dwAffinityMask &= allCoresExcept0;

        if (AkSoundEngine.Init(settings) != AKRESULT.AK_Success)
> Actually, it would have to be ~(uint)0x01 :)

Actually, no it wouldn't.

https://godbolt.org/z/SkRHnF
https://en.cppreference.com/w/c/language/integer_constant
Dan, when I try the statement "uint allCoresExcept0 = ~1", I get an error from the compiler saying "Constant value '-2' cannot be converted to uint".

Anyway, Problem solved. Thanks for the help.
...