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
28 changes: 26 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{}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see #673

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still don't get it. The function always returns ClassDRMCardAMDGPUStats{}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line checks if the struct is empty (zero-initialized) and only appends to the stats list if it is not empty. I've also modified the test file for the module. Previously, the test would produce two struct instances: one valid and one with all elements zero-initialized. It doesn't make much sense to let the empty object be passed as a valid one.

cardStats.Name = filepath.Base(card)
stats = append(stats, cardStats)
}
}
return stats, nil
}
Expand All @@ -87,6 +105,12 @@ func parseClassDRMAMDGPUCard(card string) (ClassDRMCardAMDGPUStats, error) {

stats := ClassDRMCardAMDGPUStats{Name: card}
// Read only specific files for faster data gathering.
if n, t, err := readDevInfo(card); err == nil {
stats.DevName = n
stats.DevType = t
} else {
return ClassDRMCardAMDGPUStats{}, err
}
if v, err := readDRMCardField(card, "gpu_busy_percent"); err == nil {
stats.GPUBusyPercent = *util.NewValueParser(v).PUInt64()
}
Expand Down
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