Skip to content

Commit 95c5e53

Browse files
authored
Merge pull request #21
Added the possibility to filter by user(s)
2 parents 64f2dd8 + 14ab754 commit 95c5e53

File tree

6 files changed

+59
-1
lines changed

6 files changed

+59
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.0.7
4+
5+
- Added CLI argument `--user` to filter processes by selected users.
6+
37
## 1.0.6
48

59
- Fixed coloring output. Thanks @tomix1024 !

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ A tool for enriching the output of `nvidia-smi`.
2222
otherwise print first 100 characters.
2323
-c|--color Colorize the output (green - free GPU, yellow -
2424
moderately used GPU, red - fully used GPU)
25+
-u|--user USER[,USER] Limit the list of processes to selected users
26+
(comma-separated).
2527

2628
Note: for backward compatibility, `nvidia-smi | nvidia-htop.py [-l [length]]` is also supported.
2729

nvidia-htop.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
parser = argparse.ArgumentParser()
3131
parser.add_argument('-l', '--command-length', default=20, const=100, type=int, nargs='?')
3232
parser.add_argument('-c', '--color', action='store_true')
33+
parser.add_argument('-u', '--user', default='', help="Limit the list of processes to selected users (comma-separated)")
3334
# only for testing
3435
parser.add_argument('-p', '--fake-ps', help="The list of processes to use instead of real output of `ps`")
3536

@@ -39,6 +40,7 @@
3940
command_length = args.command_length
4041
color = args.color
4142
fake_ps = args.fake_ps
43+
users = set(args.user.split(',')) if len(args.user) > 0 else None
4244

4345
# for testing, the stdin can be provided in a file
4446
fake_stdin_path = os.getenv("FAKE_STDIN_PATH", None)
@@ -132,6 +134,17 @@ def colorize(_lines):
132134
time = []
133135
command = []
134136

137+
fields = (
138+
gpu_num,
139+
pid,
140+
gpu_mem,
141+
user,
142+
cpu,
143+
mem,
144+
time,
145+
command,
146+
)
147+
135148
gpu_num_idx = 1
136149
pid_idx = 2 if not is_new_format else 4
137150
gpu_mem_idx = -3
@@ -167,12 +180,24 @@ def colorize(_lines):
167180
continue
168181
parts = re.split(r'\s+', line.strip(), 5)
169182
# idx = pid.index(parts[0])
183+
to_delete = [] # If the command is limited to selected users, we need to delete the other lines
170184
for idx in filter(lambda p: pid[p] == parts[0], range(len(pid))):
185+
if users is not None and parts[1] not in users:
186+
to_delete.append(idx)
187+
continue
171188
user[idx] = parts[1]
172189
cpu[idx] = parts[2]
173190
mem[idx] = parts[3]
174191
time[idx] = parts[4] if "-" not in parts[4] else parts[4].split("-")[0] + " days"
175192
command[idx] = parts[5]
193+
# Delete lines not corresponding to the selected users (if some are selected)
194+
for idx in reversed(sorted(to_delete)):
195+
for field in fields:
196+
del field[idx]
197+
198+
if len(pid) == 0:
199+
print("| " + no_running_process + " " * (73 - len(no_running_process)) + " |")
200+
sys.exit()
176201

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

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
long_description = (here / 'README.md').read_text(encoding='utf-8')
1010

1111
setup(name='nvidia-htop',
12-
version='1.0.6',
12+
version='1.0.7',
1313
description='A tool for enriching the output of nvidia-smi',
1414
long_description=long_description,
1515
long_description_content_type='text/markdown',
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Sun Aug 2 13:44:21 2020
2+
+-----------------------------------------------------------------------------+
3+
| NVIDIA-SMI 450.57 Driver Version: 450.57 CUDA Version: 11.0 |
4+
|-------------------------------+----------------------+----------------------+
5+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
6+
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
7+
| | | MIG M. |
8+
|===============================+======================+======================|
9+
| 0 GeForce RTX 2070 Off | 00000000:01:00.0 Off | N/A |
10+
| 0% 50C P2 27W / 175W | 749MiB / 7981MiB | 2% Default |
11+
| | | N/A |
12+
+-------------------------------+----------------------+----------------------+
13+
| 1 GeForce GTX 750 Ti Off | 00000000:04:00.0 On | N/A |
14+
| 33% 30C P8 1W / 46W | 871MiB / 2002MiB | 0% Default |
15+
| | | N/A |
16+
+-------------------------------+----------------------+----------------------+
17+
18+
+-----------------------------------------------------------------------------+
19+
| GPU PID USER GPU MEM %CPU %MEM TIME COMMAND |
20+
| 0 1032 root 745MiB 10.0 5.0 11:42:17 python 0.py |
21+
| 1 25544 test 139MiB 5.0 3.0 13:42:17 python 2.py |
22+
| 1 14518 root 1MiB 8.0 1.0 15:42:17 python3 3.py |
23+
| 1 13956 root 472MiB 2.0 0.0 16:42:17 python3 4.py |
24+
+-----------------------------------------------------------------------------+

test/test_main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def test_with_processes(self):
2222
def test_new_format(self):
2323
self.do_test('FAKE_STDIN_NEW_FORMAT', 'DESIRED_STDOUT_NEW_FORMAT')
2424

25+
def test_new_format_users(self):
26+
self.do_test('FAKE_STDIN_NEW_FORMAT', 'DESIRED_STDOUT_NEW_FORMAT_USERS', call_args=["-u", "root,test"])
27+
2528
def test_long_pids(self):
2629
self.do_test('FAKE_STDIN_LONG_PIDS', 'DESIRED_STDOUT_LONG_PIDS', fake_ps='FAKE_PS_LONG_PIDS')
2730

0 commit comments

Comments
 (0)