1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
// Copyright 2024 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build linux
// +build linux
package sysfs
import (
"fmt"
"path/filepath"
"github.com/prometheus/procfs/internal/util"
)
const drmClassPath = "class/drm"
// DRMCard contains info from files in /sys/class/drm for a
// single DRM Card device.
type DRMCard struct {
Name string
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
}
// 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, 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)
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 {
return nil, err
}
card.Ports[port.Name] = *port
}
return &card, nil
}
func parseDRMCardPort(port string) (*DRMCardPort, error) {
portStatus, err := util.SysReadFile(filepath.Join(port, "status"))
if err != nil {
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
}
drmCardPort.DPMS = portDPMS
portEnabled, err := util.SysReadFile(filepath.Join(port, "enabled"))
if err != nil {
return nil, err
}
drmCardPort.Enabled = portEnabled
return &drmCardPort, nil
}
|