[PR #1006] [MERGED] Dedicated server on Linux: world generation and packet writers behind the server thread #2981

Closed
opened 2026-11-02 16:00:37 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/1006
Author: @ludovic111
Created: 10/19/2026
Status: ✅ Merged
Merged: 10/21/2026
Merged by: @ludovic111

Base: main ← Head: perf/server-helpers-behind


📝 Commits (3)

  • c7f9fdd Dedicated server on Linux: world generation and packet writers behind the server thread
  • f137a8d Merge origin/main into perf/server-helpers-behind
  • b928dc3 Merge origin/main into perf/server-helpers-behind

📊 Changes

5 files changed (+93 additions, -2 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+4 -0)
📝 crates/horizon-client/src/lan/conn.rs (+5 -1)
📝 crates/horizon-client/src/streaming.rs (+2 -0)
📝 crates/horizon-threads/Cargo.toml (+1 -1)
📝 crates/horizon-threads/src/lib.rs (+81 -0)

📄 Description

🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.

Neutral on a quiet machine, faster when something else is using the cores. The orchestrator decided the trade is worth it (2026-10-21).

What

On Linux an unprivileged thread can't raise its priority, so #968's favour_current does nothing for the server thread there. A thread may lower its own priority, though. In a dedicated server (horizon-server's engine), the threads the tick never waits for now lower themselves as they start: the world generation and lighting workers (horizon-worker) and each connection's packet writer (horizon-lan-out). They go 10 nice levels below the process, or into SCHED_IDLE when nice runs out (a server started under nice 10 or more). The server thread keeps the process's priority and gets a core first when a tick starts.

  • horizon_threads::put_helpers_behind() is called once, in game::run, when the engine runs a dedicated server, before those threads start. horizon_threads::behind_server() is called at the top of both threads. It does nothing unless asked, and nothing off Linux (macOS and Windows already raise the server thread instead).
  • The game itself, single player and LAN hosting from the client, is unchanged.
  • Packets, chunk contents and the order of work are unchanged; only the OS scheduler's choice between threads changes.

Why

Brief perf-server-12, task 1. On the ThinkPad (i7-7500U, 2 cores / 4 threads), the worst tick_blocks spikes were 109.6 ms wall for 13.4 ms CPU: the server thread was waiting for a core behind the worker threads.

Numbers

Same setup as server-host-10's four-way benchmark (benchmarks/server-four-way-2026-10-19): ThinkPad server, tools/server_bench.py --behaviour mixed --explore-speed 0.8 --warmup 300 --seconds 300 --nice 19, bots on the Mac mini over Tailscale, seed 12345, view distance 10. Release builds of main 56fcc91e with and without this change, run alternately. MSPT, p95, p99 and chunks/s are the benchmark's own; ticks over 50 ms come from HORIZON_SPIKES=0 (every tick's wall time, last 6000 ticks; logged in the 25-bot runs).

Quiet machine (no other session's processes during the runs):

run MSPT avg p95 p99 ticks > 50 ms chunks/s
10 bots base 1 / 2 23.10 / 22.83 40.2 / 39.5 54.1 / 56.1 not logged 61.26 / 61.27
10 bots new 1 / 2 23.01 / 23.34 40.0 / 40.6 55.0 / 54.5 not logged 61.30 / 61.22
25 bots base 1 / 2 30.72 / 31.37 48.4 / 49.8 71.2 / 74.3 210 / 225 156.25 / 156.07
25 bots new 1 / 2 30.52 / 30.38 48.9 / 48.2 78.1 / 73.0 228 / 239 156.08 / 156.31

That is neutral within run-to-run noise. On a quiet machine the server thread barely waits: its run-queue wait (/proc/<pid>/task/<tid>/schedstat) at 10 bots is 0.9 % of its time on main and 0.5 % with this change. At 25 bots the tick is CPU-bound (29 ms CPU in a 30 ms tick). Chunks per second are identical.

Busy machine (other sessions' builds and game runs sharing the 4 threads): a within-run A/B. One server runs while a script switches the worker and writer threads between normal and idle priority every 60 s (chrt), so both phases see the same foreign load:

run helpers normal helpers behind
10 bots, MSPT avg 97.2 79.0 (−19 %)
25 bots, MSPT avg 296.8 271.4 (−9 %)

Under that load chunk loading slows down while the server thread runs first. That is the trade-off.

Checks

On the rebased branch (c7f9fdd7, on main 5c88c46c):

  • Mac mini: cargo fmt --all -- --check clean; cargo clippy --workspace --all-targets -- -D warnings clean; cargo test -p horizon-threads 2 passed (on macOS the helpers stay put).
  • ThinkPad (Linux): cargo clippy -p horizon-threads --all-targets -- -D warnings clean; cargo test -p horizon-threads passed when started at nice 0 (the helper goes to nice 10) and at nice 19 (it goes to SCHED_IDLE).
  • With the other two perf-server-12 branches on main ee59df13: tools/parity.py --changed origin/main ran 98 harnesses, 98 passed, and cargo test -p horizon-client --lib passed 922. No harness checks thread priority; packets and world contents don't depend on it.

Needs Ludovic

(Decided by the orchestrator on 2026-10-21: worth it, if correct; proof in the comments.)

Whether to take a change that only helps when a Linux host is shared with other work (a home server also running builds or a desktop session). On a quiet machine it changes nothing measurable, and under foreign load chunks arrive more slowly. The alternative is the systemd unit hint (CPUWeight= / Nice= for the whole service), which needs no code, but raises the whole server rather than favouring its server thread.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/1006 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/19/2026 **Status:** ✅ Merged **Merged:** 10/21/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/server-helpers-behind` --- ### 📝 Commits (3) - [`c7f9fdd`](https://github.com/horizon-rust/horizon/commit/c7f9fdd706695e55f22594b0a3d84a32bb7449e3) Dedicated server on Linux: world generation and packet writers behind the server thread - [`f137a8d`](https://github.com/horizon-rust/horizon/commit/f137a8d3cc11321ca09161c365c1997dd9f29cb9) Merge origin/main into perf/server-helpers-behind - [`b928dc3`](https://github.com/horizon-rust/horizon/commit/b928dc3c9b8be3ce7022ff5d6a6c9d13af64e100) Merge origin/main into perf/server-helpers-behind ### 📊 Changes **5 files changed** (+93 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+4 -0) 📝 `crates/horizon-client/src/lan/conn.rs` (+5 -1) 📝 `crates/horizon-client/src/streaming.rs` (+2 -0) 📝 `crates/horizon-threads/Cargo.toml` (+1 -1) 📝 `crates/horizon-threads/src/lib.rs` (+81 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. Neutral on a quiet machine, faster when something else is using the cores. The orchestrator decided the trade is worth it (2026-10-21). ## What On Linux an unprivileged thread can't raise its priority, so #968's `favour_current` does nothing for the server thread there. A thread may lower its own priority, though. In a dedicated server (`horizon-server`'s engine), the threads the tick never waits for now lower themselves as they start: the world generation and lighting workers (`horizon-worker`) and each connection's packet writer (`horizon-lan-out`). They go 10 nice levels below the process, or into `SCHED_IDLE` when nice runs out (a server started under nice 10 or more). The server thread keeps the process's priority and gets a core first when a tick starts. - `horizon_threads::put_helpers_behind()` is called once, in `game::run`, when the engine runs a dedicated server, before those threads start. `horizon_threads::behind_server()` is called at the top of both threads. It does nothing unless asked, and nothing off Linux (macOS and Windows already raise the server thread instead). - The game itself, single player and LAN hosting from the client, is unchanged. - Packets, chunk contents and the order of work are unchanged; only the OS scheduler's choice between threads changes. ## Why Brief perf-server-12, task 1. On the ThinkPad (i7-7500U, 2 cores / 4 threads), the worst `tick_blocks` spikes were 109.6 ms wall for 13.4 ms CPU: the server thread was waiting for a core behind the worker threads. ## Numbers Same setup as server-host-10's four-way benchmark (`benchmarks/server-four-way-2026-10-19`): ThinkPad server, `tools/server_bench.py --behaviour mixed --explore-speed 0.8 --warmup 300 --seconds 300 --nice 19`, bots on the Mac mini over Tailscale, seed 12345, view distance 10. Release builds of main 56fcc91e with and without this change, run alternately. MSPT, p95, p99 and chunks/s are the benchmark's own; ticks over 50 ms come from `HORIZON_SPIKES=0` (every tick's wall time, last 6000 ticks; logged in the 25-bot runs). **Quiet machine (no other session's processes during the runs):** | run | MSPT avg | p95 | p99 | ticks > 50 ms | chunks/s | |---|---:|---:|---:|---:|---:| | 10 bots base 1 / 2 | 23.10 / 22.83 | 40.2 / 39.5 | 54.1 / 56.1 | not logged | 61.26 / 61.27 | | 10 bots new 1 / 2 | 23.01 / 23.34 | 40.0 / 40.6 | 55.0 / 54.5 | not logged | 61.30 / 61.22 | | 25 bots base 1 / 2 | 30.72 / 31.37 | 48.4 / 49.8 | 71.2 / 74.3 | 210 / 225 | 156.25 / 156.07 | | 25 bots new 1 / 2 | 30.52 / 30.38 | 48.9 / 48.2 | 78.1 / 73.0 | 228 / 239 | 156.08 / 156.31 | That is neutral within run-to-run noise. On a quiet machine the server thread barely waits: its run-queue wait (`/proc/<pid>/task/<tid>/schedstat`) at 10 bots is 0.9 % of its time on main and 0.5 % with this change. At 25 bots the tick is CPU-bound (29 ms CPU in a 30 ms tick). Chunks per second are identical. **Busy machine (other sessions' builds and game runs sharing the 4 threads):** a within-run A/B. One server runs while a script switches the worker and writer threads between normal and idle priority every 60 s (`chrt`), so both phases see the same foreign load: | run | helpers normal | helpers behind | |---|---:|---:| | 10 bots, MSPT avg | 97.2 | **79.0** (−19 %) | | 25 bots, MSPT avg | 296.8 | **271.4** (−9 %) | Under that load chunk loading slows down while the server thread runs first. That is the trade-off. ## Checks On the rebased branch (c7f9fdd7, on main 5c88c46c): - Mac mini: `cargo fmt --all -- --check` clean; `cargo clippy --workspace --all-targets -- -D warnings` clean; `cargo test -p horizon-threads` 2 passed (on macOS the helpers stay put). - ThinkPad (Linux): `cargo clippy -p horizon-threads --all-targets -- -D warnings` clean; `cargo test -p horizon-threads` passed when started at nice 0 (the helper goes to nice 10) and at nice 19 (it goes to `SCHED_IDLE`). - With the other two perf-server-12 branches on main ee59df13: `tools/parity.py --changed origin/main` ran 98 harnesses, 98 passed, and `cargo test -p horizon-client --lib` passed 922. No harness checks thread priority; packets and world contents don't depend on it. ## Needs Ludovic (Decided by the orchestrator on 2026-10-21: worth it, if correct; proof in the comments.) Whether to take a change that only helps when a Linux host is shared with other work (a home server also running builds or a desktop session). On a quiet machine it changes nothing measurable, and under foreign load chunks arrive more slowly. The alternative is the systemd unit hint (`CPUWeight=` / `Nice=` for the whole service), which needs no code, but raises the whole server rather than favouring its server thread. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 16:00:37 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#2981
No description provided.