SystemInfo.cs 2.6 KB

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