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
Startup trace: HORIZON_STARTUP_TRACE writes the start's steps per thread; tools/startup_trace.py
Ludovic Mariecd593e15b90f
diff --git a/crates/horizon-client-render/src/gfx.rs b/crates/horizon-client-render/src/gfx.rsindex ad79a9924..5b3ee3b82 100644--- a/crates/horizon-client-render/src/gfx.rs+++ b/crates/horizon-client-render/src/gfx.rs@@ -1857,7 +1857,10 @@ impl Renderer {device_lost: renderer.device_lost.clone(),}}- None => open_device(&window, offscreen, api, layers).await?,+ None => {+ let _step = crate::spans::startup::step("renderer.open_device");+ open_device(&window, offscreen, api, layers).await?+ }};// Computed once: the sprite arrays, the shaders' constants and the// shader pack atlas all cut the sprites by it.@@ -1937,6 +1940,8 @@ impl Renderer {if previous.is_none() {startup_step(format_args!("creating the pipelines"));}+ let pipelines_step = crate::spans::startup::step("renderer.pipelines_textures");+ let terrain_step = crate::spans::startup::step("renderer.terrain_pipelines");let gpu_timer = timing::GpuTimer::new(&device, &queue);let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {label: Some("Horizon terrain"),@@ -2283,6 +2288,7 @@ impl Renderer {false,Some(alpha_blend),);+ drop(terrain_step);let globals = device.create_buffer(&wgpu::BufferDescriptor {label: Some("Horizon globals"),size: std::mem::size_of::<Globals>() as u64,@@ -2594,6 +2600,7 @@ impl Renderer {&glint_sampler,)?;hud.atlas_texture = Some(atlas.clone());+ let entity_step = crate::spans::startup::step("renderer.entity_pipelines");let entity_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {label: Some("Horizon entities"),source: wgpu::ShaderSource::Wgsl(entity_shader().into()),@@ -3165,6 +3172,7 @@ impl Renderer {multiview_mask: None,cache: None,});+ drop(entity_step);// WorldBorderRenderer: misc/forcefield.png as a MipmappedTexture// (repeat, linear minification with mipmaps, nearest magnification).let forcefield_size = forcefield.1;@@ -3542,6 +3550,7 @@ impl Renderer {);let panorama =panorama::Panorama::new(&device, config.format.remove_srgb_suffix(), panorama_faces);+ drop(pipelines_step);if previous.is_none() {startup_step(format_args!("pipelines ready (multi-draw {}, occlusion culling {}, GPU timer {})",diff --git a/crates/horizon-client-render/src/spans.rs b/crates/horizon-client-render/src/spans.rsindex c7663656b..7027b3fa1 100644--- a/crates/horizon-client-render/src/spans.rs+++ b/crates/horizon-client-render/src/spans.rs@@ -92,6 +92,161 @@ pub fn describe(spans: &[Span]) -> String {list.join(", ")}+/// The start's timeline: `HORIZON_STARTUP_TRACE=FILE` writes every named step of+/// the start (resource tables, the renderer, the title, a world's open, a+/// server's join, a dimension change) on whichever thread ran it, one Chrome+/// trace event per line (`tools/startup_trace.py` reads it; wrapped in `[...]`,+/// Perfetto opens it). Times are microseconds since the process started. Off,+/// a step costs one relaxed load.+pub mod startup {+ use std::{+ io::Write as _,+ sync::{+ Mutex, OnceLock,+ atomic::{AtomicBool, AtomicU32, Ordering},+ },+ time::Instant,+ };++ static ON: AtomicBool = AtomicBool::new(false);+ static SINK: OnceLock<Option<Mutex<std::fs::File>>> = OnceLock::new();+ static ORIGIN: OnceLock<Instant> = OnceLock::new();+ static NEXT_THREAD: AtomicU32 = AtomicU32::new(1);+ thread_local! {+ /// This thread's trace id, given (with its name) on its first event.+ static THREAD: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };+ }++ /// Opens the trace named by `HORIZON_STARTUP_TRACE`, timed from `origin`+ /// (the process's start). Call once, first thing.+ pub fn init(origin: Instant) {+ ORIGIN.get_or_init(|| origin);+ let sink = SINK.get_or_init(|| {+ let path = std::env::var_os("HORIZON_STARTUP_TRACE")?;+ std::fs::File::create(&path).ok().map(Mutex::new)+ });+ ON.store(sink.is_some(), Ordering::Relaxed);+ }++ /// Whether a trace is being written.+ #[inline]+ pub fn on() -> bool {+ ON.load(Ordering::Relaxed)+ }++ fn micros(at: Instant) -> u128 {+ ORIGIN.get().map_or(0, |origin| {+ at.saturating_duration_since(*origin).as_micros()+ })+ }++ fn write(event: std::fmt::Arguments) {+ let Some(Some(sink)) = SINK.get() else {+ return;+ };+ let tid = THREAD.with(|id| {+ if id.get() == 0 {+ id.set(NEXT_THREAD.fetch_add(1, Ordering::Relaxed));+ let name = std::thread::current()+ .name()+ .unwrap_or("unnamed")+ .replace(['"', '\\'], "_");+ let mut file = sink.lock().unwrap_or_else(std::sync::PoisonError::into_inner);+ let _ = writeln!(+ file,+ r#"{{"ph":"M","name":"thread_name","pid":1,"tid":{},"args":{{"name":"{name}"}}}}"#,+ id.get()+ );+ }+ id.get()+ });+ let mut file = sink+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ let _ = writeln!(file, r#"{{"pid":1,"tid":{tid},{event}}}"#);+ }++ /// A step running until it is dropped.+ #[must_use = "the step ends when this is dropped"]+ pub struct Step {+ name: &'static str,+ started: Instant,+ }+ impl Drop for Step {+ fn drop(&mut self) {+ let end = Instant::now();+ write(format_args!(+ r#""ph":"X","name":"{}","ts":{},"dur":{}"#,+ self.name,+ micros(self.started),+ end.saturating_duration_since(self.started).as_micros()+ ));+ }+ }++ /// Starts a step named `name` (a literal: no quotes or backslashes).+ #[inline]+ pub fn step(name: &'static str) -> Option<Step> {+ on().then(|| Step {+ name,+ started: Instant::now(),+ })+ }++ /// Runs `f` as a step named `name`.+ #[inline]+ pub fn time<T>(name: &'static str, f: impl FnOnce() -> T) -> T {+ let _step = step(name);+ f()+ }++ /// A moment of the start: the title drawn, the world in view.+ #[inline]+ pub fn mark(name: &'static str) {+ if on() {+ write(format_args!(+ r#""ph":"i","s":"g","name":"{name}","ts":{}"#,+ micros(Instant::now())+ ));+ }+ }++ /// The first time `name` happens, as a mark. `once` is the caller's flag.+ #[inline]+ pub fn mark_once(once: &AtomicBool, name: &'static str) {+ if on() && !once.swap(true, Ordering::Relaxed) {+ mark(name);+ }+ }++ /// Set while a world opens, a server is joined or the dimension changes,+ /// until the view first fills ([`view_filled`]).+ static FILLING: AtomicBool = AtomicBool::new(false);++ /// A world starts loading: the next full view is marked.+ pub fn arm(name: &'static str) {+ if on() {+ mark(name);+ FILLING.store(true, Ordering::Relaxed);+ }+ }++ /// Called each frame in a world: marks `world.view_full` the first frame+ /// after [`arm`] where nothing waits to load, generate, light, mesh or+ /// upload and the resident columns cover perf_bench's share of the view+ /// (0.8 pi r^2: corners are never loaded).+ #[inline]+ pub fn view_filled(resident: usize, pending: usize, radius: u32) {+ if FILLING.load(Ordering::Relaxed)+ && pending == 0+ && resident as f64 >= 0.8 * std::f64::consts::PI * f64::from(radius * radius)+ && FILLING.swap(false, Ordering::Relaxed)+ {+ mark("world.view_full");+ }+ }+}+#[cfg(test)]mod tests {use super::Span;diff --git a/crates/horizon-client-render/src/telemetry.rs b/crates/horizon-client-render/src/telemetry.rsindex afd2dbc94..3e7681b6a 100644--- a/crates/horizon-client-render/src/telemetry.rs+++ b/crates/horizon-client-render/src/telemetry.rs@@ -241,6 +241,7 @@ impl Session {pub fn title_shown(&mut self) {if !self.title_measured {self.title_measured = true;+ crate::spans::startup::mark("title.shown");let ui_ms = UI_READY_MS.load(Ordering::Relaxed);self.to_title_ms = if ui_ms != u32::MAX {Some(ui_ms)@@ -252,6 +253,9 @@ impl Session {/// A world starts loading.pub fn world_opening(&mut self) {+ if self.opening.is_none() {+ crate::spans::startup::arm("world.opening");+ }self.opening.get_or_insert_with(Instant::now);}@@ -262,10 +266,11 @@ impl Session {/// A frame drawn while playing (not in menus or loading).pub fn frame(&mut self, ms: f32) {- if let Some(opening) = self.opening.take()- && self.to_world_ms.is_none()- {- self.to_world_ms = Some(opening.elapsed().as_millis() as u32);+ if let Some(opening) = self.opening.take() {+ crate::spans::startup::mark("world.first_frame");+ if self.to_world_ms.is_none() {+ self.to_world_ms = Some(opening.elapsed().as_millis() as u32);+ }}self.frames.record(ms);self.played_ms += ms;diff --git a/crates/horizon-client/src/entry.rs b/crates/horizon-client/src/entry.rsindex 28bbdd897..dc4e03a1e 100644--- a/crates/horizon-client/src/entry.rs+++ b/crates/horizon-client/src/entry.rs@@ -5,6 +5,7 @@ use crate::{crash, game, logging, ram_limit};pub fn main() {horizon_threads::configure_allocator();horizon_client::telemetry::mark_start();+ horizon_client::spans::startup::init(std::time::Instant::now());let args: Vec<String> = std::env::args().skip(1).collect();let game_args = args.iter().take_while(|a| *a != "--dedicated");let help = game_argsdiff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rsindex d78b66334..3721e78f3 100644--- a/crates/horizon-client/src/game.rs+++ b/crates/horizon-client/src/game.rs@@ -328,6 +328,7 @@ mod world_loading;mod zombie_reinforcements;mod zombie_villagers;use event_loop::EventLoopCtl;+use horizon_client::spans::startup;use horizon_client::{Result,assets::{Assets, RenderAssets},@@ -16386,6 +16387,7 @@ impl ApplicationHandler for Game<'_> {attributes, "horizon", "horizon",);// `--offscreen` renders into a texture: its window would only show white.+ let window_step = startup::step("window.create");let window = match event_loop.create_window(attributes.with_inner_size(winit::dpi::LogicalSize::new(size[0], size[1]))@@ -16399,6 +16401,7 @@ impl ApplicationHandler for Game<'_> {return;}};+ drop(window_step);horizon_client::telemetry::note_display(&window);match self.new_renderer(window.clone()) {Ok(r) => {@@ -17845,6 +17848,15 @@ impl Game<'_> {self.perf.title_shown();} else if self.screen == Screen::Playing && self.player.is_some() {self.perf.frame(elapsed.as_secs_f32() * 1000.0);+ if startup::on() {+ startup::view_filled(+ self.world.chunks.len(),+ self.streaming.pending()+ + self.pending_uploads.len()+ + self.remote_backlog(),+ self.streaming.radius.max(0) as u32,+ );+ }self.world_frames += 1;}self.frames += 1;@@ -19059,7 +19071,7 @@ fn load_core(parallel: bool,progress: &Progress,) -> std::result::Result<std::result::Result<CoreTables, String>, String> {- let assets = match Assets::load(root) {+ let assets = match startup::time("core.assets", || Assets::load(root)) {Ok(assets) => assets,Err(e) => return Ok(Err(e.to_string())),};@@ -19071,18 +19083,22 @@ fn load_core(let render = Step::spawn(scope, parallel, move || {// A server reads no block sprites' pixels, which only the// renderer draws (32 MB).- let render = if headless {- assets.reload_for_server(root)- } else {- assets.reload(root)- }+ let render = startup::time("core.block_models_sprites", || {+ if headless {+ assets.reload_for_server(root)+ } else {+ assets.reload(root)+ }+ }).map_err(|e| e.to_string())?;let library = render.model_library.clone();- let item_renderer = if headless {- horizon_client::item_render::ItemRenderer::load_for_server(root, library)- } else {- horizon_client::item_render::ItemRenderer::load_with_library(root, library)- }+ let item_renderer = startup::time("core.item_models", || {+ if headless {+ horizon_client::item_render::ItemRenderer::load_for_server(root, library)+ } else {+ horizon_client::item_render::ItemRenderer::load_with_library(root, library)+ }+ }).map_err(|e| e.to_string());Ok((render, item_renderer))});@@ -19092,16 +19108,21 @@ fn load_core(if headless {return Ok(horizon_world::block_render::FaceVisibility::empty());}- models::load_visibility(root, registry).map_err(|e| e.to_string())+ startup::time("core.visibility", || {+ models::load_visibility(root, registry)+ })+ .map_err(|e| e.to_string())});let biomes = Step::spawn(scope, parallel, || {- Biomes::load(root).map_err(|e| e.to_string())+ startup::time("core.biomes", || Biomes::load(root)).map_err(|e| e.to_string())});let environment = Step::spawn(scope, parallel, || {- Environment::load(root).map_err(|e| e.to_string())+ startup::time("core.environment", || Environment::load(root))+ .map_err(|e| e.to_string())});let collision = Step::spawn(scope, parallel, || {- collisions::load(root, registry).map_err(|e| e.to_string())+ startup::time("core.collision", || collisions::load(root, registry))+ .map_err(|e| e.to_string())});let gameplay = gameplay_tables(root, assets, parallel, progress);(@@ -19139,6 +19160,7 @@ fn gameplay_tables(parallel: bool,progress: &Progress,) -> std::result::Result<GameplayTables, String> {+ let facts_step = startup::step("core.block_facts");let behaviour_json: serde_json::Value = serde_json::from_reader(std::io::BufReader::new(std::fs::File::open(root.join("block-behaviours.json")).map_err(|e| e.to_string())?,))@@ -19152,13 +19174,19 @@ fn gameplay_tables(.iter().any(|s| !s.name.starts_with("minecraft:")),);+ drop(facts_step);progress.set(2);let (light, motion) = std::thread::scope(|scope| {let light = Step::spawn(scope, parallel, || {- LightTables::load(root, assets.registry, &facts).map_err(|e| e.to_string())+ startup::time("core.light", || {+ LightTables::load(root, assets.registry, &facts)+ })+ .map_err(|e| e.to_string())});- let motion =- MotionResources::load_with_facts(root, assets, &facts).map_err(|e| e.to_string());+ let motion = startup::time("core.motion", || {+ MotionResources::load_with_facts(root, assets, &facts)+ })+ .map_err(|e| e.to_string());(light.join(), motion)});Ok(GameplayTables {@@ -19195,6 +19223,7 @@ fn load_resources(// A dedicated server builds only the font (UiResources::server), as the// integrated server's game does: the full atlas and panorama are 144 MB.let load_ui = |root: &std::path::Path| {+ let _step = startup::step("ui.load");if headless {UiResources::server(root)} else {@@ -19222,7 +19251,7 @@ fn load_resources(let (core, loaded_gui) = loop {let (ui, core) = std::thread::scope(|scope| {let core = Step::spawn(scope, parallel, || {- load_core(&root, headless, parallel, progress)+ startup::time("core", || load_core(&root, headless, parallel, progress))});let ui = load_ui(&root).map(|ui| offer(ui, &root));(ui, core.join())@@ -19271,7 +19300,10 @@ fn load_resources(if let Some(folder) = launch_world {world_loading::preload_launch_world(folder, &root, assets.registry, biomes.clone());}- let tables = Arc::new(MeshTables::new(&assets, &render, &facts, visibility)?);+ let tables = Arc::new(startup::time("pack.mesh_tables", || {+ MeshTables::new(&assets, &render, &facts, visibility)+ })?);+ let pack_step = startup::step("pack.assemble");let pack = PackResources::assemble(&root,assets.clone(),@@ -19281,6 +19313,7 @@ fn load_resources(biomes.clone(),Arc::new(light),);+ drop(pack_step);log::info!("load pack tables: {:.2}s", started.elapsed().as_secs_f32());progress.set(4);if !menu {@@ -19313,8 +19346,14 @@ fn load_resources(// Finish fallible CPU preparation before starting the tail, so an error// cannot detach a tail worker that races the next pack-loader generation.let prepared_at = Instant::now();- let game_cpu = menu.then(|| GameCpuResources::load(&root)).transpose()?;- let renderer_cpu = menu.then(|| prepare_renderer_textures(&pack));+ let game_cpu = menu+ .then(|| startup::time("handoff.game_cpu", || GameCpuResources::load(&root)))+ .transpose()?;+ let renderer_cpu = menu.then(|| {+ startup::time("handoff.renderer_textures", || {+ prepare_renderer_textures(&pack)+ })+ });if menu {log::info!("Horizon: handoff CPU resources prepared on worker in {} ms",@@ -19332,6 +19371,7 @@ fn load_resources(let handle = std::thread::Builder::new().name("world-rest-loader".into()).spawn(move || {+ let _step = startup::step("rest");let rest = load_world_rest(&root,&resources,@@ -19412,16 +19452,22 @@ fn load_world_rest(// Independent tables load together, within the worker budget the settings// allow. Only the recipes and the stations read another table: the items.let load_items = || -> Result<_> {- let items = Items::load(root, registry, facts, overlay)?;+ let items = startup::time("rest.items", || Items::load(root, registry, facts, overlay))?;log::info!("load items: {:.2}s", started.elapsed().as_secs_f32());progress.set(7);- let recipes = Recipes::load(root, &items, overlay)?;- let stations = horizon_client::stations::StationData::load(root, &items, overlay);+ let recipes = startup::time("rest.recipes", || Recipes::load(root, &items, overlay))?;+ let stations = startup::time("rest.stations", || {+ horizon_client::stations::StationData::load(root, &items, overlay)+ });Ok((items, recipes, stations))};- let load_behaviours =- || Behaviours::load_with_gameplay(registry, root, behaviour_json, gameplay_projection);+ let load_behaviours = || {+ startup::time("rest.behaviours", || {+ Behaviours::load_with_gameplay(registry, root, behaviour_json, gameplay_projection)+ })+ };let load_block_entity_render = || {+ let _step = startup::step("rest.block_entity_render");if !draws {return Ok(Default::default());}@@ -19430,6 +19476,7 @@ fn load_world_rest()};let load_animate = || -> Result<_> {+ let _step = startup::step("rest.animate_fog");let animate = if draws {horizon_client::animate_tick::AnimateTables::load(root, registry, biomes)?} else {@@ -19438,6 +19485,7 @@ fn load_world_rest(Ok((animate, horizon_client::fog::FogTables::load(root, biomes)))};let load_mobs = || -> Result<_> {+ let _step = startup::step("rest.mobs");let mob_types: HashMap<_, _> = MobTypes::load(root)?.types.into_iter()@@ -19451,6 +19499,7 @@ fn load_world_rest(Ok((mob_types, mob_data))};let load_transformers = || -> Result<_> {+ let _step = startup::step("rest.transformers_models");let transformers = horizon_client::block_transform::Transformers::load(root, registry)?;let models = (perspective::player_type(root),@@ -19506,6 +19555,7 @@ fn load_world_rest(threads);progress.set(8);+ let _tail = startup::step("rest.tail");Ok(WorldRest {stations,items,@@ -20012,6 +20062,7 @@ impl Loading {attributes, "horizon", "horizon",);// `--offscreen` renders into a texture: its window would only show white.+ let window_step = startup::step("window.create");let window = match event_loop.create_window(attributes.with_inner_size(winit::dpi::LogicalSize::new(size[0], size[1]))@@ -20025,6 +20076,7 @@ impl Loading {return;}};+ drop(window_step);horizon_client::telemetry::note_display(&window);// The renderer takes seconds to build and winit's window sits white// until the first frame presents, so paint the boot screen on it and@@ -20049,6 +20101,7 @@ impl Loading {stop});let offscreen = self.options.as_ref().is_some_and(|o| o.offscreen);+ let boot_step = startup::step("renderer.boot");match loading_renderer(window.clone(),offscreen,@@ -20068,6 +20121,7 @@ impl Loading {return;}}+ drop(boot_step);// The wgpu frames take the window over: stop the boot paint thread// before the first one presents.#[cfg(target_os = "windows")]@@ -20106,6 +20160,7 @@ impl Loading {renderer.release_surface();}let bundle = self.gui.as_ref().expect("the menu bundle was just set");+ let _step = startup::step("renderer.title_artwork");match loading_renderer(window,offscreen,@@ -20174,6 +20229,7 @@ impl Loading {}if !self.shown {self.shown = true;+ startup::mark("loading_screen.shown");log::info!("Horizon: loading screen shown after {} ms",self.started.elapsed().as_millis()@@ -20194,17 +20250,19 @@ impl Loading {renderer.release_surface();}let renderer_at = Instant::now();- let renderer = build_renderer(- window.clone(),- options.offscreen,- &loaded.pack,- &gui,- self.renderer.as_ref(),- loaded.renderer_cpu,- self.settings.horizon.api,- self.settings.horizon.translucent_batching,- shader_packs::picked_pack_draws_flat(&self.settings),- )?;+ let renderer = startup::time("renderer.world", || {+ build_renderer(+ window.clone(),+ options.offscreen,+ &loaded.pack,+ &gui,+ self.renderer.as_ref(),+ loaded.renderer_cpu,+ self.settings.horizon.api,+ self.settings.horizon.translucent_batching,+ shader_packs::picked_pack_draws_flat(&self.settings),+ )+ })?;log::info!("Horizon: world renderer ready after {} ms",self.started.elapsed().as_millis()@@ -20221,6 +20279,7 @@ impl Loading {// like the single copy an integrated server would share.let res: &'static Arc<Resources> = Box::leak(Box::new(loaded.resources));let game_at = Instant::now();+ let game_step = startup::step("handoff.start_game");let Some(mut game) = start_game(res,loaded.pack,@@ -20237,6 +20296,7 @@ impl Loading {else {return Ok(None);};+ drop(game_step);let game_ms = game_at.elapsed().as_millis();game.window = Some(window);game.renderer = Some(renderer);diff --git a/crates/horizon-client/src/game/integrated.rs b/crates/horizon-client/src/game/integrated.rsindex 1caec7e44..eaf35e632 100644--- a/crates/horizon-client/src/game/integrated.rs+++ b/crates/horizon-client/src/game/integrated.rs@@ -17,6 +17,7 @@ use super::{world_loading::{WorldLoader, WorldRequest},};use horizon_client::level_loading::{STATUS_RADIUS, Stage};+use horizon_client::spans::startup;use horizon_client::{Result,audio::Audio,@@ -685,9 +686,10 @@ impl Game<'_> {self.native_server_about_to_start(false);self.native_lifecycle_open_started();// `load_world`, with the world `serve` started preparing.- self.join_world_rest()?;+ startup::time("server.wait_rest", || self.join_world_rest())?;self.perf.world_opening();- self.finish_open_world(opening.world?.finish()?)?;+ let world = startup::time("server.world_prepare", || opening.world?.finish())?;+ startup::time("server.finish_open_world", || self.finish_open_world(world))?;if let Some(sync) = opening.sync {sync.adopt(&self.pack_files());}@@ -751,7 +753,9 @@ impl Game<'_> {guest_commands: false,force_game_mode: false,};- self.lan_host(settings, net, config, None)?;+ startup::time("server.lan_host", || {+ self.lan_host(settings, net, config, None)+ })?;let commands = self.allow_commands();let pipe = {let host = self@@ -781,7 +785,7 @@ impl Game<'_> {view.start(Stage::LoadInitialChunks, 0);view.finish(Stage::LoadInitialChunks);let _ = ready.send(Ok((pipe, self.level.name.clone())));- self.settle_spawn()+ startup::time("server.settle_spawn", || self.settle_spawn())}/// `MinecraftServer.stopServer`: the players are saved and sent away,@@ -1141,7 +1145,9 @@ fn serve(world,sync: preload_sync_data(&options.resources),};- let mut server = match server_game(res, pack, options, settings) {+ let mut server = match startup::time("server.game_new", || {+ server_game(res, pack, options, settings)+ }) {Ok(server) => server,Err(e) => {// The world's session lock is let go before the failure is told.diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rsindex 2023f3bc5..dbb00525b 100644--- a/crates/horizon-client/src/game/remote.rs+++ b/crates/horizon-client/src/game/remote.rs@@ -2972,6 +2972,9 @@ impl Game<'_> {&p.spawn.dimension,self.dim.id(),);+ if dimension_changed {+ horizon_client::spans::startup::arm("dimension.change");+ }self.enter_remote_dimension(&p.spawn, true, reason);if dimension_changed {// LevelExtractor.setLevel: a new level drops the highlights.diff --git a/tests/test_startup_trace.py b/tests/test_startup_trace.pynew file mode 100644index 000000000..bbae3e0c2--- /dev/null+++ b/tests/test_startup_trace.py@@ -0,0 +1,32 @@+import tempfile+import unittest+import pathlib+from tools import startup_trace+++class StartupTraceTests(unittest.TestCase):+ def test_steps_and_marks_from_the_games_lines(self):+ lines = [+ '{"ph":"M","name":"thread_name","pid":1,"tid":1,"args":{"name":"main"}}',+ '{"pid":1,"tid":1,"ph":"X","name":"renderer.boot","ts":20000,"dur":150000}',+ '{"ph":"M","name":"thread_name","pid":1,"tid":2,"args":{"name":"world-loader"}}',+ '{"pid":1,"tid":2,"ph":"X","name":"ui.load","ts":5000,"dur":400000}',+ '{"pid":1,"tid":1,"ph":"i","s":"g","name":"title.shown","ts":450000}',+ '{"pid":1,"tid":1,"ph":"i","s":"g","name":"title.shown","ts":900000}',+ '{"pid":1,"tid":1,"ph":"X","name":"torn',+ ]+ with tempfile.TemporaryDirectory() as d:+ path = pathlib.Path(d) / "trace.jsonl"+ path.write_text("\n".join(lines))+ steps, marks = startup_trace.summarize(startup_trace.read_trace(path))+ self.assertEqual(steps, [(5.0, 405.0, "ui.load", "world-loader"),+ (20.0, 170.0, "renderer.boot", "main")])+ # The first time a mark happened.+ self.assertEqual(marks, {"title.shown": 450.0})++ def test_a_missing_trace_has_no_events(self):+ self.assertEqual(startup_trace.read_trace("/nonexistent/trace.jsonl"), [])+++if __name__ == "__main__":+ unittest.main()diff --git a/tools/startup_trace.py b/tools/startup_trace.pynew file mode 100644index 000000000..076b137f7--- /dev/null+++ b/tools/startup_trace.py@@ -0,0 +1,179 @@+#!/usr/bin/env python3+"""The game's start, step by step: launches it the way a player does (no+control socket, so the title shows while the world tables load), with+`HORIZON_STARTUP_TRACE` set, and prints the timeline the game wrote: every+named step (resource tables, the renderer's device and pipelines, the world's+open on the server thread) on the thread that ran it, and the marks (title+drawn, first frame in a world, the view filled).++ python3 tools/startup_trace.py # to the title screen+ python3 tools/startup_trace.py --world T --seed 1234 # into a world, until the view is full+ python3 tools/startup_trace.py --server 127.0.0.1:25565 # a server's join+ python3 tools/startup_trace.py --runs 3 --warm # 3 later starts (caches built by a first one)++Every run gets fresh scratch folders (HORIZON_HOME, launcher and XDG dirs, the+working directory where saves go); `--warm` keeps one scratch for all runs and+starts once first, so the runs measured are later starts. `--keep FILE` writes+the trace wrapped as a JSON array: Perfetto (ui.perfetto.dev) opens it.+"""+import argparse+import json+import os+import pathlib+import shutil+import signal+import statistics+import subprocess+import sys+import tempfile+import time++ROOT = pathlib.Path(__file__).resolve().parent.parent+# What each `--until` waits for.+UNTIL = {"title": "title.shown", "world": "world.view_full", "frame": "world.first_frame"}+++def read_trace(path):+ """The trace's events: one JSON object per line, a torn last line skipped."""+ events = []+ try:+ text = pathlib.Path(path).read_text()+ except FileNotFoundError:+ return events+ for line in text.splitlines():+ try:+ events.append(json.loads(line))+ except json.JSONDecodeError:+ pass+ return events+++def summarize(events):+ """(steps, marks): steps as (start ms, end ms, name, thread) sorted by+ start, marks as {name: first ms}."""+ threads = {e["tid"]: e["args"]["name"] for e in events if e.get("ph") == "M"}+ steps, marks = [], {}+ for e in events:+ if e.get("ph") == "X":+ start = e["ts"] / 1000+ steps.append((start, start + e["dur"] / 1000, e["name"], threads.get(e["tid"], "?")))+ elif e.get("ph") == "i":+ marks.setdefault(e["name"], e["ts"] / 1000)+ steps.sort()+ return steps, marks+++def scratch_env(home):+ env = dict(os.environ)+ for d in ("home", "launcher", "data", "config", "trash"):+ (home / d).mkdir(parents=True, exist_ok=True)+ env.update(+ HORIZON_HOME=str(home / "home"),+ HORIZON_LAUNCHER_HOME=str(home / "launcher"),+ XDG_DATA_HOME=str(home / "data"),+ XDG_CONFIG_HOME=str(home / "config"),+ HORIZON_LAUNCHER_TRASH=str(home / "trash"),+ HORIZON_LAUNCHER_NO_SELF_UPDATE="1",+ HORIZON_NO_AUDIO="1",+ )+ return env+++def run_once(args, home, label):+ trace = home / f"trace-{label}.jsonl"+ env = scratch_env(home)+ env["HORIZON_STARTUP_TRACE"] = str(trace)+ cmd = [args.bin, "--resources", args.resources]+ if args.world:+ cmd += ["--world", args.world, "--seed", str(args.seed), "--creative",+ "--render-distance", str(args.render_distance)]+ elif args.server:+ cmd += ["--server", args.server, "--render-distance", str(args.render_distance)]+ cmd += args.extra+ want = UNTIL[args.until]+ log = open(home / f"game-{label}.log", "w")+ proc = subprocess.Popen(cmd, cwd=home, env=env, stdout=log, stderr=subprocess.STDOUT)+ deadline = time.monotonic() + args.timeout+ marks = {}+ while time.monotonic() < deadline and proc.poll() is None:+ _, marks = summarize(read_trace(trace))+ if want in marks:+ time.sleep(args.linger)+ break+ time.sleep(0.05)+ if proc.poll() is None:+ proc.send_signal(signal.SIGTERM)+ try:+ proc.wait(15)+ except subprocess.TimeoutExpired:+ proc.kill()+ proc.wait()+ log.close()+ steps, marks = summarize(read_trace(trace))+ if want not in marks:+ print(f"{label}: no {want} within {args.timeout} s (log: {home / f'game-{label}.log'})",+ file=sys.stderr)+ return steps, marks, trace+++def print_timeline(steps, marks, floor_ms):+ rows = [(s, e, f"{name} [{thread}]") for s, e, name, thread in steps if e - s >= floor_ms]+ rows += [(t, None, f"* {name}") for name, t in marks.items()]+ rows.sort(key=lambda r: r[0])+ for start, end, what in rows:+ if end is None:+ print(f"{start:8.0f} {what}")+ else:+ print(f"{start:8.0f} {end - start:8.0f} ms {what}")+++def main():+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)+ ap.add_argument("--bin", default=str(ROOT / "target/release/horizon-client"))+ ap.add_argument("--resources", default=str(ROOT / ".local/horizon-client/26.3"))+ ap.add_argument("--world", help="open (or create) this world from the command line")+ ap.add_argument("--seed", type=int, default=1234)+ ap.add_argument("--server", help="join this server from the command line")+ ap.add_argument("--render-distance", type=int, default=12)+ ap.add_argument("--until", choices=sorted(UNTIL), help="default: world with --world/--server, else title")+ ap.add_argument("--runs", type=int, default=1)+ ap.add_argument("--warm", action="store_true", help="start once first in the same scratch: later starts")+ ap.add_argument("--timeout", type=float, default=180)+ ap.add_argument("--linger", type=float, default=0.3, help="seconds to keep running after the mark")+ ap.add_argument("--floor", type=float, default=5, help="hide steps shorter than this (ms)")+ ap.add_argument("--keep", help="write the last run's trace here as a JSON array (Perfetto)")+ ap.add_argument("--json", action="store_true", help="print the marks of every run as JSON")+ ap.add_argument("extra", nargs="*", help="more game arguments (after --)")+ args = ap.parse_args()+ args.until = args.until or ("world" if args.world or args.server else "title")+ # The game runs in its scratch folder.+ args.bin, args.resources = (str(pathlib.Path(p).resolve()) for p in (args.bin, args.resources))++ shared = pathlib.Path(tempfile.mkdtemp(prefix="horizon-startup-")) if args.warm else None+ if shared:+ run_once(args, shared, "warmup")+ results = []+ for i in range(args.runs):+ home = shared or pathlib.Path(tempfile.mkdtemp(prefix="horizon-startup-"))+ steps, marks, trace = run_once(args, home, f"run{i + 1}")+ results.append(marks)+ print(f"== run {i + 1} ({'later start' if shared else 'first start'}, {home})")+ print_timeline(steps, marks, args.floor)+ if args.keep:+ pathlib.Path(args.keep).write_text(json.dumps(read_trace(trace)))+ if not shared:+ shutil.rmtree(home, ignore_errors=True)+ if shared:+ shutil.rmtree(shared, ignore_errors=True)+ names = sorted({n for m in results for n in m}, key=lambda n: statistics.median(+ [m[n] for m in results if n in m]))+ print("== medians (ms since start)")+ for name in names:+ values = [m[name] for m in results if name in m]+ print(f"{statistics.median(values):8.0f} {name} (n={len(values)})")+ if args.json:+ print(json.dumps(results))+++if __name__ == "__main__":+ main()