amdgpu_stats API reference

utils.py

This module contains utility functions/variables used throughout the ‘amdgpu-stats’ TUI

Variables:
  • CARDS (dict): discovered AMD GPUs and their hwmon stats directories
    • Example: {‘card0’: ‘/sys/class/drm/card0/device/hwmon/hwmon9’}

    • If no AMD cards are found, this will be empty.

  • CLOCK_DOMAINS (tuple): supported clock domains, ie: (‘core’, ‘memory’)

amdgpu_stats.utils.find_cards() dict

Searches contents of /sys/class/drm/card*/device/hwmon/hwmon*/name

Reads ‘hwmon’ names looking for ‘amdgpu’ to find cards to monitor.

If device(s) found, returns a dictionary of cards with their hwmon directories.

If none found, this will be an empty dict.

Returns:

{‘cardN’: ‘/hwmon/directory/with/stat/files’, ‘cardY’: ‘/other/hwmon/directory/for/cardY’}

Return type:

dict

amdgpu_stats.utils.format_frequency(frequency_hz: int) str

Takes a frequency (in Hz) and normalizes it: Hz, MHz, or GHz

Returns:

frequency string with the appropriate suffix applied

Return type:

str

amdgpu_stats.utils.get_available_temps(card: str | None = None) dict
Parameters:

card (str, optional) – ie: card0. See CARDS or find_cards()

Raises:

ValueError – If no AMD cards are found, or card is not one of them.

Returns:

Discovered temperature nodes and paths to their value files

If none are found, will be empty.

Example

{‘edge’: ‘/…/temp1_input’, ‘junction’: ‘/…/temp2_input’, ‘mem’: ‘/…/temp3_input’}

Return type:

dict

amdgpu_stats.utils.get_clock(domain: str, card: str | None = None, format_freq: bool | None = False) int | str
Parameters:
  • domain (str) – The GPU part of interest RE: clock speed. Must be either ‘core’ or ‘memory’

  • card (str, optional) – ie: card0. See CARDS or find_cards()

  • format_freq (bool, optional) – If True, a formatted string will be returned instead of an int. Defaults to False.

Raises:

ValueError – If no AMD cards are found, or card is not one of them. Determined with CARDS

Returns:

The clock value for the specified domain.

If format_freq is True, a formatted string with Hz/MHz/GHz will be returned instead of an int

None: The clock domain is invalid for card

Return type:

Union[int, str]

amdgpu_stats.utils.get_core_stats(card: str | None = None) dict
Parameters:

card (str, optional) – ie: card0. See CARDS or find_cards()

Raises:

ValueError – If no AMD cards are found, or card is not one of them. Determined with CARDS

Returns:

A dictionary of current GPU core/memory related statistics.

Clocks are in Hz, the format_frequency function may be used to normalize

Example

{‘sclk’: int, ‘mclk’: int, ‘voltage’: float, ‘util_pct’: int}

Return type:

dict

amdgpu_stats.utils.get_fan_rpm(card: str | None = None) int
Parameters:

card (str, optional) – ie: card0. See CARDS or find_cards()

Raises:

ValueError – If no AMD cards are found, or card is not one of them. Determined with CARDS

Returns:

The current fan RPM None: If card does not have a fan

Return type:

int

amdgpu_stats.utils.get_fan_target(card: str | None = None) int
Parameters:

card (str, optional) – ie: card0. See CARDS or find_cards()

Raises:

ValueError – If no AMD cards are found, or card is not one of them. Determined with CARDS

Returns:

The target fan RPM

Return type:

int

amdgpu_stats.utils.get_gpu_usage(card: str | None = None) int
Parameters:

card (str, optional) – ie: card0. See CARDS or find_cards()

Raises:

ValueError – If no AMD cards are found, or card is not one of them. Determined with CARDS

Returns:

The current GPU usage/utilization as a percentage

Return type:

int

amdgpu_stats.utils.get_power_stats(card: str | None = None) dict
Parameters:

card (str, optional) – ie: card0. See CARDS or find_cards()

Raises:

ValueError – If no AMD cards are found, or card is not one of them. Determined with CARDS

Returns:

A dictionary of current GPU power related statistics.

Example

{‘limit’: int, ‘average’: int, ‘capability’: int, ‘default’: int}

Return type:

dict

amdgpu_stats.utils.get_temp_stat(name: str, card: str | None = None) dict
Parameters:
  • card (str, optional) – ie: card0. See CARDS or find_cards()

  • name (str) – temperature name, ie: edge, junction, or mem

Raises:

ValueError – If no AMD cards are found, or card is not one of them. Or Invalid temperature name is provided.

Returns:

Requested GPU temperature (type, by name).

Either the first AMD card, or one specified with card=.

Driver provides temperatures in millidegrees C

Returned values are converted to ‘C’ as integers for simple comparison

Return type:

int

amdgpu_stats.utils.get_voltage(card: str | None = None) float
Parameters:

card (str, optional) – ie: card0. See CARDS or find_cards()

Raises:

ValueError – If no AMD cards are found, or card is not one of them. Determined with CARDS

Returns:

The current GPU core voltage

Return type:

float

amdgpu_stats.utils.read_stat(file: str, stat_type: str | None = None) str

Read statistic file, return the stripped contents

Parameters:
  • file (str) – The statistic file to read/return

  • stat_type (str) – Optional type, if specified - can convert data.

Returns:

Statistics from file. If stat_type=’power’, will convert mW to Watts

Return type:

str

amdgpu_stats.utils.validate_card(card: str | None = None) str

Checks the provided card identifier – if present in CARDS

If card is not provided, will yield the first AMD GPU (if any installed)

Parameters:

card (str, optional) – ie: card0.

Raises:

ValueError – If no AMD cards are found, or card is not one of them. Determined with CARDS

Returns:

Validated card identifier.

If card is provided and valid: original identifier card is returned If card is omitted: the first AMD GPU identifier is returned

Return type:

str