feat: add NPU load to /v1/metrics

This commit is contained in:
tian 2026-05-08 19:38:52 +08:00
parent c652c02c97
commit 6a3b9f2c99
4 changed files with 45 additions and 3 deletions

View File

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

View File

@ -3,10 +3,7 @@ package httpapi
import (
"encoding/json"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
)
type previewChannel struct {

View File

@ -261,3 +261,43 @@ func ParseLoadAvg(data string) (LoadAvg, error) {
}
return LoadAvg{One: one, Five: five, Fifteen: fifteen}, nil
}
type NPULoad struct {
UsagePct float64 `json:"usage_pct"`
}
func ReadNPULoad() (*NPULoad, error) {
f, err := os.Open("/proc/rknpu/load")
if err != nil {
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
var total, idle float64
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:]
}
if strings.HasSuffix(p, "%") {
continue
}
v, err := strconv.ParseFloat(strings.TrimSpace(p), 64)
if err == nil {
total += 100
idle += v
}
}
}
if total == 0 {
return nil, ErrNotSupported
}
usage := 100 - (idle / total * 100)
if usage < 0 {
usage = 0
}
return &NPULoad{UsagePct: usage}, nil
}

Binary file not shown.