Lightweight server monitoring with historical data, docker stats, and alerts.
fix(agent): parse intel_gpu_top -J arrays that omit commas between samples (#2589)
yi1117f63e65f22bf
diff --git a/agent/gpu_intel.go b/agent/gpu_intel.goindex b0813ffa1..1b37da6f2 100644--- a/agent/gpu_intel.go+++ b/agent/gpu_intel.go@@ -3,6 +3,7 @@ package agentimport ("bufio""encoding/json"+ "fmt""io""log/slog""os/exec"@@ -93,37 +94,34 @@ func (gm *GPUManager) collectIntelStats() (err error) {// parseIntelJSONStream decodes samples from intel_gpu_top -J output and// aggregates them. Since v1.28 the samples are wrapped in an array ("[", then-// comma separated objects, and "]" only when the process exits). Older-// versions print the same comma separated objects without the opening "[", so-// it is added here to let both formats decode as an array.+// objects, and "]" only when the process exits). Older versions print objects+// without the opening "[". Some builds (seen on i915 / Arrow Lake) omit the+// commas between objects inside the array; those are tolerated by reading+// successive objects rather than decoding the whole stream as one JSON array.func (gm *GPUManager) parseIntelJSONStream(r io.Reader) error {er := &eofReader{r: r}br := bufio.NewReader(er)- first, err := peekNonSpace(br)- if err != nil {- if err == io.EOF {- return errNoValidData- }- return err- }- var src io.Reader = br- if first != '[' {- src = io.MultiReader(strings.NewReader("["), br)- }- dec := json.NewDecoder(src)- if _, err := dec.Token(); err != nil { // opening "["- return err- }var hadDataRow bool// skip first data row because it sometimes has erroneous datavar skippedFirstDataRow bool- // Decode reads one object and skips the commas between them. The array is- // usually never closed, so output ending mid-array or mid-sample (the- // process was killed) is the normal end of the stream rather than an error.- for dec.More() {+ for {+ obj, err := nextIntelJSONObject(br)+ if err != nil {+ if err == io.EOF {+ break+ }+ // A truncated final sample when the process is killed is normal.+ if er.eof {+ break+ }+ return err+ }+ if obj == nil { // closing "]"+ break+ }var sample intelGpuJSONSample- if err := dec.Decode(&sample); err != nil {+ if err := json.Unmarshal(obj, &sample); err != nil {if er.eof {break}@@ -147,6 +145,72 @@ func (gm *GPUManager) parseIntelJSONStream(r io.Reader) error {return nil}+// nextIntelJSONObject returns the next top-level JSON object from br, skipping+// an optional opening "[", commas, and whitespace. A closing "]" returns+// (nil, nil). EOF with no further object returns (nil, io.EOF).+func nextIntelJSONObject(br *bufio.Reader) ([]byte, error) {+ for {+ b, err := peekNonSpace(br)+ if err != nil {+ return nil, err+ }+ switch b {+ case '[', ',':+ _, _ = br.ReadByte()+ continue+ case ']':+ _, _ = br.ReadByte()+ return nil, nil+ case '{':+ return readJSONObject(br)+ default:+ return nil, fmt.Errorf("invalid character %q looking for beginning of intel_gpu_top sample", b)+ }+ }+}++// readJSONObject reads one JSON object from br, respecting strings and nesting.+func readJSONObject(br *bufio.Reader) ([]byte, error) {+ var buf []byte+ depth := 0+ inString := false+ escape := false+ for {+ c, err := br.ReadByte()+ if err != nil {+ if err == io.EOF {+ return nil, io.ErrUnexpectedEOF+ }+ return nil, err+ }+ buf = append(buf, c)+ if inString {+ if escape {+ escape = false+ continue+ }+ switch c {+ case '\\':+ escape = true+ case '"':+ inString = false+ }+ continue+ }+ switch c {+ case '"':+ inString = true+ case '{':+ depth+++ case '}':+ depth--+ if depth == 0 {+ return buf, nil+ }+ }+ }+}+// eofReader records whether the underlying reader has returned io.EOF. The// json decoder reports a stream ending mid-value as a syntax error, so this// is how a truncated final sample is told apart from invalid output.diff --git a/agent/gpu_test.go b/agent/gpu_test.goindex 0636dc259..e87b4aa27 100644--- a/agent/gpu_test.go+++ b/agent/gpu_test.go@@ -1716,14 +1716,23 @@ func intelJSONSample(powerGPU, powerPkg float64, engines map[string]float64) str// intelJSONStream joins samples as intel_gpu_top -J prints them. Since v1.28// the output starts with "[" (withArray); older versions omit it.+// Commas between objects are the well-formed case; some i915 builds omit them (#2587).func intelJSONStream(withArray bool, samples ...string) string {+ return intelJSONStreamSep(withArray, true, samples...)+}++func intelJSONStreamSep(withArray, withCommas bool, samples ...string) string {var sb strings.Builderif withArray {sb.WriteString("[\n")}for i, s := range samples {if i > 0 {- sb.WriteString(",\n")+ if withCommas {+ sb.WriteString(",\n")+ } else {+ sb.WriteString("\n")+ }}sb.WriteString(s)}@@ -1803,6 +1812,24 @@ func TestParseIntelJSONStream(t *testing.T) {wantPkg: 5,wantEngines: classViewWant,},+ {+ // Some intel_gpu_top builds wrap samples in "["..."]" but omit commas+ // between objects ("}\n{"). Observed on Arrow Lake i915 (#2587).+ name: "array without commas between objects",+ input: intelJSONStreamSep(true, false, first, classView[0], classView[1]) + "\n]\n",+ wantCount: 2,+ wantPower: 3,+ wantPkg: 5,+ wantEngines: classViewWant,+ },+ {+ name: "open array without commas between objects",+ input: intelJSONStreamSep(true, false, first, classView[0], classView[1]),+ wantCount: 2,+ wantPower: 3,+ wantPkg: 5,+ wantEngines: classViewWant,+ },{name: "truncated final sample",input: intelJSONStream(true, first, classView[0], classView[1], `{"period": {"duration": 33`),