| @@ -39,6 +39,17 @@ To separate the lock from the rest of the scheduler, `runtime/louise_rt/examples |
| 39 | 39 | |
| 40 | 40 | The lock alone accounts for the 200 ns `fanin` reaches. A locked outer queue only helps with few senders; this is what OTP 25 had to fix. A lock-free inbox stays flat, about 3 times cheaper than a lock from 8 senders. One consumer still bounds the throughput: no design makes fan-in faster on 22 threads than on one. |
| 41 | 41 | |
| 42 | +**Where `fanin`'s time goes on 22 threads** (`perf`, user space; `perf_event_paranoid` 2 hides kernel samples). It takes 9.2 s of CPU for 0.55 s of wall time, and 7.5 s of that CPU time is in the kernel. One thread takes 0.1 s, all of it in user space. 347,277 voluntary context switches (3 on one thread), about one for every 4 messages, are threads blocking on a futex: the contended lock, or a scheduler thread going to sleep. In user space: |
| 43 | + |
| 44 | +| Function | Share | |
| 45 | +|---|---| |
| 46 | +| `Mutex::lock_contended` (the actor lock, spinning) | 47% | |
| 47 | +| `Shared::with_proc` for `deliver` (the lock's fast path, inlined) | 28% | |
| 48 | +| `Shared::cell` (finding the receiver in the actor table) | 15% | |
| 49 | +| `Proc::deliver` | 3% | |
| 50 | + |
| 51 | +`Shared::cell` takes the table's `RwLock` for reading and clones the receiver's `Arc`. Both are atomic writes to lines every sender shares, so even without the actor lock, each send would still write twice to contended lines. |
| 52 | + |
| 42 | 53 | **Memory per idle actor** (`idle.lse`, 100,000 against 1,000 actors, sampled while they wait): |
| 43 | 54 | |
| 44 | 55 | | | Per actor | |
| @@ -47,11 +58,12 @@ The lock alone accounts for the 200 ns `fanin` reaches. A locked outer queue onl |
| 47 | 58 | | Page tables (not counted as resident) | 4.3 KiB | |
| 48 | 59 | | Memory mappings | 2 | |
| 49 | 60 | |
| 50 | | −That is about 9.5 KiB per actor, 0.95 GB for 100,000, against about 2.6 KB per BEAM process. The mailbox's share is a few words: the mutex is 8 bytes, an empty map allocates nothing, and a lock-free inbox would take 8 to 16 bytes. **Stacks dominate.** Each compiled actor reserves 64 MiB (`PROGRAM_STACK_SIZE`, committed lazily) above a guard page: at least one touched 4 KiB page, a page-table page of its own (each reservation spans its own 2 MiB regions), and two mappings, since the guard page splits it. `idle` takes about 12 µs per actor, from its spawn to its unwinding at exit (not profiled: perf was not available). |
| 61 | +That is about 9.5 KiB per actor, 0.95 GB for 100,000, against about 2.6 KB per BEAM process. The mailbox's share is a few words: the mutex is 8 bytes, an empty map allocates nothing, and a lock-free inbox would take 8 to 16 bytes. **Stacks dominate.** Each compiled actor reserves 64 MiB (`PROGRAM_STACK_SIZE`, committed lazily) above a guard page: at least one touched 4 KiB page, a page-table page of its own (each reservation spans its own 2 MiB regions), and two mappings, since the guard page splits it. `idle` takes about 12 µs per actor, from its spawn to its unwinding at exit. 70% of it is kernel time (0.84 s of 1.18 on one thread), with 1.3 page faults per actor: mapping and unmapping stacks, and touching their first page. In user space, the time is spread over the scheduler loop, fiber switches and frees. |
| 51 | 62 | |
| 52 | 63 | ## Decision |
| 53 | 64 | 1. **Keep the per-actor lock for links, monitors and exits.** They are rare, and their ordering with exits is simplest under one lock. |
| 54 | 65 | 2. **Replace the mailbox's lock by a lock-free inbox with an owner-private mailbox** (to implement and measure: issue #136): |
| 66 | + - A send no longer looks the receiver up in the actor table under its `RwLock`, cloning an `Arc`: a subject carries what finds the receiver's inbox without a shared write (for example a reference kept alive by the table until the actor finishes, as `Actor::with_proc` already relies on for the running actor). |
| 55 | 67 | - The inbox is one atomic word, CAF's design: an intrusive LIFO of message nodes, pushed with a CAS, with tag values for "empty", "closed" (the actor exited: a push fails and the message goes back to its sender, as now) and "owner waiting". |
| 56 | 68 | - The push that replaces "owner waiting" schedules the actor. This replaces the sender's look at `waiting_tags` and `RunState.queued` for messages. |
| 57 | 69 | - The owner takes the whole list with one swap, reverses it (push order), and files it into the tag map it already has (`Mailbox`), which only it touches. Arrival numbers are assigned then. Per-sender order is kept. |