diff --git a/code/__DEFINES/profile.dm b/code/__DEFINES/profile.dm index 28fc7782ce..8929e0f05e 100644 --- a/code/__DEFINES/profile.dm +++ b/code/__DEFINES/profile.dm @@ -1,5 +1,5 @@ -#define PROFILE_START ;PROFILE_STORE = list();PROFILE_SET; -#define PROFILE_STOP ;PROFILE_STORE = null; +#define LINE_PROFILE_START ;PROFILE_STORE = list();PROFILE_SET; +#define LINE_PROFILE_STOP ;PROFILE_STORE = null; #define PROFILE_SET ;PROFILE_TIME = TICK_USAGE_REAL; PROFILE_LINE = __LINE__; PROFILE_FILE = __FILE__; PROFILE_SLEEPCHECK = world.time; diff --git a/code/__DEFINES/subsystems.dm b/code/__DEFINES/subsystems.dm index 5c54843df2..162c898917 100644 --- a/code/__DEFINES/subsystems.dm +++ b/code/__DEFINES/subsystems.dm @@ -47,6 +47,7 @@ // Subsystems shutdown in the reverse of the order they initialize in // The numbers just define the ordering, they are meaningless otherwise. +#define INIT_ORDER_PROFILER 101 #define INIT_ORDER_FAIL2TOPIC 22 #define INIT_ORDER_TITLE 20 #define INIT_ORDER_GARBAGE 19 diff --git a/code/controllers/configuration/entries/general.dm b/code/controllers/configuration/entries/general.dm index 95fd3d308e..cb8c0fe966 100644 --- a/code/controllers/configuration/entries/general.dm +++ b/code/controllers/configuration/entries/general.dm @@ -1,3 +1,5 @@ +/datum/config_entry/flag/auto_profile // Automatically start profiler on server start + /datum/config_entry/flag/autoadmin // if autoadmin is enabled protection = CONFIG_ENTRY_LOCKED diff --git a/code/controllers/subsystem/profiler.dm b/code/controllers/subsystem/profiler.dm new file mode 100644 index 0000000000..ec8b243073 --- /dev/null +++ b/code/controllers/subsystem/profiler.dm @@ -0,0 +1,63 @@ +#define PROFILER_FILENAME "profiler.json" + +SUBSYSTEM_DEF(profiler) + name = "Profiler" + init_order = INIT_ORDER_PROFILER + runlevels = RUNLEVELS_DEFAULT | RUNLEVEL_LOBBY + wait = 3000 + flags = SS_NO_TICK_CHECK + var/fetch_cost = 0 + var/write_cost = 0 + +/datum/controller/subsystem/profiler/stat_entry(msg) + msg += "F:[round(fetch_cost,1)]ms" + msg += "|W:[round(write_cost,1)]ms" + ..(msg) + +/datum/controller/subsystem/profiler/Initialize() + if(CONFIG_GET(flag/auto_profile)) + StartProfiling() + else + StopProfiling() //Stop the early start from world/New + return ..() + +/datum/controller/subsystem/profiler/fire() + if(CONFIG_GET(flag/auto_profile)) + DumpFile() + +/datum/controller/subsystem/profiler/Shutdown() + if(CONFIG_GET(flag/auto_profile)) + DumpFile() + return ..() + +/datum/controller/subsystem/profiler/proc/StartProfiling() +#if DM_BUILD < 1506 || DM_VERSION < 513 + stack_trace("Auto profiling unsupported on this byond version") + CONFIG_SET(flag/auto_profile, FALSE) +#else + world.Profile(PROFILE_START) +#endif + +/datum/controller/subsystem/profiler/proc/StopProfiling() +#if DM_BUILD >= 1506 && DM_VERSION >= 513 + world.Profile(PROFILE_STOP) +#endif + +/datum/controller/subsystem/profiler/proc/DumpFile() +#if DM_BUILD < 1506 || DM_VERSION < 513 + stack_trace("Auto profiling unsupported on this byond version") + CONFIG_SET(flag/auto_profile, FALSE) +#else + var/timer = TICK_USAGE_REAL + var/current_profile_data = world.Profile(PROFILE_REFRESH,format="json") + fetch_cost = MC_AVERAGE(fetch_cost, TICK_DELTA_TO_MS(TICK_USAGE_REAL - timer)) + CHECK_TICK + if(!length(current_profile_data)) //Would be nice to have explicit proc to check this + stack_trace("Warning, profiling stopped manually before dump.") + var/json_file = file("[GLOB.log_directory]/[PROFILER_FILENAME]") + if(fexists(json_file)) + fdel(json_file) + timer = TICK_USAGE_REAL + WRITE_FILE(json_file, current_profile_data) + write_cost = MC_AVERAGE(write_cost, TICK_DELTA_TO_MS(TICK_USAGE_REAL - timer)) +#endif diff --git a/code/game/world.dm b/code/game/world.dm index 0e21641abc..77361310b8 100644 --- a/code/game/world.dm +++ b/code/game/world.dm @@ -10,6 +10,10 @@ GLOBAL_LIST(topic_status_cache) /world/New() enable_debugger() +#if DM_VERSION >= 513 && DM_BUILD >= 1506 + world.Profile(PROFILE_START) +#endif + log_world("World loaded at [TIME_STAMP("hh:mm:ss", FALSE)]!") SetupExternalRSC() diff --git a/code/modules/admin/verbs/debug.dm b/code/modules/admin/verbs/debug.dm index ad2eeb7289..cf6d54336e 100644 --- a/code/modules/admin/verbs/debug.dm +++ b/code/modules/admin/verbs/debug.dm @@ -1058,7 +1058,7 @@ GLOBAL_PROTECT(AdminProcCallSpamPrevention) set name = "Start Line Profiling" set desc = "Starts tracking line by line profiling for code lines that support it" - PROFILE_START + LINE_PROFILE_START message_admins("[key_name_admin(src)] started line by line profiling.") SSblackbox.record_feedback("tally", "admin_verb", 1, "Start Line Profiling") @@ -1069,7 +1069,7 @@ GLOBAL_PROTECT(AdminProcCallSpamPrevention) set name = "Stops Line Profiling" set desc = "Stops tracking line by line profiling for code lines that support it" - PROFILE_STOP + LINE_PROFILE_STOP message_admins("[key_name_admin(src)] stopped line by line profiling.") SSblackbox.record_feedback("tally", "admin_verb", 1, "Stop Line Profiling") diff --git a/config/config.txt b/config/config.txt index 30c13cfdf7..0ab0cc911e 100644 --- a/config/config.txt +++ b/config/config.txt @@ -506,3 +506,5 @@ FAIL2TOPIC_MAX_FAILS 5 ## Firewall rule name used on physical server FAIL2TOPIC_RULE_NAME _dd_fail2topic +## Enable automatic profiling - Byond 513.1506 and newer only. +#AUTO_PROFILE diff --git a/tgstation.dme b/tgstation.dme index e7d5e50b54..1614c12e87 100755 --- a/tgstation.dme +++ b/tgstation.dme @@ -274,6 +274,7 @@ #include "code\controllers\subsystem\pathfinder.dm" #include "code\controllers\subsystem\persistence.dm" #include "code\controllers\subsystem\ping.dm" +#include "code\controllers\subsystem\profiler.dm" #include "code\controllers\subsystem\radiation.dm" #include "code\controllers\subsystem\radio.dm" #include "code\controllers\subsystem\research.dm"