LinuxSystemInfo.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Runtime.Versioning;
  7. namespace Ryujinx.Common.SystemInfo
  8. {
  9. [SupportedOSPlatform("linux")]
  10. class LinuxSystemInfo : SystemInfo
  11. {
  12. internal LinuxSystemInfo()
  13. {
  14. string cpuName = GetCpuidCpuName();
  15. if (cpuName == null)
  16. {
  17. var cpuDict = new Dictionary<string, string>(StringComparer.Ordinal)
  18. {
  19. ["model name"] = null,
  20. ["Processor"] = null,
  21. ["Hardware"] = null,
  22. };
  23. ParseKeyValues("/proc/cpuinfo", cpuDict);
  24. cpuName = cpuDict["model name"] ?? cpuDict["Processor"] ?? cpuDict["Hardware"] ?? "Unknown";
  25. }
  26. var memDict = new Dictionary<string, string>(StringComparer.Ordinal)
  27. {
  28. ["MemTotal"] = null,
  29. ["MemAvailable"] = null,
  30. };
  31. ParseKeyValues("/proc/meminfo", memDict);
  32. // Entries are in KiB
  33. ulong.TryParse(memDict["MemTotal"]?.Split(' ')[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out ulong totalKiB);
  34. ulong.TryParse(memDict["MemAvailable"]?.Split(' ')[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out ulong availableKiB);
  35. CpuName = $"{cpuName} ; {LogicalCoreCount} logical";
  36. RamTotal = totalKiB * 1024;
  37. RamAvailable = availableKiB * 1024;
  38. }
  39. private static void ParseKeyValues(string filePath, Dictionary<string, string> itemDict)
  40. {
  41. if (!File.Exists(filePath))
  42. {
  43. Logger.Error?.Print(LogClass.Application, $"File \"{filePath}\" not found");
  44. return;
  45. }
  46. int count = itemDict.Count;
  47. using StreamReader file = new(filePath);
  48. string line;
  49. while ((line = file.ReadLine()) != null)
  50. {
  51. string[] kvPair = line.Split(':', 2, StringSplitOptions.TrimEntries);
  52. if (kvPair.Length < 2)
  53. {
  54. continue;
  55. }
  56. string key = kvPair[0];
  57. if (itemDict.TryGetValue(key, out string value) && value == null)
  58. {
  59. itemDict[key] = kvPair[1];
  60. if (--count <= 0)
  61. {
  62. break;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }