Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.
Rust differential-testinggame-enginegamedevlinuxminecraftminecraft-javareimplementationrustvoxelvulkanwgpu
Dedicated server: log slow ticks and vanilla's overload warning
Ludovic Mariec9d06599c7ab
diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rsindex 97b3ece51..9771935e9 100644--- a/crates/horizon-client/src/game.rs+++ b/crates/horizon-client/src/game.rs@@ -20392,6 +20392,15 @@ pub fn run() -> Result<()> {horizon_threads::catch_stop_signals();}let started = Instant::now();+ // A dedicated server writes its slow ticks to the log with the spans+ // they spent their time in (`HORIZON_SPIKES`'s line; the variable still+ // sets another threshold), so a lag spike players felt can be traced+ // from the log alone. Timing the spans costs microseconds a tick.+ if options.spikes.is_none()+ && options.execution_role == execution_role::ExecutionRole::DedicatedServer+ {+ options.spikes = Some(dedicated::SLOW_TICK_MS);+ }horizon_client::spans::enable(options.spikes.is_some());let vanilla_root = options.resources.clone();if let Some(error) = missing_resources(&vanilla_root) {diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rsindex 54c5235e1..14ca7312e 100644--- a/crates/horizon-client/src/game/dedicated.rs+++ b/crates/horizon-client/src/game/dedicated.rs@@ -257,6 +257,16 @@ fn forwarding_secret(online: bool) -> Result<Option<Vec<u8>>> {Ok(Some(secret))}+/// `MinecraftServer.runServer`'s overload warning for a server `behind` its+/// schedule, in milliseconds and in ticks of `tick`.+fn overload_warning(behind: Duration, tick: Duration) -> String {+ format!(+ "Can't keep up! Is the server overloaded? Running {}ms or {} ticks behind",+ behind.as_millis(),+ behind.as_nanos() / tick.as_nanos().max(1)+ )+}+/// Log4j's console pattern: `[HH:mm:ss] [thread/LEVEL]: message`.pub(super) fn log(level: &str, message: &str) {log_from("Server thread", level, message);@@ -502,6 +512,13 @@ const LEGACY_PRESET_NAMES: [(&str, horizon_client::save::WorldPreset); 2] = [/// server.properties' line for `server.toml`'s `max_view_distance` (horizon-server writes it).const FAR_PROPERTY: &str = "horizon-max-view-distance";+/// A dedicated server logs a tick slower than this, in milliseconds, with+/// its spans, unless `HORIZON_SPIKES` names another threshold: five ticks'+/// worth, a stall players feel as rubber-banding.+pub(super) const SLOW_TICK_MS: f32 = 250.0;+/// `MinecraftServer.OVERLOADED_WARNING_INTERVAL_NANOS` plus 100 ticks: the+/// "Can't keep up!" warning comes at most this often.+const OVERLOAD_WARNING_INTERVAL: Duration = Duration::from_secs(15);/// server.properties' line for `server.toml`'s `admin_feedback_to_ops` (horizon-server writes it).const ADMIN_FEEDBACK_PROPERTY: &str = "horizon-admin-feedback-to-ops";@@ -995,6 +1012,7 @@ impl Game<'_> {self.fns.permission = Some(props.function_permission_level.clamp(0, 4) as u8);let watchdog = Watchdog::start(props.max_tick_time);let mut braked = Instant::now();+ let mut overload_warned: Option<Instant> = None;while !state.stop {let tick_start = Instant::now();if let Some(watchdog) = &watchdog {@@ -1084,7 +1102,17 @@ impl Game<'_> {far.brake(mspt, host.hub.config.view_distance);}state.ticked(Instant::now());- self.wait_for_next_server_tick(&mut next, paused);+ // MinecraftServer.runServer: a server more than a second's ticks+ // behind skips them and says so, at most every 15 seconds.+ if let Some(behind) = self.wait_for_next_server_tick(&mut next, paused)+ && overload_warned.is_none_or(|at| at.elapsed() >= OVERLOAD_WARNING_INTERVAL)+ {+ overload_warned = Some(Instant::now());+ log(+ "WARN",+ &overload_warning(behind, self.clock.tick_duration()),+ );+ }}if let Some(watchdog) = &watchdog {watchdog.running.store(false, Ordering::Relaxed);@@ -1950,7 +1978,7 @@ fn png_size(bytes: &[u8]) -> Option<(u32, u32)> {#[cfg(test)]mod tests {- use super::{capture_log, log, log_from, panic_message};+ use super::{capture_log, log, log_from, overload_warning, panic_message};use std::{fs,io::{self, BufRead, BufReader, Read, Write},@@ -1958,6 +1986,16 @@ mod tests {time::{SystemTime, UNIX_EPOCH},};+ /// Vanilla's words, with how far behind the server is in milliseconds and in whole ticks.+ #[test]+ fn the_overload_warning_reads_as_vanilla_s() {+ let tick = std::time::Duration::from_millis(50);+ assert_eq!(+ overload_warning(std::time::Duration::from_millis(13_027), tick),+ "Can't keep up! Is the server overloaded? Running 13027ms or 260 ticks behind"+ );+ }+/// A `panic!("literal")` carries a `&str`, a formatted one a `String`; anything else is/// named as unknown, so the crash line is never empty.#[test]diff --git a/crates/horizon-client/src/game/levels.rs b/crates/horizon-client/src/game/levels.rsindex 5c5b0dcad..11ff48b22 100644--- a/crates/horizon-client/src/game/levels.rs+++ b/crates/horizon-client/src/game/levels.rs@@ -519,8 +519,14 @@ impl Game<'_> {/// The end of `MinecraftServer.runServer`'s loop for a server thread: a/// finished `/tick sprint` reports to the operators as the server, then/// the next tick starts one tick length after this one, or straight away- /// while the sprint runs (`checkShouldSprintThisTick`).- pub(super) fn wait_for_next_server_tick(&mut self, next: &mut Instant, paused: bool) {+ /// while the sprint runs (`checkShouldSprintThisTick`). A server more+ /// than two seconds behind skips the ticks it missed: that returns how far+ /// behind it was, for the overload warning.+ pub(super) fn wait_for_next_server_tick(+ &mut self,+ next: &mut Instant,+ paused: bool,+ ) -> Option<Duration> {if let Some(report) = self.clock.take_sprint_report() {// ServerTickRateManager.finishTickSprint: the server's sendSuccess.self.console_broadcast = Some(true);@@ -535,7 +541,7 @@ impl Game<'_> {let now = Instant::now();if !paused && self.clock.sprinting() {*next = now;- return;+ return None;}*next += self.clock.tick_duration();if *next > now {@@ -547,8 +553,11 @@ impl Game<'_> {std::thread::sleep(*next - now);}} else if now - *next > Duration::from_secs(2) {+ let behind = now - *next;*next = now;+ return Some(behind);}+ None}/// The levels this server tick runs, in `MinecraftServer.levels` orderdiff --git a/docs/hosting.md b/docs/hosting.mdindex 1ecda4c60..96585723a 100644--- a/docs/hosting.md+++ b/docs/hosting.md@@ -293,6 +293,10 @@ sends, saving...), the slowest ticks, how many entities of each type, chunks, plsettings that matter (view and simulation distance, max players). They hold no names, UUIDs or addresses, so youcan attach them to an issue as they are. On the console it is `horizon-report [SECONDS]`.+Between reports, the log keeps the spikes: a tick slower than 250 ms writes a `Horizon: slow tick` line with+its phases (`HORIZON_SPIKES=MS` in the engine's environment sets another threshold), and a server more than+two seconds behind says `Can't keep up!` as vanilla's does, at most every 15 seconds.+`config set` changes `server.toml` without opening it: `horizon-server config set world.difficulty=hardserver.max_players=10`. Each key is a section and a name from the file (`properties.spawn-protection=0` for anyother vanilla key). The values are checked as the server checks them, and nothing is written if one is wrong.