-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathclass_drm_amdgpu.go
164 lines (145 loc) · 5.41 KB
/
class_drm_amdgpu.go
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2021 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 (
"errors"
"fmt"
"path/filepath"
"regexp"
"strings"
"syscall"
"github.com/prometheus/procfs/internal/util"
)
const (
// Supported device drivers.
deviceDriverAMDGPU = "amdgpu"
)
var (
hwmonInvalidMetricChars = regexp.MustCompile("[^a-z0-9:_]")
)
// ClassDRMCardAMDGPUStats contains info from files in
// /sys/class/drm/card<card>/device for a single amdgpu card.
// Not all cards expose all metrics.
// https://www.kernel.org/doc/html/latest/gpu/amdgpu.html
type ClassDRMCardAMDGPUStats struct {
Name string // The card name.
GPUBusyPercent uint64 // How busy the GPU is as a percentage.
MemoryGTTSize uint64 // The size of the graphics translation table (GTT) block in bytes.
MemoryGTTUsed uint64 // The used amount of the graphics translation table (GTT) block in bytes.
MemoryVisibleVRAMSize uint64 // The size of visible VRAM in bytes.
MemoryVisibleVRAMUsed uint64 // The used amount of visible VRAM in bytes.
MemoryVRAMSize uint64 // The size of VRAM in bytes.
MemoryVRAMUsed uint64 // The used amount of VRAM in bytes.
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.
HWmonChip string // The hwmon chip.
}
func cleanChipName(name string) string {
lower := strings.ToLower(name)
replaced := hwmonInvalidMetricChars.ReplaceAllLiteralString(lower, "_")
cleaned := strings.Trim(replaced, "_")
return cleaned
}
func readHWmonChip(dir string) (string, error) {
// generate a name for a sensor path
// construct a name based on device name, always unique
// can be used to relate to hwmon sensor metrics
devicePath, devErr := filepath.EvalSymlinks(filepath.Join(dir, "device"))
if devErr == nil {
devPathPrefix, devName := filepath.Split(devicePath)
_, devType := filepath.Split(strings.TrimRight(devPathPrefix, "/"))
cleanDevName := cleanChipName(devName)
cleanDevType := cleanChipName(devType)
if cleanDevType != "" && cleanDevName != "" {
return cleanDevType + "_" + cleanDevName, nil
}
if cleanDevName != "" {
return cleanDevName, nil
}
}
return "", devErr
}
// ClassDRMCardAMDGPUStats returns DRM card metrics for all amdgpu cards.
func (fs FS) ClassDRMCardAMDGPUStats() ([]ClassDRMCardAMDGPUStats, error) {
cards, err := filepath.Glob(fs.sys.Path("class/drm/card[0-9]"))
if err != nil {
return nil, err
}
var stats []ClassDRMCardAMDGPUStats
for _, card := range cards {
cardStats, err := parseClassDRMAMDGPUCard(card)
if err != nil {
if errors.Is(err, syscall.ENODATA) {
continue
}
return nil, err
}
if cardStats != (ClassDRMCardAMDGPUStats{}) {
cardStats.Name = filepath.Base(card)
stats = append(stats, cardStats)
}
}
return stats, nil
}
func parseClassDRMAMDGPUCard(card string) (ClassDRMCardAMDGPUStats, error) {
uevent, err := util.SysReadFile(filepath.Join(card, "device/uevent"))
if err != nil {
return ClassDRMCardAMDGPUStats{}, err
}
match, err := regexp.MatchString(fmt.Sprintf("DRIVER=%s", deviceDriverAMDGPU), uevent)
if err != nil {
return ClassDRMCardAMDGPUStats{}, err
}
if !match {
return ClassDRMCardAMDGPUStats{}, nil
}
stats := ClassDRMCardAMDGPUStats{Name: card}
// Read only specific files for faster data gathering.
if v, err := readDRMCardField(card, "gpu_busy_percent"); err == nil {
stats.GPUBusyPercent = *util.NewValueParser(v).PUInt64()
}
if v, err := readDRMCardField(card, "mem_info_gtt_total"); err == nil {
stats.MemoryGTTSize = *util.NewValueParser(v).PUInt64()
}
if v, err := readDRMCardField(card, "mem_info_gtt_used"); err == nil {
stats.MemoryGTTUsed = *util.NewValueParser(v).PUInt64()
}
if v, err := readDRMCardField(card, "mem_info_vis_vram_total"); err == nil {
stats.MemoryVisibleVRAMSize = *util.NewValueParser(v).PUInt64()
}
if v, err := readDRMCardField(card, "mem_info_vis_vram_used"); err == nil {
stats.MemoryVisibleVRAMUsed = *util.NewValueParser(v).PUInt64()
}
if v, err := readDRMCardField(card, "mem_info_vram_total"); err == nil {
stats.MemoryVRAMSize = *util.NewValueParser(v).PUInt64()
}
if v, err := readDRMCardField(card, "mem_info_vram_used"); err == nil {
stats.MemoryVRAMUsed = *util.NewValueParser(v).PUInt64()
}
if v, err := readDRMCardField(card, "mem_info_vram_vendor"); err == nil {
stats.MemoryVRAMVendor = v
}
if v, err := readDRMCardField(card, "power_dpm_force_performance_level"); err == nil {
stats.PowerDPMForcePerformanceLevel = v
}
if v, err := readDRMCardField(card, "unique_id"); err == nil {
stats.UniqueID = v
}
if v, err := readHWmonChip(card); err == nil {
stats.HWmonChip = v
}
return stats, nil
}