如何自定义 nvidia-smi 的输出以显示 PID 用户名
How to customize nvidia-smi 's output to show PID username
nvidia-smi 的正常输出如下所示:
Thu May 10 09:05:07 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.111 Driver Version: 384.111 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 108... Off | 00000000:0A:00.0 Off | N/A |
| 61% 74C P2 195W / 250W | 5409MiB / 11172MiB | 100% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 5973 C ...master_JPG/build/tools/program_pytho.bin 4862MiB |
| 0 46324 C python 537MiB |
+-----------------------------------------------------------------------------+
如您所见,它显示了 运行 和 CPU 的 PID 列表。但是我也想知道 PID 的名称。我可以自定义输出以显示每个 PID 的用户名吗?我已经知道如何显示个人 PID 的用户名:
ps -u -p $pid
请帮助我。非常感谢。
更新:我在下面发布了对我有用的解决方案。对于需要详细 GPU 信息的人,我还将其作为简单脚本上传到 Github:
这是我能想到的最好的:
nvidia-smi
ps -up `nvidia-smi |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3`
示例输出:
Thu May 10 15:23:08 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.111 Driver Version: 384.111 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 108... Off | 00000000:0A:00.0 Off | N/A |
| 41% 59C P2 251W / 250W | 5409MiB / 11172MiB | 100% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1606 C ...master_JPG/build/tools/program.bin 4862MiB |
| 0 15314 C python 537MiB |
+-----------------------------------------------------------------------------+
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user111+ 1606 134 4.8 32980224 789164 pts/19 Rl+ 15:23 0:08 /home/user111
user2 15314 0.4 10.0 17936788 1647040 pts/16 Sl+ 10:41 1:20 python server_
脚本的简短说明:
Tail
和 head
删除多余的行
Sed
删除 spaces(此后,每列将仅由 1 space 分隔)
Cut
提取相关列
输出的是PID列表,每个占1行。我们只需要使用ps -up
来显示相关信息
更新:更好的解决方案:
ps -up `nvidia-smi |tee /dev/stderr |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3`
这样,nvidia-smi
只需调用一次。
另见:
更新 2:我已将此作为简单脚本上传到 Github,供那些需要详细 GPU 信息的人使用。
我创建了一个脚本,它接受 nvidia-smi 输出并用更多信息丰富它:https://github.com/peci1/nvidia-htop .
它是一个 python 脚本,解析 GPU 进程列表,解析 PID,通过 ps
运行它们以收集更多信息,然后替换 nvidia-smi
的进程包含丰富列表的列表。
使用示例:
$ nvidia-smi | nvidia-htop.py -l
Mon May 21 15:06:35 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 390.25 Driver Version: 390.25 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 108... Off | 00000000:04:00.0 Off | N/A |
| 53% 75C P2 174W / 250W | 10807MiB / 11178MiB | 97% Default |
+-------------------------------+----------------------+----------------------+
| 1 GeForce GTX 108... Off | 00000000:05:00.0 Off | N/A |
| 66% 82C P2 220W / 250W | 10783MiB / 11178MiB | 100% Default |
+-------------------------------+----------------------+----------------------+
| 2 GeForce GTX 108... Off | 00000000:08:00.0 Off | N/A |
| 45% 67C P2 85W / 250W | 10793MiB / 11178MiB | 51% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| GPU PID USER GPU MEM %MEM %CPU COMMAND |
| 0 1032 anonymou 10781MiB 308 3.7 python train_image_classifier.py --train_dir=/mnt/xxxxxxxx/xxxxxxxx/xxxxxxxx/xxxxxxx/xxxxxxxxxxxxxxx |
| 1 11021 cannotte 10765MiB 114 1.5 python3 ./train.py --flagfile /xxxxxxxx/xxxxxxxx/xxxxxxxx/xxxxxxxxx/xx/xxxxxxxxxxxxxxx |
| 2 25544 nevermin 10775MiB 108 2.0 python -m xxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
+-----------------------------------------------------------------------------+
我是用 nvidia-smi -q -x
做的,它是 nvidia-smi
的 XML 样式输出
ps -up `nvidia-smi -q -x | grep pid | sed -e 's/<pid>//g' -e 's/<\/pid>//g' -e 's/^[[:space:]]*//'`
Jay Stanley,我可以使用 xargs
为 Junwon Lee 的命令添加别名,如下所示:
alias gpu_user_usage="nvidia-smi -q -x | grep pid | sed -e 's/<pid>//g' -e 's/<\/pid>//g' -e 's/^[[:space:]]*//' | xargs ps -up"
(由于声誉限制,我无法发表评论...)
以前的解决方案不起作用,所以我 post 这里我的解决方案。
我用的NVIDIA-SMI的版本是440.44,不过我觉得无所谓。
nvidia-smi | tee /dev/stderr | awk '/ C / {print }' | xargs -r ps -up
一点解释:
tee
: 避免两次调用 nvidia-smi
awk
: 抓取计算进程的PID列(类型C)
xargs -r
: -r
检查输入是否为空,以避免 ps -up
出现不需要的错误信息
如果你想让它成为.bash_profile
或.bashrc
中的别名:
alias nvidia-smi2='nvidia-smi | tee /dev/stderr | awk "/ C / {print $3}" | xargs -r ps -up'
区别是在
之前必须转义。
正如 Robert 的评论所建议的那样,这个答案 建议使用 gpustat,我发现它非常有用
gpustat -up
[0] NVIDIA GeForce GTX 1080 Ti | 90'C, 73 % | 6821 / 11178 MB | user1/732124(6817M)
[1] NVIDIA GeForce GTX 1080 Ti | 63'C, 0 % | 7966 / 11178 MB | user2/268172(1287M) user3/735496(6675M)
[2] NVIDIA GeForce GTX 1080 Ti | 66'C, 13 % | 2578 / 11178 MB | user2/268478(1287M) user2/725391(1287M)
[3] NVIDIA GeForce GTX 1080 Ti | 58'C, 0 % | 1291 / 11178 MB | user2/726058(1287M)
nvidia-smi 的正常输出如下所示:
Thu May 10 09:05:07 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.111 Driver Version: 384.111 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 108... Off | 00000000:0A:00.0 Off | N/A |
| 61% 74C P2 195W / 250W | 5409MiB / 11172MiB | 100% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 5973 C ...master_JPG/build/tools/program_pytho.bin 4862MiB |
| 0 46324 C python 537MiB |
+-----------------------------------------------------------------------------+
如您所见,它显示了 运行 和 CPU 的 PID 列表。但是我也想知道 PID 的名称。我可以自定义输出以显示每个 PID 的用户名吗?我已经知道如何显示个人 PID 的用户名:
ps -u -p $pid
请帮助我。非常感谢。
更新:我在下面发布了对我有用的解决方案。对于需要详细 GPU 信息的人,我还将其作为简单脚本上传到 Github:
这是我能想到的最好的:
nvidia-smi
ps -up `nvidia-smi |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3`
示例输出:
Thu May 10 15:23:08 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.111 Driver Version: 384.111 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 108... Off | 00000000:0A:00.0 Off | N/A |
| 41% 59C P2 251W / 250W | 5409MiB / 11172MiB | 100% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1606 C ...master_JPG/build/tools/program.bin 4862MiB |
| 0 15314 C python 537MiB |
+-----------------------------------------------------------------------------+
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user111+ 1606 134 4.8 32980224 789164 pts/19 Rl+ 15:23 0:08 /home/user111
user2 15314 0.4 10.0 17936788 1647040 pts/16 Sl+ 10:41 1:20 python server_
脚本的简短说明:
Tail
和head
删除多余的行Sed
删除 spaces(此后,每列将仅由 1 space 分隔)Cut
提取相关列
输出的是PID列表,每个占1行。我们只需要使用ps -up
来显示相关信息
更新:更好的解决方案:
ps -up `nvidia-smi |tee /dev/stderr |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3`
这样,nvidia-smi
只需调用一次。
另见:
更新 2:我已将此作为简单脚本上传到 Github,供那些需要详细 GPU 信息的人使用。
我创建了一个脚本,它接受 nvidia-smi 输出并用更多信息丰富它:https://github.com/peci1/nvidia-htop .
它是一个 python 脚本,解析 GPU 进程列表,解析 PID,通过 ps
运行它们以收集更多信息,然后替换 nvidia-smi
的进程包含丰富列表的列表。
使用示例:
$ nvidia-smi | nvidia-htop.py -l
Mon May 21 15:06:35 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 390.25 Driver Version: 390.25 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 108... Off | 00000000:04:00.0 Off | N/A |
| 53% 75C P2 174W / 250W | 10807MiB / 11178MiB | 97% Default |
+-------------------------------+----------------------+----------------------+
| 1 GeForce GTX 108... Off | 00000000:05:00.0 Off | N/A |
| 66% 82C P2 220W / 250W | 10783MiB / 11178MiB | 100% Default |
+-------------------------------+----------------------+----------------------+
| 2 GeForce GTX 108... Off | 00000000:08:00.0 Off | N/A |
| 45% 67C P2 85W / 250W | 10793MiB / 11178MiB | 51% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| GPU PID USER GPU MEM %MEM %CPU COMMAND |
| 0 1032 anonymou 10781MiB 308 3.7 python train_image_classifier.py --train_dir=/mnt/xxxxxxxx/xxxxxxxx/xxxxxxxx/xxxxxxx/xxxxxxxxxxxxxxx |
| 1 11021 cannotte 10765MiB 114 1.5 python3 ./train.py --flagfile /xxxxxxxx/xxxxxxxx/xxxxxxxx/xxxxxxxxx/xx/xxxxxxxxxxxxxxx |
| 2 25544 nevermin 10775MiB 108 2.0 python -m xxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
+-----------------------------------------------------------------------------+
我是用 nvidia-smi -q -x
做的,它是 nvidia-smi
ps -up `nvidia-smi -q -x | grep pid | sed -e 's/<pid>//g' -e 's/<\/pid>//g' -e 's/^[[:space:]]*//'`
Jay Stanley,我可以使用 xargs
为 Junwon Lee 的命令添加别名,如下所示:
alias gpu_user_usage="nvidia-smi -q -x | grep pid | sed -e 's/<pid>//g' -e 's/<\/pid>//g' -e 's/^[[:space:]]*//' | xargs ps -up"
(由于声誉限制,我无法发表评论...)
以前的解决方案不起作用,所以我 post 这里我的解决方案。 我用的NVIDIA-SMI的版本是440.44,不过我觉得无所谓。
nvidia-smi | tee /dev/stderr | awk '/ C / {print }' | xargs -r ps -up
一点解释:
tee
: 避免两次调用 nvidia-smiawk
: 抓取计算进程的PID列(类型C)xargs -r
:-r
检查输入是否为空,以避免ps -up
出现不需要的错误信息
如果你想让它成为.bash_profile
或.bashrc
中的别名:
alias nvidia-smi2='nvidia-smi | tee /dev/stderr | awk "/ C / {print $3}" | xargs -r ps -up'
区别是在之前必须转义。
正如 Robert 的评论所建议的那样,这个答案 建议使用 gpustat,我发现它非常有用
gpustat -up
[0] NVIDIA GeForce GTX 1080 Ti | 90'C, 73 % | 6821 / 11178 MB | user1/732124(6817M)
[1] NVIDIA GeForce GTX 1080 Ti | 63'C, 0 % | 7966 / 11178 MB | user2/268172(1287M) user3/735496(6675M)
[2] NVIDIA GeForce GTX 1080 Ti | 66'C, 13 % | 2578 / 11178 MB | user2/268478(1287M) user2/725391(1287M)
[3] NVIDIA GeForce GTX 1080 Ti | 58'C, 0 % | 1291 / 11178 MB | user2/726058(1287M)