English
runtime — shard threads, CPUs and memory
ours serves connections in several threads, shards, each with its own single-threaded scheduler. A connection lives on one shard from acceptance to close. sing-box does not have this section: it is an ours extension.
yaml
runtime:
cpus: auto
cpu_affinity: none
shards: 0
stream_placement: spread
quic_window_budget: 16 MB
zerocopy: basic| Field | Values | Default | What it sets |
|---|---|---|---|
cpus | auto or a list of CPU numbers, for example [2, 3] | auto | Which CPUs the shards run on. auto is all CPUs the process is allowed to run on (the taskset/cgroup mask). Every number in the list must be in this mask, otherwise the configuration fails the check. An empty list and repeats are an error |
cpu_affinity | pin, none | none | pin binds shard i to the i-th CPU of the list (round-robin if there are more shards). none lets each shard run on any CPU of the list wherever the system scheduler puts it |
shards | integer | 0 — a shard per CPU from cpus | How many shards to start |
quic_window_budget | a size (16 MB, 4096 kb), -1 for no limit | 1/64 of the machine's memory, from 4 to 16 MiB | How much all QUIC connections of the process together may grow their receive windows beyond the initial ones (below) |
stream_placement | spread, local | spread | Whether an HTTP/2 session (grpc, http, xhttp) and the relays of its streams may run on different shards: spread — the session moves from a saturated shard to a free one; local — everything on one shard (below) |
zerocopy | off, basic, all | basic | Which sends hand data to the kernel by MSG_ZEROCOPY instead of copying it (below) |
The command line can replace the fields: --cpus, --cpu-affinity, --shards (Command line).
What to choose
- Usually, the defaults. With
nonethe system moves a shard off a CPU busy with something else: on a router the network card's interrupts are often handled on CPU 0. pin— when you lay out the CPUs yourself, for example a separate CPU for interrupts andcpuswithout it.- To limit ours to part of the machine:
cpus: [2, 3].
pin with more shards than CPUs in cpus gives a warning: several shards share one CPU.
Distribution of connections
The shard that accepted a connection may hand it to a less loaded shard while nothing of the protocol has been read yet. A shard's load is the busyness of its CPU (with pin, of the whole CPU, including interrupts and other processes; with none, the CPU time of the thread itself) over the last second plus the number of active, new and idle connections. There is nothing to configure here.
HTTP/2 session streams
One connection of the grpc, http (with TLS) or xhttp transport carries many streams. The session — the socket's TLS, encryption, HTTP/2 frames — is run by one task, the relay of each stream with its outgoing socket by another, and at first they are all on the shard that accepted the connection. With spread, if that shard is almost fully loaded, the session is noticeable in its load and another shard is free, the session moves there (decided every half second by a load measurement). This way one heavy stream takes two cores instead of one. When all shards are busy, nothing moves and there is no extra cost. The same on the client: a session can leave the shard where the relays of the connections using it run.
local keeps a session and its streams always on one shard. With one shard the value has no effect.
QUIC receive windows
hysteria, hysteria2 and tuic connections start with small receive windows (512 KiB per stream, 1 MiB per connection) and double them up to the protocol's maximums (8/20 MiB for hysteria2) while the sender is bound by the window. The window is how much data the peer may keep in flight, and so how much may pile up in memory on the reading side (and on the sender, if it is ours). quic_window_budget limits the total window growth of the connections of the whole process: a connection grows its window only while there is room in the budget, and returns what it took on close. Without a budget, every connection, one per shard or more, could grow to the maximum at once and use a lot of memory.
The initial windows are not part of the budget: a connection works even with the budget exhausted, but no faster than 1 MiB per round trip allows. 16 MiB is enough for one connection for about 1.3 Gbit/s at an RTT of 100 ms. A larger budget is for many fast connections on long paths; a smaller one for a router with little memory.
Zerocopy
With MSG_ZEROCOPY the kernel sends from the process's own buffer instead of copying it into a freshly zeroed page, and the buffer stays with the kernel until the peer has acknowledged the data. basic: plain-TCP relays of whole buffers, and UDP batches. all adds data from TLS records, QUIC, HTTP/2 and XHTTP, and ours' own TLS records (at most two sends in flight per connection). all costs a few MiB more memory per busy TLS connection and is usually no faster. off copies every send. The environment variable OURS_ZEROCOPY overrides the field: 0 for off, all for all.
Reload
cpus, cpu_affinity and stream_placement are applied to the running shards on reload (stream_placement to the next decisions about moving), quic_window_budget to the next window growth (connections do not give back what they took until they close), zerocopy to the next sends (TLS connections keep what they had at the handshake). shards only at startup: if the value changed, the warning runtime.shards: N running, M configured: restart to change it is written to the log, and the rest of the configuration is applied.