修正npu使用率的计算错误

This commit is contained in:
tian 2026-05-12 10:12:53 +08:00
parent 6a3b9f2c99
commit d4d35ba447
4 changed files with 61 additions and 18 deletions

View File

@ -120,6 +120,11 @@ func (s *Server) handleMetrics(w http.ResponseWriter, r *http.Request) {
} else { } else {
resp["npu"] = map[string]any{"error": err.Error()} resp["npu"] = map[string]any{"error": err.Error()}
} }
if temp, err := metrics.ReadTemperature(); err == nil {
resp["temperature"] = temp
} else {
resp["temperature"] = map[string]any{"error": err.Error()}
}
media := map[string]any{"supported": s.proc != nil && s.proc.Enabled()} media := map[string]any{"supported": s.proc != nil && s.proc.Enabled()}
if s.proc == nil || !s.proc.Enabled() { if s.proc == nil || !s.proc.Enabled() {

View File

@ -263,7 +263,8 @@ func ParseLoadAvg(data string) (LoadAvg, error) {
} }
type NPULoad struct { type NPULoad struct {
UsagePct float64 `json:"usage_pct"` UsagePct float64 `json:"usage_pct"`
Cores map[string]float64 `json:"cores,omitempty"`
} }
func ReadNPULoad() (*NPULoad, error) { func ReadNPULoad() (*NPULoad, error) {
@ -273,31 +274,67 @@ func ReadNPULoad() (*NPULoad, error) {
} }
defer f.Close() defer f.Close()
scanner := bufio.NewScanner(f) scanner := bufio.NewScanner(f)
var total, idle float64 cores := map[string]float64{}
n := 0
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
parts := strings.Split(line, "%") // Find each "CoreN: XX%" pattern
for _, p := range parts { idx := 0
p = strings.TrimSpace(p) for {
if idx := strings.LastIndex(p, " "); idx >= 0 { coreStart := strings.Index(line[idx:], "Core")
p = p[idx+1:] if coreStart < 0 {
break
} }
if strings.HasSuffix(p, "%") { coreStart += idx
continue colon := strings.Index(line[coreStart:], ":")
if colon < 0 {
break
} }
v, err := strconv.ParseFloat(strings.TrimSpace(p), 64) colon += coreStart
coreName := strings.TrimSpace(line[coreStart:colon])
// Find value after colon up to % or comma or end
valEnd := strings.Index(line[colon+1:], "%")
if valEnd < 0 {
break
}
valEnd += colon + 1
valStr := strings.TrimSpace(line[colon+1 : valEnd])
v, err := strconv.ParseFloat(valStr, 64)
if err == nil { if err == nil {
total += 100 cores[coreName] = v
idle += v n++
} }
idx = valEnd + 1
} }
} }
if total == 0 { if n == 0 {
return nil, ErrNotSupported return nil, ErrNotSupported
} }
usage := 100 - (idle / total * 100) var total float64
if usage < 0 { for _, v := range cores {
usage = 0 total += v
} }
return &NPULoad{UsagePct: usage}, nil avg := total / float64(n)
return &NPULoad{UsagePct: avg, Cores: cores}, nil
}
type Temperature struct {
Zone string `json:"zone"`
Celsius float64 `json:"celsius"`
}
func ReadTemperature() (*Temperature, error) {
raw, err := os.ReadFile("/sys/class/thermal/thermal_zone0/temp")
if err != nil {
return nil, err
}
zone, _ := os.ReadFile("/sys/class/thermal/thermal_zone0/type")
milli, err := strconv.ParseFloat(strings.TrimSpace(string(raw)), 64)
if err != nil {
return nil, err
}
return &Temperature{
Zone: strings.TrimSpace(string(zone)),
Celsius: milli / 1000,
}, nil
} }

Binary file not shown.

View File

@ -1,5 +1,6 @@
#Mon May 11 15:07:02 CST 2026
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists