4
4
This module contains utility functions/variables used throughout the 'amdgpu-stats' TUI
5
5
6
6
Variables:
7
- - AMDGPU_CARDS (dict): discovered AMD GPUs and their `hwmon` stats directories
7
+ - CARDS (dict): discovered AMD GPUs and their `hwmon` stats directories
8
8
- Example: `{'card0': '/sys/class/drm/card0/device/hwmon/hwmon9'}`
9
9
- If no *AMD* cards are found, this will be empty.
10
10
- CLOCK_DOMAINS (tuple): supported clock domains, ie: `('core', 'memory')`
@@ -44,7 +44,7 @@ def find_cards() -> dict:
44
44
45
45
46
46
# discover all available AMD GPUs
47
- AMDGPU_CARDS = find_cards ()
47
+ CARDS = find_cards ()
48
48
# supported clock domains by 'get_clock' func
49
49
# is concatenated with 'clock_' to index SRC_FILES for the relevant data file
50
50
CLOCK_DOMAINS = ('core' , 'memory' )
@@ -53,7 +53,7 @@ def find_cards() -> dict:
53
53
54
54
def validate_card (card : Optional [str ] = None ) -> str :
55
55
"""
56
- Checks the provided `card` identifier -- if present in `AMDGPU_CARDS `
56
+ Checks the provided `card` identifier -- if present in `CARDS `
57
57
58
58
If `card` is not provided, will yield the first AMD GPU *(if any installed)*
59
59
@@ -62,24 +62,24 @@ def validate_card(card: Optional[str] = None) -> str:
62
62
63
63
Raises:
64
64
ValueError: If *no* AMD cards are found, or `card` is not one of them.
65
- Determined with `AMDGPU_CARDS `
65
+ Determined with `CARDS `
66
66
67
67
Returns:
68
68
str: Validated card identifier.
69
69
If `card` is provided and valid: original identifier `card` is returned
70
70
If `card` is omitted: the first AMD GPU identifier is returned
71
71
"""
72
- if card in AMDGPU_CARDS :
72
+ if card in CARDS :
73
73
# card was provided and checks out, send it back
74
74
return card
75
75
if card is None :
76
76
# if no card provided and we know some, send the first one we know back
77
- if len (AMDGPU_CARDS ) > 0 :
78
- return list (AMDGPU_CARDS .keys ())[0 ]
77
+ if len (CARDS ) > 0 :
78
+ return list (CARDS .keys ())[0 ]
79
79
# if no AMD cards found, toss an errror
80
80
raise ValueError ("No AMD GPUs or hwmon directories found" )
81
- # if 'card' was specified (not None) but invalid (not in 'AMDGPU_CARDS '), raise a helpful error
82
- raise ValueError (f"Invalid card: '{ card } '. Must be one of: { list (AMDGPU_CARDS .keys ())} " )
81
+ # if 'card' was specified (not None) but invalid (not in 'CARDS '), raise a helpful error
82
+ raise ValueError (f"Invalid card: '{ card } '. Must be one of: { list (CARDS .keys ())} " )
83
83
84
84
85
85
def read_stat (file : str , stat_type : Optional [str ] = None ) -> str :
@@ -104,7 +104,6 @@ def read_stat(file: str, stat_type: Optional[str] = None) -> str:
104
104
return None
105
105
106
106
107
-
108
107
def format_frequency (frequency_hz : int ) -> str :
109
108
"""
110
109
Takes a frequency (in Hz) and normalizes it: `Hz`, `MHz`, or `GHz`
@@ -122,11 +121,11 @@ def format_frequency(frequency_hz: int) -> str:
122
121
def get_power_stats (card : Optional [str ] = None ) -> dict :
123
122
"""
124
123
Args:
125
- card (str, optional): ie: `card0`. See `AMDGPU_CARDS ` or `find_cards()`
124
+ card (str, optional): ie: `card0`. See `CARDS ` or `find_cards()`
126
125
127
126
Raises:
128
127
ValueError: If *no* AMD cards are found, or `card` is not one of them.
129
- Determined with `AMDGPU_CARDS `
128
+ Determined with `CARDS `
130
129
131
130
Returns:
132
131
dict: A dictionary of current GPU *power* related statistics.
@@ -135,7 +134,7 @@ def get_power_stats(card: Optional[str] = None) -> dict:
135
134
`{'limit': int, 'average': int, 'capability': int, 'default': int}`
136
135
"""
137
136
card = validate_card (card )
138
- hwmon_dir = AMDGPU_CARDS [card ]
137
+ hwmon_dir = CARDS [card ]
139
138
_pwr = {"limit" : read_stat (path .join (hwmon_dir , "power1_cap" ), stat_type = 'power' ),
140
139
"limit_pct" : 0 ,
141
140
"average" : read_stat (path .join (hwmon_dir , "power1_average" ), stat_type = 'power' ),
@@ -151,11 +150,11 @@ def get_power_stats(card: Optional[str] = None) -> dict:
151
150
def get_core_stats (card : Optional [str ] = None ) -> dict :
152
151
"""
153
152
Args:
154
- card (str, optional): ie: `card0`. See `AMDGPU_CARDS ` or `find_cards()`
153
+ card (str, optional): ie: `card0`. See `CARDS ` or `find_cards()`
155
154
156
155
Raises:
157
156
ValueError: If *no* AMD cards are found, or `card` is not one of them.
158
- Determined with `AMDGPU_CARDS `
157
+ Determined with `CARDS `
159
158
160
159
Returns:
161
160
dict: A dictionary of current GPU *core/memory* related statistics.
@@ -179,14 +178,14 @@ def get_clock(domain: str, card: Optional[str] = None, format_freq: Optional[boo
179
178
domain (str): The GPU part of interest RE: clock speed.
180
179
Must be either 'core' or 'memory'
181
180
182
- card (str, optional): ie: `card0`. See `AMDGPU_CARDS ` or `find_cards()`
181
+ card (str, optional): ie: `card0`. See `CARDS ` or `find_cards()`
183
182
184
183
format_freq (bool, optional): If True, a formatted string will be returned instead of an int.
185
184
Defaults to False.
186
185
187
186
Raises:
188
187
ValueError: If *no* AMD cards are found, or `card` is not one of them.
189
- Determined with `AMDGPU_CARDS `
188
+ Determined with `CARDS `
190
189
191
190
Returns:
192
191
Union[int, str]: The clock value for the specified domain.
@@ -197,7 +196,7 @@ def get_clock(domain: str, card: Optional[str] = None, format_freq: Optional[boo
197
196
"""
198
197
# verify card -- is it AMD, do we know the hwmon directory?
199
198
card = validate_card (card )
200
- hwmon_dir = AMDGPU_CARDS [card ]
199
+ hwmon_dir = CARDS [card ]
201
200
if domain not in CLOCK_DOMAINS :
202
201
raise ValueError (f"Invalid clock domain: '{ domain } '. Must be one of: { CLOCK_DOMAINS } " )
203
202
# set the clock file based on requested domain
@@ -217,29 +216,29 @@ def get_clock(domain: str, card: Optional[str] = None, format_freq: Optional[boo
217
216
def get_voltage (card : Optional [str ] = None ) -> float :
218
217
"""
219
218
Args:
220
- card (str, optional): ie: `card0`. See `AMDGPU_CARDS ` or `find_cards()`
219
+ card (str, optional): ie: `card0`. See `CARDS ` or `find_cards()`
221
220
222
221
Raises:
223
222
ValueError: If *no* AMD cards are found, or `card` is not one of them.
224
- Determined with `AMDGPU_CARDS `
223
+ Determined with `CARDS `
225
224
226
225
Returns:
227
226
float: The current GPU core voltage
228
227
"""
229
228
# verify card -- is it AMD, do we know the hwmon directory?
230
229
card = validate_card (card )
231
- hwmon_dir = AMDGPU_CARDS [card ]
230
+ hwmon_dir = CARDS [card ]
232
231
return round (int (read_stat (path .join (hwmon_dir , "in0_input" ))) / 1000.0 , 2 )
233
232
234
233
235
234
def get_fan_rpm (card : Optional [str ] = None ) -> int :
236
235
"""
237
236
Args:
238
- card (str, optional): ie: `card0`. See `AMDGPU_CARDS ` or `find_cards()`
237
+ card (str, optional): ie: `card0`. See `CARDS ` or `find_cards()`
239
238
240
239
Raises:
241
240
ValueError: If *no* AMD cards are found, or `card` is not one of them.
242
- Determined with `AMDGPU_CARDS `
241
+ Determined with `CARDS `
243
242
244
243
245
244
Returns:
@@ -248,7 +247,7 @@ def get_fan_rpm(card: Optional[str] = None) -> int:
248
247
"""
249
248
# verify card -- is it AMD, do we know the hwmon directory?
250
249
card = validate_card (card )
251
- hwmon_dir = AMDGPU_CARDS [card ]
250
+ hwmon_dir = CARDS [card ]
252
251
_val = read_stat (path .join (hwmon_dir , "fan1_input" ))
253
252
if _val is not None :
254
253
_val = int (_val )
@@ -258,29 +257,29 @@ def get_fan_rpm(card: Optional[str] = None) -> int:
258
257
def get_fan_target (card : Optional [str ] = None ) -> int :
259
258
"""
260
259
Args:
261
- card (str, optional): ie: `card0`. See `AMDGPU_CARDS ` or `find_cards()`
260
+ card (str, optional): ie: `card0`. See `CARDS ` or `find_cards()`
262
261
263
262
Raises:
264
263
ValueError: If *no* AMD cards are found, or `card` is not one of them.
265
- Determined with `AMDGPU_CARDS `
264
+ Determined with `CARDS `
266
265
267
266
Returns:
268
267
int: The *target* fan RPM
269
268
"""
270
269
# verify card -- is it AMD, do we know the hwmon directory?
271
270
card = validate_card (card )
272
- hwmon_dir = AMDGPU_CARDS [card ]
271
+ hwmon_dir = CARDS [card ]
273
272
return int (read_stat (path .join (hwmon_dir , "fan1_target" )))
274
273
275
274
276
275
def get_gpu_usage (card : Optional [str ] = None ) -> int :
277
276
"""
278
277
Args:
279
- card (str, optional): ie: `card0`. See `AMDGPU_CARDS ` or `find_cards()`
278
+ card (str, optional): ie: `card0`. See `CARDS ` or `find_cards()`
280
279
281
280
Raises:
282
281
ValueError: If *no* AMD cards are found, or `card` is not one of them.
283
- Determined with `AMDGPU_CARDS `
282
+ Determined with `CARDS `
284
283
285
284
Returns:
286
285
int: The current GPU usage/utilization as a percentage
@@ -293,7 +292,7 @@ def get_gpu_usage(card: Optional[str] = None) -> int:
293
292
def get_available_temps (card : Optional [str ] = None ) -> dict :
294
293
"""
295
294
Args:
296
- card (str, optional): ie: `card0`. See `AMDGPU_CARDS ` or `find_cards()`
295
+ card (str, optional): ie: `card0`. See `CARDS ` or `find_cards()`
297
296
298
297
Raises:
299
298
ValueError: If *no* AMD cards are found, or `card` is not one of them.
@@ -307,7 +306,7 @@ def get_available_temps(card: Optional[str] = None) -> dict:
307
306
`{'edge': '/.../temp1_input', 'junction': '/.../temp2_input', 'mem': '/.../temp3_input'}`
308
307
"""
309
308
card = validate_card (card )
310
- hwmon_dir = AMDGPU_CARDS [card ]
309
+ hwmon_dir = CARDS [card ]
311
310
# determine temperature nodes/types, construct a dict to store them
312
311
temp_files = {}
313
312
temp_node_labels = glob .glob (path .join (hwmon_dir , "temp*_label" ))
@@ -326,7 +325,7 @@ def get_available_temps(card: Optional[str] = None) -> dict:
326
325
def get_temp_stat (name : str , card : Optional [str ] = None ) -> dict :
327
326
"""
328
327
Args:
329
- card (str, optional): ie: `card0`. See `AMDGPU_CARDS ` or `find_cards()`
328
+ card (str, optional): ie: `card0`. See `CARDS ` or `find_cards()`
330
329
name (str): temperature *name*, ie: `edge`, `junction`, or `mem`
331
330
332
331
Raises:
0 commit comments