Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e492443a2c | ||
|
|
f1bebb08e9 | ||
|
|
f3af8712d2 | ||
|
|
67a2e19ac4 | ||
|
|
2de5047eb7 |
@@ -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.2",
|
||||||
|
|
||||||
// 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
|
||||||
|
|||||||
@@ -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"),
|
||||||
@@ -291,7 +320,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;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user