gdharness 1.1.59 → 1.1.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,6 +34,14 @@ func set_editor_plugin(plugin: EditorPlugin) -> void:
34
34
 
35
35
 
36
36
  func _process(_delta: float) -> void:
37
+ _count_a_play_begun()
38
+
39
+
40
+ ## Counts a play the editor has begun since the last look, whoever began it. Looked at every frame
41
+ ## and before the count is read: read alone, a play begun in this frame went uncounted until this
42
+ ## node's turn came round, and a server asking before then was told the earlier play's number with
43
+ ## "playing", which it took for the earlier play still going.
44
+ func _count_a_play_begun() -> void:
37
45
  var playing: bool = EditorInterface.is_playing_scene()
38
46
  if playing and not _was_playing:
39
47
  if _awaited:
@@ -231,6 +239,7 @@ static func _leave() -> void:
231
239
 
232
240
 
233
241
  func playing_status(_args: Dictionary) -> Dictionary:
242
+ _count_a_play_begun()
234
243
  var playing: bool = EditorInterface.is_playing_scene()
235
244
  # The debugger's port with it, read now rather than remembered: it is taken again before every
236
245
  # play, so the one from when this editor greeted the server is a number it has moved off.
@@ -17,6 +17,11 @@ const Read = preload("../reading.gd")
17
17
  ## the server keeps this below the queue the project sets; this is that for the engine's default.
18
18
  const FRAME_FUNCTIONS: int = 1536
19
19
  const DEBUGGER_CLASS: String = "ScriptEditorDebugger"
20
+ ## What a quitting game sends after its profile's totals, and the answer it waits for. Kept in step
21
+ ## with the runtime addon's runtime_autoload.gd.
22
+ const PROFILE_CAPTURE: String = "gdharness"
23
+ const PROFILE_SENT: String = "gdharness:profile_sent"
24
+ const PROFILE_RECEIVED: String = "gdharness:profile_received"
20
25
 
21
26
 
22
27
  ## The editor's debugger sessions, numbered as the editor numbers its debugger tabs.
@@ -32,6 +37,17 @@ class Sessions:
32
37
  if joined != OK:
33
38
  push_error("gdharness could not watch debugger session %d start: %d" % [session_id, joined])
34
39
 
40
+ func _has_capture(capture: String) -> bool:
41
+ return capture == PROFILE_CAPTURE
42
+
43
+ ## Answers a quitting game that its profile's totals arrived, which it waits for before it exits.
44
+ ## The editor reads a session's messages in order, so the totals sent before this have been read.
45
+ func _capture(message: String, _data: Array, session_id: int) -> bool:
46
+ if message != PROFILE_SENT:
47
+ return false
48
+ get_session(session_id).send_message(PROFILE_RECEIVED, [])
49
+ return true
50
+
35
51
 
36
52
  var _editor_plugin: EditorPlugin = null
37
53
  var _sessions: Sessions = null
@@ -27,9 +27,21 @@ const BIND_ADDRESS_SETTING: String = "gdharness/runtime/bind_address"
27
27
  ## expects. A fixed port is for a client that cannot read the announcement.
28
28
  const PORT_SETTING: String = "gdharness/runtime/port"
29
29
 
30
- ## How long a profiled game waits as it leaves for its profile to be sent: the debugger's thread
31
- ## sends every 6.9 ms, so this is many of its turns.
32
- const PROFILE_SEND_MS: int = 100
30
+ ## The debugger messages that confirm a profile's totals arrived: the game sends the first after
31
+ ## them, and the debugger answers with the second once it has read that far. Kept in step with
32
+ ## `PROFILE_SENT` and `PROFILE_RECEIVED` in src/script-profile.ts and the editor addon's
33
+ ## profile_tools.gd.
34
+ const PROFILE_CAPTURE: String = "gdharness"
35
+ const PROFILE_SENT: String = "gdharness:profile_sent"
36
+ const PROFILE_RECEIVED: String = "gdharness:profile_received"
37
+
38
+ ## The longest a quitting game waits for that answer. A debugger that never gives one, such as an
39
+ ## editor without the gdharness addon, holds every profiled exit this long.
40
+ const PROFILE_CONFIRM_MS: int = 3000
41
+
42
+ ## How many calls of `EngineDebugger.line_poll` read the debugger's messages once: it reads them
43
+ ## on every 2048th call and only counts the others.
44
+ const POLLS_PER_READ: int = 2048
33
45
 
34
46
  ## How the engine is told to run a script instead of the game: `godot -s thing.gd`.
35
47
  const SCRIPT_FLAGS: PackedStringArray = ["-s", "--script"]
@@ -90,6 +102,7 @@ var _commands: Dictionary = {}
90
102
  ## Empty when the markers cannot be read, as in a copy made by hand.
91
103
  var _loaded_version: String = _read_marker(VERSION_MARKER)
92
104
  var _loaded_digest: String = _read_marker(DIGEST_MARKER)
105
+ var _profile_received: bool = false
93
106
 
94
107
 
95
108
  func _init() -> void:
@@ -125,12 +138,14 @@ func _ready() -> void:
125
138
  # even be un-paused over the socket. A debug server has to stay responsive while the game
126
139
  # is frozen, to inspect, capture, inject or resume it.
127
140
  process_mode = Node.PROCESS_MODE_ALWAYS
141
+ var watched: int = get_tree().root.tree_exiting.connect(_send_the_profile)
142
+ if watched != OK:
143
+ push_error("gdharness could not watch for the game quitting: %d" % watched)
128
144
  _report_errors()
129
145
  _start_server()
130
146
 
131
147
 
132
148
  func _exit_tree() -> void:
133
- _send_the_profile()
134
149
  if _errors != null:
135
150
  OS.remove_logger(_errors)
136
151
  _errors.close()
@@ -138,16 +153,51 @@ func _exit_tree() -> void:
138
153
  _cleanup()
139
154
 
140
155
 
141
- ## A profiled run's totals, sent as the game leaves the tree.
156
+ ## A profiled run's totals, sent as the game quits.
142
157
  ##
143
158
  ## The engine sends its script profiler's totals only when the profiler is switched off, and at
144
159
  ## exit it is torn down without being switched off, so a scene that did its work in _ready and
145
- ## quit took its whole profile with it, last frame included. The wait is for the debugger's own
146
- ## thread, which sends what is queued every few milliseconds and stops without emptying the queue.
160
+ ## quit took its whole profile with it, last frame included.
161
+ ##
162
+ ## The totals go out on the debugger's own thread, which stops without emptying its queue, so the
163
+ ## game waits until the debugger says it has read them. A fixed wait of 100 ms lost the totals of
164
+ ## games quitting under load, whose debugger thread did not get its turn in time.
165
+ ##
166
+ ## On the root leaving rather than this node: the root leaves only as the game quits, and after
167
+ ## every other node's _exit_tree, so what those do is counted. A project that sent this node away
168
+ ## mid-run had the profiler switched off then, and its run answered as complete with none of the
169
+ ## frames after it.
147
170
  func _send_the_profile() -> void:
148
- if EngineDebugger.is_active() and EngineDebugger.is_profiling("servers"):
149
- EngineDebugger.profiler_enable("servers", false)
150
- OS.delay_msec(PROFILE_SEND_MS)
171
+ if not (EngineDebugger.is_active() and EngineDebugger.is_profiling("servers")):
172
+ return
173
+ _profile_received = false
174
+ if not EngineDebugger.has_capture(PROFILE_CAPTURE):
175
+ EngineDebugger.register_message_capture(PROFILE_CAPTURE, _on_debugger_message)
176
+ EngineDebugger.profiler_enable("servers", false)
177
+ EngineDebugger.send_message(PROFILE_SENT, [])
178
+ var deadline: int = Time.get_ticks_msec() + PROFILE_CONFIRM_MS
179
+ while not _profile_received and Time.get_ticks_msec() < deadline:
180
+ for _poll: int in POLLS_PER_READ:
181
+ EngineDebugger.line_poll()
182
+ OS.delay_msec(5)
183
+ EngineDebugger.unregister_message_capture(PROFILE_CAPTURE)
184
+ if not _profile_received:
185
+ print(
186
+ (
187
+ (
188
+ "[gdharness] the debugger did not confirm the profiler's totals within %d ms, so the"
189
+ + " profile may be the frames that arrived without them"
190
+ )
191
+ % PROFILE_CONFIRM_MS
192
+ )
193
+ )
194
+
195
+
196
+ func _on_debugger_message(message: String, _data: Array) -> bool:
197
+ if PROFILE_CAPTURE + ":" + message == PROFILE_RECEIVED:
198
+ _profile_received = true
199
+ return true
200
+ return false
151
201
 
152
202
 
153
203
  ## The engine's error reports, written to a file for the server, for a game the editor plays, and
@@ -331,6 +331,7 @@ func _wait_until_said(node_path: String, said: String, timeout_ms: int, include_
331
331
  var looks: int = 0
332
332
  var looked_usec: int = 0
333
333
  var next_look: int = 0
334
+ var looked_at_frame: int = 0
334
335
  var found: bool = false
335
336
  while true:
336
337
  if Time.get_ticks_usec() >= next_look:
@@ -340,13 +341,19 @@ func _wait_until_said(node_path: String, said: String, timeout_ms: int, include_
340
341
  var took: int = Time.get_ticks_usec() - looking
341
342
  looked_usec += took
342
343
  next_look = Time.get_ticks_usec() + took * LOOKS_APART
344
+ looked_at_frame = frames
343
345
  if found or Time.get_ticks_msec() - started >= timeout_ms:
344
346
  break
345
347
  await _host.get_tree().process_frame
346
348
  frames += 1
347
349
  # One more look at the end, so words that arrived between two paced looks are not answered as
348
- # never having come.
350
+ # never having come. Taken once a whole frame has run since the last look, which is two frame
351
+ # starts: a first look that outlasted the timeout otherwise left none, so the wait answered with
352
+ # the game not having run at all, and a command sent during that look not yet read.
349
353
  if not found:
354
+ while frames - looked_at_frame < 2:
355
+ await _host.get_tree().process_frame
356
+ frames += 1
350
357
  var last: int = Time.get_ticks_usec()
351
358
  found = _anything_says(node_path, said, include_hidden)
352
359
  looks += 1