How to customize nvidia-smi 's output to show PID username

I created a script that takes nvidia-smi output and enriches it with some more information: https://github.com/peci1/nvidia-htop .

It is a python script that parses the GPU process list, parses the PIDs, runs them through ps to gather more information, and then substitutes the nvidia-smi's process list with the enriched listing.

Example of use:

$ nvidia-smi | nvidia-htop.py -l
Mon May 21 15:06:35 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 390.25                 Driver Version: 390.25                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 108...  Off  | 00000000:04:00.0 Off |                  N/A |
| 53%   75C    P2   174W / 250W |  10807MiB / 11178MiB |     97%      Default |
+-------------------------------+----------------------+----------------------+
|   1  GeForce GTX 108...  Off  | 00000000:05:00.0 Off |                  N/A |
| 66%   82C    P2   220W / 250W |  10783MiB / 11178MiB |    100%      Default |
+-------------------------------+----------------------+----------------------+
|   2  GeForce GTX 108...  Off  | 00000000:08:00.0 Off |                  N/A |
| 45%   67C    P2    85W / 250W |  10793MiB / 11178MiB |     51%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
|  GPU   PID     USER    GPU MEM  %MEM  %CPU  COMMAND                                                                                               |
|    0  1032 anonymou   10781MiB   308   3.7  python train_image_classifier.py --train_dir=/mnt/xxxxxxxx/xxxxxxxx/xxxxxxxx/xxxxxxx/xxxxxxxxxxxxxxx  |
|    1 11021 cannotte   10765MiB   114   1.5  python3 ./train.py --flagfile /xxxxxxxx/xxxxxxxx/xxxxxxxx/xxxxxxxxx/xx/xxxxxxxxxxxxxxx                |
|    2 25544 nevermin   10775MiB   108   2.0  python -m xxxxxxxxxxxxxxxxxxxxxxxxxxxxx                                                               |
+-----------------------------------------------------------------------------+

I did it with nvidia-smi -q -x which is XML style output of nvidia-smi

ps -up `nvidia-smi -q -x | grep pid | sed -e 's/<pid>//g' -e 's/<\/pid>//g' -e 's/^[[:space:]]*//'`

This is the best I could come up with:

nvidia-smi
ps -up `nvidia-smi |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3` 

Sample output:

Thu May 10 15:23:08 2018       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.111                Driver Version: 384.111                   |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 108...  Off  | 00000000:0A:00.0 Off |                  N/A |
| 41%   59C    P2   251W / 250W |   5409MiB / 11172MiB |    100%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1606      C   ...master_JPG/build/tools/program.bin       4862MiB |
|    0     15314      C   python                                       537MiB |
+-----------------------------------------------------------------------------+
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
user111+  1606  134  4.8 32980224 789164 pts/19 Rl+ 15:23   0:08 /home/user111
user2     15314  0.4 10.0 17936788 1647040 pts/16 Sl+ 10:41   1:20 python server_

Short explanation of the script:

  • Tail and head to remove redundant lines

  • Sed to remove spaces (after this, each column would only be separated by 1 space)

  • Cut to extract the relevant columns

The output is a list of PIDs, each occupying 1 line. We only need to use ps -up to show the relevant information

UPDATE: A better solution:

ps -up `nvidia-smi |tee /dev/stderr |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3`

This way, nvidia-smi would have to be called only once. See also:

How to output bash command to stdout and pipe to another command at the same time?

UPDATE 2: I've uploaded this to Github as a simple script for those who need detailed GPU information.

https://github.com/ManhTruongDang/check-gpu