From e59da65d543b1db9389ddb244b31309709e5cb02 Mon Sep 17 00:00:00 2001 From: sunshineinabox Date: Tue, 1 Aug 2023 11:03:52 -0700 Subject: [PATCH] Use winapi to get physical core count and revert back to WMI as fall back for CPU name --- Directory.Packages.props | 2 +- src/Ryujinx.Ava/Ryujinx.Ava.csproj | 1 - src/Ryujinx.Common/Ryujinx.Common.csproj | 2 +- src/Ryujinx.Common/SystemInfo/SystemInfo.cs | 25 ++-- .../SystemInfo/WindowsSystemInfo.cs | 123 ++++++++++++------ .../Ryujinx.Headless.SDL2.csproj | 1 - src/Ryujinx/Ryujinx.csproj | 1 - 7 files changed, 97 insertions(+), 58 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 3d84dcbda..bc740afcf 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -46,8 +46,8 @@ + - \ No newline at end of file diff --git a/src/Ryujinx.Ava/Ryujinx.Ava.csproj b/src/Ryujinx.Ava/Ryujinx.Ava.csproj index 3996621b2..1fac5400e 100644 --- a/src/Ryujinx.Ava/Ryujinx.Ava.csproj +++ b/src/Ryujinx.Ava/Ryujinx.Ava.csproj @@ -20,7 +20,6 @@ true false true - true partial diff --git a/src/Ryujinx.Common/Ryujinx.Common.csproj b/src/Ryujinx.Common/Ryujinx.Common.csproj index 434e1d153..c02b11e0c 100644 --- a/src/Ryujinx.Common/Ryujinx.Common.csproj +++ b/src/Ryujinx.Common/Ryujinx.Common.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/Ryujinx.Common/SystemInfo/SystemInfo.cs b/src/Ryujinx.Common/SystemInfo/SystemInfo.cs index 760eaf21f..daa4dd43d 100644 --- a/src/Ryujinx.Common/SystemInfo/SystemInfo.cs +++ b/src/Ryujinx.Common/SystemInfo/SystemInfo.cs @@ -6,7 +6,6 @@ using System.Linq; using System.Runtime.InteropServices; using System.Runtime.Intrinsics.X86; using System.Text; -using WmiLight; namespace Ryujinx.Common.SystemInfo { @@ -89,7 +88,7 @@ namespace Ryujinx.Common.SystemInfo } int coreCount = Environment.ProcessorCount; - + try { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) @@ -109,16 +108,16 @@ namespace Ryujinx.Common.SystemInfo else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { using (var process = new Process - { - StartInfo = new ProcessStartInfo - { - FileName = "sysctl", - Arguments = "-n hw.physicalcpu", - UseShellExecute = false, - RedirectStandardOutput = true, - CreateNoWindow = true, - } - }) + { + StartInfo = new ProcessStartInfo + { + FileName = "sysctl", + Arguments = "-n hw.physicalcpu", + UseShellExecute = false, + RedirectStandardOutput = true, + CreateNoWindow = true, + } + }) { process.Start(); coreCount = int.Parse(process.StandardOutput.ReadToEnd()); @@ -128,7 +127,7 @@ namespace Ryujinx.Common.SystemInfo } catch (Exception ex) { - Logger.Error?.Print(LogClass.Application,$"An error occurred while trying to get the physical core count: {ex.Message}"); + Logger.Error?.Print(LogClass.Application, $"An error occurred while trying to get the physical core count: {ex.Message}"); } _cachedPhysicalCoreCount = coreCount; diff --git a/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs b/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs index cda8aef64..f14792551 100644 --- a/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs +++ b/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs @@ -1,10 +1,8 @@ -using System; -using System.Runtime.Versioning; -using WmiLight; using Ryujinx.Common.Logging; -using System.Collections.Generic; -using System.Linq; +using System; +using System.Management; using System.Runtime.InteropServices; +using System.Runtime.Versioning; namespace Ryujinx.Common.SystemInfo { @@ -13,21 +11,57 @@ namespace Ryujinx.Common.SystemInfo { internal WindowsSystemInfo() { - try + CpuName = $"{GetCpuidCpuName() ?? GetCpuNameWMI()} ; {GetPhysicalCoreCount()} physical ; {LogicalCoreCount} logical"; + (RamTotal, RamAvailable) = GetMemoryStats(); + } + + private static string GetCpuNameWMI() + { + ManagementObjectCollection cpuObjs = GetWMIObjects("root\\CIMV2", "SELECT Name FROM Win32_Processor"); + + if (cpuObjs != null) { - using (WmiConnection connection = new()) + foreach (var cpuObj in cpuObjs) { - (string cpuName, PhysicalCores) = GetCpuStatsLight(connection); - CpuName = $"{cpuName ?? GetCpuidCpuName()} ; {PhysicalCores} physical ; {LogicalCoreCount} logical"; - (RamTotal, RamAvailable) = GetMemoryStats(); + return cpuObj["Name"].ToString().Trim(); } } - catch (Exception ex) - { - Logger.Error?.Print(LogClass.Application, $"WmiLight isn't available : {ex.Message}"); - } + + return Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER").Trim(); } - + + private static new int GetPhysicalCoreCount() + { + uint buffSize = 0; + GetLogicalProcessorInformation(IntPtr.Zero, ref buffSize); + IntPtr buffer = Marshal.AllocHGlobal((int)buffSize); + bool success = GetLogicalProcessorInformation(buffer, ref buffSize); + if (!success) + { + Marshal.FreeHGlobal(buffer); + return LogicalCoreCount; + } + + int physicalCores = 0; + long pos = buffer.ToInt64(); + int size = Marshal.SizeOf(typeof(SystemLogicalProcessorInformation)); + for (long offset = 0; offset + size <= buffSize; offset += size) + { + IntPtr current = new IntPtr(pos + offset); + SystemLogicalProcessorInformation info = Marshal.PtrToStructure(current); + + if (info.Relationship == LogicalProcessorRelationship.RelationProcessorCore) + { + physicalCores++; + } + } + + Marshal.FreeHGlobal(buffer); + + return physicalCores; + } + + private static (ulong Total, ulong Available) GetMemoryStats() { MemoryStatusEx memStatus = new(); @@ -41,23 +75,6 @@ namespace Ryujinx.Common.SystemInfo return (0, 0); } - private (string cpuName, int physicalCores) GetCpuStatsLight(WmiConnection connection) - { - string cpuName = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER")?.Trim(); - int physicalCores = LogicalCoreCount; - foreach (WmiObject cpuObj in GetWmiObjects(connection, "SELECT Name FROM Win32_Processor")) - { - cpuName = cpuObj["Name"].ToString().Trim(); - } - - foreach (WmiObject cpuObj in GetWmiObjects(connection, "SELECT NumberOfCores FROM Win32_Processor")) - { - physicalCores = Convert.ToInt32(cpuObj["NumberOfCores"]); - } - - return (cpuName, physicalCores); - } - [StructLayout(LayoutKind.Sequential)] private struct MemoryStatusEx { @@ -80,20 +97,46 @@ namespace Ryujinx.Common.SystemInfo [LibraryImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static partial bool GlobalMemoryStatusEx(ref MemoryStatusEx lpBuffer); - - private IEnumerable GetWmiObjects(WmiConnection connection, string query) + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool GetLogicalProcessorInformation(IntPtr buffer, ref uint returnLength); + + [StructLayout(LayoutKind.Sequential)] + public struct SystemLogicalProcessorInformation + { + public UIntPtr ProcessorMask; + public LogicalProcessorRelationship Relationship; + public ProcessorInformationUnion ProcessorInformation; + } + + [StructLayout(LayoutKind.Explicit)] + public struct ProcessorInformationUnion + { + [FieldOffset(8)] + private UInt64 Reserved2; + } + + public enum LogicalProcessorRelationship + { + RelationProcessorCore, + } + + private static ManagementObjectCollection GetWMIObjects(string scope, string query) { try { - return connection.CreateQuery(query).ToList(); + return new ManagementObjectSearcher(scope, query).Get(); } - catch (Exception ex) + catch (PlatformNotSupportedException ex) { - Logger.Error?.Print(LogClass.Application, $"WmiLight isn't available : {ex.Message}"); + Logger.Error?.Print(LogClass.Application, $"WMI isn't available : {ex.Message}"); + } + catch (COMException ex) + { + Logger.Error?.Print(LogClass.Application, $"WMI isn't available : {ex.Message}"); } - return Enumerable.Empty(); + return null; } - } -} +} \ No newline at end of file diff --git a/src/Ryujinx.Headless.SDL2/Ryujinx.Headless.SDL2.csproj b/src/Ryujinx.Headless.SDL2/Ryujinx.Headless.SDL2.csproj index 6d4b88a47..d2585c563 100644 --- a/src/Ryujinx.Headless.SDL2/Ryujinx.Headless.SDL2.csproj +++ b/src/Ryujinx.Headless.SDL2/Ryujinx.Headless.SDL2.csproj @@ -67,7 +67,6 @@ true true - true partial \ No newline at end of file diff --git a/src/Ryujinx/Ryujinx.csproj b/src/Ryujinx/Ryujinx.csproj index 8fa3e13cf..cf4435e57 100644 --- a/src/Ryujinx/Ryujinx.csproj +++ b/src/Ryujinx/Ryujinx.csproj @@ -16,7 +16,6 @@ true false true - true partial