peci1/nvidia-htop

Failed to align when lengths of PID are greater than 5.

lixinye-nju opened this issue · 7 comments

The application is very convenient but I ran into alignment issues when the PID is too long.

+-----------------------------------------------------------------------------+
|  GPU   PID     USER    GPU MEM  %CPU  %MEM      TIME  COMMAND               |
|    0  3753     root       4MiB   0.0   0.0  18:13:36  /usr/lib/xorg/Xorg v  |
|    0 164425     xxxx   10647MiB   106   0.3     35:59  python main.py -i 6   |
|    1  3753     root       4MiB   0.0   0.0  18:13:36  /usr/lib/xorg/Xorg v  |
|    1 161143     xxxx   10923MiB   100   0.6     59:46  python main.py -i 1   |
|    2  3753     root       4MiB   0.0   0.0  18:13:36  /usr/lib/xorg/Xorg v  |
|    2 161102     xxxx   10731MiB   100   0.6  01:00:02  python main.py -i 1   |
|    3  3753     root       4MiB   0.0   0.0  18:13:36  /usr/lib/xorg/Xorg v  |
|    3 160197     xxxx   10195MiB   141   0.6  01:00:49  python main.py -i 1   |
|    4  3753     root       4MiB   0.0   0.0  18:13:36  /usr/lib/xorg/Xorg v  |
|    4 160481     xxxx   11099MiB   101   0.4  01:00:40  python main.py -i 3   |
|    5  3753     root       4MiB   0.0   0.0  18:13:36  /usr/lib/xorg/Xorg v  |
|    5 159486     xxxx   10761MiB   101   0.4  01:01:59  python main.py -i 3   |
|    6  3753     root       4MiB   0.0   0.0  18:13:36  /usr/lib/xorg/Xorg v  |
|    6 159424     xxxx   10623MiB  98.2   0.5  01:02:30  python main.py -i 5   |
|    7  3753     root       4MiB   0.0   0.0  18:13:36  /usr/lib/xorg/Xorg v  |
|    7 159383     xxxx   10311MiB  98.7   0.5  01:02:53  python main.py -i 5   |
+-----------------------------------------------------------------------------+

I patched this with the following starting on line 167:

+ max_pid_length = max([len(x) for x in pid])
+ if max_pid_length >= 6:
+     command_length -= (max_pid_length - 6)

+ format = ("|  %3s %" + str(max_pid_length) + "s %8s   %8s %5s %5s %9s  %-" + str(command_length) + "." + str(command_length) + "s |")
- format = ("|  %3s %5s %8s   %8s %5s %5s %9s  %-" + str(command_length) + "." + str(command_length) + "s  |")

This worked for me.

peci1 commented

Thanks, the proposed patch looks good. Do you want to send it as a pull request to get the proper credit for it?

Thanks. But I install nvidia-htop through pip and it is inconvenient to modify the code on multiple machines. I really appreciate it if you send it as a pull request.

I patched this with the following starting on line 167:

+ max_pid_length = max([len(x) for x in pid])
+ if max_pid_length >= 6:
+     command_length -= (max_pid_length - 6)

+ format = ("|  %3s %" + str(max_pid_length) + "s %8s   %8s %5s %5s %9s  %-" + str(command_length) + "." + str(command_length) + "s |")
- format = ("|  %3s %5s %8s   %8s %5s %5s %9s  %-" + str(command_length) + "." + str(command_length) + "s  |")

This worked for me.

peci1 commented

No problem, I just wanted to give you the opportunity to claim the credit. I'll make the release shortly

You're good! I was in a meeting. I don't care about the credit, it's just an if statement.

peci1 commented

The release is out. I needed to do a bit more changes to satisfy the automated tests.

I also added a test for your case with longer PIDs. Could you please review files https://github.com/peci1/nvidia-htop/blob/master/test/FAKE_STDIN_LONG_PIDS and https://github.com/peci1/nvidia-htop/blob/master/test/FAKE_PS_LONG_PIDS and report to me whether your output looks similar (mostly regarding to the number of spaces/lengths of rows). The first one corresponds to the output of nvidia-smi, the latter one to the output of ps -o "pid,user,%cpu,%mem,etime,command".

The PIDs I have are in the 7-digits while @rookielxy had 6-digits, so I think it's good. Here's my nvidia-smi:

Tue Aug 31 12:10:51 2021       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.91.03    Driver Version: 460.91.03    CUDA Version: 11.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  GeForce RTX 3090    Off  | 00000000:01:00.0  On |                  N/A |
|  0%   43C    P8    25W / 350W |  19515MiB / 24265MiB |      4%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   1  GeForce RTX 3090    Off  | 00000000:21:00.0 Off |                  N/A |
|  0%   44C    P8    25W / 350W |  16219MiB / 24268MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A    658723      C   .../jupyterNLPenv/bin/python    18887MiB |
|    0   N/A  N/A   1429000      G   /usr/bin/gnome-shell               57MiB |
|    1   N/A  N/A    658723      C   .../jupyterNLPenv/bin/python    16201MiB |
+-----------------------------------------------------------------------------+

Here are my PIDs from ps -o "pid,user,%cpu,%mem,etime,command" as well:

    PID USER     %CPU %MEM     ELAPSED COMMAND
1429344 sjwhitak  0.0  0.0       52:42 bash
1429538 sjwhitak  9.1  0.8       52:00 /usr/lib/firefox/firefox
1429624 sjwhitak  6.5  0.8       52:00 /usr/lib/firefox/firefox -contentproc -c
1429671 sjwhitak  0.4  0.2       52:00 /usr/lib/firefox/firefox -contentproc -c
1429760 sjwhitak  0.0  0.1       51:52 /usr/lib/firefox/firefox -contentproc -c
1429824 sjwhitak  0.0  0.0       51:46 /usr/lib/firefox/firefox -contentproc -p
1440708 sjwhitak  0.0  0.0       00:00 ps -o pid,user,%cpu,%mem,etime,command

and this is nvidia-htop without the header (it's the same as nvidia-smi):

+-----------------------------------------------------------------------------+
|  GPU     PID     USER    GPU MEM  %CPU  %MEM      TIME  COMMAND             |
|    0  658723  xxxxxxx   18887MiB   1.1  10.1    2 days  /home/xxxxxxx/jupyt |
|    0 1429000 sjwhitak      36MiB   1.5   0.4     56:20  /usr/bin/gnome-shel |
|    1  658723  xxxxxxx   16201MiB   1.1  10.1    2 days  /home/xxxxxxx/jupyt |
+-----------------------------------------------------------------------------+

I'm not really sure what you want me to verify, sorry. I'd say the test files look good to me, though.