|
|
|
@@ -51,6 +51,7 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
|
|
|
|
|
|
|
|
|
|
|
|
// Parsing /proc/cpuinfo
|
|
|
|
// Parsing /proc/cpuinfo
|
|
|
|
var model_name: ?[]const u8 = null;
|
|
|
|
var model_name: ?[]const u8 = null;
|
|
|
|
|
|
|
|
var cpu_max_freq_mhz_str: ?[]const u8 = null;
|
|
|
|
|
|
|
|
|
|
|
|
var lines = std.mem.splitScalar(u8, cpuinfo_data, '\n');
|
|
|
|
var lines = std.mem.splitScalar(u8, cpuinfo_data, '\n');
|
|
|
|
while (lines.next()) |line| {
|
|
|
|
while (lines.next()) |line| {
|
|
|
|
@@ -60,14 +61,36 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
|
|
|
|
_ = parts.next(); // discards the key
|
|
|
|
_ = parts.next(); // discards the key
|
|
|
|
if (parts.next()) |value| {
|
|
|
|
if (parts.next()) |value| {
|
|
|
|
model_name = std.mem.trim(u8, value, " ");
|
|
|
|
model_name = std.mem.trim(u8, value, " ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (std.mem.startsWith(u8, trimmed, "cpu MHz") and cpu_max_freq_mhz_str == null) {
|
|
|
|
|
|
|
|
var parts = std.mem.splitScalar(u8, trimmed, ':');
|
|
|
|
|
|
|
|
_ = parts.next(); // discard the key
|
|
|
|
|
|
|
|
if (parts.next()) |value| {
|
|
|
|
|
|
|
|
cpu_max_freq_mhz_str = std.mem.trim(u8, value, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reads /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
|
|
|
|
if ((model_name != null) and (cpu_max_freq_mhz_str != null)) {
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var cpu_max_freq: f32 = 0.0;
|
|
|
|
|
|
|
|
|
|
|
|
const cpuinfo_max_freq_path = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
|
|
|
|
const cpuinfo_max_freq_path = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
|
|
|
|
|
|
|
|
var cmf_exists: bool = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Checks if /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq exists
|
|
|
|
|
|
|
|
_ = std.fs.accessAbsolute(cpuinfo_max_freq_path, .{ .mode = .read_only }) catch |err| {
|
|
|
|
|
|
|
|
if (err == std.posix.AccessError.FileNotFound) {
|
|
|
|
|
|
|
|
cmf_exists = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (cmf_exists) {
|
|
|
|
|
|
|
|
// Reads /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
|
|
|
|
var file2 = try std.fs.cwd().openFile(cpuinfo_max_freq_path, .{});
|
|
|
|
var file2 = try std.fs.cwd().openFile(cpuinfo_max_freq_path, .{});
|
|
|
|
|
|
|
|
|
|
|
|
defer file2.close();
|
|
|
|
defer file2.close();
|
|
|
|
const cpuinfo_max_freq_data = try file2.readToEndAlloc(allocator, std.math.maxInt(usize));
|
|
|
|
const cpuinfo_max_freq_data = try file2.readToEndAlloc(allocator, std.math.maxInt(usize));
|
|
|
|
defer allocator.free(cpuinfo_max_freq_data);
|
|
|
|
defer allocator.free(cpuinfo_max_freq_data);
|
|
|
|
@@ -75,7 +98,13 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
|
|
|
|
// Parsing /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
|
|
|
|
// Parsing /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
|
|
|
|
const trimmed = std.mem.trim(u8, cpuinfo_max_freq_data, " \n\r");
|
|
|
|
const trimmed = std.mem.trim(u8, cpuinfo_max_freq_data, " \n\r");
|
|
|
|
const cpu_max_freq_khz: f32 = try std.fmt.parseFloat(f32, trimmed);
|
|
|
|
const cpu_max_freq_khz: f32 = try std.fmt.parseFloat(f32, trimmed);
|
|
|
|
const cpu_max_freq: f32 = cpu_max_freq_khz / 1_000_000;
|
|
|
|
cpu_max_freq = cpu_max_freq_khz / 1_000_000;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (cpu_max_freq_mhz_str != null) {
|
|
|
|
|
|
|
|
const cpu_max_freq_mhz: f32 = try std.fmt.parseFloat(f32, cpu_max_freq_mhz_str.?);
|
|
|
|
|
|
|
|
cpu_max_freq = cpu_max_freq_mhz / 1_000;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return CpuInfo{
|
|
|
|
return CpuInfo{
|
|
|
|
.cpu_name = try allocator.dupe(u8, model_name orelse "Unknown"),
|
|
|
|
.cpu_name = try allocator.dupe(u8, model_name orelse "Unknown"),
|
|
|
|
@@ -115,9 +144,8 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.ArrayList(GpuInfo) {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const gpu_name = try allocator.dupe(u8, std.mem.span(name));
|
|
|
|
const gpu_name = try allocator.dupe(u8, std.mem.span(name));
|
|
|
|
defer allocator.free(gpu_name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const parsed_gpu_name = try parseGpuName(allocator, gpu_name);
|
|
|
|
const parsed_gpu_name = try parseGpuName(allocator, gpu_name) orelse gpu_name;
|
|
|
|
|
|
|
|
|
|
|
|
try gpu_info_list.append(GpuInfo{
|
|
|
|
try gpu_info_list.append(GpuInfo{
|
|
|
|
.gpu_name = parsed_gpu_name,
|
|
|
|
.gpu_name = parsed_gpu_name,
|
|
|
|
@@ -138,7 +166,7 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.ArrayList(GpuInfo) {
|
|
|
|
return gpu_info_list;
|
|
|
|
return gpu_info_list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn parseGpuName(allocator: std.mem.Allocator, name: []u8) ![]u8 {
|
|
|
|
fn parseGpuName(allocator: std.mem.Allocator, name: []u8) !?[]u8 {
|
|
|
|
// NOTE: for references: https://github.com/pciutils/pciutils/blob/master/pci.ids
|
|
|
|
// NOTE: for references: https://github.com/pciutils/pciutils/blob/master/pci.ids
|
|
|
|
|
|
|
|
|
|
|
|
if (std.mem.startsWith(u8, name, "Advanced Micro Devices, Inc. [AMD/ATI]")) {
|
|
|
|
if (std.mem.startsWith(u8, name, "Advanced Micro Devices, Inc. [AMD/ATI]")) {
|
|
|
|
@@ -161,7 +189,7 @@ fn parseGpuName(allocator: std.mem.Allocator, name: []u8) ![]u8 {
|
|
|
|
return parsed_gpu_name;
|
|
|
|
return parsed_gpu_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return name;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo {
|
|
|
|
pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo {
|
|
|
|
@@ -291,7 +319,7 @@ pub fn getDiskSize(disk_path: []const u8) !DiskInfo {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const total_size = stat.f_blocks * stat.f_frsize;
|
|
|
|
const total_size = stat.f_blocks * stat.f_frsize;
|
|
|
|
const free_size = stat.f_bavail * stat.f_frsize;
|
|
|
|
const free_size = stat.f_bfree * stat.f_frsize;
|
|
|
|
const used_size = total_size - free_size;
|
|
|
|
const used_size = total_size - free_size;
|
|
|
|
|
|
|
|
|
|
|
|
const used_size_percentage = (used_size * 100) / total_size;
|
|
|
|
const used_size_percentage = (used_size * 100) / total_size;
|
|
|
|
|