From ea101e3ae2f7fe9418ba50bb2bd3e9b7cc016f34 Mon Sep 17 00:00:00 2001 From: Candy Date: Fri, 28 Aug 2020 15:03:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Linux=E4=B8=8B=EF=BC=8C?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E7=BA=BF=E7=A8=8B=E4=BD=BF=E7=94=A8=E6=8C=87?= =?UTF-8?q?=E5=AE=9ACPU?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Cdy.Tag.Common/Common/ThreadHelper.cs | 48 +++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/Common/Cdy.Tag.Common/Common/ThreadHelper.cs b/Common/Cdy.Tag.Common/Common/ThreadHelper.cs index 01e829b..c79e4aa 100644 --- a/Common/Cdy.Tag.Common/Common/ThreadHelper.cs +++ b/Common/Cdy.Tag.Common/Common/ThreadHelper.cs @@ -61,7 +61,7 @@ namespace Cdy.Tag } else if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - + SetThreadAffinityMaskLinux(MaskFromIds(cpus)); } else { @@ -81,7 +81,9 @@ namespace Cdy.Tag } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - + //ulong re = 0; + //LinuxNative.pthread_getaffinity_np(LinuxNative.pthread_self(), ref re); + //return (uint)re; } else { @@ -115,6 +117,16 @@ namespace Cdy.Tag //return SetThreadAffinityMask(Win32Native.GetCurrentThread(), mask); } + + /// + /// + /// + /// + public static void SetThreadAffinityMaskLinux(ulong mask) + { + LinuxNative.pthread_setaffinity_np(LinuxNative.pthread_self(),8,ref mask); + } + /// /// Masks from ids. /// @@ -166,6 +178,38 @@ namespace Cdy.Tag #endregion ...Interfaces... } + public static class LinuxNative + { + private const string pthread = "libpthread.so.0"; + + /// + /// + /// + /// + /// + /// + /// + [DllImport(pthread, CharSet = CharSet.Auto, SetLastError = true)] + public static extern int pthread_setaffinity_np(IntPtr threadHandel, int cpusize,ref UInt64 cpuset); + + /// + /// + /// + /// + /// + /// + [DllImport(pthread, CharSet = CharSet.Auto, SetLastError = true)] + public static extern int pthread_getaffinity_np(IntPtr threadHandel,ref UInt64 cpuset); + + /// + /// + /// + /// + [DllImport(pthread, CharSet = CharSet.Auto, SetLastError = true)] + public static extern IntPtr pthread_self(); + } + + /// /// Win32Native Class /// -- GitLab