Skip to content

Commit

Permalink
Add new record to the DRM parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Deezzir committed Oct 2, 2024
1 parent 54b2b56 commit 91b5d05
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 25 deletions.
40 changes: 40 additions & 0 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 @@ -31,6 +32,10 @@ const (
deviceDriverAMDGPU = "amdgpu"
)

var (
hwmonInvalidMetricChars = regexp.MustCompile("[^a-z0-9:_]")
)

// ClassDRMCardAMDGPUStats contains info from files in
// /sys/class/drm/card<card>/device for a single amdgpu card.
// Not all cards expose all metrics.
Expand All @@ -47,6 +52,38 @@ 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.
HWmonChip string // The hwmon chip.
}

func cleanChipName(name string) string {
lower := strings.ToLower(name)
replaced := hwmonInvalidMetricChars.ReplaceAllLiteralString(lower, "_")
cleaned := strings.Trim(replaced, "_")
return cleaned
}

func readHWmonChip(dir string) (string, error) {
// generate a name for a sensor path
// construct a name based on device name, always unique
// can be used to relate to hwmon sensor metrics
devicePath, devErr := filepath.EvalSymlinks(filepath.Join(dir, "device"))
if devErr == nil {
devPathPrefix, devName := filepath.Split(devicePath)
_, devType := filepath.Split(strings.TrimRight(devPathPrefix, "/"))

cleanDevName := cleanChipName(devName)
cleanDevType := cleanChipName(devType)

if cleanDevType != "" && cleanDevName != "" {
return cleanDevType + "_" + cleanDevName, nil
}

if cleanDevName != "" {
return cleanDevName, nil
}
}

return "", devErr
}

// ClassDRMCardAMDGPUStats returns DRM card metrics for all amdgpu cards.
Expand Down Expand Up @@ -117,6 +154,9 @@ func parseClassDRMAMDGPUCard(card string) (ClassDRMCardAMDGPUStats, error) {
if v, err := readDRMCardField(card, "unique_id"); err == nil {
stats.UniqueID = v
}
if v, err := readHWmonChip(card); err == nil {
stats.HWmonChip = v
}

return stats, nil
}
1 change: 1 addition & 0 deletions sysfs/class_drm_amdgpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestClassDRMCardAMDGPUStats(t *testing.T) {
MemoryVRAMVendor: "samsung",
PowerDPMForcePerformanceLevel: "manual",
UniqueID: "0123456789abcdef",
HWmonChip: "LNXSYBUS:00_PNP0A08:00",
},
}

Expand Down
Loading

0 comments on commit 91b5d05

Please sign in to comment.