Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

fix(agent): back off slow or unsupported AMD sysfs reads (#2705)

Sven van Ginkel27af11c5e04f
diff --git a/agent/gpu_amd_linux.go b/agent/gpu_amd_linux.go
index 54a1c4093..bd751c889 100644
--- a/agent/gpu_amd_linux.go
+++ b/agent/gpu_amd_linux.go
@@ -4,6 +4,7 @@ package agent
import (
"bufio"
+ "errors"
"fmt"
"log/slog"
"os"
@@ -11,6 +12,7 @@ import (
"strconv"
"strings"
"sync"
+ "syscall"
"time"
"github.com/henrygd/beszel/agent/utils"
@@ -158,8 +160,14 @@ func (gm *GPUManager) updateAmdGpuData(cardPath string) bool {
}
// readSysfsFloat reads and parses a numeric value from a sysfs file.
+// Files that fail slowly or are unsupported are skipped for a while (see sysfsBackoff).
func readSysfsFloat(path string) (float64, error) {
+ now := time.Now()
+ if !amdSysfsBackoff.allow(path, now) {
+ return 0, errSysfsBackoff
+ }
val, err := utils.ReadStringFileLimited(path, 64)
+ amdSysfsBackoff.record(path, err, time.Since(now), now)
if err != nil {
slog.Debug("Failed to read sysfs value", "path", path, "error", err)
return 0, err
@@ -167,6 +175,69 @@ func readSysfsFloat(path string) (float64, error) {
return strconv.ParseFloat(val, 64)
}
+const (
+ // sysfsSlowReadThreshold is how long a read may take to fail before the file is backed off.
+ // Healthy amdgpu reads take well under a millisecond.
+ sysfsSlowReadThreshold = 500 * time.Millisecond
+ sysfsBackoffMin = 30 * time.Second
+ sysfsBackoffMax = 10 * time.Minute
+)
+
+var errSysfsBackoff = errors.New("sysfs read skipped during backoff")
+
+// amdSysfsBackoff tracks AMD sysfs files to skip after slow failures or unsupported reads.
+var amdSysfsBackoff = newSysfsBackoff()
+
+// sysfsBackoff skips sysfs files whose reads fail slowly or with EOPNOTSUPP.
+// Slow successes are not backed off, since a stalled host can delay a healthy read.
+// When the amdgpu driver can't reach the SMU, reads can busy-wait in the kernel for
+// several seconds before failing, which burns a CPU core on every poll
+// (https://github.com/henrygd/beszel/issues/2699).
+type sysfsBackoff struct {
+ sync.Mutex
+ entries map[string]*sysfsBackoffEntry
+}
+
+type sysfsBackoffEntry struct {
+ until time.Time
+ delay time.Duration
+}
+
+func newSysfsBackoff() *sysfsBackoff {
+ return &sysfsBackoff{entries: make(map[string]*sysfsBackoffEntry)}
+}
+
+// allow reports whether path may be read at now.
+func (b *sysfsBackoff) allow(path string, now time.Time) bool {
+ b.Lock()
+ defer b.Unlock()
+ entry, ok := b.entries[path]
+ return !ok || !now.Before(entry.until)
+}
+
+// record updates the backoff state for path after a read that took elapsed.
+// Slow failures and unsupported reads double the delay up to sysfsBackoffMax; a success resets it.
+func (b *sysfsBackoff) record(path string, err error, elapsed time.Duration, now time.Time) {
+ b.Lock()
+ defer b.Unlock()
+ if err == nil {
+ delete(b.entries, path)
+ return
+ }
+ if elapsed < sysfsSlowReadThreshold && !errors.Is(err, syscall.EOPNOTSUPP) {
+ return
+ }
+ entry, ok := b.entries[path]
+ if !ok {
+ entry = &sysfsBackoffEntry{delay: sysfsBackoffMin}
+ b.entries[path] = entry
+ } else {
+ entry.delay = min(entry.delay*2, sysfsBackoffMax)
+ }
+ entry.until = now.Add(entry.delay)
+ slog.Warn("Backing off slow or unsupported sysfs file", "path", path, "elapsed", elapsed, "error", err, "retry", entry.delay)
+}
+
// normalizeHexID normalizes hex IDs by trimming spaces, lowercasing, and dropping 0x.
func normalizeHexID(id string) string {
return strings.TrimPrefix(strings.ToLower(strings.TrimSpace(id)), "0x")
diff --git a/agent/gpu_amd_linux_test.go b/agent/gpu_amd_linux_test.go
index bbcaadbcf..9b43dcf20 100644
--- a/agent/gpu_amd_linux_test.go
+++ b/agent/gpu_amd_linux_test.go
@@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"runtime"
+ "syscall"
"testing"
"time"
@@ -147,6 +148,74 @@ func TestReadSysfsFloat(t *testing.T) {
require.Error(t, err)
}
+func TestSysfsBackoff(t *testing.T) {
+ const path = "/sys/class/drm/card1/device/gpu_busy_percent"
+ start := time.Now()
+ unsupported := &os.PathError{Op: "read", Path: path, Err: syscall.EOPNOTSUPP}
+
+ t.Run("fast reads are never backed off", func(t *testing.T) {
+ b := newSysfsBackoff()
+ b.record(path, nil, time.Millisecond, start)
+ b.record(path, os.ErrNotExist, time.Millisecond, start)
+ assert.True(t, b.allow(path, start))
+ })
+
+ t.Run("unsupported read backs off and doubles up to max", func(t *testing.T) {
+ b := newSysfsBackoff()
+ now := start
+ want := sysfsBackoffMin
+ for range 8 {
+ b.record(path, unsupported, time.Millisecond, now)
+ assert.False(t, b.allow(path, now.Add(want-time.Second)))
+ assert.True(t, b.allow(path, now.Add(want)))
+ now = now.Add(want)
+ want = min(want*2, sysfsBackoffMax)
+ }
+ assert.Equal(t, sysfsBackoffMax, b.entries[path].delay)
+ })
+
+ t.Run("slow failed read backs off", func(t *testing.T) {
+ b := newSysfsBackoff()
+ timedOut := &os.PathError{Op: "read", Path: path, Err: syscall.ETIME}
+ b.record(path, timedOut, sysfsSlowReadThreshold, start)
+ assert.False(t, b.allow(path, start))
+ assert.True(t, b.allow(path, start.Add(sysfsBackoffMin)))
+ })
+
+ t.Run("slow successful read is not backed off", func(t *testing.T) {
+ b := newSysfsBackoff()
+ b.record(path, nil, sysfsSlowReadThreshold, start)
+ assert.True(t, b.allow(path, start))
+ })
+
+ t.Run("slow successful read clears backoff", func(t *testing.T) {
+ b := newSysfsBackoff()
+ b.record(path, unsupported, time.Millisecond, start)
+ b.record(path, nil, sysfsSlowReadThreshold, start.Add(sysfsBackoffMin))
+ assert.Empty(t, b.entries)
+ })
+
+ t.Run("fast success resets delay", func(t *testing.T) {
+ b := newSysfsBackoff()
+ b.record(path, unsupported, time.Millisecond, start)
+ b.record(path, unsupported, time.Millisecond, start.Add(sysfsBackoffMin))
+ b.record(path, nil, time.Millisecond, start.Add(3*sysfsBackoffMin))
+ assert.True(t, b.allow(path, start.Add(3*sysfsBackoffMin)))
+ b.record(path, unsupported, time.Millisecond, start.Add(3*sysfsBackoffMin))
+ assert.Equal(t, sysfsBackoffMin, b.entries[path].delay)
+ })
+
+ t.Run("readSysfsFloat skips backed off files", func(t *testing.T) {
+ valPath := filepath.Join(t.TempDir(), "val")
+ require.NoError(t, os.WriteFile(valPath, []byte("42"), 0o644))
+ amdSysfsBackoff.record(valPath, unsupported, time.Millisecond, time.Now())
+ t.Cleanup(func() { amdSysfsBackoff.record(valPath, nil, 0, time.Now()) })
+
+ _, err := readSysfsFloat(valPath)
+ assert.ErrorIs(t, err, errSysfsBackoff)
+ })
+}
+
func TestIsAmdGpu(t *testing.T) {
dir := t.TempDir()
deviceDir := filepath.Join(dir, "device")