[MIRROR] Biddle Verbs: Queues the Most Expensive Verbs for the Next Tick if the Server Is Overloaded [MDB IGNORE] (#15329)

* 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>
This commit is contained in:
SkyratBot
2022-08-01 00:03:59 +02:00
committed by GitHub
parent 417c73b057
commit d8da1153b7
26 changed files with 376 additions and 95 deletions
@@ -1,53 +1,5 @@
SUBSYSTEM_DEF(speech_controller)
/// verb_manager subsystem just for handling say's
VERB_MANAGER_SUBSYSTEM_DEF(speech_controller)
name = "Speech Controller"
wait = 1
flags = SS_TICKER|SS_NO_INIT
priority = FIRE_PRIORITY_SPEECH_CONTROLLER//has to be high priority, second in priority ONLY to SSinput
runlevels = RUNLEVELS_DEFAULT | RUNLEVEL_LOBBY
///used so that an admin can force all speech verbs to execute immediately instead of queueing
var/FOR_ADMINS_IF_BROKE_immediately_execute_all_speech = FALSE
///list of the form: list(client mob, message that mob is queued to say, other say arguments (if any)).
///this is our process queue, processed every tick.
var/list/queued_says_to_execute = list()
///queues mob_to_queue into our process list so they say(message) near the start of the next tick
/datum/controller/subsystem/speech_controller/proc/queue_say_for_mob(mob/mob_to_queue, message, message_type)
if(!TICK_CHECK || FOR_ADMINS_IF_BROKE_immediately_execute_all_speech)
process_single_say(mob_to_queue, message, message_type)
return TRUE
queued_says_to_execute += list(list(mob_to_queue, message, message_type))
return TRUE
/datum/controller/subsystem/speech_controller/fire(resumed)
/// cache for sanic speed (lists are references anyways)
var/list/says_to_process = queued_says_to_execute.Copy()
queued_says_to_execute.Cut()//we should be going through the entire list every single iteration
for(var/list/say_to_process as anything in says_to_process)
var/mob/mob_to_speak = say_to_process[MOB_INDEX]//index 1 is the mob, 2 is the message, 3 is the message category
var/message = say_to_process[MESSAGE_INDEX]
var/message_category = say_to_process[CATEGORY_INDEX]
process_single_say(mob_to_speak, message, message_category)
///used in fire() to process a single mobs message through the relevant proc.
///only exists so that sleeps in the message pipeline dont cause the whole queue to wait
/datum/controller/subsystem/speech_controller/proc/process_single_say(mob/mob_to_speak, message, message_category)
set waitfor = FALSE
switch(message_category)
if(SPEECH_CONTROLLER_QUEUE_SAY_VERB)
mob_to_speak.say(message)
if(SPEECH_CONTROLLER_QUEUE_WHISPER_VERB)
mob_to_speak.whisper(message)
if(SPEECH_CONTROLLER_QUEUE_EMOTE_VERB)
mob_to_speak.emote("me",1,message,TRUE)