From 826d427d8acd2b78dbe16cce01b1a4791bba2e76 Mon Sep 17 00:00:00 2001 From: utox39 Date: Wed, 14 May 2025 12:49:20 +0200 Subject: [PATCH] feat(macos): add intel cpu frequency info --- src/macos/hardware.zig | 43 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/macos/hardware.zig b/src/macos/hardware.zig index 602691f..9f8dff1 100644 --- a/src/macos/hardware.zig +++ b/src/macos/hardware.zig @@ -85,15 +85,41 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo { return error.FailedToGetPhysicalCpuInfo; } - // TODO: add cpu frequency for Intel + // Get cpu architecture + const arch: []u8 = try getCpuArch(allocator); + defer allocator.free(arch); - const cpu_freq_mhz = try getCpuFreqAppleSilicon(); + var cpu_freq_mhz: f64 = 0.0; + + if (std.mem.eql(u8, arch, "arm64")) { + cpu_freq_mhz = try getCpuFreqAppleSilicon(); + } else if (std.mem.eql(u8, arch, "x86_64")) { + cpu_freq_mhz = getCpuFreqIntel(); + } const cpu_freq_ghz = @floor(cpu_freq_mhz) / 1000; return CpuInfo{ .cpu_name = cpu_name, .cpu_cores = n_cpu, .cpu_max_freq = cpu_freq_ghz }; } +fn getCpuArch(allocator: std.mem.Allocator) ![]u8 { + var size: usize = 0; + + if (c_sysctl.sysctlbyname("hw.machine", null, &size, null, 0) != 0) { + return error.SysctlbynameFailed; + } + + const machine: []u8 = try allocator.alloc(u8, size); + + if (c_sysctl.sysctlbyname("hw.machine", machine.ptr, &size, null, 0) != 0) { + return error.SysctlbynameFailed; + } + + defer allocator.free(machine); + + return allocator.dupe(u8, std.mem.sliceTo(machine, 0)); +} + fn getCpuFreqAppleSilicon() !f64 { // https://github.com/fastfetch-cli/fastfetch/blob/dev/src/detection/cpu/cpu_apple.c @@ -166,6 +192,19 @@ fn getCpuFreqAppleSilicon() !f64 { } } +// TODO: test on intel machine +pub fn getCpuFreqIntel() f64 { + var freq: f64 = 0; + var size: usize = @sizeOf(f64); + + if (c_sysctl.sysctlbyname("hw.cpufrequency_max", &freq, &size, null, 0) != 0) { + return 0.0; + } + + // Converts from Hz to MHz + return freq / 1_000_000.0; +} + pub fn getGpuInfo(allocator: std.mem.Allocator) !GpuInfo { // TODO: add support for non-Apple Silicon Macs