N8: Stop the soak player holding old islands, and count live islands
The soak's client memory growth (17 to 20 MB, about five islands, in some processes) came from SoakPlayer keeping a strong reference to the last world it played on. It let go only when its client next asked it for a command on the new world, so finished islands stayed alive across a match change. It now keeps only the island's seed. LiveCount counts live Simulations and SimGrounds, which are made on worker threads too, so it is thread-safe. The soak clients log the counts, and the soak report fails if, a minute into a match, any island is alive that isn't a client's world or one being made. That makes a leak name itself instead of showing up as bytes. Refs #39 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,7 +18,11 @@ var number: int = 0
|
|||||||
## Build attempts made, for the report.
|
## Build attempts made, for the report.
|
||||||
var build_attempts: int = 0
|
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
|
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`.
|
## The command for `tick`, for the player `me` on the client's `world`.
|
||||||
func command(tick: int, me: SimEntity, world: Simulation) -> PlayerCommand:
|
func command(tick: int, me: SimEntity, world: Simulation) -> PlayerCommand:
|
||||||
if world != _world:
|
if not _has_top or world.rng.match_seed != _seed:
|
||||||
_world = world
|
_seed = world.rng.match_seed
|
||||||
|
_has_top = true
|
||||||
_top = _highest_point(world.ground)
|
_top = _highest_point(world.ground)
|
||||||
var seconds: float = float(tick + number * 37) / TICKS
|
var seconds: float = float(tick + number * 37) / TICKS
|
||||||
var to_top: Vector2 = _top - Vector2(me.position.x, me.position.z)
|
var to_top: Vector2 = _top - Vector2(me.position.x, me.position.z)
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://cjyep4luipboh
|
||||||
@@ -33,6 +33,15 @@ var solids: Array[AABB] = []
|
|||||||
var _index: Dictionary = {}
|
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`.
|
## A flat world at `height`.
|
||||||
static func flat(height: float = 0.0) -> SimGround:
|
static func flat(height: float = 0.0) -> SimGround:
|
||||||
var ground: SimGround = SimGround.new()
|
var ground: SimGround = SimGround.new()
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ var _players: Array[int] = []
|
|||||||
## Generates the island from the seed, unless `world` is given. Tests that don't care
|
## Generates the island from the seed, unless `world` is given. Tests that don't care
|
||||||
## about terrain pass `SimGround.flat()` to skip worldgen.
|
## about terrain pass `SimGround.flat()` to skip worldgen.
|
||||||
func _init(match_seed: int, world: SimGround = null) -> void:
|
func _init(match_seed: int, world: SimGround = null) -> void:
|
||||||
|
LiveCount.add(&"Simulation", 1)
|
||||||
rng = SimRng.new(match_seed)
|
rng = SimRng.new(match_seed)
|
||||||
if world == null:
|
if world == null:
|
||||||
island = Island.generate(match_seed)
|
island = Island.generate(match_seed)
|
||||||
@@ -402,3 +403,8 @@ func state_hash() -> String:
|
|||||||
hasher.start(HashingContext.HASH_SHA256)
|
hasher.start(HashingContext.HASH_SHA256)
|
||||||
hasher.update(buffer.data_array)
|
hasher.update(buffer.data_array)
|
||||||
return hasher.finish().hex_encode()
|
return hasher.finish().hex_encode()
|
||||||
|
|
||||||
|
|
||||||
|
func _notification(what: int) -> void:
|
||||||
|
if what == NOTIFICATION_PREDELETE:
|
||||||
|
LiveCount.add(&"Simulation", -1)
|
||||||
|
|||||||
@@ -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)
|
||||||
+13
-3
@@ -116,11 +116,14 @@ func _process(delta: float) -> bool:
|
|||||||
return false
|
return false
|
||||||
|
|
||||||
|
|
||||||
## How many distinct worlds this process's clients still reach: their own, and the
|
## How many distinct worlds this process's clients still reach (their own, the ground
|
||||||
## ground their prediction runs on.
|
## 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:
|
func _live_worlds() -> Dictionary:
|
||||||
var worlds: Dictionary = {}
|
var worlds: Dictionary = {}
|
||||||
var grounds: Dictionary = {}
|
var grounds: Dictionary = {}
|
||||||
|
var built: int = 0
|
||||||
for client: Dictionary in _clients:
|
for client: Dictionary in _clients:
|
||||||
var game: ClientGame = client["game"]
|
var game: ClientGame = client["game"]
|
||||||
if game.world != null:
|
if game.world != null:
|
||||||
@@ -128,7 +131,14 @@ func _live_worlds() -> Dictionary:
|
|||||||
var ground: SimGround = game.prediction.get("_ground")
|
var ground: SimGround = game.prediction.get("_ground")
|
||||||
if ground != null:
|
if ground != null:
|
||||||
grounds[ground.get_instance_id()] = true
|
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.
|
## Keeps track of a client's phases, and reports each match it played once it's over.
|
||||||
|
|||||||
@@ -165,6 +165,14 @@ func _client_growth(lines: Array[Dictionary]) -> int:
|
|||||||
var worst: int = 0
|
var worst: int = 0
|
||||||
for process: int in by_process:
|
for process: int in by_process:
|
||||||
worst = maxi(worst, _judge(by_process[process], "client process %d" % 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
|
return worst
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user