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