From 9cbfa88a5cb40c4638835d05261007b53e47ddb9 Mon Sep 17 00:00:00 2001 From: leilei3167 Date: Wed, 23 Sep 2026 18:18:57 +0800 Subject: [PATCH] fix(xsysinfo): avoid startup hang on intel_gpu_top (#12206) Use -n 1 for a single JSON sample instead of -s 1, which sets a 1 ms refresh period and runs until interrupted on current intel_gpu_top. Fixes #12205 Signed-off-by: leilei3167 --- pkg/xsysinfo/gpu.go | 15 +++++++++++++-- pkg/xsysinfo/intel_gputop_internal_test.go | 12 ++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 pkg/xsysinfo/intel_gputop_internal_test.go diff --git a/pkg/xsysinfo/gpu.go b/pkg/xsysinfo/gpu.go index 2037f4eaa..b2ed765b3 100644 --- a/pkg/xsysinfo/gpu.go +++ b/pkg/xsysinfo/gpu.go @@ -3,6 +3,7 @@ package xsysinfo import ( "bufio" "bytes" + "context" "encoding/json" "io" "os" @@ -11,6 +12,7 @@ import ( "strings" "sync" "sync/atomic" + "time" "github.com/jaypipes/ghw" "github.com/jaypipes/ghw/pkg/gpu" @@ -926,14 +928,23 @@ func getIntelXPUSMI() []GPUMemoryInfo { return gpus } +// intelGPUTopArgs returns flags for a single JSON sample from intel_gpu_top. +// Do not use -s 1: on current intel_gpu_top, -s is the refresh period in +// milliseconds and the default iteration count is infinite, which blocks startup. +func intelGPUTopArgs() []string { + return []string{"-J", "-n", "1"} +} + // getIntelGPUTop queries Intel GPUs using intel_gpu_top func getIntelGPUTop() []GPUMemoryInfo { if _, err := exec.LookPath("intel_gpu_top"); err != nil { return nil } - // intel_gpu_top with -J outputs JSON, -s 1 for single sample - cmd := exec.Command("intel_gpu_top", "-J", "-s", "1") + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + cmd := exec.CommandContext(ctx, "intel_gpu_top", intelGPUTopArgs()...) var stdout, stderr bytes.Buffer cmd.Stdout = &stdout diff --git a/pkg/xsysinfo/intel_gputop_internal_test.go b/pkg/xsysinfo/intel_gputop_internal_test.go new file mode 100644 index 000000000..8e332175f --- /dev/null +++ b/pkg/xsysinfo/intel_gputop_internal_test.go @@ -0,0 +1,12 @@ +package xsysinfo + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("intelGPUTopArgs", func() { + It("requests one JSON sample instead of an infinite 1ms refresh loop", func() { + Expect(intelGPUTopArgs()).To(Equal([]string{"-J", "-n", "1"})) + }) +})