Skip to content

Commit

Permalink
Improved display of long PIDs and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peci1 committed Aug 31, 2021
1 parent bbec8f8 commit 2f5735c
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 13 deletions.
21 changes: 12 additions & 9 deletions nvidia-htop.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ def colorize(_lines):
if color:
lines_to_print = colorize(lines_to_print)

for line in lines_to_print:
# we print all but the last line which is the +---+ separator
for line in lines_to_print[:-1]:
print(line)

no_running_process = "No running processes found"
if no_running_process in lines[i] or lines[i].startswith("+--"):
print(lines[-1].strip())
print("| " + no_running_process + " " * (73 - len(no_running_process)) + " |")
# Issue #9, running inside docker and seeing no processes
if lines[i].startswith("+--"):
Expand Down Expand Up @@ -165,15 +167,16 @@ def colorize(_lines):
time[idx] = parts[4] if "-" not in parts[4] else parts[4].split("-")[0] + " days"
command[idx] = parts[5]

max_pid_length = max([len(str(x)) for x in pid])
if max_pid_length >= 6:
command_length -= (max_pid_length - 6)
max_pid_length = max(5, max([len(x) for x in pid]))
format = ("| %3s %" + str(max_pid_length) + "s %8s %8s %5s %5s %9s %-" + str(command_length) + "." + str(command_length) + "s |")

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

print(format % (
line = format % (
"GPU", "PID", "USER", "GPU MEM", "%CPU", "%MEM", "TIME", "COMMAND"
))
)

print("+" + ("-" * (len(line) - 2)) + "+")

print(line)

for i in range(len(pid)):
print(format % (
Expand All @@ -187,4 +190,4 @@ def colorize(_lines):
command[i]
))

print(lines[-1])
print("+" + ("-" * (len(line) - 2)) + "+")
4 changes: 2 additions & 2 deletions test/DESIRED_STDOUT_L
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Mon May 21 14:07:29 2018
| 46% 69C P2 249W / 250W | 7773MiB / 11178MiB | 47% Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GPU PID USER GPU MEM %CPU %MEM TIME COMMAND |
| 0 1032 root 10781MiB 10.0 5.0 11:42:17 python 0.py |
| 1 11021 admin 10765MiB 1.0 4.0 12:42:17 python3 1.py |
Expand All @@ -45,4 +45,4 @@ Mon May 21 14:07:29 2018
| 6 11527 nobody 10221MiB 5.0 10.0 01:42:17 python 9.py |
| 6 27159 hacker 577MiB 0.0 20.0 00:00:00 python3.5 10.py |
| 7 9908 nvidia 7755MiB 1.0 50.0 89 days python very_long_very_long_very_long_very_long_very_long_very_long_very_long.py |
+-----------------------------------------------------------------------------+
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
4 changes: 2 additions & 2 deletions test/DESIRED_STDOUT_L150
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Mon May 21 14:07:29 2018
| 46% 69C P2 249W / 250W | 7773MiB / 11178MiB | 47% Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GPU PID USER GPU MEM %CPU %MEM TIME COMMAND |
| 0 1032 root 10781MiB 10.0 5.0 11:42:17 python 0.py |
| 1 11021 admin 10765MiB 1.0 4.0 12:42:17 python3 1.py |
Expand All @@ -45,4 +45,4 @@ Mon May 21 14:07:29 2018
| 6 11527 nobody 10221MiB 5.0 10.0 01:42:17 python 9.py |
| 6 27159 hacker 577MiB 0.0 20.0 00:00:00 python3.5 10.py |
| 7 9908 nvidia 7755MiB 1.0 50.0 89 days python very_long_very_long_very_long_very_long_very_long_very_long_very_long.py |
+-----------------------------------------------------------------------------+
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
26 changes: 26 additions & 0 deletions test/DESIRED_STDOUT_LONG_PIDS
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Sun Aug 2 13:44:21 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.57 Driver Version: 450.57 CUDA Version: 11.0 |
|-------------------------------+----------------------+----------------------+
| 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 2070 Off | 00000000:01:00.0 Off | N/A |
| 0% 50C P2 27W / 175W | 749MiB / 7981MiB | 2% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
| 1 GeForce GTX 750 Ti Off | 00000000:04:00.0 On | N/A |
| 33% 30C P8 1W / 46W | 871MiB / 2002MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+

+-------------------------------------------------------------------------------+
| GPU PID USER GPU MEM %CPU %MEM TIME COMMAND |
| 0 1032 root 745MiB 10.0 5.0 11:42:17 python 0.py |
| 1 1111021 admin 244MiB 1.0 4.0 12:42:17 python3 1.py |
| 1 25544 test 139MiB 5.0 3.0 13:42:17 python 2.py |
| 1 4755 user1 3MiB 3.0 2.0 14:42:17 /opt/software/MATLAB |
| 1 1114518 root 1MiB 8.0 1.0 15:42:17 python3 3.py |
| 1 13956 root 472MiB 2.0 0.0 16:42:17 python3 4.py |
+-------------------------------------------------------------------------------+
14 changes: 14 additions & 0 deletions test/FAKE_PS_LONG_PIDS
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
PID USER %CPU %MEM ELAPSED COMMAND
1032 root 10.0 5.0 11:42:17 python 0.py
1111021 admin 1.0 4.0 12:42:17 python3 1.py
25544 test 5.0 3.0 13:42:17 python 2.py
4755 user1 3.0 2.0 14:42:17 /opt/software/MATLAB/matlab94/bin/glnxa64/MATLAB script.m
1114518 root 8.0 1.0 15:42:17 python3 3.py
13956 root 2.0 0.0 16:42:17 python3 4.py
14293 root 9.0 6.0 17:42:17 python3 5.py
14294 user2 50.0 7.0 18:42:17 python3 6.py
14295 user3 99.0 8.0 19:42:17 python3 7.py
17547 root 230.0 9.0 30:42:17 python3 8.py
11527 nobody 5.0 10.0 01:42:17 python 9.py
27159 hacker 0.0 20.0 00:00:00 python3.5 10.py
9908 nvidia 1.0 50.0 89-03:41:29 python very_long_very_long_very_long_very_long_very_long_very_long_very_long.py
29 changes: 29 additions & 0 deletions test/FAKE_STDIN_LONG_PIDS
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Sun Aug 2 13:44:21 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.57 Driver Version: 450.57 CUDA Version: 11.0 |
|-------------------------------+----------------------+----------------------+
| 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 2070 Off | 00000000:01:00.0 Off | N/A |
| 0% 50C P2 27W / 175W | 749MiB / 7981MiB | 2% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
| 1 GeForce GTX 750 Ti Off | 00000000:04:00.0 On | N/A |
| 33% 30C P8 1W / 46W | 871MiB / 2002MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| 0 N/A N/A 1032 C python 745MiB |
| 1 N/A N/A 1111021 G python3 244MiB |
| 1 N/A N/A 25544 G python 139MiB |
| 1 N/A N/A 4755 G ...matlab94/bin/glnxa64/MATLAB 3MiB |
| 1 N/A N/A 1114518 G python3 1MiB |
| 1 N/A N/A 13956 G python3 472MiB |
+-----------------------------------------------------------------------------+
3 changes: 3 additions & 0 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def test_with_processes(self):
def test_new_format(self):
self.do_test('FAKE_STDIN_NEW_FORMAT', 'DESIRED_STDOUT_NEW_FORMAT')

def test_long_pids(self):
self.do_test('FAKE_STDIN_LONG_PIDS', 'DESIRED_STDOUT_LONG_PIDS', fake_ps='FAKE_PS_LONG_PIDS')

def test_with_processes_color(self):
self.do_test('FAKE_STDIN', 'DESIRED_STDOUT_COLOR', call_args=["-c"])

Expand Down

0 comments on commit 2f5735c

Please sign in to comment.