| 1 | """Protects Knife harnesses from OOM and prioritizes the server over their work.""" |
| 2 | |
| 3 | import errno |
| 4 | import os |
| 5 | import re |
| 6 | import socket |
| 7 | import struct |
| 8 | import subprocess |
| 9 | import sys |
| 10 | |
| 11 | HARNESS = re.compile( |
| 12 | r"/run/podman-init|/home/clo/\.local/share/knife-server/.*" |
| 13 | r"|/home/clo/\.knife/tools/(claude/[^/]+/claude|codex/[^/]+/bin/codex)" |
| 14 | ) |
| 15 | SERVER = re.compile(r"/home/clo/\.local/share/knife-server/[^/]+/apps/server/dist/bin\.mjs") |
| 16 | RUNTIME = re.compile(r"/home/clo/\.knife/runtime/versions/[^/]+/t3") |
| 17 | PROTECTED, WORK = -1000, 800 |
| 18 | NETLINK_CONNECTOR, CN_IDX_PROC, PROC_CN_MCAST_LISTEN, PROC_EVENT_EXEC = 11, 1, 1, 2 |
| 19 | |
| 20 | |
| 21 | def adjust(pid, scope): |
| 22 | try: |
| 23 | if not open(f"/proc/{pid}/cgroup").read().startswith(f"0::{scope}/"): |
| 24 | return |
| 25 | argv = open(f"/proc/{pid}/cmdline", "rb").read().split(b"\0") |
| 26 | names = [os.readlink(f"/proc/{pid}/exe"), *(a.decode(errors="replace") for a in argv[1:2])] |
| 27 | path = f"/proc/{pid}/oom_score_adj" |
| 28 | current = int(open(path).read()) |
| 29 | server = any(SERVER.fullmatch(name) for name in names) or ( |
| 30 | argv[1:2] in ([b"serve"], [b"__service-launcher"]) |
| 31 | and any(RUNTIME.fullmatch(name) for name in names) |
| 32 | ) |
| 33 | protected = server or any(HARNESS.fullmatch(name) for name in names) |
| 34 | if protected: |
| 35 | wanted = PROTECTED |
| 36 | elif current < 0: |
| 37 | wanted = WORK |
| 38 | else: |
| 39 | wanted = current |
| 40 | if current != wanted: |
| 41 | open(path, "w").write(str(wanted)) |
| 42 | nice = -20 if server else 0 if protected else 19 |
| 43 | # Niceness belongs to each thread; renicing the leader leaves workers unchanged. |
| 44 | for tid in os.listdir(f"/proc/{pid}/task"): |
| 45 | try: |
| 46 | if os.getpriority(os.PRIO_PROCESS, int(tid)) != nice: |
| 47 | os.setpriority(os.PRIO_PROCESS, int(tid), nice) |
| 48 | except ProcessLookupError: |
| 49 | pass |
| 50 | except (FileNotFoundError, ProcessLookupError, PermissionError): |
| 51 | pass |
| 52 | |
| 53 | |
| 54 | def scan(scope): |
| 55 | for directory, _, files in os.walk(f"/sys/fs/cgroup{scope}"): |
| 56 | if "cgroup.procs" in files: |
| 57 | for pid in open(f"{directory}/cgroup.procs").read().split(): |
| 58 | adjust(pid, scope) |
| 59 | |
| 60 | |
| 61 | def main(): |
| 62 | podman = sys.argv[1] |
| 63 | scope = subprocess.run( |
| 64 | [podman, "inspect", "--format", "{{.State.CgroupPath}}", "knife-linux"], |
| 65 | check=True, capture_output=True, text=True, |
| 66 | ).stdout.strip() |
| 67 | sock = socket.socket(socket.AF_NETLINK, socket.SOCK_DGRAM, NETLINK_CONNECTOR) |
| 68 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 8 << 20) |
| 69 | sock.bind((0, CN_IDX_PROC)) |
| 70 | listen = struct.pack("=IIIIHHI", CN_IDX_PROC, 1, 0, 0, 4, 0, PROC_CN_MCAST_LISTEN) |
| 71 | sock.send(struct.pack("=IHHII", 16 + len(listen), 3, 0, 0, 0) + listen) |
| 72 | scan(scope) |
| 73 | print(f"guarding {scope}", flush=True) |
| 74 | while True: |
| 75 | try: |
| 76 | data = sock.recv(65536) |
| 77 | except OSError as error: |
| 78 | if error.errno != errno.ENOBUFS: |
| 79 | raise |
| 80 | scan(scope) |
| 81 | continue |
| 82 | offset = 0 |
| 83 | while offset + 16 <= len(data): |
| 84 | length = struct.unpack_from("=I", data, offset)[0] |
| 85 | what = struct.unpack_from("=I", data, offset + 36)[0] |
| 86 | if what == PROC_EVENT_EXEC: |
| 87 | adjust(struct.unpack_from("=I", data, offset + 56)[0], scope) |
| 88 | offset += (length + 3) & ~3 or len(data) |
| 89 | |
| 90 | |
| 91 | if __name__ == "__main__": |
| 92 | main() |