通过 NVIDIA-SMI 统计GPU使用情况

通过 NVIDIA-SMI 统计GPU使用情况

罗列本机显卡

nvidia-smi -L | --list-gpus

本示例的输出如下,也就是本机的显卡环境:

[name@gpu4 code]$ nvidia-smi -L
GPU 0: A40 (UUID: GPU-b0f3bdaa-e0e1-aff7-3cc7-9c067db6aea1)

nvidia-smi -f | --filename : 将查询结果存入文件而不是输出到终端
nvidia-smi -q | --query : 查看GPU或Unit信息,可结合以下几个参量同时使用达到特定目的:

  • -i | --id= : 仅针对指定设备ID的GPU查询,e.g. nvidia-smi -q -i 0 查看设备ID为0的GPU信息
  • -u | --unit : 查询Unit属性,不使用该参量默认查询GPU属性
  • -f | --filename= : 将查询结果保存到文件,屏蔽终端输出
  • -x | --xml-format : 生成xml格式的结果
  • -d | --display= : 有选择性地查询某些字段,支持的字段包括:memory, utilization, ecc, temperature, power, clock, compute, pids, performance, supported_clocks, page_retirement, accounting, encoder stats
  • -l | --loop= : 持续查询除非在指定秒间隔内检测到Ctrl+C中断
  • -lms | --loop-ms= :持续查询除非在指定的毫秒间隔内检测到Ctrl+C中断

自定义查询

nvidia-smi --query-gpu= : 按自定字段查询GPU信息,支持 -i | --id= , -f | --filename , -l | --loop= & -lms | --loop-ms 附加参量。可通过 --format= 指定查询信息以哪个格式输出,支持的格式类型有:

  • csv : comma separated values
  • noheader : skip the first line with column headers or field names
  • nounits : don’t print units for numerical values

支持的GPU字段包括:

  • timestamp : 查询时间,以"YYYY/MM/DD HH:MM:SS.msec"格式给出
  • driver_version : 以字符串格式给出当前安装的Nvidia显卡驱动版本
  • count : 显卡个数
  • namegpu_name : 官方给定的显卡名称
  • serialgpu_serial : 产品序列号,应与板载序列识别号一致,全球唯一
  • uuidgpu_uuid : 全球唯一设备编号,与板载识别号无关
  • pci.bus_idgpu_bus_id : 十六进制PCI总线编号 “domain🚌device.function”
  • pci.domain : 十六进制PCI域(domain number)
  • pci.bus :十六进制PCI总线 (bus number)
  • pci.device : 十六进制PCI设备 (device number)
  • pci.device_id : PCI vendor device id, in hex
  • pci.sub_device_id : PCI Sub System id, in hex
  • pcie.link.gen.current : the current pcie link generation, may be reduced when the GPU is not in use
  • pcie.link.gen.max : the maxium pcie link generation possible with this GPU and system configuration
  • pcie.link.width.current : the current pcie link width, may be reduced when the gpu is not in use
  • pcie.link.width.max : the ma pcie link width possible with this GPU and system configuration
  • index : GPU索引值,其实编号为0
  • display_mode :显卡是否连接显示器指示位,Enabled 表示有外接显示设备,其他情况 Disabled
  • display_active : 显示器是否经由GPU处理显示内容指示位,Enabled 表示GPU正负责某显示任务,Disabled表示其他情况。注意:即使显卡没有外接物理显示设备,该指示位依然可能是Enabled
  • persistence_mode : 是否出于persistence mode 指示位,Enabled 表示开启,Disabled 表示关闭。开启该模式后显卡驱动将常驻显存,降低显卡响应延时,仅Linux平台有效。
  • accounting.mode : 统计模式是否开启标识位,Enabled 或 Disabled。开启统计模式,占用显卡进程的信息将被统计便于进程执行期间查询或进程结束后查询。进程的总执行时间在进程结束之前保持为0,进程结束后更新为进程实际占用GPU时间。
  • accounting.buffer_size : 进程循环缓冲区大小,表示被统计进程的最大数量。缓存区中保持着当前被统计的进程,缓冲区满后新进程会覆盖旧进程。
  • driver_mode.current : 当前使用的驱动模式,在linux平台上该值为 N/A。 Windows平台支持TCC和WDDM两种模式,可通过 -dm-fdm 指定显卡驱动模式。TCC模式专为高性能计算优化,WDDM模式专为图形应用优化,高性能运算不建议使用WDDM模式。
  • driver_mode.pending : 预设驱动模式,下次设备重启后应用。linux平台总是为N/A
  • vbios_version : 板载BIOS版本
  • inforom.imginforom.image : Global version of the infoROM image. Image version just like VBIOS version uniquely describes the exact version of the infoROM flashed on the board in contrast to infoROM object version which is only an indicator of supported features.
  • inforom.oem : Version for the OEM configuration data.
  • inforom.ecc : Version for the ECC recording data.
  • inforom.pwr : Version for the power management data.
  • gom.currentgpu_operation_mode.current : 当前使用GOM。GOM支持通过禁用部分显卡特性来省电和提高吞吐量,可通过 --gom 进行模式切换,支持的模式包括:
    • All On :显卡功能全开,全速运行
    • Compute : 仅作为高性能运算,不支持图形操作
    • Low Double Precision :仅针对不需要高带宽、双精度计算的图形图像操作
  • gom.pending : 预设的GOM模式,设备下次重启时应用
  • fan.speed : 该值指示风扇应以多大功率运行并不是实际检测量,0表示不运行,100%表示全功率运行。如果风扇损坏或不能控制,则实际运行功率与该值可能不匹配。
  • pstate : 当前显卡性能状态等级,P0表示满级状态,P12表示最差等级状态
  • memory.total : 显卡总共可用内存
  • memory.used :显卡已消耗内存
  • memory.free :显卡闲置内存
  • compute_mode :The compute mode flag indicates whether individual or multiple compute applications may run on the GPU.
    • Default : means multiple contexts are allowed per device.
    • Exclusive_Process : means only one context is allowed per device, usable from multiple threads at a time.
    • Prohibited : means no contexts are allowed per device (no compute apps).
  • utilization.gpu : 指示在上一个采样间隔中GPU的繁忙程度,占用比。采样间隔根据产品不同,有的是1s,有的是1/6s
  • utilization.memory : 指示在上一个采样间隔中GPU繁忙程度,读写时间占比。
  • temperature.gpu : GPU核心温度

快捷查询

显卡时钟查询

查看GPU支持的时钟频率,我们可以使用 nvidia-smi --query-supported-clocks= 命令,该命令将遍历所有Memory Clock 和 Graphic Clock可能的组合,仅这里列举的时钟组合可以传递给 --applications-clocks 作为 参数。--query-support-clocks= 接受 timestamp , gpu_name , gpu_bus_id , gpu_serial , gpu_uuid , memorygraphics 等五个字段的组合。

  • timestamps : 查询时间,按照标准时间格式输出,见上文
  • gpu_name : 设备的官方名称
  • gpu_bus_id : 设备PCI 总线ID,格式见上文
  • gpu_serial :设备序列识别号,应与机身标定设备唯一序列识别号一致
  • gpu_uuid :设备唯一识别ID,与机身标识无关
  • memorymem : 支持的Memory Clock
  • graphicsgr : 支持的Graphics Clock

详细说明查看 nvidia-smi --help-query-supported-clocks 输出。

活跃进程查询

查看使用GPU设备的进程,我们可以使用 nvidia-smi --query-compute-apps= 命令。--query-compute-apps= 接受 timestamp , gpu_name , gpu_bus_id , gpu_serial , gpu_uuid , pid , used_gpu_memoryprocess_name 等字段的组合。

  • timestamps : 查询时间,按照标准时间格式输出,见上文
  • gpu_name : 设备的官方名称
  • gpu_bus_id : 设备PCI 总线ID,格式见上文
  • gpu_serial :设备序列识别号,应与机身标定设备唯一序列识别号一致
  • gpu_uuid :设备唯一识别ID,与机身标识无关
  • pid : 进程ID
  • progress_name 或 name : 进程名称
  • used_gpu_memoryused_memory : 进程占用的内存,在windows平台上当设备运行在WDDM模式下时该值不可用,因为显存由Windows KMD接管而非Nvidia驱动程序

详细说明查看 nvidia-smi --help-query-compute-apps 输出。

进程统计查询

查看被设备统计的进程,即在统计循环缓冲区中的进程,使用 nvidia-smi --query-accounted-apps= 命令。--query-accounted-apps= 接受timestamp , gpu_name , gpu_bus_id , gpu_serial , gpu_uuid , pid , gpu_utilization , mem_utilization , max_memory_usagetime 等字段的组合。

  • timestamps : 查询时间,按照标准时间格式输出,见上文
  • gpu_name : 设备的官方名称
  • gpu_bus_id : 设备PCI 总线ID,格式见上文
  • gpu_serial :设备序列识别号,应与机身标定设备唯一序列识别号一致
  • gpu_uuid :设备唯一识别ID,与机身标识无关
  • pid : 进程ID
  • gpu_utilization or gpu_util :GPU使用
  • mem_utilization or mem_util :进程显存使用占比
  • max_memory_usage :进程最大内存占用量
  • time :进程活动时常,单位ms

详细说明查看 nvidia-smi --help-query-accounted-apps 输出。

实例

控制台以csv格式输出想要的信息

nvidia-smi --query-gpu=timestamp,driver_version,count,name,index,display_mode,memory.total,pstate,memory.used,memory.free,utilization.gpu,utilization.memory,temperature.gpu --format=csv  -lms 100 

以固定的时长检测信息

我原先的实现方法是通过bash中的 sleep 0.1 实现,但是发现其并不能精确的统计,因为终端输出到屏幕之间的IO需要时间,而且每次输出都有表头出现。主要问题有:

  1. 如果我想要用 --format=noheader 去掉表头,则提示:Format modifier --format=csv is mandatory. 难以去掉,每一次统计都带有表头。有些许强迫症的我实在难以忍受!
  2. 多次尝试发现以 sleep 0.1 ,则 1s 大约只能输出8次,也就是额外消耗了 200ms。

因此,选用 timeout 则完美解决了上述问题。

# 原理是加上 timeout
## 比如想要运行2s,则
timeout 2 nvidia-smi --query-gpu=timestamp,driver_version,count,name,index,display_mode,memory.total,pstate,memory.used,memory.free,utilization.gpu,utilization.memory,temperature.gpu --format=csv  -lms 100 

输出如下:

timestamp, driver_version, count, name, index, display_mode, memory.total [MiB], pstate, memory.used [MiB], memory.free [MiB], utilization.gpu [%], utilization.memory [%], temperature.gpu
2024/02/24 15:26:22.296, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 100 %, 77 %, 61
2024/02/24 15:26:22.397, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 100 %, 77 %, 61
2024/02/24 15:26:22.499, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 97 %, 82 %, 61
2024/02/24 15:26:22.600, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 97 %, 82 %, 62
2024/02/24 15:26:22.701, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 98 %, 71 %, 62
2024/02/24 15:26:22.803, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 98 %, 71 %, 61
2024/02/24 15:26:22.904, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 96 %, 74 %, 61
2024/02/24 15:26:23.005, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 96 %, 74 %, 61
2024/02/24 15:26:23.106, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 98 %, 81 %, 62
2024/02/24 15:26:23.207, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 98 %, 81 %, 62
2024/02/24 15:26:23.308, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 100 %, 79 %, 62
2024/02/24 15:26:23.410, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 100 %, 79 %, 59
2024/02/24 15:26:23.512, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 100 %, 79 %, 58
2024/02/24 15:26:23.613, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 13 %, 6 %, 57
2024/02/24 15:26:23.714, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 13 %, 6 %, 56
2024/02/24 15:26:23.816, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 0 %, 0 %, 56
2024/02/24 15:26:23.917, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 0 %, 0 %, 56
2024/02/24 15:26:24.021, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 0 %, 0 %, 56
2024/02/24 15:26:24.122, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 0 %, 0 %, 56
2024/02/24 15:26:24.223, 460.91.03, 1, A40, 0, Enabled, 45634 MiB, P0, 17041 MiB, 28593 MiB, 0 %, 0 %, 55

保存CSV信息到文件

# 原生方法
nvidia-smi --query-gpu=timestamp,driver_version,count,name,index,display_mode,memory.total,pstate,memory.used,memory.free,utilization.gpu,utilization.memory,temperature.gpu --format=csv  -lms 100 --filename=gpu_info.log

# 第三方实现,主要是在结尾增加:2>&1 | tee gpu_info.log
nvidia-smi --query-gpu=timestamp,driver_version,count,name,index,display_mode,memory.total,pstate,memory.used,memory.free,utilization.gpu,utilization.memory,temperature.gpu --format=csv  -lms 100 2>&1 | tee gpu_info.log

可能你需要的

监测20分钟,然后保存到 gpu_info.log 文件。并且在后台运行,不进行屏幕输出。

timeout 20m nvidia-smi --query-gpu=timestamp,driver_version,count,name,index,display_mode,memory.total,pstate,memory.used,memory.free,utilization.gpu,utilization.memory,temperature.gpu --format=csv  -lms 100 --filename=gpu_info.log &

reference

@misc{Albert2022Jul,
author = {Albert},
title = {{nvidia-smi Cheat Sheet { ∣ \vert } SeiMaxim}},
year = {2022},
month = jul,
urldate = {2024-02-24},
language = {english},
note = {[Online; accessed 24. Feb. 2024]},
url = {https://www.seimaxim.com/kb/gpu/nvidia-smi-cheat-sheet}
}
@misc{BibEntry2024Feb,
title = {{每天学习一个命令xn–ih7ctimeout 命令{ _ \_ _}timeout不是内部或外部命令-CSDN博客}},
year = {2024},
month = feb,
urldate = {2024-02-24},
language = {chinese},
note = {[Online; accessed 24. Feb. 2024]},
url = {https://blog.csdn.net/weixin_42816196/article/details/100573826}
}

  • 31
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

或许,这就是梦想吧!

如果对你有用,欢迎打赏。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值