nvidia-smiでNVIDIA GPU使用状況をモニタリングする

NVIDIA GPUに対してnvidia-smiコマンドを利用し、GPUの利用状況をモニタリングする方法を紹介します。
2020.09.29

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

GPUリソースの使用状況やどのプロセスが GPU リソースをどのくらい消費しているのかモニタリングしたいことがあります。

本エントリーでは、NVIDIA GPUに対してnvidia-smiコマンドで利用状況を取得する方法を紹介します。

nvidia-smi について

NVIDIA System Management Interface (nvidia-smi/NVSMI) は NVIDIA GPUデバイスの管理・モニタリングを行うコマンドラインユーティリティーです。

NVIDIA GPU ドライバーに付随してインストールされます。

基本的な使い方は簡単です。nvidia-smi を呼び出すだけです。

※重要なフィールドは赤枠で囲んでいます。

メモリ使用率(Memory Usage)GPU使用率(GPu Utilization)の違いは何でしょうか?

$ nvidia-smi --help-query-gpu が教えてくれます。

  • "utilization.gpu"
    • Percent of time over the past sample period during which one or more kernels was executing on the GPU. The sample period may be between 1 second and 1/6 second depending on the product.
  • "utilization.memory"
    • Percent of time over the past sample period during which global (device) memory was being read or written. The sample period may be between 1 second and 1/6 second depending on the product.

デフォルトのテーブル表示は目視には向いているものの、他のシステムと連携する場合、出力内容・出力形式をカスタマイズしたいところです。

以下では、nvidia-smi に様々なコマンドラインオプションを渡し、出力内容・出力形式の変更やファイル出力などを変えてみます。

GPU 情報を出力

GPU 情報を問い合せるには --query-gpu を利用し、抽出フィールドを指定します。

実行間隔は -l オプションで秒で指定します。

デフォルトではテーブル形式で出力されるため、処理しやすい用にCSV形式(--format=csv)で出力します。

$ nvidia-smi \
  --query-gpu=timestamp,name,utilization.gpu,utilization.memory,memory.used,memory.free,memory.used\
  --format=csv \
  -l 1
timestamp, name, utilization.gpu [%], utilization.memory [%], memory.used [MiB], memory.free [MiB], memory.used [MiB]
2020/09/24 16:37:35.597, Tesla T4, 0 %, 0 %, 0 MiB, 15109 MiB, 0 MiB
2020/09/24 16:37:36.598, Tesla T4, 0 %, 0 %, 0 MiB, 15109 MiB, 0 MiB
2020/09/24 16:37:37.549, Tesla T4, 100 %, 41 %, 13612 MiB, 1497 MiB, 13612 MiB
2020/09/24 16:37:38.549, Tesla T4, 100 %, 41 %, 13612 MiB, 1497 MiB, 13612 MiB

ファイル出力する場合、出力先を -f オプションで指定します。

$ sudo nvidia-smi \
  --query-gpu=timestamp,name,utilization.gpu,utilization.memory,memory.used,memory.free \
  --format=csv -l 1 \
  -f /var/log/gpu.log

$ tail /var/log/gpu.log
2020/09/24 16:42:32.204, Tesla T4, 0 %, 0 %, 0 MiB, 15109 MiB
2020/09/24 16:42:33.205, Tesla T4, 0 %, 0 %, 132 MiB, 14977 MiB
2020/09/24 16:42:34.205, Tesla T4, 100 %, 41 %, 13612 MiB, 1497 MiB
2020/09/24 16:42:35.206, Tesla T4, 100 %, 41 %, 13612 MiB, 1497 MiB
2020/09/24 16:42:36.207, Tesla T4, 100 %, 41 %, 13612 MiB, 1497 MiB
2020/09/24 16:42:37.208, Tesla T4, 100 %, 74 %, 13612 MiB, 1497 MiB
2020/09/24 16:42:38.208, Tesla T4, 100 %, 40 %, 13612 MiB, 1497 MiB
2020/09/24 16:42:39.209, Tesla T4, 100 %, 40 %, 13612 MiB, 1497 MiB
2020/09/24 16:42:40.210, Tesla T4, 100 %, 40 %, 13612 MiB, 1497 MiB
2020/09/24 16:42:41.210, Tesla T4, 100 %, 40 %, 13612 MiB, 1497 MiB

プロセス情報を出力

GPU を利用しているプロセス情報を問い合せるには --query-compute-apps を利用し、抽出フィールドを指定します。

$ nvidia-smi \
  --query-compute-apps=pid,process_name,used_memory \
  -l 60 \
  --format=csv,noheader
18271, ./gpu_burn, 13609 MiB
18271, ./gpu_burn, 13609 MiB

nvidia-smi pmonでプロセス監視

nvidia-smi のサブコマンド nvidia-smi pmon を利用すると、GPU を利用しているプロセス情報を取得できます。

$ nvidia-smi pmon \
  --delay 10 \
  -s u \
  -o DT
#Date       Time        gpu        pid  type    sm   mem   enc   dec   command
#YYYYMMDD   HH:MM:SS    Idx          #   C/G     %     %     %     %   name
 20200925   09:44:10      0          -     -     -     -     -     -   -
 20200925   09:44:20      0      16156     C     -     -     -     -   gpu_burn
 20200925   09:44:30      0      16156     C     0     0     -     -   gpu_burn
 20200925   09:44:40      0          -     -     -     -     -     -   -
 20200925   09:44:50      0      26758     C    28    11     -     -   gpu_burn
 20200925   09:44:50      0      27486     C    17     7     -     -   gpu_burn

sm は Streaming Multiprocessor です。

オプションは以下の通りです。

$ nvidia-smi pmon help
Option "help" is not recognized.

    Usage: nvidia-smi pmon [options]

    Options include:
    [-i | --id]:          Comma separated Enumeration index, PCI bus ID or UUID
    [-d | --delay]:       Collection delay/interval in seconds [default=1sec, max=10secs]
    [-c | --count]:       Collect specified number of samples and exit
    [-s | --select]:      One or more metrics [default=u]
                          Can be any of the following:
                              u - Utilization
                              m - FB memory usage
    [-o | --options]:     One or more from the following:
                              D - Include Date (YYYYMMDD) in scrolling output
                              T - Include Time (HH:MM:SS) in scrolling output
    [-f | --filename]:    Log to a specified file, rather than to stdout
    [-h | --help]:        Display help information

検証環境

AWS上で次のEC2を利用して検証しました。

  • インスタンスタイプ : g4.xlarge
  • AMI : Deep Learning AMI Version 34.0
  • OS : Ubuntu 18.04

補足

EC2 の GPU 情報を CloudWatch で監視したい場合は、次のブログを参照ください。

nvidia-smi は GPU の監視だけでなく、電源・クロックといった管理も可能です。

nvidia-smi には Python バインディング(nvidia-ml-py)もあり、プログラマブルに操作することも可能です。

nvidia-ml-py · PyPI

参考