|
3 | 3 | #######
|
4 | 4 | # USAGE
|
5 | 5 | #
|
6 |
| -# [nvidia-smi | ] nvidia-htop.py [-l [length]] |
| 6 | +# [nvidia-smi | ] nvidia-htop.py [-l [length]] [-i ID] |
7 | 7 | # print GPU utilization with usernames and CPU stats for each GPU-utilizing process
|
8 | 8 | #
|
9 | 9 | # -l|--command-length [length] Print longer part of the commandline. If `length'
|
10 | 10 | # is provided, use it as the commandline length,
|
11 | 11 | # otherwise print first 100 characters.
|
12 | 12 | # -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). |
14 | 15 | ######
|
15 | 16 |
|
16 | 17 | import sys
|
|
31 | 32 | parser.add_argument('-l', '--command-length', default=20, const=100, type=int, nargs='?')
|
32 | 33 | parser.add_argument('-c', '--color', action='store_true')
|
33 | 34 | 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)") |
34 | 36 | # only for testing
|
35 | 37 | parser.add_argument('-p', '--fake-ps', help="The list of processes to use instead of real output of `ps`")
|
36 | 38 |
|
|
53 | 55 | lines = f.readlines()
|
54 | 56 | elif stdin_lines:
|
55 | 57 | 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) |
56 | 61 | 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) |
58 | 66 | 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) |
62 | 70 | lines_proc = ps_call.stdout.decode().split("\n")
|
63 | 71 | lines = [line + '\n' for line in lines_proc[:-1]]
|
64 | 72 | lines += lines_proc[-1]
|
|
0 commit comments