From 3dbc48727eb34fc1e62d452c1cd18d5598597104 Mon Sep 17 00:00:00 2001 From: henrygd Date: Fri, 31 Oct 2025 19:09:53 -0400 Subject: [PATCH] add INTEL_GPU_DEVICE env var (#1285) --- agent/gpu_intel.go | 7 ++++++- agent/gpu_test.go | 41 +++++++++++++++++++++++++++++++++++++++ supplemental/CHANGELOG.md | 4 ++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/agent/gpu_intel.go b/agent/gpu_intel.go index 2a718a74..31c982c3 100644 --- a/agent/gpu_intel.go +++ b/agent/gpu_intel.go @@ -49,7 +49,12 @@ func (gm *GPUManager) updateIntelFromStats(sample *intelGpuStats) bool { // collectIntelStats executes intel_gpu_top in text mode (-l) and parses the output func (gm *GPUManager) collectIntelStats() (err error) { - cmd := exec.Command(intelGpuStatsCmd, "-s", intelGpuStatsInterval, "-l") + // Build command arguments, optionally selecting a device via -d + args := []string{"-s", intelGpuStatsInterval, "-l"} + if dev, ok := GetEnv("INTEL_GPU_DEVICE"); ok && dev != "" { + args = append(args, "-d", dev) + } + cmd := exec.Command(intelGpuStatsCmd, args...) // Avoid blocking if intel_gpu_top writes to stderr cmd.Stderr = io.Discard stdout, err := cmd.StdoutPipe() diff --git a/agent/gpu_test.go b/agent/gpu_test.go index d204cf77..748037b7 100644 --- a/agent/gpu_test.go +++ b/agent/gpu_test.go @@ -4,8 +4,10 @@ package agent import ( + "fmt" "os" "path/filepath" + "strings" "testing" "time" @@ -1624,3 +1626,42 @@ func TestParseIntelData(t *testing.T) { }) } } + +func TestIntelCollectorDeviceEnv(t *testing.T) { + dir := t.TempDir() + t.Setenv("PATH", dir) + + // Prepare a file to capture args + argsFile := filepath.Join(dir, "args.txt") + + // Create a fake intel_gpu_top that records its arguments and prints minimal valid output + scriptPath := filepath.Join(dir, "intel_gpu_top") + script := fmt.Sprintf(`#!/bin/sh +echo "$@" > %s +echo "Freq MHz IRQ RC6 Power W IMC MiB/s RCS VCS" +echo " req act /s %% gpu pkg rd wr %% se wa %% se wa" +echo "226 223 338 58 2.00 2.69 1820 965 0.00 0 0 0.00 0 0" +echo "189 187 412 67 1.80 2.45 1950 823 8.50 2 1 15.00 1 0" +`, argsFile) + if err := os.WriteFile(scriptPath, []byte(script), 0755); err != nil { + t.Fatal(err) + } + + // Set device selector via prefixed env var + t.Setenv("BESZEL_AGENT_INTEL_GPU_DEVICE", "sriov") + + gm := &GPUManager{GpuDataMap: make(map[string]*system.GPUData)} + if err := gm.collectIntelStats(); err != nil { + t.Fatalf("collectIntelStats error: %v", err) + } + + // Verify that -d sriov was passed + data, err := os.ReadFile(argsFile) + if err != nil { + t.Fatalf("failed reading args file: %v", err) + } + argsStr := strings.TrimSpace(string(data)) + require.Contains(t, argsStr, "-d sriov") + require.Contains(t, argsStr, "-s ") + require.Contains(t, argsStr, "-l") +} diff --git a/supplemental/CHANGELOG.md b/supplemental/CHANGELOG.md index 5953cf57..25fd9215 100644 --- a/supplemental/CHANGELOG.md +++ b/supplemental/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.15.4 + +- Add `INTEL_GPU_DEVICE` environment variable to select Intel GPU device. (#1285) + ## 0.15.3 - Improve parsing of edge case S.M.A.R.T. power on times. (#1347)