Skip to content

Commit e003010

Browse files
committed
Added test for -i arguemnt, issue a warning when it is used incorrectly.
1 parent 930ee89 commit e003010

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ A tool for enriching the output of `nvidia-smi`.
2424
moderately used GPU, red - fully used GPU)
2525
-u|--user USER[,USER] Limit the list of processes to selected users
2626
(comma-separated).
27-
-i|--id ID[,ID] Limit the command to selected GPU IDs (comma-separated).
27+
-i|--id ID[,ID[,ID]] Limit the command to selected GPU IDs (comma-separated).
2828

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

nvidia-htop.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
# is provided, use it as the commandline length,
1111
# otherwise print first 100 characters.
1212
# -c|--color Colorize the output (green - free GPU, yellow -
13-
# moderately used GPU, red - fully used GPU)
14-
# -i|--id ID Limit the command to selected GPU IDs (comma-separated)
13+
# moderately used GPU, red - fully used GPU).
14+
# -i|--id ID[,ID[,ID...]] Limit the command to selected GPU IDs (comma-separated).
1515
######
1616

1717
import sys
@@ -55,15 +55,18 @@
5555
lines = f.readlines()
5656
elif stdin_lines:
5757
lines = stdin_lines
58+
if len(args.id) > 0:
59+
print('nvidia-htop argument -i/--id cannot be used when nvidia-smi output is being piped into it. To filter the'
60+
' shown GPUs, pass the -i argument to the nvidia-smi call instead.', file=sys.stderr)
5861
else:
5962
nvidiasmi_args = []
6063
if len(args.id) > 0:
6164
nvidiasmi_args = ['-i', args.id]
6265
ps_call = subprocess.run(['nvidia-smi'] + nvidiasmi_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
6366
if ps_call.returncode != 0:
64-
print('nvidia-smi exited with error code {}:'.format(ps_call.returncode))
65-
print(ps_call.stdout.decode() + ps_call.stderr.decode())
66-
sys.exit()
67+
print('nvidia-smi exited with error code {}:'.format(ps_call.returncode), file=sys.stderr)
68+
print(ps_call.stdout.decode() + ps_call.stderr.decode(), file=sys.stderr)
69+
sys.exit(ps_call.returncode)
6770
lines_proc = ps_call.stdout.decode().split("\n")
6871
lines = [line + '\n' for line in lines_proc[:-1]]
6972
lines += lines_proc[-1]

test/test_main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def test_new_format(self):
2525
def test_new_format_users(self):
2626
self.do_test('FAKE_STDIN_NEW_FORMAT', 'DESIRED_STDOUT_NEW_FORMAT_USERS', call_args=["-u", "root,test"])
2727

28+
# The --id option cannot be tested in this way. So we just check that the option is considered valid.
29+
def test_new_format_filter_ids(self):
30+
self.do_test('FAKE_STDIN_NEW_FORMAT', 'DESIRED_STDOUT_NEW_FORMAT', call_args=["-i", "1,2"])
31+
2832
def test_long_pids(self):
2933
self.do_test('FAKE_STDIN_LONG_PIDS', 'DESIRED_STDOUT_LONG_PIDS', fake_ps='FAKE_PS_LONG_PIDS')
3034

0 commit comments

Comments
 (0)