fix(overview): report the CPU clock speed

This commit is contained in:
Sanaei
2026-12-03 19:22:47 +03:30
parent eae4ff8e8f
commit 952f293b4e
7 changed files with 97 additions and 10 deletions
+5 -1
View File
@@ -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'),
+5
View File
@@ -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 {
+20 -1
View File
@@ -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.
+59
View File
@@ -2,11 +2,40 @@
package sys
/*
#cgo LDFLAGS: -framework IOKit -framework CoreFoundation
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
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
}
+2
View File
@@ -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.
+2
View File
@@ -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" {
+4 -8
View File
@@ -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: