Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ option(MORPH_CLIENT_ONLY
OFF
)

# Observability (`morph::observe`: metric and trace sinks) is on by default. OFF
# compiles the emit sites out of the dispatch path -- the clock reads and the
# in-flight counter `LocalBackend` keeps for its metrics included -- and makes
# installing a sink a compile error naming this option. PUBLIC on the morph
# target, not per-consumer: the macro changes what inline functions in the
# public headers are, so two translation units disagreeing about it would
# violate ODR. See docs/spec/core/observability.md.
option(MORPH_ENABLE_OBSERVABILITY
"Build the metric and trace seams (morph::observe). OFF compiles them out of the dispatch path and makes installing a sink a compile error."
ON
)

# Both Qt consumers (the WebSocket backend and the Qt Quick frontend) need
# Qt6 auto-detected on Windows before their find_package(Qt6 ...) calls run.
Expand Down Expand Up @@ -528,6 +539,9 @@ endif()
if(MORPH_CLIENT_ONLY)
target_compile_definitions(morph PUBLIC MORPH_CLIENT_ONLY)
endif()
if(NOT MORPH_ENABLE_OBSERVABILITY)
target_compile_definitions(morph PUBLIC MORPH_OBSERVABILITY_DISABLED)
endif()
if(MORPH_ENABLE_TRACY)
target_compile_definitions(morph PUBLIC MORPH_TRACY_ENABLED)
target_link_libraries(morph PUBLIC Tracy::TracyClient)
Expand Down
8 changes: 7 additions & 1 deletion docs/BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ need `VCPKG_ROOT` set.
| `MORPH_BUILD_CLANG_TIDY` | `OFF` | clang-tidy during the build, warnings as errors |
| `MORPH_INSTALL` | top-level project | Install and export rules |

Two options change what the library itself compiles, for deployments that need
Three options change what the library itself compiles, for deployments that need
them:

- **`MORPH_CLIENT_ONLY`** (`OFF`) suppresses the registrars that construct and
Expand All @@ -75,6 +75,12 @@ them:
- **`MORPH_REQUIRE_VETTED_HMAC`** (`OFF`) removes the default reference HMAC from
the token signer and verifier, so every call site must pass a vetted MAC
function. See [`spec/security.md`](spec/security.md).
- **`MORPH_ENABLE_OBSERVABILITY`** (`ON`) builds the metric and trace seams.
`OFF` compiles them out of the dispatch path (the clock reads and in-flight
counter a local call keeps for its metrics included) and makes installing a
sink (`setMetricSink`, `setTraceSink`, `ScopedObserveOverride`) a compile
error. See [`spec/core/observability.md`](spec/core/observability.md),
"Compile-time switch".

## Using morph in your own project

Expand Down
10 changes: 7 additions & 3 deletions docs/spec/core/backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ handlers belong to the caller's owner (the bridge's, given through `setOwner`)
and are touched only there, without a lock; each verb checks it in a debug
build. Only the strand tasks the backend posts run elsewhere, and they reach
nothing of the backend but what each carries — the holder, the call, and the
cancel record below.
shared run state below (the cancel record and the in-flight count).

**Lifecycle:**
- `bindModel` — overridden to keep the shared directory: register (private),
Expand All @@ -502,8 +502,12 @@ cancel record below.
- `executeInto` — looks up the holder; if `mid` is unknown it immediately
settles the sink with `std::runtime_error("model not found: id=<n>")`.
Otherwise it tracks the sink in the pending list, counts the call in flight,
and posts the call to the model's strand (serialised per-model), where it
enters the instance's action gate, sets up a `ScopedContext` (from
and posts the call to the model's strand (serialised per-model). The call
carries one `shared_ptr` to the backend's shared run state (the cancel record
and the in-flight counter, one allocation with one owner), and a Task run
carries a second, to the strands its resumer and completion callback reach
after the strand task returns; an ordinary run copies no strands pointer. On
the strand the call enters the instance's action gate, sets up a `ScopedContext` (from
`call.session`) and runs `localOp` (or starts `localOpAsync`'s Task). The
strand side emits `executeLatencyMs`/`executeErrors`, the in-flight
decrement and `beginSpan`/`endSpan` before settling the sink — see
Expand Down
2 changes: 1 addition & 1 deletion docs/spec/core/bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ means.

| Decision | Choice | Why |
|---|---|---|
| Binding storage | **`vector<weak_ptr<HandlerBinding>>`** | `Bridge` does not own the bindings — the handler (`BridgeHandler` or `RawHandler`) holds the `shared_ptr`. Weak references let `switchBackend` and the reconnect handler skip dead bindings without keeping handlers alive. |
| Binding storage | **`vector<weak_ptr<HandlerBinding>>`** | `Bridge` does not own the bindings — the handler (`BridgeHandler` or `RawHandler`) holds the `shared_ptr`. Weak references let `switchBackend` and the reconnect handler skip dead bindings without keeping handlers alive. Each binding records its slot in the vector, so removal swaps the last entry into the slot and pops: O(1), not a scan of every live handler. The vector is therefore unordered; `switchBackend`, the reconnect handler and `~Bridge` each visit every live binding once and need no order. Entries whose binding died without deregistering are compacted whenever the vector has doubled since the last compaction, and by `switchBackend`. |
| Threading | **One owner, given at construction; every verb owner-only, asserted in debug builds** | Consumers call their `Bridge` from one thread, and a free-threaded contract would cost a lock per runtime-settable field, a lifetime gate for teardown on any thread, and a registration handoff for replies that settle on a thread the bridge does not own. Handing the owner to `bindModel`/`promoteModel` as the delivery executor, and having backends post reconnects to it, makes every one of those a task on the owner instead. Teardown follows: handlers before the bridge, on the owner. |
| Registration | **Asynchronous, with no policy: the bind rule** | A handler can be constructed inside a running action, off the owner, so its registration has to be posted; once it can be, every bind can be a `Completion` delivered on the owner. A backend that settles before returning keeps the synchronous feel — a `LocalBackend` handler is bound when its constructor returns — and the caller never has to gate a first dispatch on readiness, because `execute` is held until the bind settles. |
| Reconnect handler | **Posted to the owner; liveness guard + weak-backend guard + stale check; cleared in `~Bridge`** | `IBackend::setReconnectHandler` takes the executor the handler is posted to, and the bridge passes its owner, so the re-bind is an ordinary owner task. The handler checks the bridge's `CallbackToken`, then that the backend that reconnected is still the active one; a reconnect of a backend switched away from is ignored. `~Bridge` and `switchBackend` clear the outgoing backend's handler. |
Expand Down
120 changes: 111 additions & 9 deletions docs/spec/core/executor.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ threading, and serialisation semantics differ per implementation.
- [Type overview](#type-overview)
- [`IExecutor` — the abstract interface](#iexecutor--the-abstract-interface)
- [`ThreadPoolExecutor`](#threadpoolexecutor)
- [Spin before sleep](#spin-before-sleep)
- [`MainThreadExecutor`](#mainthreadexecutor)
- [`QtExecutor`](#qtexecutor)
- [Strands: `ModelStrands` and `ModelId`](#strands-modelstrands-and-modelid)
Expand All @@ -37,7 +38,7 @@ across `morph::exec` and `morph::exec::detail` (in `executor.hpp`, `strand.hpp`,
| Type | Namespace | Purpose |
|---|---|---|
| `IExecutor` | `morph::exec` | Abstract base: a single pure-virtual `post(task)`, and `coreExecutor()`, the executor's stable core-cpp identity. |
| `ThreadPoolExecutor` | `morph::exec` | Fixed-size thread pool, FIFO queue, task exceptions logged (never propagate). |
| `ThreadPoolExecutor` | `morph::exec` | Fixed-size thread pool, FIFO queue, idle workers spin briefly before sleeping (`PoolOptions`), task exceptions logged (never propagate). |
| `MainThreadExecutor` | `morph::exec` | Collects tasks from any thread, drains on `runFor()` from the owning thread. |
| `QtExecutor` | `morph::qt` | Posts tasks to a Qt event loop; they run on the configured context object's thread (the `QCoreApplication`/GUI thread by default). |
| `InlineExecutor` | `morph::exec::detail` (in `executor.hpp`) | Runs each task at once on the posting thread; reached through `detail::inlineExecutor()`. Not serial, so never a `Completion`'s owner. |
Expand Down Expand Up @@ -88,8 +89,9 @@ must be serial: a `Completion` refuses any other (see
## `ThreadPoolExecutor`

A fixed-size thread pool. The constructor spawns `n` worker threads; each worker
loops, waiting for tasks on a shared condition variable. Tasks are dispatched in
FIFO order from a single mutex-protected queue.
loops, taking tasks from one mutex-protected FIFO queue. A worker that finds the
queue empty spins briefly before it sleeps on a shared condition variable (see
[Spin before sleep](#spin-before-sleep)).

`n` is **clamped to a minimum of 1**. A pool with zero workers would accept
posted tasks that no thread could ever run, so every `post()` would hang forever
Expand All @@ -109,6 +111,103 @@ exit and may be silently lost. Exceptions from tasks are caught in the worker
propagate out of a worker, so one failing task neither kills its thread nor
aborts sibling tasks.

### Spin before sleep

Waking a sleeping worker costs a system call on the poster's side (a futex wake
on Linux) and a scheduler round trip on the worker's. For a task that is cheap
to run, that is most of its cost, and an owner thread that posts to a pool of
sleeping workers spends a large share of its time in the wake call.

`PoolOptions` (the constructor's second argument) lets idle workers wait without
sleeping:

| Option | Default | Meaning |
|---|---|---|
| `spinBeforeSleep` | 25 us | How long one worker spins on an atomic queue counter after the queue empties, before it takes the lock and sleeps. `0` disables spinning: a worker sleeps the moment it finds nothing to do. |
| `maxSpinners` | 1 | How many workers may spin at once; the rest sleep at once. `0` also disables spinning. |
| `afterSpin` | empty | Test seam, called on a worker after its spin ends and before it takes the lock. |

The rules that keep the pool correct:

- **Counts change under the lock.** `_sleeping` (workers waiting on the condition
variable) and `_spinning` (workers between their decision to spin and the
moment they retake the lock) are read and written only with `_m` held. The
spin loop itself reads two atomics only: `_queued`, a mirror of the queue size
written under `_m`, and `_stop`.
- **`post()` notifies when `_sleeping > 0` and more tasks are queued than workers
are spinning** (`queue size > _spinning`). A single spinner is expected to
take one queued task by itself; a second queued task, or a task queued while
nobody spins, wakes a sleeper. Posting a burst of blocking tasks therefore
still runs them side by side.
- **A worker that takes a task applies the same rule** to what remains in the
queue: if it was the spinner, the tasks left behind have no spinner to take
them.
- **No lost wakeup.** A worker leaving its spin retakes `_m` and tests the queue
before it waits. A task posted after the spin ended but before the lock was
retaken saw the worker as spinning and did not notify; the re-test under the
lock is what finds it. `afterSpin` exists so a test can hold a worker exactly
in that window.
- **`_sleeping` is decremented by the woken worker**, after it wakes, so a
spurious wakeup neither skews the count nor lets two notifies go to the same
worker unaccounted.
- **Stop.** The destructor sets `_stop` under `_m` and notifies all. A spinner
sees the flag within one check, so the join waits at most one spin period past
the drain.

A worker therefore burns at most `spinBeforeSleep` of CPU after each task that
leaves the queue empty, and none while the pool is idle for longer than that.
At most `maxSpinners` workers do it at the same time, so a pool of 64 workers
costs one spinning core.

The spin is bounded by elapsed time (`steady_clock`), never by a count of
iterations, because a CPU relax hint costs about 10 cycles on some CPUs and 140
on others. The hint is `detail::cpuRelax()`: `_mm_pause` on x86 and x64,
`__yield` on ARM64 under MSVC and clang-cl, `yield` on ARM64 under GCC and clang,
nothing elsewhere. It is deliberately not `std::this_thread::yield()`, which is
a system call.

#### Choosing the default

The default of 25 us comes from a sweep of `spinBeforeSleep` over {0, 10, 25, 50,
100} us with a pool of 8 workers on a 12-core x86-64 Linux machine (GCC 16,
`-O2`), driving `LocalBackend` with a tiny model from a sleeping owner thread.
The machine was loaded by other builds (load average about 19) and the runs are
2 s each, so wall-clock figures carry large noise; the instruction and CPU-time
columns are stable. "Instructions" counts every user-space instruction of the
process, spin loops included, so it rises with the spin while CPU time falls.
The no-spin baseline is the pool before `PoolOptions` existed.

| `spinBeforeSleep` | 1 in flight: calls/s | 1 in flight: CPU (user+sys) per call | 64 in flight: calls/s | 64 in flight: instructions per call | 64 in flight: CPU per call | 64 in flight, 16 workers: calls/s | two processes, 8 workers each: calls/s |
|---|---|---|---|---|---|---|---|
| before | 22.4k | 21.4 us | 62.5k | 7.45k | 16.5 us | not run | not run |
| 0 | 20.2k | 22.1 us | 50.4k | 7.60k | 16.9 us | 44.2k | 53.0k and 48.5k |
| 10 us | 53k to 73k | 13.2 us | 215k | 7.94k | 4.4 us | 202k | 228k and 233k |
| 25 us | 59k to 69k | 12.1 us | 203k to 211k | 8.02k | 4.4 us | 216k | 237k and 229k |
| 50 us | 60k to 76k | 10.9 us | 225k | 8.04k | 4.2 us | 202k | 226k and 206k |
| 100 us | 59k to 69k | 10.9 us | 224k | 8.20k | 4.3 us | 208k | 248k and 233k |

Reading it: the whole gain arrives by 10 us and the differences between 10 and
100 us are inside the noise, so the choice is the cheapest value with margin
for a scheduler hiccup on the worker, and for the owner's turnaround between a
reply and its next call, which is a few microseconds to tens of microseconds.
That is 25 us. The oversubscribed rows (16 workers, and two processes on 12
cores) stay on the same plateau because at most one worker per pool spins, so
two pools cost two spinning cores and not one per worker.

Owner-thread view from `perf record` (64 in flight, 8 workers): the share of
the owner thread's own samples spent in the kernel falls from 69% (30.2 of 43.7
points of all samples) to 29% (12.4 of 42.2). What remains is the owner sleeping
until a reply arrives, which belongs to the owner's executor, not to this pool.

What a spin costs when nothing arrives soon: one worker burns up to
`spinBeforeSleep` of CPU after each task that leaves the queue empty. A caller
that posts rarely and cares about power sets `spinBeforeSleep` to zero.

Windows has not been measured. A condition-variable wake there costs at least
as much as a futex wake on Linux, so the spin is expected to help, but the
default is chosen from the Linux numbers.


## `MainThreadExecutor`

A single-thread executor that does **not** spawn its own thread. Tasks posted
Expand Down Expand Up @@ -349,10 +448,12 @@ ever seen. Retired strands kept for reuse are bounded at 32.

Every executor's `post()` is safe to call from any thread concurrently.

- `ThreadPoolExecutor` guards its queue and `_stop` flag with a single mutex
`_m` and coordinates workers on `_cv`. Multiple workers pop under the lock, so
the FIFO order is a total order across producers; task *execution* is
concurrent across the `n` workers.
- `ThreadPoolExecutor` guards its queue, `_stop` flag and its sleeping and
spinning counts with a single mutex `_m` and coordinates sleeping workers on
`_cv`. A spinning worker reads only two atomics (`_queued`, `_stop`), both
written under `_m`. Multiple workers pop under the lock, so the FIFO order is a
total order across producers; task *execution* is concurrent across the `n`
workers. See [Spin before sleep](#spin-before-sleep).
- `MainThreadExecutor` guards its queue with `_m`. `post()` may be called from
any thread, but `runFor()` must be called only from the single owning
("main") thread; concurrent `runFor()` calls are not supported.
Expand Down Expand Up @@ -653,10 +754,11 @@ and `IoLoop::post` catch `(...)` and so contain every exception type.

| Member | Signature | Notes |
|---|---|---|
| ctor | `explicit ThreadPoolExecutor(std::size_t n)` | Spawns `max(n, 1)` worker threads. `n == 0` is clamped to 1 (a zero-worker pool would hang every task). |
| ctor | `explicit ThreadPoolExecutor(std::size_t n, PoolOptions options = {})` | Spawns `max(n, 1)` worker threads. `n == 0` is clamped to 1 (a zero-worker pool would hang every task). `options` sets how idle workers wait (see [Spin before sleep](#spin-before-sleep)). |
| dtor | `~ThreadPoolExecutor() override` | Signals stop, then joins all workers, which drain the queue (run every already-queued task) before exiting. Tasks posted concurrently with or after destruction may be lost. |
| `post` | `void post(std::function<void()> task) override` | Enqueues to FIFO; notifies one worker. Thread-safe. Each task runs inside a scope naming this pool; exceptions are caught and logged in the worker loop. |
| `post` | `void post(std::function<void()> task) override` | Enqueues to FIFO; notifies one worker only when one is asleep and more tasks are queued than workers are spinning. Thread-safe. Each task runs inside a scope naming this pool; exceptions are caught and logged in the worker loop. |
| `isSerial` | `bool isSerial() const noexcept override` | `false`: the workers run tasks at once. |
| `sleepingWorkers` | `std::size_t sleepingWorkers() const` | Workers asleep on the condition variable, a notified one included until it runs. For tests. |

### `MainThreadExecutor : IExecutor`

Expand Down
Loading
Loading