feat(linux): add gpu name info

This commit is contained in:
utox39
2025-07-11 14:16:35 +02:00
parent b4ec1b6d2d
commit 7993cab4df

View File

@@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const c_unistd = @cImport(@cInclude("unistd.h")); const c_unistd = @cImport(@cInclude("unistd.h"));
const c_statvfs = @cImport(@cInclude("sys/statvfs.h")); const c_statvfs = @cImport(@cInclude("sys/statvfs.h"));
const c_libpci = @cImport(@cInclude("pci/pci.h"));
/// Struct representing CPU informations /// Struct representing CPU informations
pub const CpuInfo = struct { pub const CpuInfo = struct {
@@ -9,6 +10,13 @@ pub const CpuInfo = struct {
cpu_max_freq: f32, cpu_max_freq: f32,
}; };
/// Struct representing GPU informations
pub const GpuInfo = struct {
gpu_name: []u8,
gpu_cores: i32,
gpu_freq: f64,
};
/// Struct representing RAM usage informations /// Struct representing RAM usage informations
pub const RamInfo = struct { pub const RamInfo = struct {
ram_size: f64, ram_size: f64,
@@ -76,6 +84,45 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
}; };
} }
pub fn getGpuInfo(allocator: std.mem.Allocator) !GpuInfo {
var gpu_info = GpuInfo{
.gpu_name = try allocator.dupe(u8, "Unknown"),
.gpu_cores = 0,
.gpu_freq = 0.0,
};
const display_controller = 0x03;
const pacc = c_libpci.pci_alloc();
defer c_libpci.pci_cleanup(pacc);
c_libpci.pci_init(pacc);
c_libpci.pci_scan_bus(pacc);
var devices = pacc.*.devices;
while (devices != null) : (devices = devices.*.next) {
// TODO: handle return value
_ = c_libpci.pci_fill_info(devices, c_libpci.PCI_FILL_IDENT | c_libpci.PCI_FILL_CLASS);
if ((devices.*.device_class >> 8) == display_controller) {
var name_buffer: [256]u8 = undefined;
const name = c_libpci.pci_lookup_name(
pacc,
&name_buffer,
name_buffer.len,
c_libpci.PCI_LOOKUP_VENDOR | c_libpci.PCI_LOOKUP_DEVICE,
devices.*.vendor_id,
devices.*.device_id,
);
allocator.free(gpu_info.gpu_name);
gpu_info.gpu_name = try allocator.dupe(u8, std.mem.span(name));
}
}
return gpu_info;
}
pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo { pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo {
// Reads /proc/meminfo // Reads /proc/meminfo
const meminfo_path = "/proc/meminfo"; const meminfo_path = "/proc/meminfo";