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

2024-02-25 07:52
文章标签 统计 使用 gpu 情况 nvidia smi

本文主要是介绍通过 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}
}

这篇关于通过 NVIDIA-SMI 统计GPU使用情况的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/744846

相关文章

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows