addftool 0.1.2__py3-none-any.whl → 0.1.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ import argparse
2
+ from addftool.process import add_killer_args, killer_main
3
+
4
+
5
+ def get_args():
6
+ parser = argparse.ArgumentParser(description="Addf's tool")
7
+
8
+ subparsers = parser.add_subparsers(dest='command', help='Sub-command help')
9
+ add_killer_args(subparsers)
10
+
11
+ return parser.parse_args()
12
+
13
+
14
+ def main():
15
+ args = get_args()
16
+ if args.command == "kill":
17
+ killer_main(args)
18
+ else:
19
+ print("Unknown command: ", args.command)
20
+
21
+
22
+ if __name__ == "__main__":
23
+ main()
@@ -0,0 +1,88 @@
1
+ import psutil
2
+ import time
3
+
4
+ from .utils import get_processes, get_process_using_rocm, get_process_using_cuda
5
+
6
+
7
+ def add_killer_args(subparsers):
8
+ process_killer_parser = subparsers.add_parser('kill', help='process kill')
9
+
10
+ process_killer_parser.add_argument("-c", "--contains", help="contains of command", action='store_true', default=False)
11
+ process_killer_parser.add_argument("--timeout", help="timeout of command", default=5, type=int)
12
+ process_killer_parser.add_argument("--try_count", help="try count of command", default=3, type=int)
13
+
14
+ process_killer_parser.add_argument("--rocm", help="kill process using rocm", action='store_true', default=False)
15
+ process_killer_parser.add_argument("--cuda", help="kill process using cuda", action='store_true', default=False)
16
+
17
+ process_killer_parser.add_argument("-v", "--view", help="view process", action='store_true', default=False)
18
+
19
+ process_killer_parser.add_argument("name", nargs='?', help="name of process", type=str, default="")
20
+
21
+
22
+ def kill_process(processes, timeout=5, try_count=3):
23
+ for process in processes:
24
+ try:
25
+ proc = psutil.Process(process['pid'])
26
+ print(f"Killing process {proc.pid} - {proc.cmdline()}")
27
+ proc.terminate()
28
+ except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
29
+ print(f"Process {process['pid']} not found or access denied")
30
+ continue
31
+
32
+ time.sleep(timeout)
33
+ for _ in range(try_count):
34
+ all_terminated = True
35
+ for process in processes:
36
+ try:
37
+ proc = psutil.Process(process['pid'])
38
+ print("Process still running: ", proc.pid)
39
+ proc.kill()
40
+ all_terminated = False
41
+ except (psutil.NoSuchProcess):
42
+ continue
43
+ except (psutil.AccessDenied):
44
+ print(f"Process {process['pid']} access denied")
45
+ continue
46
+ except (psutil.ZombieProcess):
47
+ print(f"Process {process['pid']} is a zombie process")
48
+ continue
49
+ except Exception as e:
50
+ print(f"Error killing process {process['pid']}: {e}")
51
+ continue
52
+ if all_terminated:
53
+ print("All processes terminated")
54
+ break
55
+
56
+ if not all_terminated:
57
+ print("Some processes are still running. Please check manually.")
58
+ exit(1)
59
+
60
+
61
+ def find_and_kill_process(command="", contains=False, use_rocm=False, use_cuda=False, timeout=5, try_count=3, only_view=False):
62
+ do_not_do_anything = command is None or len(command) == 0
63
+ if use_rocm:
64
+ processes = get_process_using_rocm()
65
+ elif use_cuda:
66
+ processes = get_process_using_cuda()
67
+ elif do_not_do_anything and not only_view:
68
+ print("Use top or htop to find the process you want to kill")
69
+ return
70
+ else:
71
+ processes = get_processes(command=command, contains=contains)
72
+
73
+ if only_view:
74
+ print(f"Found {len(processes)} processes")
75
+ for process in processes:
76
+ print(f"PID: {process['pid']}, Command: {' '.join(process['command'])}")
77
+ return
78
+
79
+ if len(processes) > 0:
80
+ print(f"Found {len(processes)} processes to kill")
81
+ kill_process(processes, timeout, try_count)
82
+
83
+
84
+ def killer_main(args):
85
+ find_and_kill_process(
86
+ args.name, args.contains, use_rocm=args.rocm, use_cuda=args.cuda,
87
+ timeout=args.timeout, try_count=args.try_count, only_view=args.view,
88
+ )
@@ -0,0 +1,71 @@
1
+ import psutil
2
+ import subprocess
3
+
4
+
5
+ def get_processes(command="", contains=False, pids=None):
6
+ """获取进程的PID和命令"""
7
+
8
+ processes = []
9
+ for proc in psutil.process_iter(['pid', 'cmdline']):
10
+ try:
11
+ pid = proc.info['pid']
12
+ cmdline = proc.info['cmdline']
13
+ if cmdline is None or len(cmdline) == 0:
14
+ continue
15
+ if pids is not None and pid not in pids:
16
+ continue
17
+ if len(command) > 0:
18
+ if contains and command not in cmdline[0]:
19
+ continue
20
+ if not contains and command != cmdline[0]:
21
+ continue
22
+ if len(cmdline) > 1 and cmdline[1].endswith('addf'):
23
+ continue
24
+ processes.append({'pid': pid, 'command': cmdline})
25
+ except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
26
+ pass
27
+
28
+ return processes
29
+
30
+
31
+ def get_process_using_rocm():
32
+ result = subprocess.run(['rocm-smi', '--showpidgpus'], capture_output=True, text=True)
33
+ if result.returncode != 0:
34
+ return False
35
+
36
+ gpu_pids = []
37
+ output_lines = result.stdout.strip().split('\n')
38
+ for line in output_lines:
39
+ if "PID" in line and ":" in line:
40
+ # Extract PID from lines like "GPU[0-7]: PID: 12345"
41
+ try:
42
+ pid_part = line.split("PID:")[1].strip()
43
+ pid = int(pid_part.split()[0])
44
+ gpu_pids.append(pid)
45
+ except (IndexError, ValueError):
46
+ continue
47
+
48
+ # Remove duplicates
49
+ gpu_pids = list(set(gpu_pids))
50
+ return get_processes(pids=gpu_pids)
51
+
52
+
53
+ def get_process_using_cuda():
54
+ result = subprocess.run(['nvidia-smi', '--query-compute-apps=pid', '--format=csv,noheader'], capture_output=True, text=True)
55
+ if result.returncode != 0:
56
+ return False
57
+
58
+ gpu_pids = []
59
+ output_lines = result.stdout.strip().split('\n')
60
+ for line in output_lines:
61
+ try:
62
+ pid = int(line.strip())
63
+ gpu_pids.append(pid)
64
+ except ValueError:
65
+ continue
66
+
67
+ # Remove duplicates
68
+ gpu_pids = list(set(gpu_pids))
69
+
70
+ # get process id and cmdline
71
+ return get_processes(pids=gpu_pids)
@@ -1,6 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: addftool
3
- Version: 0.1.2
3
+ Version: 0.1.4
4
4
  Requires-Dist: cryptography
5
5
  Requires-Dist: requests
6
6
  Requires-Dist: PyYAML
7
+ Requires-Dist: psutil
@@ -0,0 +1,12 @@
1
+ addftool/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ addftool/addf_portal.py,sha256=U52TdNcwWOEvv_C5r-guWYxn3ntzwYI2eBzJIE7IdcY,493
3
+ addftool/blob.py,sha256=NZOItDyFUIdV1tfhJZJJBEzGy296CE5NCictTzP4OPc,8282
4
+ addftool/tool.py,sha256=EuKQ2t2InN7yB-_oYLcdsA7vRqzRGTunwIxplUSqEG0,2054
5
+ addftool/util.py,sha256=Q3A68vJDxgfeNiEFmk54HuMuworVndocXpSbVpvGMfc,362
6
+ addftool/process/__init__.py,sha256=OB-cZXP1jK7l8uN8nKfhg_bCX6Slz6DeeBpEodt-IK4,3515
7
+ addftool/process/utils.py,sha256=me4HqMz5OgRcQMUJmVhKdTJh4SW5BB-pd_lq7g8-UwE,2252
8
+ addftool-0.1.4.dist-info/METADATA,sha256=ohQCdQdVevHRzMRu7ByPhBGxqPlxmEKnDxNmfcy_WII,148
9
+ addftool-0.1.4.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
10
+ addftool-0.1.4.dist-info/entry_points.txt,sha256=9lkmuWMInwUAtev8w8poNkNd7iML9Bjd5CBCFVxg2b8,111
11
+ addftool-0.1.4.dist-info/top_level.txt,sha256=jqj56-plrBbyzY0tIxB6wPzjAA8kte4hUlajyyQygN4,9
12
+ addftool-0.1.4.dist-info/RECORD,,
@@ -1,3 +1,4 @@
1
1
  [console_scripts]
2
+ addf = addftool.addf_portal:main
2
3
  addfblob = addftool.blob:main
3
4
  addftool = addftool.tool:main
@@ -1,9 +0,0 @@
1
- addftool/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- addftool/blob.py,sha256=NZOItDyFUIdV1tfhJZJJBEzGy296CE5NCictTzP4OPc,8282
3
- addftool/tool.py,sha256=EuKQ2t2InN7yB-_oYLcdsA7vRqzRGTunwIxplUSqEG0,2054
4
- addftool/util.py,sha256=Q3A68vJDxgfeNiEFmk54HuMuworVndocXpSbVpvGMfc,362
5
- addftool-0.1.2.dist-info/METADATA,sha256=gmakYu_DywA1K1z3yEoLN0h43nBUFbtNy0JmKcEbTgw,126
6
- addftool-0.1.2.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
7
- addftool-0.1.2.dist-info/entry_points.txt,sha256=h5TlQy4AQw-J55HqI7FRkIVRUfw-x3pStFWCoYtoCTM,78
8
- addftool-0.1.2.dist-info/top_level.txt,sha256=jqj56-plrBbyzY0tIxB6wPzjAA8kte4hUlajyyQygN4,9
9
- addftool-0.1.2.dist-info/RECORD,,