-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkill_process.py
More file actions
42 lines (38 loc) · 1.12 KB
/
kill_process.py
File metadata and controls
42 lines (38 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import subprocess
import re
import os
import signal
def get_kfd_pids_with_vram():
try:
output = subprocess.check_output(["rocm-smi", "--showpids"], text=True)
except subprocess.CalledProcessError as e:
print("Failed to run rocm-smi:", e)
return []
pids_to_kill = []
capture = False
for line in output.splitlines():
if "PID" in line and "VRAM USED" in line:
capture = True
continue
if "===" in line:
continue
if capture and line.strip():
parts = re.split(r'\s+', line.strip())
if len(parts) < 5:
continue
pid = int(parts[0])
try:
vram = int(parts[3])
if vram > 0:
pids_to_kill.append(pid)
except ValueError:
continue
return pids_to_kill
def kill_pids(pids):
for pid in pids:
os.system(f"sudo kill -9 {pid}")
print("killed pid:", pid)
if __name__ == "__main__":
pids = get_kfd_pids_with_vram()
kill_pids(pids)
os.system("rocm-smi --showpids")