mirror of
https://github.com/element-hq/synapse.git
synced 2026-08-14 18:01:13 +00:00
Port `set_current_context` — the logcontext switch primitive, on the hottest path in the logging system — from Python into Rust, and read the thread CPU usage directly via `getrusage(RUSAGE_THREAD)` (libc) instead of through the `resource` module. This removes the per-switch Python frame, the `stop`/`start` method dispatch, and the `struct_rusage` object allocated on every switch; `current.stop()`/`context.start()` for the common base-`LoggingContext` case now run inline in Rust. `usage_start` becomes a private native `(ru_utime, ru_stime)` snapshot rather than a Python `struct_rusage` — nothing outside the module read it. The `start`/`stop` methods keep their abuse-detection behaviour (thread affinity, double-start, stop-without-start) via a shared native core (`start_inner`/ `stop_inner`) that both the pymethods and the switch fast path call, so those checks still run on every switch. `_get_cputime` folds into a native `cputime_delta` helper. Subclasses (`BackgroundProcessLoggingContext`) still work unchanged: the switch dispatches to them through Python (`obj.start(rusage)` / `obj.stop(rusage)`), materialising the rusage as a plain `(utime, stime)` tuple only on that off-hot-path branch, so their overrides run. `getrusage(RUSAGE_THREAD)` is Linux-only; other platforms return `None` (no per-context CPU accounting), as before. Removes the now-dead Python rusage plumbing (`get_thread_resource_usage`, the `RUSAGE_THREAD` support probe, `is_thread_resource_usage_supported`) — Rust owns rusage now. The Phase 0 characterization test's rusage stand-in changes from an opaque `object()` to a `(0.0, 0.0)` tuple to match the native representation; it still pins the same observable abuse-detection behaviour. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011LnJSXQ86AAWNtihR4C5PH