Commands to get NVIDIA GPU state (temperature, fan speed, memory, performance)

There are ways to browse some GPU info graphically, for example by using nvidia-settings utility. But sometimes we need to grab GPU info in a terminal to use it in bash scripts. This article exactly about that.

First, we need nvidia-smi utility to be installed, by default this must to be installed as a dependency with NVIDIA driver:

nvidia-smi
...
NVIDIA-SMI 390.116                Driver Version: 390.116

Read right here how to install nvidia-smi (based on the NVIDIA driver version you currently use,

check it by using dpkg -l | grep nvidia | grep driver on Debian-based systems. The package must have one of the names: nvidia-driver or nvidia-legacy-{VERSION}-driver ).

Now we can start.

A complete list of all the GPU information can be obtained by the following command:

nvidia-smi -q
...
Driver Version                      : 390.116

Attached GPUs                       : 1
GPU 00000000:01:00.0
    Product Name                    : GeForce GT 610
    Product Brand                   : GeForce
    Display Mode                    : N/A
    Display Active                  : N/A
    Persistence Mode                : Disabled
    Accounting Mode                 : N/A
    Accounting Mode Buffer Size     : N/A
...

There are a lot of it.

Of course, we can use grep to parse certain information:

nvidia-smi -q | grep "Fan Speed"
...
Fan Speed                       : 49 %

But this is unnecessary and requires formatting the string.

Instead, the utility allows us to make requests for certain information and filter it:

nvidia-smi --query-gpu=fan.speed --format=csv,noheader
...
45 %

It is more comfortable and minimalistic.

Don’t include a percent sign:

nvidia-smi --query-gpu=fan.speed --format=csv,noheader,nounits
...
45

Show all supported queries with description:

nvidia-smi --help-query-gpu

Get GPU temperature value in Celsius:

nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader
...
52

We can modify it and add a sign:

echo "$(nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader)℃"
...
52℃

if we separate requests by commas, we can form a line with more detailed information:

nvidia-smi --query-gpu=temperature.gpu,fan.speed --format=csv,noheader
...
52, 45 %

But there is an extra comma and a space before the percent sign. In a bash script, this can be solved in the following way:

GPU_STATE=$(nvidia-smi --query-gpu=temperature.gpu,fan.speed --format=csv,noheader,nounits)
GPU_STATE="${GPU_STATE:0:2}°C${GPU_STATE:3}%"
echo "$GPU_STATE"
...
52°C 45%

There are also options that can show and change the work mode of GPU. This can reduce or increase the energy consumption and performance of GPU. But my GPU doesn’t support this technology:

nvidia-smi --query-gpu=power.management --format=csv,noheader
...
[Not Supported]

Also about performance, we can see performance state. States range from P0 (maximum performance) to P12 (minimum performance). When there is a load on GPU, this value tends to 12. We can change this boundaries within which GPU will work (if supported). For now I have Firefox opened with hardware acceleration support, so the value is P8:

nvidia-smi --query-gpu=pstate --format=csv,noheader
...
P8

Total memory amount that GPU has:

nvidia-smi --query-gpu=memory.total --format=csv,noheader
...
964 MiB

Free memory amount:

nvidia-smi --query-gpu=memory.free --format=csv,noheader
...
828 MiB

Used memory amount:

nvidia-smi --query-gpu=memory.used --format=csv,noheader
...
136 MiB

There are more supported queries, these are the most basic.

Don’t forget about:

nvidia-smi --help-query-gpu    # Show all supported queries

Leave a Reply

Your email address will not be published.