From c7f9fdd706695e55f22594b0a3d84a32bb7449e3 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 19 Oct 2026 10:36:30 +0200 Subject: [PATCH] Dedicated server on Linux: world generation and packet writers behind the server thread Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 4 ++ crates/horizon-client/src/lan/conn.rs | 6 +- crates/horizon-client/src/streaming.rs | 2 + crates/horizon-threads/Cargo.toml | 2 +- crates/horizon-threads/src/lib.rs | 81 ++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 225c1a5e2..fd57830c9 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -12132,6 +12132,10 @@ pub fn run() -> Result<()> { if options.dedicated.is_some() && !dedicated::preflight(&mut options)? { return Ok(()); } + if options.dedicated.is_some() { + // Before any worker starts: on Linux they go behind the server thread. + horizon_threads::put_helpers_behind(); + } let started = Instant::now(); horizon_client::spans::enable(options.spikes.is_some()); let vanilla_root = options.resources.clone(); diff --git a/crates/horizon-client/src/lan/conn.rs b/crates/horizon-client/src/lan/conn.rs index 2805cf1da..e70b872d5 100644 --- a/crates/horizon-client/src/lan/conn.rs +++ b/crates/horizon-client/src/lan/conn.rs @@ -774,7 +774,11 @@ fn play(c: Connection, shared: &Arc, parts: Parts, address: std::net::So let (alive, latency, pending) = (alive.clone(), latency.clone(), pending.clone()); std::thread::Builder::new() .name(format!("horizon-lan-out-{conn_id}")) - .spawn(move || write_loop(writer, rx, alive, latency, pending)) + .spawn(move || { + // A dedicated server's tick comes first (horizon_threads). + horizon_threads::behind_server(); + write_loop(writer, rx, alive, latency, pending) + }) .ok() }; let joined = Joined { diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 933219a21..0a63428e9 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -433,6 +433,8 @@ impl Streaming { thread::Builder::new() .name(format!("horizon-worker-{i}")) .spawn(move || { + // A dedicated server's tick comes first (horizon_threads). + horizon_threads::behind_server(); let mut worker = generator.worker(); // The face visibility table and its occlusion cache stay with // the worker across mesh jobs, like vanilla's per-thread cache: diff --git a/crates/horizon-threads/Cargo.toml b/crates/horizon-threads/Cargo.toml index 35f7e3222..3d7581fe9 100644 --- a/crates/horizon-threads/Cargo.toml +++ b/crates/horizon-threads/Cargo.toml @@ -6,7 +6,7 @@ license.workspace = true rust-version = "1.85" description = "Scheduling hints for the game's threads: the OS calls the crates that forbid unsafe code can't make" -[target.'cfg(target_vendor = "apple")'.dependencies] +[target.'cfg(any(target_vendor = "apple", target_os = "linux"))'.dependencies] libc = "0.2" [target.'cfg(windows)'.dependencies] diff --git a/crates/horizon-threads/src/lib.rs b/crates/horizon-threads/src/lib.rs index 6436a4729..d6cd135e0 100644 --- a/crates/horizon-threads/src/lib.rs +++ b/crates/horizon-threads/src/lib.rs @@ -1,6 +1,8 @@ //! Scheduling hints for the game's threads: the few OS calls that the other //! crates, which forbid unsafe code, can't make themselves. +use std::sync::atomic::{AtomicBool, Ordering}; + /// Ask the OS to run the calling thread ahead of the process's other /// threads, as vanilla's `MinecraftServer.spin` does for its server thread /// (`Thread.setPriority(8)` when there are more than four processors), so @@ -38,6 +40,55 @@ fn favour() -> bool { false } +/// Set once by a dedicated server, before its helper threads start: they +/// then go behind its server thread (`behind_server`). +static HELPERS_BEHIND: AtomicBool = AtomicBool::new(false); + +/// How many nice levels a helper thread goes below the process on Linux. +#[cfg(target_os = "linux")] +const BEHIND: i32 = 10; + +/// A dedicated server: from now on, the helper threads that call +/// `behind_server` as they start go behind the server thread. Call it +/// before they start. +pub fn put_helpers_behind() { + HELPERS_BEHIND.store(true, Ordering::Relaxed); +} + +/// Called by a helper thread as it starts (world generation and lighting, +/// a connection's packet writer): work the tick never waits for. In a +/// dedicated server (`put_helpers_behind`) on Linux, where the server thread +/// can't be raised (`favour_current`), the helper lowers itself instead, +/// which an unprivileged thread may: 10 nice levels below the process, or, +/// where nice runs out (a server started under nice 10 or more), the idle +/// class, which runs the thread only when no ordinary thread wants its core. +/// The server thread then gets a core when its tick starts instead of +/// waiting behind them. Nothing elsewhere. Whether the OS took it. +pub fn behind_server() -> bool { + HELPERS_BEHIND.load(Ordering::Relaxed) && behind() +} + +#[cfg(target_os = "linux")] +fn behind() -> bool { + // SAFETY: these read and set the calling thread's own nice value or + // policy (who 0, pid 0 is the calling thread on Linux); `param` lives + // across the call. + unsafe { + let nice = libc::getpriority(libc::PRIO_PROCESS as _, 0); + if nice + BEHIND <= 19 { + libc::setpriority(libc::PRIO_PROCESS as _, 0, nice + BEHIND) == 0 + } else { + let param = libc::sched_param { sched_priority: 0 }; + libc::sched_setscheduler(0, libc::SCHED_IDLE, ¶m) == 0 + } + } +} + +#[cfg(not(target_os = "linux"))] +fn behind() -> bool { + false +} + #[cfg(test)] mod tests { /// The hint holds on the platforms that take it. @@ -63,4 +114,34 @@ mod tests { .join() .unwrap(); } + + /// A helper goes behind only once a dedicated server asks for it, and + /// then, on Linux, 10 nice levels below the process or in the idle class. + #[test] + fn helpers_go_behind_only_when_asked() { + assert!(!std::thread::spawn(super::behind_server).join().unwrap()); + super::put_helpers_behind(); + #[cfg(target_os = "linux")] + std::thread::spawn(|| { + // SAFETY: reads the calling thread's own nice value. + let before = unsafe { libc::getpriority(libc::PRIO_PROCESS as _, 0) }; + assert!(super::behind_server()); + // SAFETY: reads the calling thread's own nice value and policy. + let (after, policy) = unsafe { + ( + libc::getpriority(libc::PRIO_PROCESS as _, 0), + libc::sched_getscheduler(0), + ) + }; + if before + super::BEHIND <= 19 { + assert_eq!(after, before + super::BEHIND); + } else { + assert_eq!(policy, libc::SCHED_IDLE); + } + }) + .join() + .unwrap(); + #[cfg(not(target_os = "linux"))] + assert!(!std::thread::spawn(super::behind_server).join().unwrap()); + } }