Checking how many agents are working

How this works
Contatti
← Blog
A dark graphics card with a small olive gauge on its fan shroud and a teal thermometer line rising beside it, the AMARBARO mark centered.
✎post del blog

mojo-amdgpu: reading your graphics card's temperature and memory

The short version

mojo-amdgpu reads 17 AMD GPU metrics from sysfs, hwmon and KFD with no ROCm needed, and a sample costs 8 microseconds, about 2.2 times faster than Python with pread.

Part 18 of 20 in Tools we built and why. Previous: mojo-imkit: a simple app window drawn by the GPU · Next: mojo-sqlite: a whole database in one file

What this is about

Is your graphics card hot? How much of its memory is in use? Is it busy right now? On Linux you do not need a special program to find out. The driver publishes these numbers as small files in a part of the system called /sys. Reading a number means reading a file.

mojo-amdgpu is our library that reads those files for AMD cards. It needs no ROCm and no rocm-smi. You give it a card, ask it to refresh, and read 17 values: memory, busy time, clocks, three temperatures, power, power cap, fan speed and voltage.

Why it matters

If you run a model or a game on a graphics card, the card's health is your first clue when something goes wrong. A card at its memory limit will fail new jobs. A card at a high temperature will slow itself down. A card that shows zero load while your program claims to be working tells you the program is stuck on the processor.

Seeing these numbers is the first step in measuring anything else. A card that is already full of memory is a reason to wait before you start another job.

How it works

Linux treats hardware as files. On this machine, which has an AMD card, the card's folder holds files named gpu_busy_percent, mem_info_vram_used and, in a hwmon subfolder, temp1_input and power1_input. Each file holds a number. You can read them with cat.

A monitor does that again and again. The library does it carefully:

  1. Open once. When you create a Gpu, it finds the card with the most memory and opens its files one time.
  2. Read in place. Each refresh() reads every file into one buffer with pread. There is no memory allocated per sample.
  3. Say NA. A metric the card does not have reads NA, never 0. A zero would look like a real, cold, idle reading.

The root path is a parameter, so tests and CI run on a copy of a real sysfs tree, never on the GPU. The copy was taken from a 7900 XTX plus its integrated GPU.

A tiny monitor, in a few lines

This is the example from the README. It reads a stored copy of the files so it runs anywhere.

from amdgpu import Gpu, VRAM_USED, GPU_BUSY, TEMP_JUNCTION, POWER_UW

def main() raises:
    var g = Gpu("tests/fixtures/sys")
    for _ in range(3):
        g.refresh()
        print(g.card, "vram used MiB", g.get(VRAM_USED) // 1048576,
              "busy %", g.get(GPU_BUSY), "junction mC", g.get(TEMP_JUNCTION))

To watch your own card, create the Gpu with the default root, /sys, and sleep one second between samples. The temperature is in thousandths of a degree Celsius, which is why the name ends in mC, and power is in microwatts.

How fast is it?

The benchmark takes 100,000 samples of the best card's 17 files on the stored copy. That measures the cost of pread from the page cache. Real sysfs adds the driver's own read time to both sides. Three runs each:

method time per sample
Python, files opened once, os.pread 17.7 to 18.1 microseconds
Python, open, read and close each time 57.9 to 60.0 microseconds
Mojo, Gpu.refresh() 8.0 to 8.6 microseconds

The sample time is mostly the 17 system calls, about 470 nanoseconds each. Peak memory is the same for both languages, about 11.6 to 11.9 MB, which is the floor of each runtime. It does not grow over 100,000 samples, and a Gpu object is 488 bytes.

The numbers say the library is cheap. They do not say you need it to be: a monitor that samples once a second would not notice the difference.

What else is in the box

  • cards() and best_card(): the cards the system lists, and the one with the most memory.
  • kfd_nodes(): the compute nodes the driver exposes, with the graphics target name, number of SIMD units, wave size and device id.
  • vram_free(): how much memory is left.

What it does not do

  • It needs Linux and the amdgpu driver, and Mojo 1.2.
  • It reads numbers. It cannot change a clock or a fan speed.
  • Different cards publish different files, which is why some values read NA.
  • Real card times depend on the driver.

Reading the numbers well

A few habits help. Look at memory use and busy percent together. High memory with low busy means a model is loaded and waiting. High busy with low memory means a small job is working hard. Watch the junction temperature over several minutes and not at one instant, because a card warms up as it works. And remember that power is reported in microwatts and temperatures in thousandths of a degree, so divide before you compare with a number you know.

Words you may not know

  • sysfs: a part of Linux where hardware shows up as files.
  • hwmon: the Linux folder for temperatures, power and fans.
  • VRAM: the memory on a graphics card.
  • Junction temperature: the hottest spot the chip reports.
  • pread: a system call that reads bytes from a file at a position.
  • Telemetry: numbers a device reports about itself.
  • KFD: the kernel part that lets programs compute on an AMD card.

Try it

  • Look at your own card first. Run cat /sys/class/drm/card0/device/gpu_busy_percent. The card number may differ on your machine.
  • Read the README at github.com/amarbaro/mojo-amdgpu.
  • Install with pixi add mojo-amdgpu, and use the import name amdgpu.
  • See heaptide to find leaked graphics memory, and wavewait to see what a kernel waits for.

Commenti

Ancora nessun commento.

Accedi per commentare.