Skip to content

Commit a73833f

Browse files
authored
Merge pull request #22
Added the possibility to filter by GPU IDs
2 parents 95c5e53 + e003010 commit a73833f

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +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[,ID]] Limit the command to selected GPU IDs (comma-separated).
2728

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

nvidia-htop.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
#######
44
# USAGE
55
#
6-
# [nvidia-smi | ] nvidia-htop.py [-l [length]]
6+
# [nvidia-smi | ] nvidia-htop.py [-l [length]] [-i ID]
77
# print GPU utilization with usernames and CPU stats for each GPU-utilizing process
88
#
99
# -l|--command-length [length] Print longer part of the commandline. If `length'
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)
13+
# moderately used GPU, red - fully used GPU).
14+
# -i|--id ID[,ID[,ID...]] Limit the command to selected GPU IDs (comma-separated).
1415
######
1516

1617
import sys
@@ -31,6 +32,7 @@
3132
parser.add_argument('-l', '--command-length', default=20, const=100, type=int, nargs='?')
3233
parser.add_argument('-c', '--color', action='store_true')
3334
parser.add_argument('-u', '--user', default='', help="Limit the list of processes to selected users (comma-separated)")
35+
parser.add_argument('-i', '--id', default='', help="Limit the command to selected GPU IDs (comma-separated)")
3436
# only for testing
3537
parser.add_argument('-p', '--fake-ps', help="The list of processes to use instead of real output of `ps`")
3638

@@ -53,12 +55,18 @@
5355
lines = f.readlines()
5456
elif stdin_lines:
5557
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)
5661
else:
57-
ps_call = subprocess.run('nvidia-smi', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
62+
nvidiasmi_args = []
63+
if len(args.id) > 0:
64+
nvidiasmi_args = ['-i', args.id]
65+
ps_call = subprocess.run(['nvidia-smi'] + nvidiasmi_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
5866
if ps_call.returncode != 0:
59-
print('nvidia-smi exited with error code {}:'.format(ps_call.returncode))
60-
print(ps_call.stdout.decode() + ps_call.stderr.decode())
61-
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)
6270
lines_proc = ps_call.stdout.decode().split("\n")
6371
lines = [line + '\n' for line in lines_proc[:-1]]
6472
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)