示例#1
0
 internal static void SetCustomThreadAffinity(PoolOfThreads.PooledThread pooledThread)
 {
     ChangeThreadAffinityWithRetries(pooledThread.CurrentProcess, currentAffinity =>
     {
         SetCustomThreadAffinityInternal(pooledThread, currentAffinity);
     });
 }
示例#2
0
        internal static bool ResetThreadAffinity(PoolOfThreads.PooledThread pooledThread)
        {
            _customAffinityThreads.TryRemove(pooledThread);

            return(ChangeThreadAffinityWithRetries(pooledThread.CurrentProcess, currentAffinity =>
            {
                SetThreadAffinity(pooledThread, currentAffinity);
            }));
        }
示例#3
0
        private static void SetThreadAffinity(PoolOfThreads.PooledThread pooledThread, long affinity)
        {
            if (PlatformDetails.RunningOnPosix == false)
            {
                // windows
                pooledThread.CurrentProcessThread.ProcessorAffinity = new IntPtr(affinity);
                return;
            }

            if (PlatformDetails.RunningOnLinux)
            {
                var ulongAffinity = (ulong)affinity;
                var result        = Syscall.sched_setaffinity((int)pooledThread.CurrentUnmanagedThreadId, new IntPtr(sizeof(ulong)), ref ulongAffinity);
                if (result != 0)
                {
                    throw new InvalidOperationException(
                              $"Failed to set affinity for thread: {pooledThread.CurrentUnmanagedThreadId}, " +
                              $"affinity: {affinity}, result: {result}, error: {Marshal.GetLastWin32Error()}");
                }
            }
        }
示例#4
0
        private static void SetCustomThreadAffinityInternal(PoolOfThreads.PooledThread pooledThread, long currentAffinity)
        {
            var numberOfCoresToReduce = pooledThread.NumberOfCoresToReduce;
            var threadMask            = pooledThread.ThreadMask;

            if (numberOfCoresToReduce <= 0 && threadMask == null)
            {
                _customAffinityThreads.TryRemove(pooledThread);
                return;
            }

            _customAffinityThreads.TryAdd(pooledThread);

            // we can't reduce the number of cores to a zero or negative number, in this case, just use the processor cores
            if (threadMask == null && Bits.NumberOfSetBits(currentAffinity) <= numberOfCoresToReduce)
            {
                SetThreadAffinity(pooledThread, currentAffinity);
                return;
            }

            if (threadMask == null)
            {
                for (int i = 0; i < numberOfCoresToReduce; i++)
                {
                    // remove the N least significant bits
                    // we do that because it is typical that the first cores (0, 1, etc) are more
                    // powerful and we want to keep them for other things, such as request processing
                    currentAffinity &= currentAffinity - 1;
                }
            }
            else
            {
                currentAffinity &= threadMask.Value;
            }

            SetThreadAffinity(pooledThread, currentAffinity);
        }