| ... | @@ -1,8 +1,4 @@ | ... | @@ -1,8 +1,4 @@ |
| 1 | """Exempts the Knife, Claude, and Codex harnesses in knife-linux from the OOM killer. | 1 | """Protects Knife harnesses from OOM and prioritizes the server over their work.""" |
| 2 | | | |
| 3 | Children inherit oom_score_adj at fork, so each exec outside the harness set gets a | | |
| 4 | killable score back from the kernel's process-event connector before it can grow. | | |
| 5 | """ | | |
| 6 | | 2 | |
| 7 | import errno | 3 | import errno |
| 8 | import os | 4 | import os |
| ... | @@ -16,6 +12,7 @@ HARNESS = re.compile( | ... | @@ -16,6 +12,7 @@ HARNESS = re.compile( |
| 16 | r"/run/podman-init|/home/clo/\.local/share/knife-server/.*" | 12 | r"/run/podman-init|/home/clo/\.local/share/knife-server/.*" |
| 17 | r"|/home/clo/\.knife/tools/(claude/[^/]+/claude|codex/[^/]+/bin/codex)" | 13 | r"|/home/clo/\.knife/tools/(claude/[^/]+/claude|codex/[^/]+/bin/codex)" |
| 18 | ) | 14 | ) |
| | 15 | SERVER = re.compile(r"/home/clo/\.local/share/knife-server/[^/]+/apps/server/dist/bin\.mjs") |
| 19 | PROTECTED, WORK = -1000, 800 | 16 | PROTECTED, WORK = -1000, 800 |
| 20 | NETLINK_CONNECTOR, CN_IDX_PROC, PROC_CN_MCAST_LISTEN, PROC_EVENT_EXEC = 11, 1, 1, 2 | 17 | NETLINK_CONNECTOR, CN_IDX_PROC, PROC_CN_MCAST_LISTEN, PROC_EVENT_EXEC = 11, 1, 1, 2 |
| 21 | | 18 | |
| ... | @@ -28,14 +25,23 @@ def adjust(pid, scope): | ... | @@ -28,14 +25,23 @@ def adjust(pid, scope): |
| 28 | names = [os.readlink(f"/proc/{pid}/exe"), *(a.decode(errors="replace") for a in argv[1:2])] | 25 | names = [os.readlink(f"/proc/{pid}/exe"), *(a.decode(errors="replace") for a in argv[1:2])] |
| 29 | path = f"/proc/{pid}/oom_score_adj" | 26 | path = f"/proc/{pid}/oom_score_adj" |
| 30 | current = int(open(path).read()) | 27 | current = int(open(path).read()) |
| 31 | if any(HARNESS.fullmatch(name) for name in names): | 28 | protected = any(HARNESS.fullmatch(name) for name in names) |
| | 29 | if protected: |
| 32 | wanted = PROTECTED | 30 | wanted = PROTECTED |
| 33 | elif current < 0: | 31 | elif current < 0: |
| 34 | wanted = WORK | 32 | wanted = WORK |
| 35 | else: | 33 | else: |
| 36 | return | 34 | wanted = current |
| 37 | if current != wanted: | 35 | if current != wanted: |
| 38 | open(path, "w").write(str(wanted)) | 36 | open(path, "w").write(str(wanted)) |
| | 37 | nice = -20 if any(SERVER.fullmatch(name) for name in names) else 0 if protected else 19 |
| | 38 | # Niceness belongs to each thread; renicing the leader leaves workers unchanged. |
| | 39 | for tid in os.listdir(f"/proc/{pid}/task"): |
| | 40 | try: |
| | 41 | if os.getpriority(os.PRIO_PROCESS, int(tid)) != nice: |
| | 42 | os.setpriority(os.PRIO_PROCESS, int(tid), nice) |
| | 43 | except ProcessLookupError: |
| | 44 | pass |
| 39 | except (FileNotFoundError, ProcessLookupError, PermissionError): | 45 | except (FileNotFoundError, ProcessLookupError, PermissionError): |
| 40 | pass | 46 | pass |
| 41 | | 47 | |
| ... | @@ -77,4 +83,5 @@ def main(): | ... | @@ -77,4 +83,5 @@ def main(): |
| 77 | offset += (length + 3) & ~3 or len(data) | 83 | offset += (length + 3) & ~3 or len(data) |
| 78 | | 84 | |
| 79 | | 85 | |
| 80 | main() | 86 | if __name__ == "__main__": |
| | 87 | main() |