File size: 7,483 Bytes
cdf34e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from __future__ import annotations

import json
import subprocess
import threading
import time
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path


ROOT = Path(__file__).resolve().parent
RESULTS = ROOT / ".work" / "results"


def process_rss() -> dict[str, dict[int, int]]:
    output = subprocess.run(
        ["ps", "-axo", "pid=,rss=,command="],
        check=True,
        capture_output=True,
        text=True,
    ).stdout
    result: dict[str, dict[int, int]] = {"renderer": {}, "gpu": {}}
    for line in output.splitlines():
        pieces = line.strip().split(maxsplit=2)
        if len(pieces) != 3:
            continue
        pid_text, rss_text, command = pieces
        if "T3 Code (Alpha)" not in command:
            continue
        if "--type=renderer" in command and "Helper (Renderer)" in command:
            result["renderer"][int(pid_text)] = int(rss_text) * 1024
        elif "--type=gpu-process" in command and "user-data-dir=/Users/admin/Library/Application Support/t3code" in command:
            result["gpu"][int(pid_text)] = int(rss_text) * 1024
    return result


class ProcessSampler:
    def __init__(self, run_id: str) -> None:
        self.run_id = run_id
        self.started_at = time.time()
        self.phase = "startup"
        self.samples: list[dict] = []
        self.stop_event = threading.Event()
        self.thread = threading.Thread(target=self._run, daemon=True)
        self.thread.start()

    def _run(self) -> None:
        while not self.stop_event.is_set():
            try:
                self.samples.append(
                    {"time": time.time(), "phase": self.phase, **process_rss()}
                )
            except Exception as error:  # diagnostic sampling must not break OCR
                self.samples.append({"time": time.time(), "error": str(error)})
            self.stop_event.wait(0.05)

    def mark(self, phase: str) -> None:
        self.phase = phase

    def finish(self) -> dict:
        self.stop_event.set()
        self.thread.join(timeout=2)
        usable = [sample for sample in self.samples if "renderer" in sample]
        if not usable:
            return {"samples": len(self.samples), "error": "no usable process samples"}
        baseline = usable[0]

        def summarize(kind: str) -> dict:
            baseline_by_pid = baseline[kind]
            peak_by_pid: dict[int, int] = {}
            first_by_pid: dict[int, int] = dict(baseline_by_pid)
            for sample in usable:
                for pid, rss in sample[kind].items():
                    first_by_pid.setdefault(pid, rss)
                    peak_by_pid[pid] = max(peak_by_pid.get(pid, 0), rss)
            deltas = {
                pid: peak - first_by_pid.get(pid, peak)
                for pid, peak in peak_by_pid.items()
            }
            selected_pid = max(deltas, key=deltas.get) if deltas else None
            phases: dict[str, dict[str, int]] = {}
            if selected_pid:
                for sample in usable:
                    rss = sample[kind].get(selected_pid)
                    if rss is None:
                        continue
                    phase = sample.get("phase", "unknown")
                    phase_summary = phases.setdefault(
                        phase,
                        {"startBytes": rss, "endBytes": rss, "peakBytes": rss},
                    )
                    phase_summary["endBytes"] = rss
                    phase_summary["peakBytes"] = max(phase_summary["peakBytes"], rss)
                for phase_summary in phases.values():
                    phase_summary["peakDeltaBytes"] = (
                        phase_summary["peakBytes"] - phase_summary["startBytes"]
                    )
            return {
                "selectedPid": selected_pid,
                "baselineBytes": first_by_pid.get(selected_pid, 0) if selected_pid else 0,
                "peakBytes": peak_by_pid.get(selected_pid, 0) if selected_pid else 0,
                "peakDeltaBytes": deltas.get(selected_pid, 0) if selected_pid else 0,
                "allPeakDeltas": deltas,
                "phases": phases,
            }

        return {
            "scope": "T3 renderer with largest RSS delta plus shared T3 GPU process",
            "samples": len(usable),
            "durationSeconds": time.time() - self.started_at,
            "renderer": summarize("renderer"),
            "gpu": summarize("gpu"),
        }


active_sampler: ProcessSampler | None = None
sampler_lock = threading.Lock()


class Handler(SimpleHTTPRequestHandler):
    def do_POST(self) -> None:
        global active_sampler
        length = int(self.headers.get("Content-Length", "0"))
        if length <= 0 or length > 2_000_000:
            self.send_error(400, "invalid request size")
            return
        payload = json.loads(self.rfile.read(length))
        if self.path == "/benchmark-start":
            run_id = str(payload.get("runId", ""))
            if not run_id or "/" in run_id or ".." in run_id:
                self.send_error(400, "invalid run id")
                return
            with sampler_lock:
                if active_sampler:
                    active_sampler.finish()
                active_sampler = ProcessSampler(run_id)
            self.send_response(204)
            self.end_headers()
            return
        if self.path == "/benchmark-mark":
            run_id = str(payload.get("runId", ""))
            phase = str(payload.get("phase", ""))
            if not phase or len(phase) > 80:
                self.send_error(400, "invalid benchmark phase")
                return
            with sampler_lock:
                sampler = active_sampler
                if not sampler or sampler.run_id != run_id:
                    self.send_error(409, "benchmark sampler mismatch")
                    return
                sampler.mark(phase)
            self.send_response(204)
            self.end_headers()
            return
        if self.path == "/benchmark-result":
            run_id = str(payload.get("runId", ""))
            with sampler_lock:
                sampler = active_sampler
                active_sampler = None
            if not sampler or sampler.run_id != run_id:
                self.send_error(409, "benchmark sampler mismatch")
                return
            payload["processMemory"] = sampler.finish()
            RESULTS.mkdir(parents=True, exist_ok=True)
            destination = RESULTS / f"{run_id}.json"
            destination.write_text(
                json.dumps(payload, ensure_ascii=False, indent=2) + "\n",
                encoding="utf-8",
            )
            response = json.dumps(payload["processMemory"]).encode()
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.send_header("Content-Length", str(len(response)))
            self.end_headers()
            self.wfile.write(response)
            return
        self.send_error(404)

    def end_headers(self) -> None:
        self.send_header("Cross-Origin-Opener-Policy", "same-origin")
        self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
        self.send_header("Cross-Origin-Resource-Policy", "same-origin")
        self.send_header("Cache-Control", "no-store")
        super().end_headers()


if __name__ == "__main__":
    ThreadingHTTPServer(("127.0.0.1", 8765), Handler).serve_forever()