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