Skip to content

Commit 90870f5

Browse files
committed
https://github.com/tgstation/tgstation/pull/75924
1 parent 1b89586 commit 90870f5

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

code/controllers/configuration/entries/general.dm

+6
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,12 @@
585585

586586
/datum/config_entry/flag/auto_profile
587587

588+
/datum/config_entry/number/drift_dump_threshold
589+
default = 4 SECONDS
590+
591+
/datum/config_entry/number/drift_profile_delay
592+
default = 15 SECONDS
593+
588594
/datum/config_entry/string/centcom_ban_db // URL for the CentCom Galactic Ban DB API
589595

590596
/datum/config_entry/string/centcom_source_whitelist

code/controllers/master.dm

+20-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ GLOBAL_REAL(Master, /datum/controller/master) = new
4343
var/init_timeofday
4444
var/init_time
4545
var/tickdrift = 0
46+
/// Tickdrift as of last tick, w no averaging going on
47+
var/olddrift = 0
4648

4749
/// How long is the MC sleeping between runs, read only (set by Loop() based off of anti-tick-contention heuristics)
4850
var/sleep_delta = 1
@@ -71,6 +73,10 @@ GLOBAL_REAL(Master, /datum/controller/master) = new
7173
/// Outside of initialization, returns null.
7274
var/current_initializing_subsystem = null
7375

76+
/// The last decisecond we force dumped profiling information
77+
/// Used to avoid spamming profile reads since they can be expensive (string memes)
78+
var/last_profiled = 0
79+
7480
var/static/restart_clear = 0
7581
var/static/restart_timeout = 0
7682
var/static/restart_count = 0
@@ -388,9 +394,14 @@ GLOBAL_REAL(Master, /datum/controller/master) = new
388394
canary.use_variable()
389395
//the actual loop.
390396
while (1)
391-
tickdrift = max(0, MC_AVERAGE_FAST(tickdrift, (((REALTIMEOFDAY - init_timeofday) - (world.time - init_time)) / world.tick_lag)))
397+
var/newdrift = ((REALTIMEOFDAY - init_timeofday) - (world.time - init_time)) / world.tick_lag
398+
tickdrift = max(0, MC_AVERAGE_FAST(tickdrift, newdrift))
392399
var/starting_tick_usage = TICK_USAGE
393400

401+
if(newdrift - olddrift >= CONFIG_GET(number/drift_dump_threshold))
402+
AttemptProfileDump(CONFIG_GET(number/drift_profile_delay))
403+
olddrift = newdrift
404+
394405
if (init_stage != init_stage_completed)
395406
return MC_LOOP_RTN_NEWSTAGES
396407
if (processing <= 0)
@@ -763,3 +774,11 @@ GLOBAL_REAL(Master, /datum/controller/master) = new
763774
for (var/thing in subsystems)
764775
var/datum/controller/subsystem/SS = thing
765776
SS.OnConfigLoad()
777+
778+
/// Attempts to dump our current profile info into a file, triggered if the MC thinks shit is going down
779+
/// Accepts a delay in deciseconds of how long ago our last dump can be, this saves causing performance problems ourselves
780+
/datum/controller/master/proc/AttemptProfileDump(delay)
781+
if(REALTIMEOFDAY - last_profiled <= delay)
782+
return FALSE
783+
last_profiled = REALTIMEOFDAY
784+
SSprofiler.DumpFile(allow_yield = FALSE)

code/controllers/subsystem/profiler.dm

+14-5
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ SUBSYSTEM_DEF(profiler)
2323
StopProfiling() //Stop the early start profiler
2424
return ..()
2525

26-
/datum/controller/subsystem/profiler/fire()
26+
/datum/controller/subsystem/profiler/OnConfigLoad()
2727
if(CONFIG_GET(flag/auto_profile))
28-
DumpFile()
28+
StartProfiling()
29+
can_fire = TRUE
30+
else
31+
StopProfiling()
32+
can_fire = FALSE
33+
34+
/datum/controller/subsystem/profiler/fire()
35+
DumpFile()
2936

3037
/datum/controller/subsystem/profiler/Shutdown()
3138
if(CONFIG_GET(flag/auto_profile))
32-
DumpFile()
39+
DumpFile(allow_yield = FALSE)
3340
world.Profile(PROFILE_CLEAR, type = "sendmaps")
3441
return ..()
3542

@@ -42,12 +49,14 @@ SUBSYSTEM_DEF(profiler)
4249
world.Profile(PROFILE_STOP, type = "sendmaps")
4350

4451

45-
/datum/controller/subsystem/profiler/proc/DumpFile()
52+
/datum/controller/subsystem/profiler/proc/DumpFile(allow_yield = TRUE)
4653
var/timer = TICK_USAGE_REAL
4754
var/current_profile_data = world.Profile(PROFILE_REFRESH, format = "json")
4855
var/current_sendmaps_data = world.Profile(PROFILE_REFRESH, type = "sendmaps", format="json")
4956
fetch_cost = MC_AVERAGE(fetch_cost, TICK_DELTA_TO_MS(TICK_USAGE_REAL - timer))
50-
CHECK_TICK
57+
58+
if(allow_yield)
59+
CHECK_TICK
5160

5261
if(!length(current_profile_data)) //Would be nice to have explicit proc to check this
5362
stack_trace("Warning, profiling stopped manually before dump.")

config/config.txt

+6
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,12 @@ DEFAULT_VIEW_SQUARE 15x15
555555
## Enable automatic profiling - Byond 513.1506 and newer only.
556556
AUTO_PROFILE
557557

558+
## Threshold (in deciseconds) for real time between ticks before we start dumping profiles
559+
DRIFT_DUMP_THRESHOLD 40
560+
561+
## How long to wait (in deciseconds) after a profile dump before logging another tickdrift sourced one
562+
DRIFT_PROFILE_DELAY 150
563+
558564
## Uncomment to enable global ban DB using the provided URL. The API should expect to receive a ckey at the end of the URL.
559565
## More API details can be found here: https://centcom.melonmesa.com
560566
CENTCOM_BAN_DB https://centcom.melonmesa.com/ban/search

0 commit comments

Comments
 (0)