Replace System.Managment with Microsoft.Management.Infrastructure. System.Managment has known issue that does not function without complicated COMWraper implementation while publishing trimmed. Will also use MMI as a way to get RAM stats.

This commit is contained in:
sunshineinabox 2023-07-21 18:33:56 -07:00
parent 83ce40af2e
commit 3c45020dca
4 changed files with 50 additions and 56 deletions

View file

@ -21,6 +21,7 @@
<PackageVersion Include="LibHac" Version="0.18.0" /> <PackageVersion Include="LibHac" Version="0.18.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" /> <PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" /> <PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" />
<PackageVersion Include="Microsoft.Management.Infrastructure" Version="2.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.6.3" /> <PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageVersion Include="Microsoft.IO.RecyclableMemoryStream" Version="2.3.2" /> <PackageVersion Include="Microsoft.IO.RecyclableMemoryStream" Version="2.3.2" />
<PackageVersion Include="MsgPack.Cli" Version="1.0.1" /> <PackageVersion Include="MsgPack.Cli" Version="1.0.1" />
@ -46,7 +47,6 @@
<PackageVersion Include="System.Drawing.Common" Version="7.0.0" /> <PackageVersion Include="System.Drawing.Common" Version="7.0.0" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="6.31.0" /> <PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="6.31.0" />
<PackageVersion Include="System.IO.Hashing" Version="7.0.0" /> <PackageVersion Include="System.IO.Hashing" Version="7.0.0" />
<PackageVersion Include="System.Management" Version="7.0.2" />
<PackageVersion Include="UnicornEngine.Unicorn" Version="2.0.2-rc1-fb78016" /> <PackageVersion Include="UnicornEngine.Unicorn" Version="2.0.2-rc1-fb78016" />
<PackageVersion Include="XamlNameReferenceGenerator" Version="1.6.1" /> <PackageVersion Include="XamlNameReferenceGenerator" Version="1.6.1" />
</ItemGroup> </ItemGroup>

View file

@ -8,8 +8,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" /> <PackageReference Include="Microsoft.IO.RecyclableMemoryStream" />
<PackageReference Include="Microsoft.Management.Infrastructure" />
<PackageReference Include="MsgPack.Cli" /> <PackageReference Include="MsgPack.Cli" />
<PackageReference Include="System.Management" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -1,4 +1,5 @@
using Ryujinx.Common.Logging; using Microsoft.Management.Infrastructure;
using Ryujinx.Common.Logging;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
@ -92,9 +93,11 @@ namespace Ryujinx.Common.SystemInfo
{ {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{ {
foreach (var item in new System.Management.ManagementObjectSearcher("Select NumberOfCores from Win32_Processor").Get()) var session = CimSession.Create(null);
var instances = session.QueryInstances(@"root\cimv2", "WQL", "SELECT NumberOfCores FROM Win32_Processor");
foreach (CimInstance instance in instances)
{ {
coreCount = int.Parse(item["NumberOfCores"].ToString()); coreCount = int.Parse(instance.CimInstanceProperties["NumberOfCores"].Value.ToString());
} }
} }
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))

View file

@ -1,7 +1,8 @@
using Microsoft.Management.Infrastructure;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using System; using System;
using System.Management; using System.Collections.Generic;
using System.Runtime.InteropServices; using System.Linq;
using System.Runtime.Versioning; using System.Runtime.Versioning;
namespace Ryujinx.Common.SystemInfo namespace Ryujinx.Common.SystemInfo
@ -11,77 +12,67 @@ namespace Ryujinx.Common.SystemInfo
{ {
internal WindowsSystemInfo() internal WindowsSystemInfo()
{ {
CpuName = $"{GetCpuidCpuName() ?? GetCpuNameWMI()} ; {GetPhysicalCoreCount()} physical ; {LogicalCoreCount} logical"; // WMI is very slow CpuName = $"{GetCpuidCpuName() ?? GetCpuNameMMI()} ; {GetPhysicalCoreCount()} physical ; {LogicalCoreCount} logical"; // WMI is very slow
(RamTotal, RamAvailable) = GetMemoryStats(); (RamTotal, RamAvailable) = GetMemoryStatsMMI();
} }
private static (ulong Total, ulong Available) GetMemoryStats() private static string GetCpuNameMMI()
{ {
MemoryStatusEx memStatus = new(); var cpuObjs = GetMMIObjects(@"root\cimv2", "SELECT * FROM Win32_Processor");
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 static string GetCpuNameWMI()
{
ManagementObjectCollection cpuObjs = GetWMIObjects("root\\CIMV2", "SELECT * FROM Win32_Processor");
if (cpuObjs != null) if (cpuObjs != null)
{ {
foreach (var cpuObj in cpuObjs) foreach (var cpuObj in cpuObjs)
{ {
return cpuObj["Name"].ToString().Trim(); return cpuObj.CimInstanceProperties["Name"].Value.ToString().Trim();
} }
} }
return Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER").Trim(); return Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER")?.Trim();
} }
[StructLayout(LayoutKind.Sequential)] private static (ulong TotalPhys, ulong AvailPhys) GetMemoryStatsMMI()
private struct MemoryStatusEx
{ {
public uint Length; var memObjs = GetMMIObjects(@"root\cimv2", "SELECT * FROM Win32_ComputerSystem");
public uint MemoryLoad; var memObjs2 = GetMMIObjects(@"root\cimv2", "SELECT * FROM Win32_OperatingSystem");
public ulong TotalPhys;
public ulong AvailPhys;
public ulong TotalPageFile;
public ulong AvailPageFile;
public ulong TotalVirtual;
public ulong AvailVirtual;
public ulong AvailExtendedVirtual;
public MemoryStatusEx() ulong TotalPhys = 0;
ulong AvailPhys = 0;
if (memObjs != null)
{ {
Length = (uint)Marshal.SizeOf<MemoryStatusEx>(); foreach (var memObj in memObjs)
{
TotalPhys = (ulong)memObj.CimInstanceProperties["TotalPhysicalMemory"].Value;
} }
} }
[LibraryImport("kernel32.dll", SetLastError = true)] if (memObjs2 != null)
[return: MarshalAs(UnmanagedType.Bool)] {
private static partial bool GlobalMemoryStatusEx(ref MemoryStatusEx lpBuffer); foreach (var memObj2 in memObjs2)
{
AvailPhys = (ulong)memObj2.CimInstanceProperties["FreePhysicalMemory"].Value*1000;
}
}
private static ManagementObjectCollection GetWMIObjects(string scope, string query) return (TotalPhys, AvailPhys);
}
private static IEnumerable<CimInstance> GetMMIObjects(string namespaceName, string query)
{ {
try try
{ {
return new ManagementObjectSearcher(scope, query).Get(); using (CimSession session = CimSession.Create(null))
}
catch (PlatformNotSupportedException ex)
{ {
Logger.Error?.Print(LogClass.Application, $"WMI isn't available : {ex.Message}"); return session.QueryInstances(namespaceName, "WQL", query).ToList();
} }
catch (COMException ex) }
catch (CimException ex)
{ {
Logger.Error?.Print(LogClass.Application, $"WMI isn't available : {ex.Message}"); Logger.Error?.Print(LogClass.Application, $"MMI isn't available : {ex.Message}");
} }
return null; return Enumerable.Empty<CimInstance>();
} }
} }
} }