interplane.Get Omega
Read in your language

synapse

A component of the AienOS ecosystem.

Brought by drakestapleton

branch main Branches Tags History

synapse / docs / G3_SHM_RING.md

61 lines · 7352 bytes Raw Blame History Rendered
1# G3: Shared-Memory Ring for Local Agents (experiment X5)
2
3ADR 0003 section 9 step G3 (issue aien-spark/synapse#46), crate `synapse-wire`. A ring buffer in memory shared by two local processes, carrying the G1 frame session without a socket. **Status: not adopted.** The X5 rule did not favour it over the Unix socket (section 5), so the socket stays the default transport and the ring is an optional, tested transport that nothing selects by default.
4
5Code: `crates/synapse-wire/src/ring.rs` (ring, sender, receiver, `RingStream`), `crates/synapse-wire/src/shm.rs` (the only new unsafe code: sealed `memfd`, `mmap`, futex). Tests: `crates/synapse-wire/tests/g3_ring.rs`. Measurement: `crates/synapse-bench/src/bin/wire-transport.rs` (`--arms ring,wire`).
6
7## 1. Layout
8
9One region holds a control block of 32 words (8 bytes each) and two rings, one per direction. The creator sends on ring 0, the attacher on ring 1. Control words: magic `SYNRING1`, capacity, start index, then (per ring) head (written by the sender), closed flag, tail (written by the receiver), receiver-sleeping flag and a doorbell. Indices are byte counts that increase without bound and wrap at `u64::MAX`; the data offset is the index masked by the capacity (a power of two from 64 B to 1 GiB, default 1 MiB).
10
11A record is a 16-byte header (payload length word, sequence word) followed by the payload padded to 8 bytes. A record may wrap past the end of the ring; every word is addressed through the mask. The sender publishes the head with a release store after the record is written, and the receiver reads it with an acquire load.
12
13## 2. Hostile peer rules
14
15The reader treats the writer as hostile. Each header word is loaded once into a local, the checks and the copy use only those locals, and the payload is copied out of the ring into the reader's own buffer, so a writer that changes bytes mid-read cannot change what was checked or make the reader read twice. Refused with a typed `RingRefusal` (surfaced as `RefusalCode::WireRingViolation`): head misaligned, head beyond the sender's own capacity window, head moving backwards, a record length over the configured cap or past the published bytes, a torn record (header not yet complete within published bytes), a wrong sequence number. After any refusal the endpoint is poisoned and refuses all later calls, so a record is never accepted twice. The sender applies the same rules to the tail the receiver publishes.
16
17Memory safety: all shared access goes through `AtomicU64` with explicit orderings; offsets are masked, so no index can address outside the ring.
18
19## 3. Sealing
20
21A reader of a file-backed mapping takes `SIGBUS` if the peer shrinks the file under it. The creator makes a `memfd` and seals it against shrinking, growing and re-sealing before anyone can attach; an attacher refuses a file without the shrink seal or with the wrong size. Test: `attached_peer_cannot_shrink_or_grow_the_sealed_region`, `attach_refuses_an_unsealed_wrong_size_or_mismatched_region`.
22
23## 4. Waiting
24
25The receiver polls for 50 microseconds, then sets a sleeping flag, issues a sequentially consistent fence, re-checks, and sleeps on a shared futex (10 ms timeout, so a missed wake only costs the timeout). The sender wakes the doorbell only when the sleeping flag is set. Linux 64-bit only for the shared mode; other targets get the in-process ring.
26
27## 5. Acceptance
28
29| Acceptance item (issue #46) | Test or evidence | Result |
30| :--- | :--- | :--- |
31| Hostile writer (bad lengths) cannot make the reader panic or read outside the ring | `hostile_writer_bad_lengths_cannot_make_the_reader_panic_or_read_outside_the_ring` | PASS |
32| Hostile writer (index wrap) | `hostile_writer_index_wrap_cannot_make_the_reader_panic_or_read_outside_the_ring`, `honest_indices_wrap_past_u64_max_and_the_ring_end_without_loss` | PASS |
33| Hostile writer (torn frames) | `hostile_writer_torn_frames_are_refused_and_payloads_are_copied_out_before_validation` | PASS |
34| Reader never accepts a frame twice | `hostile_writer_cannot_make_the_reader_accept_a_record_twice`, `hostile_writer_random_pokes_never_panic_and_never_deliver_a_record_twice` (3,000 seeds, all six peer-fault kinds hit) | PASS |
35| Tests can fail | `negative_control_trusting_reader_panics_or_accepts_twice_on_the_same_inputs` | PASS |
36| No frame lost or duplicated in a model test | `model_test_no_frame_is_lost_or_duplicated` (400 seeds, over 100,000 records), `model_test_two_threads_two_mappings_no_record_lost_or_duplicated` (200,000), `cross_process_ring_delivers_every_record_once_in_order` | PASS |
37| Carries the G1 session | `ring_stream_carries_the_g1_session_and_acknowledges_durable_changes`, `ring_refusal_reaches_the_frame_transport_as_wire_ring_violation` | PASS |
38| Adopted only if the X5 rule, fixed in writing before the run, favours it | `evidence/g3-shm-ring/x5/RULE.md` committed in 864aeca before `run1`; verdict in `evidence/g3-shm-ring/x5/RESULTS.md` | NOT ADOPTED |
39
40Red before green: `evidence/g3-shm-ring/red-green/red.txt` (two stubs of `ring.rs`: a trusting reader; unmasked offsets; diffs alongside) and `green.txt`.
41
42## 6. X5 result
43
44Same workload and bound as G1 (`docs/G1_WIRE_FRAMES.md` section 5; t_floor 0.806 ms, E_floor 18.6 mJ). 5 repetitions, 2,000 changes per arm per rep, raw samples in `evidence/g3-shm-ring/x5/run1/result.json`.
45
46| Arm | Median s per change | Fraction of time bound | SoC package J per change | Fraction of energy bound |
47| :--- | ---: | ---: | ---: | ---: |
48| `ring` | 1.541 x 10^-3 | 0.524 | 46.4 x 10^-3 | 0.401 |
49| `wire` (Unix socket) | 1.606 x 10^-3 | 0.502 | 46.1 x 10^-3 | 0.403 |
50| `direct_auth` (no transport) | 1.521 x 10^-3 | 0.530 | 40.4 x 10^-3 | 0.461 |
51
52The ring's median is 4 percent faster, but its slowest repetition (1.863 ms) is slower than the socket's fastest (1.466 ms), and joules are level. The rule needs both seconds and joules beyond run-to-run spread, so the verdict is NOT ADOPTED. Supporting echo test (no store): p50 6.8 us per round trip for the ring against 9.1 us for the socket; the commit dominates cost per change, which is why the gain does not show in the table. Details and limits: `evidence/g3-shm-ring/x5/RESULTS.md`. G1's own figures (1.628 ms, 40.6 mJ for `wire`) were a different run on a differently loaded machine; only within-run comparisons are used here.
53
54## 7. Limits and deviations
55
56* Not wired as a default or selected anywhere; ADR 0003 is unchanged.
57* Measurement runs client and server as two threads of one process over one region; a separate client process is covered by the safety test only.
58* Joules are the `aien_spbm` SoC package meter: package only, uncalibrated, NVMe unmeasured. The machine was shared (load about 5.8 during the idle window), so spread is wide.
59* A receiver can block until the peer closes, as a socket reader does; there is no keepalive. A sender facing a full ring waits without a limit.
60* Shared mode (memfd, futex) is Linux 64-bit only (aarch64 and x86_64 futex numbers); the system call numbers and constants were taken from the local glibc 2.39 headers and the `libc` 0.2.189 source. No new crates.
61* Small additions outside the crate: `RefusalCode::WireRingViolation` in `synapse-core`, the refusal list in `docs/SCHEMA_AND_CONSISTENCY.md`, the `.crumb` role text, the `--arms/--step/--rule/--echo` options of `wire-transport`.