修正npu使用率的计算错误
This commit is contained in:
parent
6a3b9f2c99
commit
d4d35ba447
@ -120,6 +120,11 @@ func (s *Server) handleMetrics(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
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()}
|
||||
if s.proc == nil || !s.proc.Enabled() {
|
||||
|
||||
@ -263,7 +263,8 @@ func ParseLoadAvg(data string) (LoadAvg, error) {
|
||||
}
|
||||
|
||||
type NPULoad struct {
|
||||
UsagePct float64 `json:"usage_pct"`
|
||||
UsagePct float64 `json:"usage_pct"`
|
||||
Cores map[string]float64 `json:"cores,omitempty"`
|
||||
}
|
||||
|
||||
func ReadNPULoad() (*NPULoad, error) {
|
||||
@ -273,31 +274,67 @@ func ReadNPULoad() (*NPULoad, error) {
|
||||
}
|
||||
defer f.Close()
|
||||
scanner := bufio.NewScanner(f)
|
||||
var total, idle float64
|
||||
cores := map[string]float64{}
|
||||
n := 0
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
parts := strings.Split(line, "%")
|
||||
for _, p := range parts {
|
||||
p = strings.TrimSpace(p)
|
||||
if idx := strings.LastIndex(p, " "); idx >= 0 {
|
||||
p = p[idx+1:]
|
||||
// Find each "CoreN: XX%" pattern
|
||||
idx := 0
|
||||
for {
|
||||
coreStart := strings.Index(line[idx:], "Core")
|
||||
if coreStart < 0 {
|
||||
break
|
||||
}
|
||||
if strings.HasSuffix(p, "%") {
|
||||
continue
|
||||
coreStart += idx
|
||||
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 {
|
||||
total += 100
|
||||
idle += v
|
||||
cores[coreName] = v
|
||||
n++
|
||||
}
|
||||
idx = valEnd + 1
|
||||
}
|
||||
}
|
||||
if total == 0 {
|
||||
if n == 0 {
|
||||
return nil, ErrNotSupported
|
||||
}
|
||||
usage := 100 - (idle / total * 100)
|
||||
if usage < 0 {
|
||||
usage = 0
|
||||
var total float64
|
||||
for _, v := range cores {
|
||||
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.
@ -1,5 +1,6 @@
|
||||
#Mon May 11 15:07:02 CST 2026
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
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
|
||||
zipStorePath=wrapper/dists
|
||||
|
||||
Loading…
Reference in New Issue
Block a user