通过 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

相关文章

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Ubuntu如何分配​​未使用的空间

《Ubuntu如何分配​​未使用的空间》Ubuntu磁盘空间不足,实际未分配空间8.2G因LVM卷组名称格式差异(双破折号误写)导致无法扩展,确认正确卷组名后,使用lvextend和resize2fs... 目录1:原因2:操作3:报错5:解决问题:确认卷组名称​6:再次操作7:验证扩展是否成功8:问题已解

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构