11 Commits

Author SHA1 Message Date
utox39
3b3c29e22d Merge pull request #8 from utox39/fix-linux-gpu/fix-memory-leak
Fix linux gpu/fix memory leak
2025-07-29 17:08:10 +02:00
utox39
a866952f86 build: bump version to 0.7.4 2025-07-29 17:06:40 +02:00
utox39
5d7bdd8041 fix(linux-gpu): fix memory leak 2025-07-29 17:05:30 +02:00
utox39
635dfe00db Merge pull request #7 from utox39/fix-linux-gpu/fix-double-free
Fix linux gpu/fix double free
2025-07-28 23:40:56 +02:00
utox39
064e8e9597 build: bump version to 0.7.3 2025-07-28 23:39:40 +02:00
utox39
785915d833 fix(linux-gpu): fix double free 2025-07-28 23:38:36 +02:00
utox39
e492443a2c build: bump version to 0.7.2 2025-07-27 14:54:36 +02:00
utox39
f1bebb08e9 Merge pull request #6 from utox39/fix/fix-wrong-used-size
fix: fix wrong used size calculation
2025-07-27 14:52:37 +02:00
utox39
f3af8712d2 Merge pull request #5 from utox39/fix-linux/fix-cpuinfo_max_freq
fix(linux-cpu): fix FileNotFound error
2025-07-27 14:50:18 +02:00
utox39
67a2e19ac4 fix: fix wrong used size calculation 2025-07-27 14:42:52 +02:00
utox39
2de5047eb7 fix(linux-cpu): fix FileNotFound error 2025-07-22 22:32:48 +02:00
3 changed files with 54 additions and 18 deletions

View File

@@ -10,7 +10,7 @@
// This is a [Semantic Version](https://semver.org/). // This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication. // In a future version of Zig it will be used for package deduplication.
.version = "0.7.0", .version = "0.7.4",
// Together with name, this represents a globally unique package // Together with name, this represents a globally unique package
// identifier. This field is generated by the Zig toolchain when the // identifier. This field is generated by the Zig toolchain when the

View File

@@ -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,22 +61,50 @@ 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, " ");
}
}
if ((model_name != null) and (cpu_max_freq_mhz_str != null)) {
break;
} }
} }
// Reads /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq var cpu_max_freq: f32 = 0.0;
const cpuinfo_max_freq_path = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
var file2 = try std.fs.cwd().openFile(cpuinfo_max_freq_path, .{});
defer file2.close();
const cpuinfo_max_freq_data = try file2.readToEndAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(cpuinfo_max_freq_data);
// Parsing /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq const cpuinfo_max_freq_path = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
const trimmed = std.mem.trim(u8, cpuinfo_max_freq_data, " \n\r"); var cmf_exists: bool = true;
const cpu_max_freq_khz: f32 = try std.fmt.parseFloat(f32, trimmed);
const cpu_max_freq: f32 = cpu_max_freq_khz / 1_000_000; // 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, .{});
defer file2.close();
const cpuinfo_max_freq_data = try file2.readToEndAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(cpuinfo_max_freq_data);
// Parsing /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
const trimmed = std.mem.trim(u8, cpuinfo_max_freq_data, " \n\r");
const cpu_max_freq_khz: f32 = try std.fmt.parseFloat(f32, trimmed);
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,16 @@ 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 maybe_parsed_gpu_name = try parseGpuName(allocator, gpu_name);
var parsed_gpu_name: []u8 = undefined;
if (maybe_parsed_gpu_name != null) {
allocator.free(gpu_name);
parsed_gpu_name = maybe_parsed_gpu_name.?;
} else {
parsed_gpu_name = 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 +174,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 +197,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 +327,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;

View File

@@ -418,7 +418,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;