Dedicated server on Linux: world generation and packet writers behind the server thread

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-19 10:36:30 +02:00
parent 5c88c46c51
commit c7f9fdd706
5 changed files with 93 additions and 2 deletions
+4
View File
@@ -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();
+5 -1
View File
@@ -774,7 +774,11 @@ fn play(c: Connection, shared: &Arc<Shared>, 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 {
+2
View File
@@ -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:
+1 -1
View File
@@ -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]
+81
View File
@@ -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, &param) == 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());
}
}