From cfb5089f475f1a720bf34b1ab12215aabc2f6a33 Mon Sep 17 00:00:00 2001 From: Scott Wadden Date: Mon, 13 Nov 2023 15:50:52 -0400 Subject: [PATCH] Metrics. Faster sync. --- .vscode/launch.json | 8 +++--- enu.nimble | 5 ++-- src/config.nims | 1 + src/controllers/script_controllers/worker.nim | 15 ++++++----- src/game.nim | 26 ++++++++++++++++--- src/user_config.example.nims | 2 ++ 6 files changed, 40 insertions(+), 17 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 94e415d3..039d6a4c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "name": "Client: Build and Debug", "program": "${workspaceFolder}/vendor/godot/bin/godot.osx.opt.tools.arm64", - "env": {"ENU_CONNECT_ADDRESS": "127.0.0.1"}, + "env": {"ENU_CONNECT_ADDRESS": "127.0.0.1", "ENU_METRICS_PORT": "8001"}, "windows": { "program": "${workspaceFolder}/vendor/godot/bin/godot.windows.opt.tools.64" }, @@ -28,7 +28,7 @@ "request": "launch", "name": "Host: Build and Debug", "program": "${workspaceFolder}/vendor/godot/bin/godot.osx.opt.tools.arm64", - "env": {"ENU_LISTEN_ADDRESS": "0.0.0.0"}, + "env": {"ENU_LISTEN_ADDRESS": "0.0.0.0", "ENU_METRICS_PORT": "8000"}, "windows": { "program": "${workspaceFolder}/vendor/godot/bin/godot.windows.opt.tools.64" }, @@ -46,7 +46,7 @@ "request": "launch", "name": "Client: Debug", "program": "${workspaceFolder}/vendor/godot/bin/godot.osx.opt.tools.arm64", - "env": {"ENU_CONNECT_ADDRESS": "127.0.0.1"}, + "env": {"ENU_CONNECT_ADDRESS": "127.0.0.1", "ENU_METRICS_PORT": "8001"}, "windows": { "program": "${workspaceFolder}/vendor/godot/bin/godot.windows.opt.tools.64" }, @@ -62,7 +62,7 @@ "request": "launch", "name": "Host: Debug", "program": "${workspaceFolder}/vendor/godot/bin/godot.osx.opt.tools.arm64", - "env": {"ENU_LISTEN_ADDRESS": "0.0.0.0"}, + "env": {"ENU_LISTEN_ADDRESS": "0.0.0.0", "ENU_METRICS_PORT": "8000"}, "windows": { "program": "${workspaceFolder}/vendor/godot/bin/godot.windows.opt.tools.64" }, diff --git a/enu.nimble b/enu.nimble index db8492d8..58727ae3 100644 --- a/enu.nimble +++ b/enu.nimble @@ -31,7 +31,7 @@ else: requires "nim >= 1.6.10", "https://github.com/arnetheduck/nim-results#f3c666a", "https://github.com/dsrw/godot-nim#892c482", - "https://github.com/dsrw/model_citizen 0.18.13", + "https://github.com/dsrw/model_citizen 0.18.15", "https://github.com/dsrw/nanoid.nim 0.2.1", "cligen 1.6.0", "https://github.com/treeform/pretty", @@ -39,7 +39,8 @@ requires "nim >= 1.6.10", "markdown", "chronicles", "dotenv", - "nimibook" + "nimibook", + "metrics" proc godot_bin(target = target): string = result = this_dir() & &"/vendor/godot/bin/godot.{target}.opt.tools.{cpu}{exe_ext}" diff --git a/src/config.nims b/src/config.nims index 2bc52500..fc4b40b4 100644 --- a/src/config.nims +++ b/src/config.nims @@ -1,6 +1,7 @@ --threads:on --mm:orc --tls_emulation:off +--deepcopy:on if host_os == "windows": --pass_l:"-static" diff --git a/src/controllers/script_controllers/worker.nim b/src/controllers/script_controllers/worker.nim index d179429f..07b1fb5b 100644 --- a/src/controllers/script_controllers/worker.nim +++ b/src/controllers/script_controllers/worker.nim @@ -153,8 +153,8 @@ proc worker_thread(params: (ZenContext, GameState)) {.gcsafe.} = var listen_address = main_thread_state.config.listen_address let worker_ctx = ZenContext.init(id = \"work-{generate_id()}", - chan_size = 500, buffer = true, - listen_address = listen_address) + chan_size = 500, buffer = false, listen_address = listen_address, + label = "worker") Zen.thread_ctx = worker_ctx ctx.subscribe(Zen.thread_ctx) @@ -266,7 +266,7 @@ proc worker_thread(params: (ZenContext, GameState)) {.gcsafe.} = let timeout = frame_start + max_time let wait_until = frame_start + min_time - Zen.thread_ctx.boop(max_duration = (1.0 / 30).seconds) + Zen.thread_ctx.boop inc state.frame_count for ctx_name in Zen.thread_ctx.unsubscribed: var i = 0 @@ -283,15 +283,15 @@ proc worker_thread(params: (ZenContext, GameState)) {.gcsafe.} = var batched: HashSet[Unit] - while to_process.len > 0 and get_mono_time() < timeout and - Zen.thread_ctx.pressure < 0.5: + while Zen.thread_ctx.pressure < 0.9 and to_process.len > 0 and + get_mono_time() < timeout: let units = to_process to_process = @[] for unit in units: if Ready in unit.global_flags: if unit.batch_changes: - batched.incl unit + batched.incl unit if worker.advance_unit(unit, timeout): to_process.add(unit) @@ -308,7 +308,8 @@ proc worker_thread(params: (ZenContext, GameState)) {.gcsafe.} = Zen.thread_ctx.boop -proc launch_worker*(ctx: ZenContext, state: GameState): Thread[tuple[ctx: ZenContext, state: GameState]] = +proc launch_worker*(ctx: ZenContext, state: GameState): Thread[tuple[ + ctx: ZenContext, state: GameState]] = worker_lock.acquire result.create_thread(worker_thread, (ctx, state)) work_done.wait(worker_lock) diff --git a/src/game.nim b/src/game.nim index 9853c97a..6a46e8b7 100644 --- a/src/game.nim +++ b/src/game.nim @@ -1,5 +1,5 @@ -import std / [monotimes, os, jsonutils, json, math, locks, random] -import pkg / [godot] +import std / [monotimes, os, jsonutils, json, math, locks, random, net] +import pkg / [godot, metrics, metrics / stdlib_httpserver] from dotenv import nil import godotapi / [input, input_event, gd_os, node, scene_tree, packed_scene, sprite, control, viewport, viewport_texture, performance, label, theme, @@ -11,6 +11,11 @@ import core, types, globals, controllers, models / [serializers, units, colors] if file_exists(".env"): dotenv.overload() +when defined(metrics): + set_system_metrics_automatic_update(false) + +ZenContext.init_metrics "main", "worker" + gdobj Game of Node: var reticle: Control @@ -21,13 +26,18 @@ gdobj Game of Node: last_tool = BlueBlock saved_mouse_position: Vector2 rescale_at = get_mono_time() + update_metrics_at = get_mono_time() node_controller: NodeController script_controller: ScriptController method process*(delta: float) = - Zen.thread_ctx.boop(max_duration = (1.0 / 30.0).seconds) + Zen.thread_ctx.boop inc state.frame_count let time = get_mono_time() + when defined(metrics): + if self.update_metrics_at < time: + update_thread_metrics() + self.update_metrics_at = time + 10.seconds if state.config.show_stats: let fps = get_monitor(TIME_FPS) @@ -90,7 +100,7 @@ world: {state.world_name} var initial_user_config = load_user_config(get_user_data_dir()) Zen.thread_ctx = ZenContext.init(id = \"main-{generate_id()}", - chan_size = 100, buffer = false) + chan_size = 2000, buffer = true, label = "main") state = GameState.init state.nodes.game = self @@ -140,6 +150,14 @@ world: {state.world_name} state.set_flag(God, uc.god_mode ||= false) set_window_fullscreen state.config.start_full_screen + when defined(metrics): + let metrics_port = if ?get_env("ENU_METRICS_PORT"): + get_env("ENU_METRICS_PORT").parse_int + else: + 8000 + + {.cast(gcsafe).}: + start_metrics_http_server("0.0.0.0", Port(metrics_port)) self.add_platform_input_actions() diff --git a/src/user_config.example.nims b/src/user_config.example.nims index 6ca30f56..2c2b6c70 100644 --- a/src/user_config.example.nims +++ b/src/user_config.example.nims @@ -1,5 +1,7 @@ # --define:"chronicles_log_level=DEBUG" # --define:"chronicles_disabled_topics=networking,scripting,publishing" +# --define:"metrics" + # --define:"zen_trace" # --define:"dump_zen_objects"