SystemInfo.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using System.Runtime.Intrinsics.X86;
  5. using System.Text;
  6. namespace Ryujinx.Common.SystemInfo
  7. {
  8. public class SystemInfo
  9. {
  10. public string OsDescription { get; protected set; }
  11. public string CpuName { get; protected set; }
  12. public ulong RamTotal { get; protected set; }
  13. public ulong RamAvailable { get; protected set; }
  14. protected static int LogicalCoreCount => Environment.ProcessorCount;
  15. protected SystemInfo()
  16. {
  17. OsDescription = $"{RuntimeInformation.OSDescription} ({RuntimeInformation.OSArchitecture})";
  18. CpuName = "Unknown";
  19. }
  20. private static string ToMiBString(ulong bytesValue) => (bytesValue == 0) ? "Unknown" : $"{bytesValue / 1024 / 1024} MiB";
  21. public void Print()
  22. {
  23. Logger.Notice.Print(LogClass.Application, $"Operating System: {OsDescription}");
  24. Logger.Notice.Print(LogClass.Application, $"CPU: {CpuName}");
  25. Logger.Notice.Print(LogClass.Application, $"RAM: Total {ToMiBString(RamTotal)} ; Available {ToMiBString(RamAvailable)}");
  26. }
  27. public static SystemInfo Gather()
  28. {
  29. if (OperatingSystem.IsWindows())
  30. {
  31. return new WindowsSystemInfo();
  32. }
  33. else if (OperatingSystem.IsLinux())
  34. {
  35. return new LinuxSystemInfo();
  36. }
  37. else if (OperatingSystem.IsMacOS())
  38. {
  39. return new MacOSSystemInfo();
  40. }
  41. else
  42. {
  43. Logger.Error?.Print(LogClass.Application, "SystemInfo unsupported on this platform");
  44. return new SystemInfo();
  45. }
  46. }
  47. // x86 exposes a 48 byte ASCII "CPU brand" string via CPUID leaves 0x80000002-0x80000004.
  48. internal static string GetCpuidCpuName()
  49. {
  50. if (!X86Base.IsSupported)
  51. {
  52. return null;
  53. }
  54. // Check if CPU supports the query
  55. if ((uint)X86Base.CpuId(unchecked((int)0x80000000), 0).Eax < 0x80000004)
  56. {
  57. return null;
  58. }
  59. int[] regs = new int[12];
  60. for (uint i = 0; i < 3; ++i)
  61. {
  62. (regs[4 * i], regs[4 * i + 1], regs[4 * i + 2], regs[4 * i + 3]) = X86Base.CpuId((int)(0x80000002 + i), 0);
  63. }
  64. string name = Encoding.ASCII.GetString(MemoryMarshal.Cast<int, byte>(regs)).Replace('\0', ' ').Trim();
  65. return string.IsNullOrEmpty(name) ? null : name;
  66. }
  67. }
  68. }