From 64e38a4d40fe9ce81ff3200dbd1ec7cf0b45453d Mon Sep 17 00:00:00 2001
From: PhyxionNL <7643972+PhyxionNL@users.noreply.github.com>
Date: Tue, 14 Mar 2023 17:01:03 +0100
Subject: [PATCH] Cleanup thread affinity and support > 64 threads.
---
.../Hardware/Cpu/CpuGroup.cs | 10 +--
LibreHardwareMonitorLib/Hardware/OpCode.cs | 3 +-
.../Hardware/ThreadAffinity.cs | 62 ++++---------------
3 files changed, 16 insertions(+), 59 deletions(-)
diff --git a/LibreHardwareMonitorLib/Hardware/Cpu/CpuGroup.cs b/LibreHardwareMonitorLib/Hardware/Cpu/CpuGroup.cs
index 5a4af0d..ed1d2da 100644
--- a/LibreHardwareMonitorLib/Hardware/Cpu/CpuGroup.cs
+++ b/LibreHardwareMonitorLib/Hardware/Cpu/CpuGroup.cs
@@ -127,21 +127,17 @@ internal class CpuGroup : IGroup
for (int i = 0; i < ThreadAffinity.ProcessorGroupCount; i++)
{
- for (int j = 0; j < 64; j++)
+ for (int j = 0; j < 192; j++)
{
try
{
- if (!ThreadAffinity.IsValid(GroupAffinity.Single((ushort)i, j)))
- continue;
-
var cpuid = CpuId.Get(i, j);
if (cpuid != null)
threads.Add(cpuid);
}
catch (ArgumentOutOfRangeException)
{
- // All cores found.
- break;
+ // Continue...
}
}
}
@@ -211,4 +207,4 @@ internal class CpuGroup : IGroup
r.AppendLine();
}
}
-}
\ No newline at end of file
+}
diff --git a/LibreHardwareMonitorLib/Hardware/OpCode.cs b/LibreHardwareMonitorLib/Hardware/OpCode.cs
index 432a9e4..8d5fd5c 100644
--- a/LibreHardwareMonitorLib/Hardware/OpCode.cs
+++ b/LibreHardwareMonitorLib/Hardware/OpCode.cs
@@ -217,7 +217,6 @@ internal static class OpCode
else
{
rdTscCode = Rdtsc64;
-
cpuidCode = Software.OperatingSystem.IsUnix ? CpuId64Linux : CpuId64Windows;
}
@@ -299,4 +298,4 @@ internal static class OpCode
ThreadAffinity.Set(previousAffinity);
return true;
}
-}
\ No newline at end of file
+}
diff --git a/LibreHardwareMonitorLib/Hardware/ThreadAffinity.cs b/LibreHardwareMonitorLib/Hardware/ThreadAffinity.cs
index eebe967..4c2ebac 100644
--- a/LibreHardwareMonitorLib/Hardware/ThreadAffinity.cs
+++ b/LibreHardwareMonitorLib/Hardware/ThreadAffinity.cs
@@ -27,34 +27,6 @@ internal static class ThreadAffinity
///
public static int ProcessorGroupCount { get; }
- ///
- /// Returns true if the is valid.
- ///
- /// The affinity.
- /// true if the specified affinity is valid; otherwise, false.
- public static bool IsValid(GroupAffinity affinity)
- {
- if (Software.OperatingSystem.IsUnix)
- {
- if (affinity.Group > 0)
- return false;
-
- return true;
- }
-
- UIntPtr uIntPtrMask;
- try
- {
- uIntPtrMask = (UIntPtr)affinity.Mask;
- }
- catch (OverflowException)
- {
- return false;
- }
-
- return true;
- }
-
///
/// Sets the processor group affinity for the current thread.
///
@@ -75,33 +47,23 @@ internal static class ThreadAffinity
return GroupAffinity.Undefined;
ulong mask = affinity.Mask;
- if (LibC.sched_setaffinity(0, (IntPtr)8, ref mask) != 0)
- return GroupAffinity.Undefined;
-
- return new GroupAffinity(0, result);
+ return LibC.sched_setaffinity(0, (IntPtr)8, ref mask) != 0
+ ? GroupAffinity.Undefined
+ : new GroupAffinity(0, result);
}
- UIntPtr uIntPtrMask;
- try
- {
- uIntPtrMask = (UIntPtr)affinity.Mask;
- }
- catch (OverflowException)
- {
+ ulong maxValue = IntPtr.Size == 8 ? ulong.MaxValue : uint.MaxValue;
+ if (affinity.Mask > maxValue)
throw new ArgumentOutOfRangeException(nameof(affinity));
- }
- var groupAffinity = new Kernel32.GROUP_AFFINITY { Group = affinity.Group, Mask = uIntPtrMask };
+ var groupAffinity = new Kernel32.GROUP_AFFINITY { Group = affinity.Group, Mask = (UIntPtr)affinity.Mask };
IntPtr currentThread = Kernel32.GetCurrentThread();
- if (Kernel32.SetThreadGroupAffinity(currentThread,
- ref groupAffinity,
- out Kernel32.GROUP_AFFINITY previousGroupAffinity))
- {
- return new GroupAffinity(previousGroupAffinity.Group, (ulong)previousGroupAffinity.Mask);
- }
-
- return GroupAffinity.Undefined;
+ return Kernel32.SetThreadGroupAffinity(currentThread,
+ ref groupAffinity,
+ out Kernel32.GROUP_AFFINITY previousGroupAffinity)
+ ? new GroupAffinity(previousGroupAffinity.Group, (ulong)previousGroupAffinity.Mask)
+ : GroupAffinity.Undefined;
}
-}
\ No newline at end of file
+}