diff --git a/src/bots/soak_player.gd b/src/bots/soak_player.gd index bea6c0f..e0eae35 100644 --- a/src/bots/soak_player.gd +++ b/src/bots/soak_player.gd @@ -18,7 +18,11 @@ var number: int = 0 ## Build attempts made, for the report. var build_attempts: int = 0 -var _world: Simulation = null +## The seed of the island `_top` was found on. Only the seed, never the world itself: +## holding the last match's Simulation kept a whole island alive per client until it +## next played, which the soak's memory check counted as a leak (#39). +var _seed: int = 0 +var _has_top: bool = false var _top: Vector2 = Vector2.ZERO @@ -28,8 +32,9 @@ func _init(client_number: int) -> void: ## The command for `tick`, for the player `me` on the client's `world`. func command(tick: int, me: SimEntity, world: Simulation) -> PlayerCommand: - if world != _world: - _world = world + if not _has_top or world.rng.match_seed != _seed: + _seed = world.rng.match_seed + _has_top = true _top = _highest_point(world.ground) var seconds: float = float(tick + number * 37) / TICKS var to_top: Vector2 = _top - Vector2(me.position.x, me.position.z) diff --git a/src/sim/live_count.gd b/src/sim/live_count.gd new file mode 100644 index 0000000..030e27e --- /dev/null +++ b/src/sim/live_count.gd @@ -0,0 +1,22 @@ +class_name LiveCount +extends RefCounted +## Counts live instances of a few heavy simulation classes, so leak checks (the soak) +## can tell how many islands a process still holds. Thread-safe: islands are made on +## worker threads. + +static var _lock: Mutex = Mutex.new() +static var _counts: Dictionary = {} + + +static func add(kind: StringName, delta: int) -> void: + _lock.lock() + _counts[kind] = int(_counts.get(kind, 0)) + delta + _lock.unlock() + + +## How many of `kind` are alive now. +static func of(kind: StringName) -> int: + _lock.lock() + var count: int = int(_counts.get(kind, 0)) + _lock.unlock() + return count diff --git a/src/sim/live_count.gd.uid b/src/sim/live_count.gd.uid new file mode 100644 index 0000000..7564495 --- /dev/null +++ b/src/sim/live_count.gd.uid @@ -0,0 +1 @@ +uid://cjyep4luipboh diff --git a/src/sim/sim_ground.gd b/src/sim/sim_ground.gd index 1439687..f45beeb 100644 --- a/src/sim/sim_ground.gd +++ b/src/sim/sim_ground.gd @@ -33,6 +33,15 @@ var solids: Array[AABB] = [] var _index: Dictionary = {} + +func _init() -> void: + LiveCount.add(&"SimGround", 1) + + +func _notification(what: int) -> void: + if what == NOTIFICATION_PREDELETE: + LiveCount.add(&"SimGround", -1) + ## A flat world at `height`. static func flat(height: float = 0.0) -> SimGround: var ground: SimGround = SimGround.new() diff --git a/src/sim/simulation.gd b/src/sim/simulation.gd index d3a0973..21ddf30 100644 --- a/src/sim/simulation.gd +++ b/src/sim/simulation.gd @@ -67,6 +67,7 @@ var _players: Array[int] = [] ## Generates the island from the seed, unless `world` is given. Tests that don't care ## about terrain pass `SimGround.flat()` to skip worldgen. func _init(match_seed: int, world: SimGround = null) -> void: + LiveCount.add(&"Simulation", 1) rng = SimRng.new(match_seed) if world == null: island = Island.generate(match_seed) @@ -402,3 +403,8 @@ func state_hash() -> String: hasher.start(HashingContext.HASH_SHA256) hasher.update(buffer.data_array) return hasher.finish().hex_encode() + + +func _notification(what: int) -> void: + if what == NOTIFICATION_PREDELETE: + LiveCount.add(&"Simulation", -1) diff --git a/test/bots/test_soak_player.gd b/test/bots/test_soak_player.gd new file mode 100644 index 0000000..93c4a7f --- /dev/null +++ b/test/bots/test_soak_player.gd @@ -0,0 +1,34 @@ +extends TestCase + +## N8 (#39): the soak's scripted client must not keep a finished match's island alive, +## and the live counts the soak's leak check reads must follow islands as they come and go. + + +func test_soak_player_lets_go_of_the_last_island() -> void: + var before: int = LiveCount.of(&"Simulation") + var player: SoakPlayer = SoakPlayer.new(3) + var world: Simulation = Simulation.new(4, SimGround.flat(2.0)) + assert_eq(LiveCount.of(&"Simulation"), before + 1) + var me: SimEntity = world.entities.find(world.add_player()) + assert_not_null(player.command(10, me, world)) + me = null + world = null + assert_eq(LiveCount.of(&"Simulation"), before, "the player held on to the island") + + +func test_soak_player_finds_the_top_again_on_a_new_island() -> void: + var player: SoakPlayer = SoakPlayer.new(0) + var low: Simulation = Simulation.new(4, SimGround.flat(2.0)) + player.command(1, low.entities.find(low.add_player()), low) + var first: Vector2 = player.get("_top") + var other: Simulation = Simulation.new(5) + player.command(2, other.entities.find(other.add_player()), other) + assert_ne(player.get("_top"), first, "a new seed, a new high point") + + +func test_grounds_are_counted_while_alive() -> void: + var before: int = LiveCount.of(&"SimGround") + var held: Array[SimGround] = [SimGround.flat(1.0)] + assert_eq(LiveCount.of(&"SimGround"), before + 1) + held.clear() + assert_eq(LiveCount.of(&"SimGround"), before) diff --git a/tools/soak_clients.gd b/tools/soak_clients.gd index c36f945..ae471c5 100644 --- a/tools/soak_clients.gd +++ b/tools/soak_clients.gd @@ -116,11 +116,14 @@ func _process(delta: float) -> bool: return false -## How many distinct worlds this process's clients still reach: their own, and the -## ground their prediction runs on. +## How many distinct worlds this process's clients still reach (their own, the ground +## their prediction runs on, and islands made in the background but not yet taken +## up), and how many Simulations and grounds are alive in all. `extra_islands` counts +## live Simulations nothing here accounts for: a leak (#39). func _live_worlds() -> Dictionary: var worlds: Dictionary = {} var grounds: Dictionary = {} + var built: int = 0 for client: Dictionary in _clients: var game: ClientGame = client["game"] if game.world != null: @@ -128,7 +131,14 @@ func _live_worlds() -> Dictionary: var ground: SimGround = game.prediction.get("_ground") if ground != null: grounds[ground.get_instance_id()] = true - return {"worlds": worlds.size(), "prediction_grounds": grounds.size()} + var made: Simulation = game.get("_built") + if made != null and made != game.world: + built += 1 + if game.building_world(): + built += 1 + var live: int = LiveCount.of(&"Simulation") + return {"worlds": worlds.size(), "prediction_grounds": grounds.size(), "built": built, + "live_simulations": live, "live_grounds": LiveCount.of(&"SimGround"), "extra_islands": maxi(0, live - worlds.size() - built)} ## Keeps track of a client's phases, and reports each match it played once it's over. diff --git a/tools/soak_report.gd b/tools/soak_report.gd index 17d3cc6..302c982 100644 --- a/tools/soak_report.gd +++ b/tools/soak_report.gd @@ -165,6 +165,14 @@ func _client_growth(lines: Array[Dictionary]) -> int: var worst: int = 0 for process: int in by_process: worst = maxi(worst, _judge(by_process[process], "client process %d" % process)) + # A minute into each match, every island alive must be a client's world or one being + # made. Anything more is an island nothing should still hold (#39). + for record: Dictionary in lines: + if String(record.get("event", "")) != "memory_late": + continue + var extra: int = int((record.get("worlds", {}) as Dictionary).get("extra_islands", 0)) + if extra > 0: + _fail("client process %d still held %d islands beyond its clients' worlds in match %d" % [int(record.get("_process", 0)), extra, int(record.get("match", -1))]) return worst