diff --git a/frontend/src/pages/index/IndexPage.tsx b/frontend/src/pages/index/IndexPage.tsx index 9311889c3..98760a2e1 100644 --- a/frontend/src/pages/index/IndexPage.tsx +++ b/frontend/src/pages/index/IndexPage.tsx @@ -268,7 +268,11 @@ export default function IndexPage() { detail={ status.resourceScope === 'container' ? `${status.logicalPro} vCPU` - : `${CPUFormatter.cpuCoreFormat(status.cpuCores)} / ${status.logicalPro}T · ${CPUFormatter.cpuSpeedFormat(status.cpuSpeedMhz)}` + : CPUFormatter.cpuSpecFormat( + status.cpuCores, + status.logicalPro, + status.cpuSpeedMhz, + ) } footLeft={metricFoot( t('pages.index.avg'), diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index 622514ccc..24b29656c 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -763,6 +763,11 @@ export class CPUFormatter { static cpuCoreFormat(cores: number): string { return cores === 1 ? '1 Core' : cores + ' Cores'; } + + static cpuSpecFormat(cores: number, threads: number, speedMhz: number): string { + const spec = `${CPUFormatter.cpuCoreFormat(cores)} / ${threads}T`; + return speedMhz > 0 ? `${spec} · ${CPUFormatter.cpuSpeedFormat(speedMhz)}` : spec; + } } export class TimeFormatter { diff --git a/internal/util/sys/cpu_times.go b/internal/util/sys/cpu_times.go index 18194397a..39200f565 100644 --- a/internal/util/sys/cpu_times.go +++ b/internal/util/sys/cpu_times.go @@ -1,6 +1,25 @@ package sys -import "github.com/shirou/gopsutil/v4/cpu" +import ( + "errors" + + "github.com/shirou/gopsutil/v4/cpu" +) + +// CPUSpeedMHz reports 0 when the platform exposes no clock speed. +func CPUSpeedMHz() (float64, error) { + infos, err := cpu.Info() + if err != nil { + return 0, err + } + if len(infos) == 0 { + return 0, errors.New("could not find cpu info") + } + if infos[0].Mhz > 0 { + return infos[0].Mhz, nil + } + return platformCPUSpeedMHz() +} // CPUBusyPercent is the busy share of CPU time between two cumulative samples, // counting iowait as busy; ok is false when no time elapsed or the counters reset. diff --git a/internal/util/sys/sys_darwin.go b/internal/util/sys/sys_darwin.go index 218228213..501b8c7e6 100644 --- a/internal/util/sys/sys_darwin.go +++ b/internal/util/sys/sys_darwin.go @@ -2,11 +2,40 @@ package sys +/* +#cgo LDFLAGS: -framework IOKit -framework CoreFoundation +#include +#include + +static CFDataRef copyPCoreVoltageStates(void) { + io_iterator_t devices; + if (IOServiceGetMatchingServices(MACH_PORT_NULL, IOServiceMatching("AppleARMIODevice"), &devices) != KERN_SUCCESS) { + return NULL; + } + CFDataRef states = NULL; + io_object_t device; + while (states == NULL && (device = IOIteratorNext(devices)) != 0) { + CFTypeRef value = IORegistryEntryCreateCFProperty(device, CFSTR("voltage-states5-sram"), kCFAllocatorDefault, 0); + if (value != NULL && CFGetTypeID(value) == CFDataGetTypeID()) { + states = value; + } else if (value != NULL) { + CFRelease(value); + } + IOObjectRelease(device); + } + IOObjectRelease(devices); + return states; +} +*/ +import "C" + import ( "encoding/binary" + "errors" "fmt" "sync" "syscall" + "unsafe" "github.com/shirou/gopsutil/v4/net" "golang.org/x/sys/unix" @@ -89,3 +118,33 @@ func CPUPercentRaw() (float64, error) { } return pct, nil } + +// gopsutil reads the table only from the "pmgr" node; M6 moved it to +// "pmgr-child", so every AppleARMIODevice is searched for it here. +func platformCPUSpeedMHz() (float64, error) { + states := C.copyPCoreVoltageStates() + if states == 0 { + return 0, errors.New("no AppleARMIODevice carries voltage-states5-sram") + } + defer C.CFRelease(C.CFTypeRef(states)) + return appleSiliconMaxMHz(C.GoBytes(unsafe.Pointer(C.CFDataGetBytePtr(states)), C.int(C.CFDataGetLength(states)))) +} + +// No core clocks below 100 MHz, so a smaller frequency word is in kHz. +const appleSiliconKHzCeiling = 100_000_000 + +// appleSiliconMaxMHz reads a voltage-states table of little-endian +// (frequency, millivolt) pairs: Hz through M3, kHz from M4 on. +func appleSiliconMaxMHz(states []byte) (float64, error) { + var fastest uint64 + for pair := 0; pair+8 <= len(states); pair += 8 { + fastest = max(fastest, uint64(binary.LittleEndian.Uint32(states[pair:]))) + } + if fastest == 0 { + return 0, errors.New("voltage-states table holds no frequency") + } + if fastest < appleSiliconKHzCeiling { + fastest *= 1000 + } + return float64(fastest) / 1_000_000, nil +} diff --git a/internal/util/sys/sys_linux.go b/internal/util/sys/sys_linux.go index 4cca0fe57..e17683947 100644 --- a/internal/util/sys/sys_linux.go +++ b/internal/util/sys/sys_linux.go @@ -16,6 +16,8 @@ import ( var SIGUSR1 = syscall.SIGUSR1 +func platformCPUSpeedMHz() (float64, error) { return 0, nil } + // countConnections returns the number of entries in a /proc/net/{tcp,udp}[6] // file. Returns 0 if the file is absent (e.g. /proc/net/tcp6 when IPv6 is // disabled) and excludes the column header line. diff --git a/internal/util/sys/sys_windows.go b/internal/util/sys/sys_windows.go index a6a39ef2f..b81e68cd9 100644 --- a/internal/util/sys/sys_windows.go +++ b/internal/util/sys/sys_windows.go @@ -14,6 +14,8 @@ import ( var SIGUSR1 = syscall.Signal(0) +func platformCPUSpeedMHz() (float64, error) { return 0, nil } + // GetConnectionCount returns the number of active connections for the specified protocol ("tcp" or "udp"). func GetConnectionCount(proto string) (int, error) { if proto != "tcp" && proto != "udp" { diff --git a/internal/web/service/server.go b/internal/web/service/server.go index e78963123..6e286b8e5 100644 --- a/internal/web/service/server.go +++ b/internal/web/service/server.go @@ -695,17 +695,13 @@ func (s *ServerService) GetStatus(lastStatus *Status) *Status { done := make(chan struct{}) go func() { defer close(done) - cpuInfos, err := cpu.Info() + mhz, err := sys.CPUSpeedMHz() if err != nil { - logger.Warning("get cpu info failed:", err) + logger.Warning("get cpu speed failed:", err) return } - if len(cpuInfos) > 0 { - s.cachedCpuSpeedMhz = cpuInfos[0].Mhz - status.CpuSpeedMhz = s.cachedCpuSpeedMhz - } else { - logger.Warning("could not find cpu info") - } + s.cachedCpuSpeedMhz = mhz + status.CpuSpeedMhz = mhz }() select { case <-done: