summaryrefslogtreecommitdiffstats
path: root/glances/plugins/gpu/cards/amd.py
blob: ff1619978189d5c7ca81138f3b475dc6c4a5df7b (plain)
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
# -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
# SPDX-FileCopyrightText: 2024 Nicolas Hennion <nicolas@nicolargo.com>
#
# SPDX-License-Identifier: LGPL-3.0-only
#

"""AMD Extension unit for Glances' GPU plugin.

The class grabs the stats from the /sys/class/drm/ directory.

See: https://wiki.archlinux.org/title/AMDGPU#Manually
"""

# Example
# test-data/plugins/gpu/amd/
# └── sys
#     ├── class
#     │   └── drm
#     │       └── card0
#     │           └── device
#     │               ├── gpu_busy_percent
#     │               ├── hwmon
#     │               │   └── hwmon0
#     │               │       └── temp1_input
#     │               ├── mem_info_vram_total
#     │               ├── mem_info_vram_used
#     │               ├── pp_dpm_mclk
#     │               └── pp_dpm_sclk
#     └── kernel
#         └── debug
#             └── dri
#                 └── 0
#                     └── amdgpu_pm_info

from glances.logger import logger
import re
import os

DRM_ROOT_FOLDER: str = '/sys/class/drm'
CARD_REGEX: str = r"^card\d$"
DEVICE_FOLDER: str = 'device'
GPU_PROC_PERCENT: str = 'gpu_busy_percent'
GPU_MEM_TOTAL: str = 'mem_info_vram_total'
GPU_MEM_USED: str = 'mem_info_vram_used'
HWMON_REGEXP: str = r"^hwmon\d$"
GPU_TEMPERATURE_REGEXP: str = r"^temp\d_input"


class AmdGPU:
    """GPU card class."""

    def __init__(self, drm_root_folder: str = DRM_ROOT_FOLDER):
        """Init AMD  GPU card class."""
        self.drm_root_folder = drm_root_folder
        self.device_folders = get_device_list(drm_root_folder)

    def exit(self):
        """Close AMD GPU class."""
        pass

    def get_device_stats(self):
        """Get AMD GPU stats."""
        stats = []

        for index, device in enumerate(self.device_folders):
            device_stats = dict()
            # Dictionary key is the GPU_ID
            device_stats['key'] = 'gpu_id'
            # GPU id (for multiple GPU, start at 0)
            device_stats['gpu_id'] = f'amd{index}'
            # GPU name
            device_stats['name'] = get_device_name(device)
            # Memory consumption in % (not available on all GPU)
            device_stats['mem'] = get_mem(device)
            # Processor consumption in %
            device_stats['proc'] = get_proc(device)
            # Processor temperature in °C
            device_stats['temperature'] = get_temperature(device)
            # Fan speed in %
            device_stats['fan_speed'] = get_fan_speed(device)
            stats.append(device_stats)

        return stats


def get_device_list(drm_root_folder: str) -> list:
    """Return a list of path to the device stats."""
    ret = []
    for root, dirs, _ in os.walk(drm_root_folder):
        for d in dirs:
            if re.match(CARD_REGEX, d) and \
               DEVICE_FOLDER in os.listdir(os.path.join(root, d)) and \
               os.path.isfile(os.path.join(root, d, DEVICE_FOLDER, GPU_PROC_PERCENT)):
                # If the GPU busy file is present then take the card into account
                ret.append(os.path.join(root, d, DEVICE_FOLDER))
    return ret


def get_device_name(device_folder: str) -> str:
    """Return the GPU name."""
    return 'AMD GPU'


def get_mem(device_folder: str) -> int:
    """Return the memory consumption in %."""
    mem_info_vram_total = os.path.join(device_folder, GPU_MEM_TOTAL)
    mem_info_vram_used = os.path.join(device_folder, GPU_MEM_USED)
    if os.path.isfile(mem_info_vram_total) and os.path.isfile(mem_info_vram_used):
        with open(mem_info_vram_total) as f:
            mem_info_vram_total = int(f.read())
        with open(mem_info_vram_used) as f:
            mem_info_vram_used = int(f.read())
        if mem_info_vram_total > 0:
            return round(mem_info_vram_used / mem_info_vram_total * 100)
    return None


def get_proc(device_folder: str) -> int:
    """Return the processor consumption in %."""
    gpu_busy_percent = os.path.join(device_folder, GPU_PROC_PERCENT)
    if os.path.isfile(gpu_busy_percent):
        with open(gpu_busy_percent) as f:
            return int(f.read())
    return None


def get_temperature(device_folder: str) -> int:
    """Return the processor temperature in °C (mean of all HWMON)"""
    temp_input = []
    for root, dirs, _ in os.walk(device_folder):
        for d in dirs:
            if re.match(HWMON_REGEXP, d):
                for _, _, files in os.walk(os.path.join(root, d)):
                    for f in files:
                        if re.match(GPU_TEMPERATURE_REGEXP, f):
                            with open(os.path.join(root, d, f)) as f:
                                temp_input.append(int(f.read()))
    if len(temp_input) > 0:
        return round(sum(temp_input) / len(temp_input) / 1000)
    else:
        return None


def get_fan_speed(device_folder: str) -> int:
    """Return the fan speed in %."""
    return None