|
30 | 30 | parser = argparse.ArgumentParser()
|
31 | 31 | parser.add_argument('-l', '--command-length', default=20, const=100, type=int, nargs='?')
|
32 | 32 | 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)") |
33 | 34 | # only for testing
|
34 | 35 | parser.add_argument('-p', '--fake-ps', help="The list of processes to use instead of real output of `ps`")
|
35 | 36 |
|
|
39 | 40 | command_length = args.command_length
|
40 | 41 | color = args.color
|
41 | 42 | fake_ps = args.fake_ps
|
| 43 | +users = set(args.user.split(',')) if len(args.user) > 0 else None |
42 | 44 |
|
43 | 45 | # for testing, the stdin can be provided in a file
|
44 | 46 | fake_stdin_path = os.getenv("FAKE_STDIN_PATH", None)
|
@@ -132,6 +134,17 @@ def colorize(_lines):
|
132 | 134 | time = []
|
133 | 135 | command = []
|
134 | 136 |
|
| 137 | +fields = ( |
| 138 | + gpu_num, |
| 139 | + pid, |
| 140 | + gpu_mem, |
| 141 | + user, |
| 142 | + cpu, |
| 143 | + mem, |
| 144 | + time, |
| 145 | + command, |
| 146 | +) |
| 147 | + |
135 | 148 | gpu_num_idx = 1
|
136 | 149 | pid_idx = 2 if not is_new_format else 4
|
137 | 150 | gpu_mem_idx = -3
|
@@ -167,12 +180,24 @@ def colorize(_lines):
|
167 | 180 | continue
|
168 | 181 | parts = re.split(r'\s+', line.strip(), 5)
|
169 | 182 | # idx = pid.index(parts[0])
|
| 183 | + to_delete = [] # If the command is limited to selected users, we need to delete the other lines |
170 | 184 | 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 |
171 | 188 | user[idx] = parts[1]
|
172 | 189 | cpu[idx] = parts[2]
|
173 | 190 | mem[idx] = parts[3]
|
174 | 191 | time[idx] = parts[4] if "-" not in parts[4] else parts[4].split("-")[0] + " days"
|
175 | 192 | 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() |
176 | 201 |
|
177 | 202 | max_pid_length = max(5, max([len(x) for x in pid]))
|
178 | 203 | format = ("| %3s %" + str(max_pid_length) + "s %8s %8s %5s %5s %9s %-" + str(command_length) + "." + str(command_length) + "s |")
|
|
0 commit comments