| @@ -39,7 +39,9 @@ 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: |
| 42 | +**Where `fanin`'s time goes on 22 threads** (`perf record -g`, kernel included). 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. It makes 347,277 voluntary context switches (3 on one thread), about one for every 4 messages. |
| 43 | + |
| 44 | +Two thirds of all cycles (66–70%) are in the kernel's `native_queued_spin_lock_slowpath`, under the lock of the futex hash bucket. 40% come through `futex_wait`, called from the actor lock in `deliver`: senders parking on the collector's lock. Another 26–28% come through `futex_wake`, as each unlock wakes a parked sender. Every sender waits on the same futex word, so they queue on one kernel lock as well. The user-space part, out of user time only: |
| 43 | 45 | |
| 44 | 46 | | Function | Share | |
| 45 | 47 | |---|---| |
| @@ -58,7 +60,12 @@ The lock alone accounts for the 200 ns `fanin` reaches. A locked outer queue onl |
| 58 | 60 | | Page tables (not counted as resident) | 4.3 KiB | |
| 59 | 61 | | Memory mappings | 2 | |
| 60 | 62 | |
| 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. |
| 63 | +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. Of all its cycles: |
| 64 | +- 40–43% are in `munmap`: as each actor unwinds at exit, its stack is unmapped (the pool keeps 256), in a tree of 200,000 mappings; |
| 65 | +- 16% are page faults: the first touch of each stack, with its page table; |
| 66 | +- 5.5% are in `mprotect`, for the guard pages; `mmap` itself is negligible (the pages are committed lazily). |
| 67 | + |
| 68 | +In user space, the time is spread over the scheduler loop, fiber switches and frees. |
| 62 | 69 | |
| 63 | 70 | ## Decision |
| 64 | 71 | 1. **Keep the per-actor lock for links, monitors and exits.** They are rare, and their ordering with exits is simplest under one lock. |