Skip to content

Commit

Permalink
rework drm representation
Browse files Browse the repository at this point in the history
  • Loading branch information
jritter committed Jun 9, 2024
1 parent 011faec commit e2c8662
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 127 deletions.
108 changes: 84 additions & 24 deletions sysfs/class_drm_card.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,118 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !windows
// +build !windows

package sysfs

import (
"errors"
"fmt"
"path/filepath"
"syscall"

"github.com/prometheus/procfs/internal/util"
)

type ClassDrmCard struct {
const drmClassPath = "class/drm"

// DrmCard contains info from files in /sys/class/drm for a
// single DRM Card device.
type DrmCard struct {
Name string
Enable uint64
Driver string
Ports map[string]DrmCardPort
}

// DrmCardPort contains info from files in
// /sys/class/drm/<Card>/<Card>-<Name>
// for a single port of one DrmCard device.
type DrmCardPort struct {
Name string
Status string
Dpms string
Enabled string
}

func (fs FS) ClassDrmCard() ([]ClassDrmCard, error) {
// DrmCardClass is a collection of every Card device in
// /sys/class/drm.
//
// The map keys are the names of the InfiniBand devices.
type DrmCardClass map[string]DrmCard

// DrmCardClass returns infos for all Drm devices read from
// /sys/class/drm.
func (fs FS) DrmCardClass() (DrmCardClass, error) {

cards, err := filepath.Glob(fs.sys.Path("class/drm/card[0-9]"))

if err != nil {
return nil, err
return nil, fmt.Errorf("failed to list DRM card ports at %q: %w", cards, err)
}

drmCardClass := make(DrmCardClass, len(cards))
for _, c := range cards {
card, err := fs.parseDrmCard(filepath.Base(c))
if err != nil {
return nil, err
}

drmCardClass[card.Name] = *card
}

return drmCardClass, nil
}

// Parse one DrmCard.
func (fs FS) parseDrmCard(name string) (*DrmCard, error) {
path := fs.sys.Path(drmClassPath, name)
card := DrmCard{Name: name}

// Read the kernel module of the card
cardDriverPath, err := filepath.EvalSymlinks(filepath.Join(path, "device/driver"))
if err != nil {
return nil, fmt.Errorf("failed to read driver: %w", err)
}
card.Driver = filepath.Base(cardDriverPath)

stats := make([]ClassDrmCard, 0, len(cards))
for _, card := range cards {
cardStats, err := parseClassDrmCard(card)
portsPath, err := filepath.Glob(filepath.Join(path, filepath.Base(path)+"-*-*"))

if err != nil {
return nil, fmt.Errorf("failed to list DRM card ports at %q: %w", portsPath, err)
}

card.Ports = make(map[string]DrmCardPort, len(portsPath))
for _, d := range portsPath {
port, err := parseDrmCardPort(d)
if err != nil {
if errors.Is(err, syscall.ENODATA) {
continue
}
return nil, err
}
cardStats.Name = filepath.Base(card)
stats = append(stats, cardStats)

card.Ports[port.Name] = *port
}
return stats, nil

return &card, nil
}

func parseClassDrmCard(port string) (ClassDrmCard, error) {
cardEnable, err := util.ReadIntFromFile(filepath.Join(port, "device/enable"))
func parseDrmCardPort(port string) (*DrmCardPort, error) {
portStatus, err := util.SysReadFile(filepath.Join(port, "status"))
if err != nil {
return ClassDrmCard{}, err
return nil, err
}

drmCardPort := DrmCardPort{Name: filepath.Base(port), Status: portStatus}

portDpms, err := util.SysReadFile(filepath.Join(port, "dpms"))
if err != nil {
return nil, err
}

cardDriverPath, err := filepath.EvalSymlinks(filepath.Join(port, "device/driver"))
drmCardPort.Dpms = portDpms

portEnabled, err := util.SysReadFile(filepath.Join(port, "enabled"))
if err != nil {
return ClassDrmCard{}, err
return nil, err
}
drmCardPort.Enabled = portEnabled

return ClassDrmCard{
Enable: uint64(cardEnable),
Driver: filepath.Base(cardDriverPath),
}, nil
return &drmCardPort, nil
}
94 changes: 0 additions & 94 deletions sysfs/class_drm_card_port.go

This file was deleted.

32 changes: 23 additions & 9 deletions sysfs/class_drm_card_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package sysfs

import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestClassDRMCard(t *testing.T) {
Expand All @@ -27,25 +28,38 @@ func TestClassDRMCard(t *testing.T) {
t.Fatal(err)
}

drmCardTest, err := fs.ClassDrmCard()
got, err := fs.DrmCardClass()
if err != nil {
t.Fatal(err)
}

classDrmCard := []ClassDrmCard{
{
want := DrmCardClass{
"card0": DrmCard{
Name: "card0",
Enable: 1,
Driver: "amdgpu",
Ports: map[string]DrmCardPort{},
},
{
"card1": DrmCard{
Name: "card1",
Enable: 1,
Driver: "i915",
Ports: map[string]DrmCardPort{
"card1-DP-1": {
Name: "card1-DP-1",
Dpms: "Off",
Enabled: "disabled",
Status: "disconnected",
},
"card1-DP-5": {
Name: "card1-DP-5",
Dpms: "On",
Enabled: "enabled",
Status: "connected",
},
},
},
}

if !reflect.DeepEqual(classDrmCard, drmCardTest) {
t.Errorf("Result not correct: want %v, have %v", classDrmCard, drmCardTest)
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("unexpected DRMCard class (-want +got):\n%s", diff)
}
}
28 changes: 28 additions & 0 deletions testdata/fixtures.ttar
Original file line number Diff line number Diff line change
Expand Up @@ -4958,6 +4958,34 @@ Mode: 755
Directory: fixtures/sys/class/drm/card1/card1-DP-1
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/drm/card1/card1-DP-1/connector_id
Lines: 1
103
Mode: 444
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/drm/card1/card1-DP-1/dpms
Lines: 1
Off
Mode: 444
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/drm/card1/card1-DP-1/edid
Lines: 0
Mode: 444
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/drm/card1/card1-DP-1/enabled
Lines: 1
disabled
Mode: 444
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/drm/card1/card1-DP-1/modes
Lines: 0
Mode: 444
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/drm/card1/card1-DP-1/status
Lines: 1
disconnected
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Directory: fixtures/sys/class/drm/card1/card1-DP-5
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down

0 comments on commit e2c8662

Please sign in to comment.