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.
|
||||
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)
|
||||
|
||||
@@ -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 = {}
|
||||
|
||||
|
||||
|
||||
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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user