本文介绍了如何自定义nvidia-smi的输出以显示PID用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

nvidia-smi的正常输出如下:

the normal output of nvidia-smi looks like this:

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的用户名:

As you can see it shows the list of PIDs which are running the CPU. However I also want to know the names of the PIDs. Can I customize the output to show the username of each PID ? I already know how to show username of individual PID:

ps -u -p $pid

请帮助我.非常感谢.

更新:我已经在下面发布了适用于我的解决方案.对于那些需要详细的GPU信息的人,我还将其作为简单的脚本上传到了Github:

UPDATE: I've posted the solution that worked for me below. I've also uploaded this to Github as a simple script for those who need detailed GPU information:

https://github.com/ManhTruongDang/check-gpu

推荐答案

这是我能想到的最好的方法:

This is the best I could come up with:

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_

脚本的简短说明:

  • 尾巴 head 删除多余的行

Sed 删除空格(此后,每列将仅被1个空格分隔)

Sed to remove spaces (after this, each column would only be separated by 1 space)

剪切提取相关列

输出是PID列表,每个PID占用1行.我们只需要使用 ps -up 来显示相关信息

The output is a list of PIDs, each occupying 1 line. We only need to use ps -up to show the relevant information

更新:一个更好的解决方案:

UPDATE: A better solution:

ps -up `nvidia-smi |tee /dev/stderr |tail -n +16 | head -n -1 | sed 's/\s\s*/ /g' | cut -d' ' -f3`

这样, nvidia-smi 只需调用一次.另请参阅:

This way, nvidia-smi would have to be called only once.See also:

更新2:我已将其作为简单脚本上传到Github,以供需要详细GPU信息的人使用.

UPDATE 2: I've uploaded this to Github as a simple script for those who need detailed GPU information.

https://github.com/ManhTruongDang/check-gpu

这篇关于如何自定义nvidia-smi的输出以显示PID用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:04