Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions sysfs/class_drm_amdgpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"path/filepath"
"regexp"
"strings"
"syscall"

"github.com/prometheus/procfs/internal/util"
Expand All @@ -47,6 +48,21 @@ type ClassDRMCardAMDGPUStats struct {
MemoryVRAMVendor string // The VRAM vendor name.
PowerDPMForcePerformanceLevel string // The current power performance level.
UniqueID string // The unique ID of the GPU that will persist from machine to machine.
DevName string // The device name.
DevType string // The device type.
}

// readDev reads the device name and type from the "device" symlink in the given directory.
func readDevInfo(dir string) (string, string, error) {
devicePath, devErr := filepath.EvalSymlinks(filepath.Join(dir, "device"))
if devErr == nil {
devPathPrefix, devName := filepath.Split(devicePath)
_, devType := filepath.Split(strings.TrimRight(devPathPrefix, "/"))

return devName, devType, nil
}

return "", "", devErr
}

// ClassDRMCardAMDGPUStats returns DRM card metrics for all amdgpu cards.
Expand All @@ -65,8 +81,10 @@ func (fs FS) ClassDRMCardAMDGPUStats() ([]ClassDRMCardAMDGPUStats, error) {
}
return nil, err
}
cardStats.Name = filepath.Base(card)
stats = append(stats, cardStats)
if cardStats != (ClassDRMCardAMDGPUStats{}) {
cardStats.Name = filepath.Base(card)
stats = append(stats, cardStats)
}
}
return stats, nil
}
Expand Down Expand Up @@ -117,6 +135,10 @@ func parseClassDRMAMDGPUCard(card string) (ClassDRMCardAMDGPUStats, error) {
if v, err := readDRMCardField(card, "unique_id"); err == nil {
stats.UniqueID = v
}
if n, t, err := readDevInfo(card); err == nil {
stats.DevName = n
stats.DevType = t
}

return stats, nil
}
12 changes: 2 additions & 10 deletions sysfs/class_drm_amdgpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,8 @@ func TestClassDRMCardAMDGPUStats(t *testing.T) {
MemoryVRAMVendor: "samsung",
PowerDPMForcePerformanceLevel: "manual",
UniqueID: "0123456789abcdef",
},
{
Name: "card1",
GPUBusyPercent: 0,
MemoryGTTSize: 0,
MemoryGTTUsed: 0,
MemoryVisibleVRAMSize: 0,
MemoryVisibleVRAMUsed: 0,
MemoryVRAMSize: 0,
MemoryVRAMUsed: 0,
DevName: "device",
DevType: "card0",
},
}

Expand Down