fix(device): macOS 补上 uptime / load / 可用内存

darwin 分支原来只取内存总量,其余一律 -1。改为调 sysctl(kern.boottime /
vm.loadavg) 与 vm_stat;可用内存按 free+inactive+speculative 算(只看 free
会把缓存当占用,数字小得吓人)。温度需要 SMC,保持 -1。
This commit is contained in:
2026-11-19 19:31:36 +08:00
parent 938a99cabe
commit 1e7f3e76ee
+86 -5
View File
@@ -2,15 +2,18 @@
package main package main
// 主机指标:Linux 走 /proc,macOS 只取内存总量与磁盘,其余给 -1(不支持的平台别编数据)。 // 主机指标:Linux 读 /proc,macOS 调 sysctl / vm_stat(health 调用不频繁,允许 fork)。
// 拿不到的指标给 -1,不编数据。
import ( import (
"context"
"os" "os"
"os/exec" "os/exec"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
"time"
) )
type healthInfo struct { type healthInfo struct {
@@ -64,12 +67,36 @@ func hostHealth() healthInfo {
} }
} }
case "darwin": case "darwin":
// macOS 没有 /proc;内存总量走 sysctl(调用很少,允许 fork 一次) // macOS 没有 /proc,走 sysctl / vm_stat;每次 health 只 fork 三四个进程,频率极低
if out, err := exec.Command("sysctl", "-n", "hw.memsize").Output(); err == nil { if v, err := sysctlValue("hw.memsize"); err == nil {
if v, err := strconv.ParseInt(strings.TrimSpace(string(out)), 10, 64); err == nil { if n, err := strconv.ParseInt(v, 10, 64); err == nil {
h.MemTotalMB = v / (1 << 20) h.MemTotalMB = n >> 20
} }
} }
// 开机时长:kern.boottime 形如 { sec = 1794298276, usec = 155984 } Tue Nov 10 …
if v, err := sysctlValue("kern.boottime"); err == nil {
if i := strings.Index(v, "sec = "); i >= 0 {
rest := v[i+6:]
if j := strings.IndexAny(rest, ",}"); j > 0 {
if sec, err := strconv.ParseInt(strings.TrimSpace(rest[:j]), 10, 64); err == nil {
h.HostUptimeSec = time.Now().Unix() - sec
}
}
}
}
// 负载:vm.loadavg 形如 { 1.11 1.40 1.59 }
if v, err := sysctlValue("vm.loadavg"); err == nil {
f := strings.FieldsFunc(v, func(r rune) bool { return r == '{' || r == '}' || r == ' ' })
if len(f) >= 3 {
h.Load1, _ = strconv.ParseFloat(f[0], 64)
h.Load5, _ = strconv.ParseFloat(f[1], 64)
h.Load15, _ = strconv.ParseFloat(f[2], 64)
}
}
// 可用内存 ≈ free + inactive + speculative:macOS 上只看 free 会小得吓人(大半是缓存)
if mb := darwinAvailableMB(); mb >= 0 {
h.MemFreeMB = mb
}
} }
var st syscall.Statfs_t var st syscall.Statfs_t
@@ -79,3 +106,57 @@ func hostHealth() healthInfo {
} }
return h return h
} }
// sysctlValue 读一个 sysctl 键,1s 超时兜底。
func sysctlValue(key string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
out, err := exec.CommandContext(ctx, "sysctl", "-n", key).Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
// darwinAvailableMB 解析 vm_stat,返回 free+inactive+speculative 的 MB 数;解析失败给 -1。
func darwinAvailableMB() int64 {
ctx, cancel := context.WithTimeout(context.Background(), 1200*time.Millisecond)
defer cancel()
out, err := exec.CommandContext(ctx, "vm_stat").Output()
if err != nil {
return -1
}
pageSize := int64(4096)
free, inactive, speculative := int64(-1), int64(0), int64(0)
for _, line := range strings.Split(string(out), "\n") {
if i := strings.Index(line, "page size of "); i >= 0 {
rest := line[i+len("page size of "):]
if j := strings.Index(rest, " bytes"); j > 0 {
if v, err := strconv.ParseInt(rest[:j], 10, 64); err == nil {
pageSize = v
}
}
continue
}
num := func(prefix string) (int64, bool) {
if !strings.HasPrefix(line, prefix) {
return 0, false
}
v, err := strconv.ParseInt(strings.TrimSuffix(strings.TrimSpace(line[len(prefix):]), "."), 10, 64)
return v, err == nil
}
if v, ok := num("Pages free:"); ok {
free = v
}
if v, ok := num("Pages inactive:"); ok {
inactive = v
}
if v, ok := num("Pages speculative:"); ok {
speculative = v
}
}
if free < 0 {
return -1
}
return (free + inactive + speculative) * pageSize / (1 << 20)
}