Docs · Context Runtime
Inside the Context Runtime.
The runtime is a small set of cooperating managers inside one process per node. This page names each one, says what it owns, and follows a task through them. It describes structure, not APIs; for the calls themselves see the API reference, and for the reasoning behind the mechanisms see the Design section.
- Process
- one per node
- Core pieces
- config, IPC, module, pool, work orchestrator, viz
- Threads
- runtime.num_threads workers + monitor + dashboard pool
The pieces at a glance
The managers
- Config manager
- Reads
~/.clio/clio.yaml(or the file named by the environment), the hostfile and thecomposelist, and applies environment overrides. Everything else asks it for settings such as the thread count, the scheduler name and the ports. - Module manager
- At startup, scans the runtime's own library directory,
CLIO_REPO_PATH,LD_LIBRARY_PATHand default paths for shared libraries whose names contain_runtime, opens each one, and records its factory functions. It can then construct a container of any loaded module by name. On load it also builds a throwaway prototype container so the module can register its dashboard pages before any pool exists. - Pool manager
- Owns the table of pools and their local containers. It creates and destroys pools on request from the admin module, resolves a task's pool id and
PoolQueryto a container (here or on another node), saves and restores each container's learned cost model, and hands containers to workers through a lightweight handle cached at routing time. - IPC manager
- Owns every way a task can enter or leave the process, described below, plus node identity, peer liveness and the heartbeat clients use to notice a restarted runtime.
- Work orchestrator
- Spawns and owns the worker threads and their lanes, holds the active scheduler, and runs the monitor thread that calls the scheduler's load-balancing hook every 100 ms.
- Viz server
- A small HTTP server with its own thread pool. It serves the dashboard, a registry of REST routes that modules add, and static assets a module ships in a
viz/directory.
Tasks and futures
A task is a C++ struct with input and output fields plus a header: its pool, method, PoolQuery, flags and return code. Submitting a task returns a future. The future's completion flag lives in memory both sides can see, so a caller in another process or on a GPU can wait for it without a message. Only inputs travel toward the container and only outputs travel back: tasks are serialized at every process boundary and are never shared objects.
Inside the runtime a handler that submits a subtask passes it by pointer, with no serialization, and co_awaits its future. When the subtask completes, the parent coroutine is put back on its worker's queue.
Transports and queues
The IPC manager picks a transport for each direction a task can travel. Every transport has the same four steps: send the input, receive the input, send the output, receive the output.
| Path | Mechanism | Used by |
|---|---|---|
| Runtime to itself | Pointer handed to a worker lane; no copy | Subtasks from handlers, periodic tasks |
| Client to runtime, same machine | Shared-memory ring buffers and a shared data segment per client | Default for local clients |
| Client to runtime, socket | ZeroMQ over a Unix socket or TCP, read by a dedicated receive thread. A worker queues the reply; the network receive worker writes it | Containers, remote clients, or when shared memory is unavailable. Probed fastest-first unless CLIO_IPC_MODE forces one |
| Runtime to runtime | ZeroMQ TCP: DEALER sockets per peer for sends, one ROUTER for receives; messages carry an incarnation number | Any task whose PoolQuery resolves to another node |
| GPU to runtime | A device-memory ring per GPU. The kernel fills a pre-allocated task, the GPU worker copies it in, runs it, and writes the output back | Memory expansion and GPU kernels |
Worker lanes are multi-producer, single-consumer ring buffers: any thread may push, and only the owning worker pops. A worker that would push onto its own full lane is redirected to another worker, so a full queue becomes back-pressure instead of a deadlock.
Inside the process: lanes, workers and queues
Workers, coroutines and the scheduler
Each worker loops over its shared-memory shard, its event queue (completions of subtasks its handlers await), its lane, its GPU rings, and its periodic and retry queues. Running a task means resuming its handler coroutine until it finishes or suspends. Handlers are C++20 stackless coroutines; with CLIO_CORE_ENABLE_BOOST_COROUTINES they are Boost.Context fibers behind the same macros, which allows suspending from nested ordinary functions at the price of a stack per task.
Roles are fixed by position. Worker 0 is the scheduler worker. With three or more threads the last two are the network send worker, which drains the outbound network queue, and the network receive worker, which writes replies to socket clients. Messages from other runtimes and from socket clients are read by two dedicated threads that are not workers, so send-side back-pressure never stops incoming replies and probes from draining. A GPU worker is carved out before the network pair when there are five or more threads, and otherwise shares the scheduler worker. The rest are general workers.
The scheduler is an object behind a small interface, chosen by runtime.local_sched. The default one predicts how long each task will hold a worker, using a per-method linear model that each container learns from measured run times and that the pool manager saves to disk between runs. It keeps each worker below about 100 µs of booked work: a task stays on the submitting worker when it fits, otherwise it goes to the least-loaded worker, and when nothing fits the pool grows by one thread. Every 100 ms the monitor thread treats a worker stuck in one task for over a second as stalled, starts a replacement and moves its backlog, lets idle workers steal, and retires idle extra threads. The local scheduler maps tasks by a hash of the submitting process and thread and never rebalances. Write a scheduler describes the interface.
Pools, containers and routing
A pool has one container per node, and the container id is the node id. When a task is routed, its PoolQuery selects containers: the local one, one by id or hash, a range, all of them, a physical node, or a collective. Tasks for this node go to a worker lane; tasks for another node go to the network send worker. A module that holds data receives Expand when a node joins, so it can re-partition, and Recover when a container is rebuilt on another node.
Collectives. A ManyToOne or AllToOne query sends each submitter's task to a leader, where the batch manager parks it, grouped by pool, method, target and batch key. When the batch window closes, it builds one aggregate task by folding each member's inputs in through the container's AggregateIn, runs it once, and copies the output back into every member. A thousand ranks asking for the same thing cost one execution.
A task's trip to another node and back
What persists
- The restart log is an append-only log of the compose files that should be re-composed at the next
clio-run start. Pools created withrestart: trueare recorded the same way.--freshdiscards it. - Cost models are saved per module, pool, node and container, flushed about once a second while they change and on shutdown, and restored when the container is recreated, so a restarted runtime schedules well from the first task.
- Module state is the module's own business: the CTE keeps its metadata log, a block device its allocation log. A container's recovery callback is where it reloads them.
The dashboard
The viz server answers built-in routes such as /api/health, /api/topology, /api/pools, /api/config, /api/chimods and /api/routes (the list of every registered route). Module routes and asset directories are added at load time. Handlers run on the server's own threads, not on workers, so a slow page never delays a task; they may submit a task and wait for it.
Costs of this design
| Decision | Buys | Costs |
|---|---|---|
| Everything is a module | One mechanism for built-in and custom code | A new library needs a runtime restart to be found; online code swap is not yet enabled |
| Serialized tasks at process boundaries | Crash isolation between clients and runtime | Inputs and outputs are copied once per crossing |
| Cooperative coroutines | Thousands of waiting tasks on a few threads | A handler that blocks without yielding holds its worker until the monitor rescues the queue behind it |
| Learned hold-time scheduling | Few threads, low latency, no manual classes | Predictions are only as good as a module's reported task size |
| Fixed network workers | Receives never wait behind sends | Two workers and two receive threads per node reserved for networking |
The Runtime and block devices design page discusses failure detection, membership and the block-device allocator in more depth.