mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2025-12-10 09:42:29 +00:00
* Biddle Verbs: Queues the Most Expensive Verbs for the Next Tick if the Server Is Overloaded (#65589) This pr goes through: /client/Click(), /client/Topic(), /mob/living/verb/resist(), /mob/verb/quick_equip(), /mob/verb/examinate(), and /mob/verb/mode() and makes them queue their functionality to a subsystem to execute in the next tick if the server is overloaded. To do this a new subsystem is made to handle most verbs called SSverb_manager, if the server is overloaded the verb queues itself in the subsystem and returns, then near the start of the next tick that verb is resumed with the provided callback. The verbs are called directly after SSinput, and the subsystem does not yield until its queue is completely finished. The exception are clicks from player input since they are extremely important for the feeling of responsiveness. I considered not queuing them but theyre too expensive not to, suffering from a death of a thousand cuts performance wise from many many things in the process adding up. Instead clicks are executed at the very start of the next tick, as the first action that SSinput completes, before player movement is processed even. A few months ago, before I died I was trying to figure out why games at midpop (40-50 people) had non zero and consistent time dilation without maptick being consistently above 28% (which is when the MC stops yielding for maptick if its overloaded). I found it out, started working on this pr, then promptly died. luckily im a bit less dead now the current MC has a problem: the cost of verbs is completely and totally invisible to it, it cannot account for them. Why is this bad? because verbs are the last thing to execute in the tick, after the MC and SendMaps have finished executing. tick diagram2 If the MC is overloaded and uses 100% of the time it allots itself this means that if SendMaps uses the amount its expected to take, verbs have at most 2% of the tick to execute in before they are overtiming and thus delaying the start of the next tick. This is bad, and im 99% sure this is the majority of our overtime. Take Click() for example. Click isnt listed as a verb but since its called as a result of client commands its executed at the end of the tick like other verbs. in this random 80 pop sybil round profile i had saved on my computer sybil 80 pop (2).txt /client/Click() has an overtime of only 1.8 seconds, which isnt that bad. however it has a self cpu of 2.5 seconds meaning 1.8/2.5 = 72% of its time is overtiming, and it also is calling 80.2 seconds worth of total cpu, which means that more than 57.7 seconds of overtime is attributed to just /client/Click() executing at the very end of a tick. the reason why this isnt obvious is just because the verbs themselves typically dont have high enough self cpu to get high enough on the rankings of overtiming procs to be noticed, all of their overtime is distributed among a ton of procs they call in the chain. Since i cant guarantee the MC resumes at the very start of the next tick due to other sleeping procs almost always resuming first: I time the duration between clicks being queued up for the next tick and when theyre actually executed. if it exceeds 20 milliseconds of added latency (less than one tenth the average human reaction time) clicks will execute immediately instead of queuing, this should make instances where a player can notice the added latency a vanishingly small minority of cases. still, this should be tm'd * Biddle Verbs: Queues the Most Expensive Verbs for the Next Tick if the Server Is Overloaded Co-authored-by: Kylerace <kylerlumpkin1@gmail.com>
99 lines
3.8 KiB
Plaintext
99 lines
3.8 KiB
Plaintext
VERB_MANAGER_SUBSYSTEM_DEF(input)
|
|
name = "Input"
|
|
init_order = INIT_ORDER_INPUT
|
|
init_stage = INITSTAGE_EARLY
|
|
flags = SS_TICKER
|
|
priority = FIRE_PRIORITY_INPUT
|
|
runlevels = RUNLEVELS_DEFAULT | RUNLEVEL_LOBBY
|
|
|
|
use_default_stats = FALSE
|
|
|
|
var/list/macro_set
|
|
|
|
///running average of how many clicks inputted by a player the server processes every second. used for the subsystem stat entry
|
|
var/clicks_per_second = 0
|
|
///count of how many clicks onto atoms have elapsed before being cleared by fire(). used to average with clicks_per_second.
|
|
var/current_clicks = 0
|
|
///acts like clicks_per_second but only counts the clicks actually processed by SSinput itself while clicks_per_second counts all clicks
|
|
var/delayed_clicks_per_second = 0
|
|
///running average of how many movement iterations from player input the server processes every second. used for the subsystem stat entry
|
|
var/movements_per_second = 0
|
|
///running average of the amount of real time clicks take to truly execute after the command is originally sent to the server.
|
|
///if a click isnt delayed at all then it counts as 0 deciseconds.
|
|
var/average_click_delay = 0
|
|
|
|
/datum/controller/subsystem/verb_manager/input/Initialize()
|
|
setup_default_macro_sets()
|
|
|
|
initialized = TRUE
|
|
|
|
refresh_client_macro_sets()
|
|
|
|
return ..()
|
|
|
|
// This is for when macro sets are eventualy datumized
|
|
/datum/controller/subsystem/verb_manager/input/proc/setup_default_macro_sets()
|
|
macro_set = list(
|
|
"Any" = "\"KeyDown \[\[*\]\]\"",
|
|
"Any+UP" = "\"KeyUp \[\[*\]\]\"",
|
|
"Back" = "\".winset \\\"input.text=\\\"\\\"\\\"\"",
|
|
"Tab" = "\".winset \\\"input.focus=true?map.focus=true:input.focus=true\\\"\"",
|
|
"Escape" = "Reset-Held-Keys",
|
|
)
|
|
|
|
// Badmins just wanna have fun ♪
|
|
/datum/controller/subsystem/verb_manager/input/proc/refresh_client_macro_sets()
|
|
var/list/clients = GLOB.clients
|
|
for(var/i in 1 to clients.len)
|
|
var/client/user = clients[i]
|
|
user.set_macros()
|
|
|
|
/datum/controller/subsystem/verb_manager/input/can_queue_verb(datum/callback/verb_callback/incoming_callback, control)
|
|
//make sure the incoming verb is actually something we specifically want to handle
|
|
if(control != "mapwindow.map")
|
|
return FALSE
|
|
|
|
if(average_click_delay >= MAXIMUM_CLICK_LATENCY || !..())
|
|
current_clicks++
|
|
average_click_delay = MC_AVG_FAST_UP_SLOW_DOWN(average_click_delay, 0)
|
|
return FALSE
|
|
|
|
return TRUE
|
|
|
|
///stupid workaround for byond not recognizing the /atom/Click typepath for the queued click callbacks
|
|
/atom/proc/_Click(location, control, params)
|
|
if(usr)
|
|
Click(location, control, params)
|
|
|
|
|
|
/datum/controller/subsystem/verb_manager/input/fire()
|
|
var/moves_this_run = 0
|
|
var/deferred_clicks_this_run = 0 //acts like current_clicks but doesnt count clicks that dont get processed by SSinput
|
|
|
|
for(var/datum/callback/verb_callback/queued_click as anything in verb_queue)
|
|
if(!istype(queued_click))
|
|
stack_trace("non /datum/callback/verb_callback instance inside SSinput's verb_queue!")
|
|
continue
|
|
|
|
average_click_delay = MC_AVG_FAST_UP_SLOW_DOWN(average_click_delay, (REALTIMEOFDAY - queued_click.creation_time) SECONDS)
|
|
queued_click.InvokeAsync()
|
|
|
|
current_clicks++
|
|
deferred_clicks_this_run++
|
|
|
|
verb_queue.Cut() //is ran all the way through every run, no exceptions
|
|
|
|
for(var/mob/user in GLOB.keyloop_list)
|
|
moves_this_run += user.focus?.keyLoop(user.client)//only increments if a player changes their movement input from the last tick
|
|
|
|
clicks_per_second = MC_AVG_SECONDS(clicks_per_second, current_clicks, wait TICKS)
|
|
delayed_clicks_per_second = MC_AVG_SECONDS(delayed_clicks_per_second, deferred_clicks_this_run, wait TICKS)
|
|
movements_per_second = MC_AVG_SECONDS(movements_per_second, moves_this_run, wait TICKS)
|
|
|
|
current_clicks = 0
|
|
|
|
/datum/controller/subsystem/verb_manager/input/stat_entry(msg)
|
|
. = ..()
|
|
. += "M/S:[round(movements_per_second,0.01)] | C/S:[round(clicks_per_second,0.01)]([round(delayed_clicks_per_second,0.01)] | CD: [round(average_click_delay,0.01)])"
|
|
|