LinuxSystemInfo.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Runtime.Versioning;
  6. using Ryujinx.Common.Logging;
  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 KB
  33. ulong.TryParse(memDict["MemTotal"]?.Split(' ')[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out ulong totalKB);
  34. ulong.TryParse(memDict["MemAvailable"]?.Split(' ')[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out ulong availableKB);
  35. CpuName = $"{cpuName} ; {LogicalCoreCount} logical";
  36. RamTotal = totalKB * 1024;
  37. RamAvailable = availableKB * 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 StreamReader(filePath))
  48. {
  49. string line;
  50. while ((line = file.ReadLine()) != null)
  51. {
  52. string[] kvPair = line.Split(':', 2, StringSplitOptions.TrimEntries);
  53. if (kvPair.Length < 2) continue;
  54. string key = kvPair[0];
  55. if (itemDict.TryGetValue(key, out string value) && value == null)
  56. {
  57. itemDict[key] = kvPair[1];
  58. if (--count <= 0) break;
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }