Skip to content
Merged
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 @@ -20,6 +20,7 @@ import (
"fmt"
"path/filepath"
"regexp"
"strings"
"syscall"

"github.com/prometheus/procfs/internal/util"
Expand All @@ -46,6 +47,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 @@ -64,8 +80,10 @@ func (fs FS) ClassDRMCardAMDGPUStats() ([]ClassDRMCardAMDGPUStats, error) {
}
return nil, err
}
cardStats.Name = filepath.Base(card)
stats = append(stats, cardStats)
if cardStats != (ClassDRMCardAMDGPUStats{}) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why?

Copy link
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess this comes from the logic in parseClassDRMAMDGPUCard().

  match, err := regexp.MatchString(fmt.Sprintf("DRIVER=%s", deviceDriverAMDGPU), uevent)
  if err != nil {
    return ClassDRMCardAMDGPUStats{}, err
  }
  if !match {
    return ClassDRMCardAMDGPUStats{}, nil
  }

The no match returns an empty card, but no error. Seems suspect.

But this workaround is probably fine for now. We would need to look over the history of the parser to find out why it's like that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looking at the current test fixtures, we have two drm GPUs. One amdgpu and one i915.

It seems like this parser would return an empty card in the case of a non-amd GPU. That seems weird to me. I think this is probably better to return nothing for non-matching cards.

cardStats.Name = filepath.Base(card)
stats = append(stats, cardStats)
}
}
return stats, nil
}
Expand All @@ -86,6 +104,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 @@ -45,16 +45,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