-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPUInfoGetter.cs
70 lines (68 loc) · 2.48 KB
/
GPUInfoGetter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.IO;
using System.Collections.Generic;
using PCIIdentificationResolver;
namespace gtkfetch
{
class GPU
{
public ushort VendorID;
public ushort DeviceID;
public ushort SubsystemVendorID;
public ushort SubsystemDeviceID;
/// <summary> Constructor for creating a GPU instance </summary>
public GPU(ushort vid, ushort did, ushort svid, ushort sdid)
{
VendorID = vid;
DeviceID = did;
SubsystemVendorID = svid;
SubsystemDeviceID = sdid;
}
}
class GPUInfoGetter
{
public static List<GPU> GPUs = new List<GPU>();
/// <summary> Returns all GPUs in system </summary>
public static List<string> GetGPUInfo()
{
List<string> cards = new List<string>();
foreach(GPU gpu in GPUs)
{
try
{
PCISubSystem card = PCIIdentificationDatabase.GetSubSystem(gpu.VendorID, gpu.DeviceID, gpu.SubsystemVendorID, gpu.SubsystemDeviceID);
cards.Add(card.SubSystemName);
}
catch (System.NullReferenceException)
{
try
{
PCIDevice cardalt = PCIIdentificationDatabase.GetDevice(gpu.VendorID, gpu.DeviceID);
cards.Add(cardalt.DeviceName.ToString());
}
catch
{
cards.Add("unknown");
}
}
}
return cards;
}
/// <summary> Finds all GPUs and creates a GPU instance for each </summary>
public static void GetGPUInstances()
{
var devices = Directory.EnumerateDirectories("/sys/bus/pci/devices/");
foreach(string directory in devices)
{
if(File.Exists($"{directory}/boot_vga"))
{
ushort vid = Convert.ToUInt16(FileReader.ReadLine($"{directory}/vendor"), 16);
ushort did = Convert.ToUInt16(FileReader.ReadLine($"{directory}/device"), 16);
ushort svid = Convert.ToUInt16(FileReader.ReadLine($"{directory}/subsystem_vendor"), 16);
ushort sdid = Convert.ToUInt16(FileReader.ReadLine($"{directory}/subsystem_device"), 16);
GPUs.Add(new GPU(vid, did, svid, sdid));
}
}
}
}
}