...

Source file src/github.com/prometheus/procfs/sysfs/class_drm_amdgpu.go

Documentation: github.com/prometheus/procfs/sysfs

     1  // Copyright 2021 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  	"errors"
    21  	"fmt"
    22  	"path/filepath"
    23  	"regexp"
    24  	"syscall"
    25  
    26  	"github.com/prometheus/procfs/internal/util"
    27  )
    28  
    29  const (
    30  	// Supported device drivers.
    31  	deviceDriverAMDGPU = "amdgpu"
    32  )
    33  
    34  // ClassDRMCardAMDGPUStats contains info from files in
    35  // /sys/class/drm/card<card>/device for a single amdgpu card.
    36  // Not all cards expose all metrics.
    37  // https://www.kernel.org/doc/html/latest/gpu/amdgpu.html
    38  type ClassDRMCardAMDGPUStats struct {
    39  	Name                          string // The card name.
    40  	GPUBusyPercent                uint64 // How busy the GPU is as a percentage.
    41  	MemoryGTTSize                 uint64 // The size of the graphics translation table (GTT) block in bytes.
    42  	MemoryGTTUsed                 uint64 // The used amount of the graphics translation table (GTT) block in bytes.
    43  	MemoryVisibleVRAMSize         uint64 // The size of visible VRAM in bytes.
    44  	MemoryVisibleVRAMUsed         uint64 // The used amount of visible VRAM in bytes.
    45  	MemoryVRAMSize                uint64 // The size of VRAM in bytes.
    46  	MemoryVRAMUsed                uint64 // The used amount of VRAM in bytes.
    47  	MemoryVRAMVendor              string // The VRAM vendor name.
    48  	PowerDPMForcePerformanceLevel string // The current power performance level.
    49  	UniqueID                      string // The unique ID of the GPU that will persist from machine to machine.
    50  }
    51  
    52  // ClassDRMCardAMDGPUStats returns DRM card metrics for all amdgpu cards.
    53  func (fs FS) ClassDRMCardAMDGPUStats() ([]ClassDRMCardAMDGPUStats, error) {
    54  	cards, err := filepath.Glob(fs.sys.Path("class/drm/card[0-9]"))
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	var stats []ClassDRMCardAMDGPUStats
    60  	for _, card := range cards {
    61  		cardStats, err := parseClassDRMAMDGPUCard(card)
    62  		if err != nil {
    63  			if errors.Is(err, syscall.ENODATA) {
    64  				continue
    65  			}
    66  			return nil, err
    67  		}
    68  		cardStats.Name = filepath.Base(card)
    69  		stats = append(stats, cardStats)
    70  	}
    71  	return stats, nil
    72  }
    73  
    74  func parseClassDRMAMDGPUCard(card string) (ClassDRMCardAMDGPUStats, error) {
    75  	uevent, err := util.SysReadFile(filepath.Join(card, "device/uevent"))
    76  	if err != nil {
    77  		return ClassDRMCardAMDGPUStats{}, err
    78  	}
    79  
    80  	match, err := regexp.MatchString(fmt.Sprintf("DRIVER=%s", deviceDriverAMDGPU), uevent)
    81  	if err != nil {
    82  		return ClassDRMCardAMDGPUStats{}, err
    83  	}
    84  	if !match {
    85  		return ClassDRMCardAMDGPUStats{}, nil
    86  	}
    87  
    88  	stats := ClassDRMCardAMDGPUStats{Name: card}
    89  	// Read only specific files for faster data gathering.
    90  	if v, err := readDRMCardField(card, "gpu_busy_percent"); err == nil {
    91  		stats.GPUBusyPercent = *util.NewValueParser(v).PUInt64()
    92  	}
    93  	if v, err := readDRMCardField(card, "mem_info_gtt_total"); err == nil {
    94  		stats.MemoryGTTSize = *util.NewValueParser(v).PUInt64()
    95  	}
    96  	if v, err := readDRMCardField(card, "mem_info_gtt_used"); err == nil {
    97  		stats.MemoryGTTUsed = *util.NewValueParser(v).PUInt64()
    98  	}
    99  	if v, err := readDRMCardField(card, "mem_info_vis_vram_total"); err == nil {
   100  		stats.MemoryVisibleVRAMSize = *util.NewValueParser(v).PUInt64()
   101  	}
   102  	if v, err := readDRMCardField(card, "mem_info_vis_vram_used"); err == nil {
   103  		stats.MemoryVisibleVRAMUsed = *util.NewValueParser(v).PUInt64()
   104  	}
   105  	if v, err := readDRMCardField(card, "mem_info_vram_total"); err == nil {
   106  		stats.MemoryVRAMSize = *util.NewValueParser(v).PUInt64()
   107  	}
   108  	if v, err := readDRMCardField(card, "mem_info_vram_used"); err == nil {
   109  		stats.MemoryVRAMUsed = *util.NewValueParser(v).PUInt64()
   110  	}
   111  	if v, err := readDRMCardField(card, "mem_info_vram_vendor"); err == nil {
   112  		stats.MemoryVRAMVendor = v
   113  	}
   114  	if v, err := readDRMCardField(card, "power_dpm_force_performance_level"); err == nil {
   115  		stats.PowerDPMForcePerformanceLevel = v
   116  	}
   117  	if v, err := readDRMCardField(card, "unique_id"); err == nil {
   118  		stats.UniqueID = v
   119  	}
   120  
   121  	return stats, nil
   122  }
   123  

View as plain text