From a15eb00ca7d86503366330ebaaed1dec189fcff7 Mon Sep 17 00:00:00 2001 From: sunshineinabox Date: Mon, 31 Jul 2023 14:54:59 -0700 Subject: [PATCH] Do not use WMI for memory stats revert to original method --- .../SystemInfo/WindowsSystemInfo.cs | 51 ++++++++++++------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs b/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs index e85dc93f0..cda8aef64 100644 --- a/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs +++ b/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs @@ -4,11 +4,12 @@ using WmiLight; using Ryujinx.Common.Logging; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; namespace Ryujinx.Common.SystemInfo { [SupportedOSPlatform("windows")] - class WindowsSystemInfo : SystemInfo + partial class WindowsSystemInfo : SystemInfo { internal WindowsSystemInfo() { @@ -17,8 +18,8 @@ namespace Ryujinx.Common.SystemInfo using (WmiConnection connection = new()) { (string cpuName, PhysicalCores) = GetCpuStatsLight(connection); - CpuName = $"{cpuName} ; {PhysicalCores} physical ; {LogicalCoreCount} logical"; - (RamTotal, RamAvailable) = GetMemoryStatsWmiLight(connection); + CpuName = $"{cpuName ?? GetCpuidCpuName()} ; {PhysicalCores} physical ; {LogicalCoreCount} logical"; + (RamTotal, RamAvailable) = GetMemoryStats(); } } catch (Exception ex) @@ -26,7 +27,19 @@ namespace Ryujinx.Common.SystemInfo Logger.Error?.Print(LogClass.Application, $"WmiLight isn't available : {ex.Message}"); } } + + private static (ulong Total, ulong Available) GetMemoryStats() + { + MemoryStatusEx memStatus = new(); + if (GlobalMemoryStatusEx(ref memStatus)) + { + return (memStatus.TotalPhys, memStatus.AvailPhys); // Bytes + } + Logger.Error?.Print(LogClass.Application, $"GlobalMemoryStatusEx failed. Error {Marshal.GetLastWin32Error():X}"); + + return (0, 0); + } private (string cpuName, int physicalCores) GetCpuStatsLight(WmiConnection connection) { @@ -44,26 +57,30 @@ namespace Ryujinx.Common.SystemInfo return (cpuName, physicalCores); } - - private (ulong TotalPhys, ulong AvailPhys) GetMemoryStatsWmiLight(WmiConnection connection) + + [StructLayout(LayoutKind.Sequential)] + private struct MemoryStatusEx { - ulong TotalPhys = 0; - ulong AvailPhys = 0; + public uint Length; + public uint MemoryLoad; + public ulong TotalPhys; + public ulong AvailPhys; + public ulong TotalPageFile; + public ulong AvailPageFile; + public ulong TotalVirtual; + public ulong AvailVirtual; + public ulong AvailExtendedVirtual; - foreach (WmiObject memObj in GetWmiObjects(connection, "SELECT TotalPhysicalMemory FROM Win32_ComputerSystem")) + public MemoryStatusEx() { - TotalPhys = Convert.ToUInt64(memObj["TotalPhysicalMemory"]); + Length = (uint)Marshal.SizeOf(); } - - foreach (WmiObject memObj2 in GetWmiObjects(connection, "SELECT FreePhysicalMemory FROM Win32_OperatingSystem")) - { - AvailPhys = Convert.ToUInt64(memObj2["FreePhysicalMemory"]) * 1000; - } - - return (TotalPhys, AvailPhys); } - + [LibraryImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + private static partial bool GlobalMemoryStatusEx(ref MemoryStatusEx lpBuffer); + private IEnumerable GetWmiObjects(WmiConnection connection, string query) { try