nvitop¶
简介¶
- nvitop 是一个用于监控和显示 NVIDIA GPU 使用情况的命令行工具。它可以提供实时的 GPU 统计信息,包括 GPU 的利用率、显存使用情况、温度、功耗等。它的界面类似于 Linux 系统中的 top 命令,但专门针对 NVIDIA GPU。
主要特点:
1.实时监控:显示 GPU 的实时使用情况,不同于nvidia-smi只单次显示。
2.详细信息:包括 GPU 的利用率、显存使用、温度、功耗等。
3.简洁易用:命令行界面简单明了。
安装环境¶
序号 |
集群 |
平台 |
版本 |
安装方式 |
---|---|---|---|---|
1 |
hpckapok2 |
Gpu |
1.3.2 |
conda |
以交互式的方法打开Nvidia-GPU的方法
$ source activate nvitop #加载conda虚拟环境
$ nvitop #在终端输入命令


快速开始¶
基于以下 API 监控 GPU 设备的最小脚本nvitop:
from nvitop import Device
devices = Device.all() # or Device.cuda.all()
for device in devices:
processes = device.processes() # type: Dict[int, GpuProcess]
sorted_pids = sorted(processes)
print(device)
print(f' - Fan speed: {device.fan_speed()}%')
print(f' - Temperature: {device.temperature()}C')
print(f' - GPU utilization: {device.gpu_utilization()}%')
print(f' - Total memory: {device.memory_total_human()}')
print(f' - Used memory: {device.memory_used_human()}')
print(f' - Free memory: {device.memory_free_human()}')
print(f' - Processes ({len(processes)}): {sorted_pids}')
for pid in sorted_pids:
print(f' - {processes[pid]}')
print('-' * 120)
python <yourscript>.py #执行脚本

另一种更清晰带着色的方法¶
import time
from nvitop import Device, GpuProcess, NA, colored
print(colored(time.strftime('%a %b %d %H:%M:%S %Y'), color='red', attrs=('bold',)))
devices = Device.cuda.all() # or `Device.all()` to use NVML ordinal instead
separator = False
for device in devices:
processes = device.processes() # type: Dict[int, GpuProcess]
print(colored(str(device), color='green', attrs=('bold',)))
print(colored(' - Fan speed: ', color='blue', attrs=('bold',)) + f'{device.fan_speed()}%')
print(colored(' - Temperature: ', color='blue', attrs=('bold',)) + f'{device.temperature()}C')
print(colored(' - GPU utilization: ', color='blue', attrs=('bold',)) + f'{device.gpu_utilization()}%')
print(colored(' - Total memory: ', color='blue', attrs=('bold',)) + f'{device.memory_total_human()}')
print(colored(' - Used memory: ', color='blue', attrs=('bold',)) + f'{device.memory_used_human()}')
print(colored(' - Free memory: ', color='blue', attrs=('bold',)) + f'{device.memory_free_human()}')
if len(processes) > 0:
processes = GpuProcess.take_snapshots(processes.values(), failsafe=True)
processes.sort(key=lambda process: (process.username, process.pid))
print(colored(f' - Processes ({len(processes)}):', color='blue', attrs=('bold',)))
fmt = ' {pid:<5} {username:<8} {cpu:>5} {host_memory:>8} {time:>8} {gpu_memory:>8} {sm:>3} {command:<}'.format
print(colored(fmt(pid='PID', username='USERNAME',
cpu='CPU%', host_memory='HOST-MEM', time='TIME',
gpu_memory='GPU-MEM', sm='SM%',
command='COMMAND'),
attrs=('bold',)))
for snapshot in processes:
print(fmt(pid=snapshot.pid,
username=snapshot.username[:7] + ('+' if len(snapshot.username) > 8 else snapshot.username[7:8]),
cpu=snapshot.cpu_percent, host_memory=snapshot.host_memory_human,
time=snapshot.running_time_human,
gpu_memory=(snapshot.gpu_memory_human if snapshot.gpu_memory_human is not NA else 'WDDM:N/A'),
sm=snapshot.gpu_sm_utilization,
command=snapshot.command))
else:
print(colored(' - No Running Processes', attrs=('bold',)))
if separator:
print('-' * 120)
separator = True
python <yourscript>.py #执行脚本

参考资料¶
Contributor:A君
最后更新:
2025 年 06 月 30 日