Saturday, May 2, 2015

Setting Affinity for Thread in C# is it required to include process affinity first?

I'm setting the affinity of a managed thread. Before I do that I first get the system and process affinity using the GetProcessAffinityMask Win32 call:

[DllImport("kernel32.dll", SetLastError = true)]
 public static extern bool GetProcessAffinityMask(
 IntPtr handleToProcess, 
 ref UIntPtr processAffinityMask, 
 ref UIntPtr systemAffinityMask);

Normally what I would do is:

actualAffinity = desiredAffinity & processAffinityMask & systemAffinityMask

This ensures the affinity mask is valid and within the cores defined by the process and system. Then I would use that value (actualAffinity) to set the affinity of the thread (between Thread.BeginThreadAffinity(); and Thread.EndThreadAffinity(); of course)

My question(s) are:

Do I really need to include the processAffinityMask? What if I want to have the thread run outside of the logical cores defined for the process (not the system). Is that just bad form and I should always make sure I'm in the defined affinity boundry of the process in C#? Reason I ask is I made a gui to allow an admin to set graphically, if the process is using the affinity 0x3 (CPU 0+ CPU1)  and they select CPU3 (0x8) to run a task on should that be allowed? I can definitely see the value in ensuring the affinity is bitwise & with system affinity to ensure its valid, but process affinity I was not sure about. Plus if a user uses task manager to set the affinity of a process to CPU0 only. Then they want to run a task on CPU 1 I cannot see why I would not allow that, but then I wonder why they would set the process affinity to only 0, seems like the threads that are being manually managed is better to be within the process affinity boundry that outside of it .Just wondering if anyone had any insight on this or best practice advice, or maybe just say I'm crazy don't micromanage and why not let the user put the admin but the thread where its needed and pay not attention to the process affinity and only the system affinity.

No comments:

Post a Comment