Skip to content

Commit

Permalink
Metrics. Faster sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrw committed Nov 13, 2023
1 parent c54a95c commit cfb5089
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
8 changes: 4 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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"
},
Expand All @@ -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"
},
Expand All @@ -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"
},
Expand Down
5 changes: 3 additions & 2 deletions enu.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ 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",
"chroma",
"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}"
Expand Down
1 change: 1 addition & 0 deletions src/config.nims
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
--threads:on
--mm:orc
--tls_emulation:off
--deepcopy:on

if host_os == "windows":
--pass_l:"-static"
Expand Down
15 changes: 8 additions & 7 deletions src/controllers/script_controllers/worker.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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)
Expand Down
26 changes: 22 additions & 4 deletions src/game.nim
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 2 additions & 0 deletions src/user_config.example.nims
Original file line number Diff line number Diff line change
@@ -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"

Expand Down

0 comments on commit cfb5089

Please sign in to comment.