From 90dff0ae632ad98b21bade60ae9871eec52d4b11 Mon Sep 17 00:00:00 2001 From: Leshana Date: Tue, 30 May 2017 21:00:37 -0400 Subject: [PATCH] Ports /tg's StonedMC Subsystem from Baystation12 * Partial port of @PsiOmegaDelta's https://github.com/Baystation12/Baystation12/pull/16820 * Only ports the StonedMC changes, not the garbage collector (forthcoming in future) --- code/__defines/MC.dm | 53 ++ code/__defines/math.dm | 10 + code/__defines/misc.dm | 2 + code/__defines/tick.dm | 7 + code/_helpers/lists.dm | 63 ++ code/_helpers/sorts/TimSort.dm | 17 + code/_helpers/sorts/__main.dm | 656 ++++++++++++++++++ code/_helpers/sorts/comparators.dm | 20 + code/_helpers/time.dm | 22 +- code/_macros.dm | 11 + .../ProcessScheduler/core/process.dm | 1 - code/controllers/Processes/ticker.dm | 3 + code/controllers/configuration.dm | 10 +- code/controllers/controller.dm | 19 + code/controllers/failsafe.dm | 102 +++ code/controllers/master.dm | 519 ++++++++++++++ code/controllers/master_controller.dm | 6 +- code/controllers/subsystem.dm | 192 +++++ code/controllers/subsystems.dm | 46 -- code/controllers/verbs.dm | 54 +- polaris.dme | 11 +- 21 files changed, 1763 insertions(+), 61 deletions(-) create mode 100644 code/__defines/MC.dm create mode 100644 code/__defines/math.dm create mode 100644 code/__defines/tick.dm create mode 100644 code/_helpers/sorts/TimSort.dm create mode 100644 code/_helpers/sorts/__main.dm create mode 100644 code/_helpers/sorts/comparators.dm create mode 100644 code/controllers/controller.dm create mode 100644 code/controllers/failsafe.dm create mode 100644 code/controllers/master.dm create mode 100644 code/controllers/subsystem.dm delete mode 100644 code/controllers/subsystems.dm diff --git a/code/__defines/MC.dm b/code/__defines/MC.dm new file mode 100644 index 0000000000..90724a2f98 --- /dev/null +++ b/code/__defines/MC.dm @@ -0,0 +1,53 @@ +#define MC_TICK_CHECK ( ( world.tick_usage > CURRENT_TICKLIMIT || src.state != SS_RUNNING ) ? pause() : 0 ) +// Used to smooth out costs to try and avoid oscillation. +#define MC_AVERAGE_FAST(average, current) (0.7 * (average) + 0.3 * (current)) +#define MC_AVERAGE(average, current) (0.8 * (average) + 0.2 * (current)) +#define MC_AVERAGE_SLOW(average, current) (0.9 * (average) + 0.1 * (current)) +#define NEW_SS_GLOBAL(varname) if(varname != src){if(istype(varname)){Recover();qdel(varname);}varname = src;} + +#define START_PROCESSING(Processor, Datum) if (!Datum.isprocessing) {Datum.isprocessing = 1;Processor.processing += Datum} +#define STOP_PROCESSING(Processor, Datum) Datum.isprocessing = 0;Processor.processing -= Datum + +//SubSystem flags (Please design any new flags so that the default is off, to make adding flags to subsystems easier) + +//subsystem should fire during pre-game lobby. +#define SS_FIRE_IN_LOBBY 1 + +//subsystem does not initialize. +#define SS_NO_INIT 2 + +//subsystem does not fire. +// (like can_fire = 0, but keeps it from getting added to the processing subsystems list) +// (Requires a MC restart to change) +#define SS_NO_FIRE 4 + +//subsystem only runs on spare cpu (after all non-background subsystems have ran that tick) +// SS_BACKGROUND has its own priority bracket +#define SS_BACKGROUND 8 + +//subsystem does not tick check, and should not run unless there is enough time (or its running behind (unless background)) +#define SS_NO_TICK_CHECK 16 + +//Treat wait as a tick count, not DS, run every wait ticks. +// (also forces it to run first in the tick, above even SS_NO_TICK_CHECK subsystems) +// (implies SS_FIRE_IN_LOBBY because of how it works) +// (overrides SS_BACKGROUND) +// This is designed for basically anything that works as a mini-mc (like SStimer) +#define SS_TICKER 32 + +//keep the subsystem's timing on point by firing early if it fired late last fire because of lag +// ie: if a 20ds subsystem fires say 5 ds late due to lag or what not, its next fire would be in 15ds, not 20ds. +#define SS_KEEP_TIMING 64 + +//Calculate its next fire after its fired. +// (IE: if a 5ds wait SS takes 2ds to run, its next fire should be 5ds away, not 3ds like it normally would be) +// This flag overrides SS_KEEP_TIMING +#define SS_POST_FIRE_TIMING 128 + +//SUBSYSTEM STATES +#define SS_IDLE 0 //aint doing shit. +#define SS_QUEUED 1 //queued to run +#define SS_RUNNING 2 //actively running +#define SS_PAUSED 3 //paused by mc_tick_check +#define SS_SLEEPING 4 //fire() slept. +#define SS_PAUSING 5 //in the middle of pausing diff --git a/code/__defines/math.dm b/code/__defines/math.dm new file mode 100644 index 0000000000..0729a4526d --- /dev/null +++ b/code/__defines/math.dm @@ -0,0 +1,10 @@ +//"fancy" math for calculating time in ms from tick_usage percentage and the length of ticks +//percent_of_tick_used * (ticklag * 100(to convert to ms)) / 100(percent ratio) +//collapsed to percent_of_tick_used * tick_lag +#define TICK_DELTA_TO_MS(percent_of_tick_used) ((percent_of_tick_used) * world.tick_lag) +#define TICK_USAGE_TO_MS(starting_tickusage) (TICK_DELTA_TO_MS(world.tick_usage-starting_tickusage)) + +//time of day but automatically adjusts to the server going into the next day within the same round. +//for when you need a reliable time number that doesn't depend on byond time. +#define REALTIMEOFDAY (world.timeofday + (MIDNIGHT_ROLLOVER * MIDNIGHT_ROLLOVER_CHECK)) +#define MIDNIGHT_ROLLOVER_CHECK ( rollovercheck_last_timeofday != world.timeofday ? update_midnight_rollover() : midnight_rollovers ) diff --git a/code/__defines/misc.dm b/code/__defines/misc.dm index f85ac5e7d4..12deebbfc1 100644 --- a/code/__defines/misc.dm +++ b/code/__defines/misc.dm @@ -196,3 +196,5 @@ #define TSC_MORPH "Morpheus" #define TSC_XION "Xion" // Not really needed but consistancy I guess. #define TSC_GIL "Gilthari" + +#define MIDNIGHT_ROLLOVER 864000 //number of deciseconds in a day diff --git a/code/__defines/tick.dm b/code/__defines/tick.dm new file mode 100644 index 0000000000..3fa6e21bf3 --- /dev/null +++ b/code/__defines/tick.dm @@ -0,0 +1,7 @@ +#define TICK_LIMIT_RUNNING 80 +#define TICK_LIMIT_TO_RUN 78 +#define TICK_LIMIT_MC 70 +#define TICK_LIMIT_MC_INIT_DEFAULT 98 + +#define TICK_CHECK ( world.tick_usage > CURRENT_TICKLIMIT ) +#define CHECK_TICK if TICK_CHECK stoplag() diff --git a/code/_helpers/lists.dm b/code/_helpers/lists.dm index 30df4693db..e647cd1df7 100644 --- a/code/_helpers/lists.dm +++ b/code/_helpers/lists.dm @@ -599,3 +599,66 @@ proc/dd_sortedTextList(list/incoming) for(var/path in subtypesof(prototype)) L += new path() return L + +//creates every subtype of prototype (excluding prototype) and adds it to list L as a type/instance pair. +//if no list/L is provided, one is created. +/proc/init_subtypes_assoc(prototype, list/L) + if(!istype(L)) L = list() + for(var/path in subtypesof(prototype)) + L[path] = new path() + return L + +//Move a single element from position fromIndex within a list, to position toIndex +//All elements in the range [1,toIndex) before the move will be before the pivot afterwards +//All elements in the range [toIndex, L.len+1) before the move will be after the pivot afterwards +//In other words, it's as if the range [fromIndex,toIndex) have been rotated using a <<< operation common to other languages. +//fromIndex and toIndex must be in the range [1,L.len+1] +//This will preserve associations ~Carnie +/proc/moveElement(list/L, fromIndex, toIndex) + if(fromIndex == toIndex || fromIndex+1 == toIndex) //no need to move + return + if(fromIndex > toIndex) + ++fromIndex //since a null will be inserted before fromIndex, the index needs to be nudged right by one + + L.Insert(toIndex, null) + L.Swap(fromIndex, toIndex) + L.Cut(fromIndex, fromIndex+1) + +//Move elements [fromIndex,fromIndex+len) to [toIndex-len, toIndex) +//Same as moveElement but for ranges of elements +//This will preserve associations ~Carnie +/proc/moveRange(list/L, fromIndex, toIndex, len=1) + var/distance = abs(toIndex - fromIndex) + if(len >= distance) //there are more elements to be moved than the distance to be moved. Therefore the same result can be achieved (with fewer operations) by moving elements between where we are and where we are going. The result being, our range we are moving is shifted left or right by dist elements + if(fromIndex <= toIndex) + return //no need to move + fromIndex += len //we want to shift left instead of right + + for(var/i=0, i toIndex) + fromIndex += len + + for(var/i=0, i= 2) + fromIndex = fromIndex % L.len + toIndex = toIndex % (L.len+1) + if(fromIndex <= 0) + fromIndex += L.len + if(toIndex <= 0) + toIndex += L.len + 1 + + sortInstance.L = L + sortInstance.cmp = cmp + sortInstance.associative = associative + + sortInstance.timSort(fromIndex, toIndex) + + return L diff --git a/code/_helpers/sorts/__main.dm b/code/_helpers/sorts/__main.dm new file mode 100644 index 0000000000..622d88f147 --- /dev/null +++ b/code/_helpers/sorts/__main.dm @@ -0,0 +1,656 @@ + //These are macros used to reduce on proc calls +#define fetchElement(L, i) (associative) ? L[L[i]] : L[i] + + //Minimum sized sequence that will be merged. Anything smaller than this will use binary-insertion sort. + //Should be a power of 2 +#define MIN_MERGE 32 + + //When we get into galloping mode, we stay there until both runs win less often than MIN_GALLOP consecutive times. +#define MIN_GALLOP 7 + + //This is a global instance to allow much of this code to be reused. The interfaces are kept separately +var/datum/sortInstance/sortInstance = new() +/datum/sortInstance + //The array being sorted. + var/list/L + + //The comparator proc-reference + var/cmp = /proc/cmp_numeric_asc + + //whether we are sorting list keys (0: L[i]) or associated values (1: L[L[i]]) + var/associative = 0 + + //This controls when we get *into* galloping mode. It is initialized to MIN_GALLOP. + //The mergeLo and mergeHi methods nudge it higher for random data, and lower for highly structured data. + var/minGallop = MIN_GALLOP + + //Stores information regarding runs yet to be merged. + //Run i starts at runBase[i] and extends for runLen[i] elements. + //runBase[i] + runLen[i] == runBase[i+1] + //var/stackSize + var/list/runBases = list() + var/list/runLens = list() + + +/datum/sortInstance/proc/timSort(start, end) + runBases.Cut() + runLens.Cut() + + var/remaining = end - start + + //If array is small, do a 'mini-TimSort' with no merges + if(remaining < MIN_MERGE) + var/initRunLen = countRunAndMakeAscending(start, end) + binarySort(start, end, start+initRunLen) + return + + //March over the array finding natural runs + //Extend any short natural runs to runs of length minRun + var/minRun = minRunLength(remaining) + + do + //identify next run + var/runLen = countRunAndMakeAscending(start, end) + + //if run is short, extend to min(minRun, remaining) + if(runLen < minRun) + var/force = (remaining <= minRun) ? remaining : minRun + + binarySort(start, start+force, start+runLen) + runLen = force + + //add data about run to queue + runBases.Add(start) + runLens.Add(runLen) + + //maybe merge + mergeCollapse() + + //Advance to find next run + start += runLen + remaining -= runLen + + while(remaining > 0) + + + //Merge all remaining runs to complete sort + //ASSERT(start == end) + mergeForceCollapse(); + //ASSERT(runBases.len == 1) + + //reset minGallop, for successive calls + minGallop = MIN_GALLOP + + return L + +/* +Sorts the specified portion of the specified array using a binary +insertion sort. This is the best method for sorting small numbers +of elements. It requires O(n log n) compares, but O(n^2) data +movement (worst case). + +If the initial part of the specified range is already sorted, +this method can take advantage of it: the method assumes that the +elements in range [lo,start) are already sorted + +lo the index of the first element in the range to be sorted +hi the index after the last element in the range to be sorted +start the index of the first element in the range that is not already known to be sorted +*/ +/datum/sortInstance/proc/binarySort(lo, hi, start) + //ASSERT(lo <= start && start <= hi) + if(start <= lo) + start = lo + 1 + + for(,start < hi, ++start) + var/pivot = fetchElement(L,start) + + //set left and right to the index where pivot belongs + var/left = lo + var/right = start + //ASSERT(left <= right) + + //[lo, left) elements <= pivot < [right, start) elements + //in other words, find where the pivot element should go using bisection search + while(left < right) + var/mid = (left + right) >> 1 //round((left+right)/2) + if(call(cmp)(fetchElement(L,mid), pivot) > 0) + right = mid + else + left = mid+1 + + //ASSERT(left == right) + moveElement(L, start, left) //move pivot element to correct location in the sorted range + +/* +Returns the length of the run beginning at the specified position and reverses the run if it is back-to-front + +A run is the longest ascending sequence with: + a[lo] <= a[lo + 1] <= a[lo + 2] <= ... +or the longest descending sequence with: + a[lo] > a[lo + 1] > a[lo + 2] > ... + +For its intended use in a stable mergesort, the strictness of the +definition of "descending" is needed so that the call can safely +reverse a descending sequence without violating stability. +*/ +/datum/sortInstance/proc/countRunAndMakeAscending(lo, hi) + //ASSERT(lo < hi) + + var/runHi = lo + 1 + if(runHi >= hi) + return 1 + + var/last = fetchElement(L,lo) + var/current = fetchElement(L,runHi++) + + if(call(cmp)(current, last) < 0) + while(runHi < hi) + last = current + current = fetchElement(L,runHi) + if(call(cmp)(current, last) >= 0) + break + ++runHi + reverseRange(L, lo, runHi) + else + while(runHi < hi) + last = current + current = fetchElement(L,runHi) + if(call(cmp)(current, last) < 0) + break + ++runHi + + return runHi - lo + +//Returns the minimum acceptable run length for an array of the specified length. +//Natural runs shorter than this will be extended with binarySort +/datum/sortInstance/proc/minRunLength(n) + //ASSERT(n >= 0) + var/r = 0 //becomes 1 if any bits are shifted off + while(n >= MIN_MERGE) + r |= (n & 1) + n >>= 1 + return n + r + +//Examines the stack of runs waiting to be merged and merges adjacent runs until the stack invariants are reestablished: +// runLen[i-3] > runLen[i-2] + runLen[i-1] +// runLen[i-2] > runLen[i-1] +//This method is called each time a new run is pushed onto the stack. +//So the invariants are guaranteed to hold for i= 2) + var/n = runBases.len - 1 + if(n > 1 && runLens[n-1] <= runLens[n] + runLens[n+1]) + if(runLens[n-1] < runLens[n+1]) + --n + mergeAt(n) + else if(runLens[n] <= runLens[n+1]) + mergeAt(n) + else + break //Invariant is established + + +//Merges all runs on the stack until only one remains. +//Called only once, to finalise the sort +/datum/sortInstance/proc/mergeForceCollapse() + while(runBases.len >= 2) + var/n = runBases.len - 1 + if(n > 1 && runLens[n-1] < runLens[n+1]) + --n + mergeAt(n) + + +//Merges the two consecutive runs at stack indices i and i+1 +//Run i must be the penultimate or antepenultimate run on the stack +//In other words, i must be equal to stackSize-2 or stackSize-3 +/datum/sortInstance/proc/mergeAt(i) + //ASSERT(runBases.len >= 2) + //ASSERT(i >= 1) + //ASSERT(i == runBases.len - 1 || i == runBases.len - 2) + + var/base1 = runBases[i] + var/base2 = runBases[i+1] + var/len1 = runLens[i] + var/len2 = runLens[i+1] + + //ASSERT(len1 > 0 && len2 > 0) + //ASSERT(base1 + len1 == base2) + + //Record the legth of the combined runs. If i is the 3rd last run now, also slide over the last run + //(which isn't involved in this merge). The current run (i+1) goes away in any case. + runLens[i] += runLens[i+1] + runLens.Cut(i+1, i+2) + runBases.Cut(i+1, i+2) + + + //Find where the first element of run2 goes in run1. + //Prior elements in run1 can be ignored (because they're already in place) + var/k = gallopRight(fetchElement(L,base2), base1, len1, 0) + //ASSERT(k >= 0) + base1 += k + len1 -= k + if(len1 == 0) + return + + //Find where the last element of run1 goes in run2. + //Subsequent elements in run2 can be ignored (because they're already in place) + len2 = gallopLeft(fetchElement(L,base1 + len1 - 1), base2, len2, len2-1) + //ASSERT(len2 >= 0) + if(len2 == 0) + return + + //Merge remaining runs, using tmp array with min(len1, len2) elements + if(len1 <= len2) + mergeLo(base1, len1, base2, len2) + else + mergeHi(base1, len1, base2, len2) + + +/* + Locates the position to insert key within the specified sorted range + If the range contains elements equal to key, this will return the index of the LEFTMOST of those elements + + key the element to be inserted into the sorted range + base the index of the first element of the sorted range + len the length of the sorted range, must be greater than 0 + hint the offset from base at which to begin the search, such that 0 <= hint < len; i.e. base <= hint < base+hint + + Returns the index at which to insert element 'key' +*/ +/datum/sortInstance/proc/gallopLeft(key, base, len, hint) + //ASSERT(len > 0 && hint >= 0 && hint < len) + + var/lastOffset = 0 + var/offset = 1 + if(call(cmp)(key, fetchElement(L,base+hint)) > 0) + var/maxOffset = len - hint + while(offset < maxOffset && call(cmp)(key, fetchElement(L,base+hint+offset)) > 0) + lastOffset = offset + offset = (offset << 1) + 1 + + if(offset > maxOffset) + offset = maxOffset + + lastOffset += hint + offset += hint + + else + var/maxOffset = hint + 1 + while(offset < maxOffset && call(cmp)(key, fetchElement(L,base+hint-offset)) <= 0) + lastOffset = offset + offset = (offset << 1) + 1 + + if(offset > maxOffset) + offset = maxOffset + + var/temp = lastOffset + lastOffset = hint - offset + offset = hint - temp + + //ASSERT(-1 <= lastOffset && lastOffset < offset && offset <= len) + + //Now L[base+lastOffset] < key <= L[base+offset], so key belongs somewhere to the right of lastOffset but no farther than + //offset. Do a binary search with invariant L[base+lastOffset-1] < key <= L[base+offset] + ++lastOffset + while(lastOffset < offset) + var/m = lastOffset + ((offset - lastOffset) >> 1) + + if(call(cmp)(key, fetchElement(L,base+m)) > 0) + lastOffset = m + 1 + else + offset = m + + //ASSERT(lastOffset == offset) + return offset + +/** + * Like gallopLeft, except that if the range contains an element equal to + * key, gallopRight returns the index after the rightmost equal element. + * + * @param key the key whose insertion point to search for + * @param a the array in which to search + * @param base the index of the first element in the range + * @param len the length of the range; must be > 0 + * @param hint the index at which to begin the search, 0 <= hint < n. + * The closer hint is to the result, the faster this method will run. + * @param c the comparator used to order the range, and to search + * @return the int k, 0 <= k <= n such that a[b + k - 1] <= key < a[b + k] + */ +/datum/sortInstance/proc/gallopRight(key, base, len, hint) + //ASSERT(len > 0 && hint >= 0 && hint < len) + + var/offset = 1 + var/lastOffset = 0 + if(call(cmp)(key, fetchElement(L,base+hint)) < 0) //key <= L[base+hint] + var/maxOffset = hint + 1 //therefore we want to insert somewhere in the range [base,base+hint] = [base+,base+(hint+1)) + while(offset < maxOffset && call(cmp)(key, fetchElement(L,base+hint-offset)) < 0) //we are iterating backwards + lastOffset = offset + offset = (offset << 1) + 1 //1 3 7 15 + //if(offset <= 0) //int overflow, not an issue here since we are using floats + // offset = maxOffset + + if(offset > maxOffset) + offset = maxOffset + + var/temp = lastOffset + lastOffset = hint - offset + offset = hint - temp + + else //key > L[base+hint] + var/maxOffset = len - hint //therefore we want to insert somewhere in the range (base+hint,base+len) = [base+hint+1, base+hint+(len-hint)) + while(offset < maxOffset && call(cmp)(key, fetchElement(L,base+hint+offset)) >= 0) + lastOffset = offset + offset = (offset << 1) + 1 + //if(offset <= 0) //int overflow, not an issue here since we are using floats + // offset = maxOffset + + if(offset > maxOffset) + offset = maxOffset + + lastOffset += hint + offset += hint + + //ASSERT(-1 <= lastOffset && lastOffset < offset && offset <= len) + + ++lastOffset + while(lastOffset < offset) + var/m = lastOffset + ((offset - lastOffset) >> 1) + + if(call(cmp)(key, fetchElement(L,base+m)) < 0) //key <= L[base+m] + offset = m + else //key > L[base+m] + lastOffset = m + 1 + + //ASSERT(lastOffset == offset) + + return offset + + +//Merges two adjacent runs in-place in a stable fashion. +//For performance this method should only be called when len1 <= len2! +/datum/sortInstance/proc/mergeLo(base1, len1, base2, len2) + //ASSERT(len1 > 0 && len2 > 0 && base1 + len1 == base2) + + var/cursor1 = base1 + var/cursor2 = base2 + + //degenerate cases + if(len2 == 1) + moveElement(L, cursor2, cursor1) + return + + if(len1 == 1) + moveElement(L, cursor1, cursor2+len2) + return + + + //Move first element of second run + moveElement(L, cursor2++, cursor1++) + --len2 + + outer: + while(1) + var/count1 = 0 //# of times in a row that first run won + var/count2 = 0 // " " " " " " second run won + + //do the straightfoward thin until one run starts winning consistently + + do + //ASSERT(len1 > 1 && len2 > 0) + if(call(cmp)(fetchElement(L,cursor2), fetchElement(L,cursor1)) < 0) + moveElement(L, cursor2++, cursor1++) + --len2 + + ++count2 + count1 = 0 + + if(len2 == 0) + break outer + else + ++cursor1 + + ++count1 + count2 = 0 + + if(--len1 == 1) + break outer + + while((count1 | count2) < minGallop) + + + //one run is winning consistently so galloping may provide huge benifits + //so try galloping, until such time as the run is no longer consistently winning + do + //ASSERT(len1 > 1 && len2 > 0) + + count1 = gallopRight(fetchElement(L,cursor2), cursor1, len1, 0) + if(count1) + cursor1 += count1 + len1 -= count1 + + if(len1 <= 1) + break outer + + moveElement(L, cursor2, cursor1) + ++cursor2 + ++cursor1 + if(--len2 == 0) + break outer + + count2 = gallopLeft(fetchElement(L,cursor1), cursor2, len2, 0) + if(count2) + moveRange(L, cursor2, cursor1, count2) + + cursor2 += count2 + cursor1 += count2 + len2 -= count2 + + if(len2 == 0) + break outer + + ++cursor1 + if(--len1 == 1) + break outer + + --minGallop + + while((count1|count2) > MIN_GALLOP) + + if(minGallop < 0) + minGallop = 0 + minGallop += 2; // Penalize for leaving gallop mode + + + if(len1 == 1) + //ASSERT(len2 > 0) + moveElement(L, cursor1, cursor2+len2) + + //else + //ASSERT(len2 == 0) + //ASSERT(len1 > 1) + + +/datum/sortInstance/proc/mergeHi(base1, len1, base2, len2) + //ASSERT(len1 > 0 && len2 > 0 && base1 + len1 == base2) + + var/cursor1 = base1 + len1 - 1 //start at end of sublists + var/cursor2 = base2 + len2 - 1 + + //degenerate cases + if(len2 == 1) + moveElement(L, base2, base1) + return + + if(len1 == 1) + moveElement(L, base1, cursor2+1) + return + + moveElement(L, cursor1--, cursor2-- + 1) + --len1 + + outer: + while(1) + var/count1 = 0 //# of times in a row that first run won + var/count2 = 0 // " " " " " " second run won + + //do the straightfoward thing until one run starts winning consistently + do + //ASSERT(len1 > 0 && len2 > 1) + if(call(cmp)(fetchElement(L,cursor2), fetchElement(L,cursor1)) < 0) + moveElement(L, cursor1--, cursor2-- + 1) + --len1 + + ++count1 + count2 = 0 + + if(len1 == 0) + break outer + else + --cursor2 + --len2 + + ++count2 + count1 = 0 + + if(len2 == 1) + break outer + while((count1 | count2) < minGallop) + + //one run is winning consistently so galloping may provide huge benifits + //so try galloping, until such time as the run is no longer consistently winning + do + //ASSERT(len1 > 0 && len2 > 1) + + count1 = len1 - gallopRight(fetchElement(L,cursor2), base1, len1, len1-1) //should cursor1 be base1? + if(count1) + cursor1 -= count1 + + moveRange(L, cursor1+1, cursor2+1, count1) //cursor1+1 == cursor2 by definition + + cursor2 -= count1 + len1 -= count1 + + if(len1 == 0) + break outer + + --cursor2 + + if(--len2 == 1) + break outer + + count2 = len2 - gallopLeft(fetchElement(L,cursor1), cursor1+1, len2, len2-1) + if(count2) + cursor2 -= count2 + len2 -= count2 + + if(len2 <= 1) + break outer + + moveElement(L, cursor1--, cursor2-- + 1) + --len1 + + if(len1 == 0) + break outer + + --minGallop + while((count1|count2) > MIN_GALLOP) + + if(minGallop < 0) + minGallop = 0 + minGallop += 2 // Penalize for leaving gallop mode + + if(len2 == 1) + //ASSERT(len1 > 0) + + cursor1 -= len1 + moveRange(L, cursor1+1, cursor2+1, len1) + + //else + //ASSERT(len1 == 0) + //ASSERT(len2 > 0) + + +/datum/sortInstance/proc/mergeSort(start, end) + var/remaining = end - start + + //If array is small, do an insertion sort + if(remaining < MIN_MERGE) + //var/initRunLen = countRunAndMakeAscending(start, end) + binarySort(start, end, start/*+initRunLen*/) + return + + var/minRun = minRunLength(remaining) + + do + var/runLen = (remaining <= minRun) ? remaining : minRun + + binarySort(start, start+runLen, start) + + //add data about run to queue + runBases.Add(start) + runLens.Add(runLen) + + //Advance to find next run + start += runLen + remaining -= runLen + + while(remaining > 0) + + while(runBases.len >= 2) + var/n = runBases.len - 1 + if(n > 1 && runLens[n-1] <= runLens[n] + runLens[n+1]) + if(runLens[n-1] < runLens[n+1]) + --n + mergeAt2(n) + else if(runLens[n] <= runLens[n+1]) + mergeAt2(n) + else + break //Invariant is established + + while(runBases.len >= 2) + var/n = runBases.len - 1 + if(n > 1 && runLens[n-1] < runLens[n+1]) + --n + mergeAt2(n) + + return L + +/datum/sortInstance/proc/mergeAt2(i) + var/cursor1 = runBases[i] + var/cursor2 = runBases[i+1] + + var/end1 = cursor1+runLens[i] + var/end2 = cursor2+runLens[i+1] + + var/val1 = fetchElement(L,cursor1) + var/val2 = fetchElement(L,cursor2) + + while(1) + if(call(cmp)(val1,val2) < 0) + if(++cursor1 >= end1) + break + val1 = fetchElement(L,cursor1) + else + moveElement(L,cursor2,cursor1) + + ++cursor2 + if(++cursor2 >= end2) + break + ++end1 + ++cursor1 + //if(++cursor1 >= end1) + // break + + val2 = fetchElement(L,cursor2) + + + //Record the legth of the combined runs. If i is the 3rd last run now, also slide over the last run + //(which isn't involved in this merge). The current run (i+1) goes away in any case. + runLens[i] += runLens[i+1] + runLens.Cut(i+1, i+2) + runBases.Cut(i+1, i+2) + +#undef MIN_GALLOP +#undef MIN_MERGE + +#undef fetchElement diff --git a/code/_helpers/sorts/comparators.dm b/code/_helpers/sorts/comparators.dm new file mode 100644 index 0000000000..8c1f954a00 --- /dev/null +++ b/code/_helpers/sorts/comparators.dm @@ -0,0 +1,20 @@ +// +// Comparators for use with /datum/sortInstance (or wherever you want) +// They should return negative, zero, or positive numbers for a < b, a == b, and a > b respectively. +// + +// Sorts numeric ascending +/proc/cmp_numeric_asc(a,b) + return a - b + +// Sorts subsystems alphabetically +/proc/cmp_subsystem_display(datum/controller/subsystem/a, datum/controller/subsystem/b) + return sorttext(b.name, a.name) + +// Sorts subsystems by init_order +/proc/cmp_subsystem_init(datum/controller/subsystem/a, datum/controller/subsystem/b) + return b.init_order - a.init_order + +// Sorts subsystems by priority +/proc/cmp_subsystem_priority(datum/controller/subsystem/a, datum/controller/subsystem/b) + return a.priority - b.priority diff --git a/code/_helpers/time.dm b/code/_helpers/time.dm index 991d64e20b..b3989a32a4 100644 --- a/code/_helpers/time.dm +++ b/code/_helpers/time.dm @@ -100,4 +100,24 @@ var/round_start_time = 0 /hook/startup/proc/set_roundstart_hour() roundstart_hour = pick(2,7,12,17) - return 1 \ No newline at end of file + return 1 + +/var/midnight_rollovers = 0 +/var/rollovercheck_last_timeofday = 0 +/proc/update_midnight_rollover() + if (world.timeofday < rollovercheck_last_timeofday) //TIME IS GOING BACKWARDS! + return midnight_rollovers++ + return midnight_rollovers + +//Increases delay as the server gets more overloaded, +//as sleeps aren't cheap and sleeping only to wake up and sleep again is wasteful +#define DELTA_CALC max(((max(world.tick_usage, world.cpu) / 100) * max(Master.sleep_delta,1)), 1) + +/proc/stoplag() + . = 0 + var/i = 1 + do + . += round(i*DELTA_CALC) + sleep(i*world.tick_lag*DELTA_CALC) + i *= 2 + while (world.tick_usage > min(TICK_LIMIT_TO_RUN, CURRENT_TICKLIMIT)) diff --git a/code/_macros.dm b/code/_macros.dm index 463a7da26d..befbc99a54 100644 --- a/code/_macros.dm +++ b/code/_macros.dm @@ -49,5 +49,16 @@ #define RANDOM_BLOOD_TYPE pick(4;"O-", 36;"O+", 3;"A-", 28;"A+", 1;"B-", 20;"B+", 1;"AB-", 5;"AB+") #define to_chat(target, message) target << message +#define to_world(message) world << message +#define to_world_log(message) world.log << message +// TODO - Baystation has this log to crazy places. For now lets just world.log, but maybe look into it later. +#define log_world(message) world.log << message + #define CanInteract(user, state) (CanUseTopic(user, state) == STATUS_INTERACTIVE) + +#define qdel_null_list(x) if(x) { for(var/y in x) { qdel(y) } ; x = null } + +#define qdel_null(x) if(x) { qdel(x) ; x = null } + +#define ARGS_DEBUG log_debug("[__FILE__] - [__LINE__]") ; for(var/arg in args) { log_debug("\t[log_info_line(arg)]") } diff --git a/code/controllers/ProcessScheduler/core/process.dm b/code/controllers/ProcessScheduler/core/process.dm index 323c5f7663..edcb8d703c 100644 --- a/code/controllers/ProcessScheduler/core/process.dm +++ b/code/controllers/ProcessScheduler/core/process.dm @@ -35,7 +35,6 @@ * Config vars */ // Process name - var/name // Process schedule interval // This controls how often the process would run under ideal conditions. diff --git a/code/controllers/Processes/ticker.dm b/code/controllers/Processes/ticker.dm index b7c45ba91a..6fa6d43237 100644 --- a/code/controllers/Processes/ticker.dm +++ b/code/controllers/Processes/ticker.dm @@ -33,3 +33,6 @@ var/global/datum/controller/process/ticker/tickerProcess /datum/controller/process/ticker/proc/getLastTickerTimeDuration() return lastTickerTimeDuration + +/world/proc/has_round_started() + return (ticker && ticker.current_state >= GAME_STATE_PLAYING) diff --git a/code/controllers/configuration.dm b/code/controllers/configuration.dm index 5021453462..64ae30116a 100644 --- a/code/controllers/configuration.dm +++ b/code/controllers/configuration.dm @@ -46,7 +46,8 @@ var/list/gamemode_cache = list() var/continous_rounds = 0 // Gamemodes which end instantly will instead keep on going until the round ends by escape shuttle or nuke. var/allow_Metadata = 0 // Metadata is supported. var/popup_admin_pm = 0 //adminPMs to non-admins show in a pop-up 'reply' window when set to 1. - var/Ticklag = 0.9 + var/fps = 20 + var/tick_limit_mc_init = TICK_LIMIT_MC_INIT_DEFAULT //SSinitialization throttling var/Tickcomp = 0 var/socket_talk = 0 // use socket_talk to communicate with other processes var/list/resource_urls = null @@ -564,7 +565,12 @@ var/list/gamemode_cache = list() irc_bot_export = 1 if("ticklag") - Ticklag = text2num(value) + var/ticklag = text2num(value) + if(ticklag > 0) + fps = 10 / ticklag + + if("tick_limit_mc_init") + tick_limit_mc_init = text2num(value) if("allow_antag_hud") config.antag_hud_allowed = 1 diff --git a/code/controllers/controller.dm b/code/controllers/controller.dm new file mode 100644 index 0000000000..c9d5f1e565 --- /dev/null +++ b/code/controllers/controller.dm @@ -0,0 +1,19 @@ +/datum/controller + var/name + // The object used for the clickable stat() button. + var/obj/effect/statclick/statclick + +/datum/controller/proc/Initialize() + +//cleanup actions +/datum/controller/proc/Shutdown() + +//when we enter dmm_suite.load_map +/datum/controller/proc/StartLoadingMap() + +//when we exit dmm_suite.load_map +/datum/controller/proc/StopLoadingMap() + +/datum/controller/proc/Recover() + +/datum/controller/proc/stat_entry() diff --git a/code/controllers/failsafe.dm b/code/controllers/failsafe.dm new file mode 100644 index 0000000000..98c6a5d89d --- /dev/null +++ b/code/controllers/failsafe.dm @@ -0,0 +1,102 @@ + /** + * Failsafe + * + * Pretty much pokes the MC to make sure it's still alive. + **/ + +var/datum/controller/failsafe/Failsafe + +/datum/controller/failsafe // This thing pretty much just keeps poking the master controller + name = "Failsafe" + + // The length of time to check on the MC (in deciseconds). + // Set to 0 to disable. + var/processing_interval = 20 + // The alert level. For every failed poke, we drop a DEFCON level. Once we hit DEFCON 1, restart the MC. + var/defcon = 5 + //the world.time of the last check, so the mc can restart US if we hang. + // (Real friends look out for *eachother*) + var/lasttick = 0 + + // Track the MC iteration to make sure its still on track. + var/master_iteration = 0 + var/running = TRUE + +/datum/controller/failsafe/New() + // Highlander-style: there can only be one! Kill off the old and replace it with the new. + if(Failsafe != src) + if(istype(Failsafe)) + qdel(Failsafe) + Failsafe = src + Initialize() + +/datum/controller/failsafe/Initialize() + set waitfor = 0 + Failsafe.Loop() + if(!deleted(src)) + qdel(src) //when Loop() returns, we delete ourselves and let the mc recreate us + +/datum/controller/failsafe/Destroy() + running = FALSE + ..() + // return QDEL_HINT_HARDDEL_NOW // TODO - Once we port garbage.dm + +/datum/controller/failsafe/proc/Loop() + while(running) + lasttick = world.time + if(!Master) + // Replace the missing Master! This should never, ever happen. + new /datum/controller/master() + // Only poke it if overrides are not in effect. + if(processing_interval > 0) + if(Master.processing && Master.iteration) + // Check if processing is done yet. + if(Master.iteration == master_iteration) + switch(defcon) + if(4,5) + --defcon + if(3) + to_chat(admins, "Notice: DEFCON [defcon_pretty()]. The Master Controller has not fired in the last [(5-defcon) * processing_interval] ticks.") + --defcon + if(2) + to_chat(admins, "Warning: DEFCON [defcon_pretty()]. The Master Controller has not fired in the last [(5-defcon) * processing_interval] ticks. Automatic restart in [processing_interval] ticks.") + --defcon + if(1) + + to_chat(admins, "Warning: DEFCON [defcon_pretty()]. The Master Controller has still not fired within the last [(5-defcon) * processing_interval] ticks. Killing and restarting...") + --defcon + var/rtn = Recreate_MC() + if(rtn > 0) + defcon = 4 + master_iteration = 0 + to_chat(admins, "MC restarted successfully") + else if(rtn < 0) + log_game("FailSafe: Could not restart MC, runtime encountered. Entering defcon 0") + to_chat(admins, "ERROR: DEFCON [defcon_pretty()]. Could not restart MC, runtime encountered. I will silently keep retrying.") + //if the return number was 0, it just means the mc was restarted too recently, and it just needs some time before we try again + //no need to handle that specially when defcon 0 can handle it + if(0) //DEFCON 0! (mc failed to restart) + var/rtn = Recreate_MC() + if(rtn > 0) + defcon = 4 + master_iteration = 0 + to_chat(admins, "MC restarted successfully") + else + defcon = min(defcon + 1,5) + master_iteration = Master.iteration + if (defcon <= 1) + sleep(processing_interval*2) + else + sleep(processing_interval) + else + defcon = 5 + sleep(initial(processing_interval)) + +/datum/controller/failsafe/proc/defcon_pretty() + return defcon + +/datum/controller/failsafe/stat_entry() + if(!statclick) + statclick = new/obj/effect/statclick/debug(null, "Initializing...", src) + + stat("Failsafe Controller:", statclick.update("Defcon: [defcon_pretty()] (Interval: [Failsafe.processing_interval] | Iteration: [Failsafe.master_iteration])")) diff --git a/code/controllers/master.dm b/code/controllers/master.dm new file mode 100644 index 0000000000..4dcf58d45e --- /dev/null +++ b/code/controllers/master.dm @@ -0,0 +1,519 @@ + /** + * StonedMC + * + * Designed to properly split up a given tick among subsystems + * Note: if you read parts of this code and think "why is it doing it that way" + * Odds are, there is a reason + * + **/ +var/datum/controller/master/Master = new() +var/MC_restart_clear = 0 +var/MC_restart_timeout = 0 +var/MC_restart_count = 0 + + +//current tick limit, assigned by the queue controller before running a subsystem. +//used by check_tick as well so that the procs subsystems call can obey that SS's tick limits +var/CURRENT_TICKLIMIT = TICK_LIMIT_RUNNING + +/datum/controller/master + name = "Master" + + // Are we processing (higher values increase the processing delay by n ticks) + var/processing = 1 + // How many times have we ran + var/iteration = 0 + + // world.time of last fire, for tracking lag outside of the mc + var/last_run + + // List of subsystems to process(). + var/list/subsystems + + // Vars for keeping track of tick drift. + var/init_timeofday + var/init_time + var/tickdrift = 0 + + var/sleep_delta + + var/make_runtime = 0 + + var/initializations_finished_with_no_players_logged_in //I wonder what this could be? + // Has round started? (So we know what subsystems to run) + var/round_started = 0 + + // The type of the last subsystem to be process()'d. + var/last_type_processed + + var/datum/controller/subsystem/queue_head //Start of queue linked list + var/datum/controller/subsystem/queue_tail //End of queue linked list (used for appending to the list) + var/queue_priority_count = 0 //Running total so that we don't have to loop thru the queue each run to split up the tick + var/queue_priority_count_bg = 0 //Same, but for background subsystems + var/map_loading = FALSE //Are we loading in a new map? + +/datum/controller/master/New() + // Highlander-style: there can only be one! Kill off the old and replace it with the new. + subsystems = list() + if (Master != src) + if (istype(Master)) + Recover() + qdel(Master) + else + init_subtypes(/datum/controller/subsystem, subsystems) + Master = src + +/datum/controller/master/Destroy() + ..() + // Tell qdel() to Del() this object. + // return QDEL_HINT_HARDDEL_NOW // TODO - Once we port garbage.dm + +/datum/controller/master/Shutdown() + processing = FALSE + for(var/datum/controller/subsystem/ss in subsystems) + ss.Shutdown() + +// Returns 1 if we created a new mc, 0 if we couldn't due to a recent restart, +// -1 if we encountered a runtime trying to recreate it +/proc/Recreate_MC() + . = -1 //so if we runtime, things know we failed + if (world.time < MC_restart_timeout) + return 0 + if (world.time < MC_restart_clear) + MC_restart_count *= 0.5 + + var/delay = 50 * ++MC_restart_count + MC_restart_timeout = world.time + delay + MC_restart_clear = world.time + (delay * 2) + Master.processing = 0 //stop ticking this one + try + new/datum/controller/master() + catch + return -1 + return 1 + + +/datum/controller/master/Recover() + var/msg = "## DEBUG: [time2text(world.timeofday)] MC restarted. Reports:\n" + for (var/varname in Master.vars) + switch (varname) + if("name", "tag", "bestF", "type", "parent_type", "vars", "statclick") // Built-in junk. + continue + else + var/varval = Master.vars[varname] + if (istype(varval, /datum)) // Check if it has a type var. + var/datum/D = varval + msg += "\t [varname] = [D]([D.type])\n" + else + msg += "\t [varname] = [varval]\n" + log_world(msg) + if (istype(Master.subsystems)) + subsystems = Master.subsystems + StartProcessing(10) + else + to_chat(world, "The Master Controller is having some issues, we will need to re-initialize EVERYTHING") + Initialize(20, TRUE) + + +// Please don't stuff random bullshit here, +// Make a subsystem, give it the SS_NO_FIRE flag, and do your work in it's Initialize() +/datum/controller/master/Initialize(delay, init_sss) + set waitfor = 0 + + if(delay) + sleep(delay) + + if(init_sss) + init_subtypes(/datum/controller/subsystem, subsystems) + + to_chat(world, "Initializing subsystems...") + + // Sort subsystems by init_order, so they initialize in the correct order. + sortTim(subsystems, /proc/cmp_subsystem_init) + + var/start_timeofday = REALTIMEOFDAY + // Initialize subsystems. + CURRENT_TICKLIMIT = config.tick_limit_mc_init + for (var/datum/controller/subsystem/SS in subsystems) + if (SS.flags & SS_NO_INIT) + continue + SS.Initialize(REALTIMEOFDAY) + CHECK_TICK + CURRENT_TICKLIMIT = TICK_LIMIT_RUNNING + var/time = (REALTIMEOFDAY - start_timeofday) / 10 + + var/msg = "Initializations complete within [time] second[time == 1 ? "" : "s"]!" + to_chat(world, "[msg]") + log_world(msg) + + // Sort subsystems by display setting for easy access. + sortTim(subsystems, /proc/cmp_subsystem_display) + // Set world options. + world.sleep_offline = 1 + world.fps = config.fps + var/initialized_tod = REALTIMEOFDAY + sleep(1) + initializations_finished_with_no_players_logged_in = initialized_tod < REALTIMEOFDAY - 10 + // Loop. + Master.StartProcessing(0) + +// Notify the MC that the round has started. +/datum/controller/master/proc/RoundStart() + round_started = 1 + var/timer = world.time + for (var/datum/controller/subsystem/SS in subsystems) + if (SS.flags & SS_FIRE_IN_LOBBY || SS.flags & SS_TICKER) + continue //already firing + // Stagger subsystems. + timer += world.tick_lag * rand(1, 5) + SS.next_fire = timer + +// Starts the mc, and sticks around to restart it if the loop ever ends. +/datum/controller/master/proc/StartProcessing(delay) + set waitfor = 0 + if(delay) + sleep(delay) + var/rtn = Loop() + if (rtn > 0 || processing < 0) + return //this was suppose to happen. + //loop ended, restart the mc + log_game("MC crashed or runtimed, restarting") + message_admins("MC crashed or runtimed, restarting") + var/rtn2 = Recreate_MC() + if (rtn2 <= 0) + log_game("Failed to recreate MC (Error code: [rtn2]), it's up to the failsafe now") + message_admins("Failed to recreate MC (Error code: [rtn2]), it's up to the failsafe now") + Failsafe.defcon = 2 + +// Main loop. +/datum/controller/master/proc/Loop() + . = -1 + //Prep the loop (most of this is because we want MC restarts to reset as much state as we can, and because + // local vars rock + + // Schedule the first run of the Subsystems. + round_started = world.has_round_started() + //all this shit is here so that flag edits can be refreshed by restarting the MC. (and for speed) + var/list/tickersubsystems = list() + var/list/normalsubsystems = list() + var/list/lobbysubsystems = list() + var/timer = world.time + for (var/thing in subsystems) + var/datum/controller/subsystem/SS = thing + if (SS.flags & SS_NO_FIRE) + continue + SS.queued_time = 0 + SS.queue_next = null + SS.queue_prev = null + SS.state = SS_IDLE + if (SS.flags & SS_TICKER) + tickersubsystems += SS + timer += world.tick_lag * rand(1, 5) + SS.next_fire = timer + continue + if (SS.flags & SS_FIRE_IN_LOBBY) + lobbysubsystems += SS + timer += world.tick_lag * rand(1, 5) + SS.next_fire = timer + else if (round_started) + timer += world.tick_lag * rand(1, 5) + SS.next_fire = timer + normalsubsystems += SS + + queue_head = null + queue_tail = null + //these sort by lower priorities first to reduce the number of loops needed to add subsequent SS's to the queue + //(higher subsystems will be sooner in the queue, adding them later in the loop means we don't have to loop thru them next queue add) + sortTim(tickersubsystems, /proc/cmp_subsystem_priority) + sortTim(normalsubsystems, /proc/cmp_subsystem_priority) + sortTim(lobbysubsystems, /proc/cmp_subsystem_priority) + + normalsubsystems += tickersubsystems + lobbysubsystems += tickersubsystems + + init_timeofday = REALTIMEOFDAY + init_time = world.time + + iteration = 1 + var/error_level = 0 + var/sleep_delta = 0 + var/list/subsystems_to_check + //the actual loop. + while (1) + tickdrift = max(0, MC_AVERAGE_FAST(tickdrift, (((REALTIMEOFDAY - init_timeofday) - (world.time - init_time)) / world.tick_lag))) + if (processing <= 0) + CURRENT_TICKLIMIT = TICK_LIMIT_RUNNING + sleep(10) + continue + + //if there are mutiple sleeping procs running before us hogging the cpu, we have to run later + // because sleeps are processed in the order received, so longer sleeps are more likely to run first + if (world.tick_usage > TICK_LIMIT_MC) + sleep_delta += 2 + CURRENT_TICKLIMIT = TICK_LIMIT_RUNNING - (TICK_LIMIT_RUNNING * 0.5) + sleep(world.tick_lag * (processing + sleep_delta)) + continue + + sleep_delta = MC_AVERAGE_FAST(sleep_delta, 0) + if (last_run + (world.tick_lag * processing) > world.time) + sleep_delta += 1 + if (world.tick_usage > (TICK_LIMIT_MC*0.5)) + sleep_delta += 1 + + if (make_runtime) + var/datum/controller/subsystem/SS + SS.can_fire = 0 + if (!Failsafe || (Failsafe.processing_interval > 0 && (Failsafe.lasttick+(Failsafe.processing_interval*5)) < world.time)) + new/datum/controller/failsafe() // (re)Start the failsafe. + if (!queue_head || !(iteration % 3)) + if (round_started) + subsystems_to_check = normalsubsystems + else + subsystems_to_check = lobbysubsystems + else + subsystems_to_check = tickersubsystems + if (CheckQueue(subsystems_to_check) <= 0) + if (!SoftReset(tickersubsystems, normalsubsystems, lobbysubsystems)) + log_world("MC: SoftReset() failed, crashing") + return + if (!error_level) + iteration++ + error_level++ + CURRENT_TICKLIMIT = TICK_LIMIT_RUNNING + sleep(10) + continue + + if (queue_head) + if (RunQueue() <= 0) + if (!SoftReset(tickersubsystems, normalsubsystems, lobbysubsystems)) + log_world("MC: SoftReset() failed, crashing") + return + if (!error_level) + iteration++ + error_level++ + CURRENT_TICKLIMIT = TICK_LIMIT_RUNNING + sleep(10) + continue + error_level-- + if (!queue_head) //reset the counts if the queue is empty, in the off chance they get out of sync + queue_priority_count = 0 + queue_priority_count_bg = 0 + + iteration++ + last_run = world.time + src.sleep_delta = MC_AVERAGE_FAST(src.sleep_delta, sleep_delta) + CURRENT_TICKLIMIT = TICK_LIMIT_RUNNING - (TICK_LIMIT_RUNNING * 0.25) //reserve the tail 1/4 of the next tick for the mc. + sleep(world.tick_lag * (processing + sleep_delta)) + + + + +// This is what decides if something should run. +/datum/controller/master/proc/CheckQueue(list/subsystemstocheck) + . = 0 //so the mc knows if we runtimed + + //we create our variables outside of the loops to save on overhead + var/datum/controller/subsystem/SS + var/SS_flags + + for (var/thing in subsystemstocheck) + if (!thing) + subsystemstocheck -= thing + SS = thing + if (SS.state != SS_IDLE) + continue + if (SS.can_fire <= 0) + continue + if (SS.next_fire > world.time) + continue + SS_flags = SS.flags + if (SS_flags & SS_NO_FIRE) + subsystemstocheck -= SS + continue + if (!(SS_flags & SS_TICKER) && (SS_flags & SS_KEEP_TIMING) && SS.last_fire + (SS.wait * 0.75) > world.time) + continue + SS.enqueue() + . = 1 + + +// Run thru the queue of subsystems to run, running them while balancing out their allocated tick precentage +/datum/controller/master/proc/RunQueue() + . = 0 + var/datum/controller/subsystem/queue_node + var/queue_node_flags + var/queue_node_priority + var/queue_node_paused + + var/current_tick_budget + var/tick_precentage + var/tick_remaining + var/ran = TRUE //this is right + var/ran_non_ticker = FALSE + var/bg_calc //have we swtiched current_tick_budget to background mode yet? + var/tick_usage + + //keep running while we have stuff to run and we haven't gone over a tick + // this is so subsystems paused eariler can use tick time that later subsystems never used + while (ran && queue_head && world.tick_usage < TICK_LIMIT_MC) + ran = FALSE + bg_calc = FALSE + current_tick_budget = queue_priority_count + queue_node = queue_head + while (queue_node) + if (ran && world.tick_usage > TICK_LIMIT_RUNNING) + break + + queue_node_flags = queue_node.flags + queue_node_priority = queue_node.queued_priority + + //super special case, subsystems where we can't make them pause mid way through + //if we can't run them this tick (without going over a tick) + //we bump up their priority and attempt to run them next tick + //(unless we haven't even ran anything this tick, since its unlikely they will ever be able run + // in those cases, so we just let them run) + if (queue_node_flags & SS_NO_TICK_CHECK) + if (queue_node.tick_usage > TICK_LIMIT_RUNNING - world.tick_usage && ran_non_ticker) + queue_node.queued_priority += queue_priority_count * 0.10 + queue_priority_count -= queue_node_priority + queue_priority_count += queue_node.queued_priority + current_tick_budget -= queue_node_priority + queue_node = queue_node.queue_next + continue + + if ((queue_node_flags & SS_BACKGROUND) && !bg_calc) + current_tick_budget = queue_priority_count_bg + bg_calc = TRUE + + tick_remaining = TICK_LIMIT_RUNNING - world.tick_usage + + if (current_tick_budget > 0 && queue_node_priority > 0) + tick_precentage = tick_remaining / (current_tick_budget / queue_node_priority) + else + tick_precentage = tick_remaining + + CURRENT_TICKLIMIT = world.tick_usage + tick_precentage + + if (!(queue_node_flags & SS_TICKER)) + ran_non_ticker = TRUE + ran = TRUE + tick_usage = world.tick_usage + queue_node_paused = (queue_node.state == SS_PAUSED || queue_node.state == SS_PAUSING) + last_type_processed = queue_node + + queue_node.state = SS_RUNNING + + var/state = queue_node.ignite(queue_node_paused) + if (state == SS_RUNNING) + state = SS_IDLE + current_tick_budget -= queue_node_priority + tick_usage = world.tick_usage - tick_usage + + if (tick_usage < 0) + tick_usage = 0 + + queue_node.state = state + + if (state == SS_PAUSED) + queue_node.paused_ticks++ + queue_node.paused_tick_usage += tick_usage + queue_node = queue_node.queue_next + continue + + queue_node.ticks = MC_AVERAGE(queue_node.ticks, queue_node.paused_ticks) + tick_usage += queue_node.paused_tick_usage + + queue_node.tick_usage = MC_AVERAGE_FAST(queue_node.tick_usage, tick_usage) + + queue_node.cost = MC_AVERAGE_FAST(queue_node.cost, TICK_DELTA_TO_MS(tick_usage)) + queue_node.paused_ticks = 0 + queue_node.paused_tick_usage = 0 + + if (queue_node_flags & SS_BACKGROUND) //update our running total + queue_priority_count_bg -= queue_node_priority + else + queue_priority_count -= queue_node_priority + + queue_node.last_fire = world.time + queue_node.times_fired++ + + if (queue_node_flags & SS_TICKER) + queue_node.next_fire = world.time + (world.tick_lag * queue_node.wait) + else if (queue_node_flags & SS_POST_FIRE_TIMING) + queue_node.next_fire = world.time + queue_node.wait + else if (queue_node_flags & SS_KEEP_TIMING) + queue_node.next_fire += queue_node.wait + else + queue_node.next_fire = queue_node.queued_time + queue_node.wait + + queue_node.queued_time = 0 + + //remove from queue + queue_node.dequeue() + + queue_node = queue_node.queue_next + + . = 1 + +//resets the queue, and all subsystems, while filtering out the subsystem lists +// called if any mc's queue procs runtime or exit improperly. +/datum/controller/master/proc/SoftReset(list/ticker_SS, list/normal_SS, list/lobby_SS) + . = 0 + log_world("MC: SoftReset called, resetting MC queue state.") + if (!istype(subsystems) || !istype(ticker_SS) || !istype(normal_SS) || !istype(lobby_SS)) + log_world("MC: SoftReset: Bad list contents: '[subsystems]' '[ticker_SS]' '[normal_SS]' '[lobby_SS]' Crashing!") + return + var/subsystemstocheck = subsystems + ticker_SS + normal_SS + lobby_SS + + for (var/thing in subsystemstocheck) + var/datum/controller/subsystem/SS = thing + if (!SS || !istype(SS)) + //list(SS) is so if a list makes it in the subsystem list, we remove the list, not the contents + subsystems -= list(SS) + ticker_SS -= list(SS) + normal_SS -= list(SS) + lobby_SS -= list(SS) + log_world("MC: SoftReset: Found bad entry in subsystem list, '[SS]'") + continue + if (SS.queue_next && !istype(SS.queue_next)) + log_world("MC: SoftReset: Found bad data in subsystem queue, queue_next = '[SS.queue_next]'") + SS.queue_next = null + if (SS.queue_prev && !istype(SS.queue_prev)) + log_world("MC: SoftReset: Found bad data in subsystem queue, queue_prev = '[SS.queue_prev]'") + SS.queue_prev = null + SS.queued_priority = 0 + SS.queued_time = 0 + SS.state = SS_IDLE + if (queue_head && !istype(queue_head)) + log_world("MC: SoftReset: Found bad data in subsystem queue, queue_head = '[queue_head]'") + queue_head = null + if (queue_tail && !istype(queue_tail)) + log_world("MC: SoftReset: Found bad data in subsystem queue, queue_tail = '[queue_tail]'") + queue_tail = null + queue_priority_count = 0 + queue_priority_count_bg = 0 + log_world("MC: SoftReset: Finished.") + . = 1 + + + +/datum/controller/master/stat_entry() + if(!statclick) + statclick = new/obj/effect/statclick/debug(null, "Initializing...", src) + + stat("Byond:", "(FPS:[world.fps]) (TickCount:[world.time/world.tick_lag]) (TickDrift:[round(Master.tickdrift,1)]([round((Master.tickdrift/(world.time/world.tick_lag))*100,0.1)]%))") + stat("Master Controller:", statclick.update("(TickRate:[Master.processing]) (Iteration:[Master.iteration])")) + +/datum/controller/master/StartLoadingMap() + //disallow more than one map to load at once, multithreading it will just cause race conditions + while(map_loading) + stoplag() + for(var/S in subsystems) + var/datum/controller/subsystem/SS = S + SS.StartLoadingMap() + map_loading = TRUE + +/datum/controller/master/StopLoadingMap(bounds = null) + map_loading = FALSE + for(var/S in subsystems) + var/datum/controller/subsystem/SS = S + SS.StopLoadingMap() diff --git a/code/controllers/master_controller.dm b/code/controllers/master_controller.dm index 62209a1014..3075682438 100644 --- a/code/controllers/master_controller.dm +++ b/code/controllers/master_controller.dm @@ -2,6 +2,10 @@ //It ensures master_controller.process() is never doubled up by killing the MC (hence terminating any of its sleeping procs) //WIP, needs lots of work still +// +// TODO - This will be completely replaced by master.dm in time. +// + var/global/datum/controller/game_controller/master_controller //Set in world.New() var/global/controller_iteration = 0 @@ -31,8 +35,6 @@ datum/controller/game_controller/New() if(!syndicate_code_response) syndicate_code_response = generate_code_phrase() datum/controller/game_controller/proc/setup() - world.tick_lag = config.Ticklag - spawn(20) createRandomZlevel() diff --git a/code/controllers/subsystem.dm b/code/controllers/subsystem.dm new file mode 100644 index 0000000000..ce9558ca4f --- /dev/null +++ b/code/controllers/subsystem.dm @@ -0,0 +1,192 @@ +/datum/controller/subsystem + // Metadata; you should define these. + name = "fire coderbus" //name of the subsystem + var/init_order = 0 //order of initialization. Higher numbers are initialized first, lower numbers later. Can be decimal and negative values. + var/wait = 20 //time to wait (in deciseconds) between each call to fire(). Must be a positive integer. + var/priority = 50 //When mutiple subsystems need to run in the same tick, higher priority subsystems will run first and be given a higher share of the tick before MC_TICK_CHECK triggers a sleep + + var/flags = 0 //see MC.dm in __DEFINES Most flags must be set on world start to take full effect. (You can also restart the mc to force them to process again) + + //set to 0 to prevent fire() calls, mostly for admin use or subsystems that may be resumed later + // use the SS_NO_FIRE flag instead for systems that never fire to keep it from even being added to the list + var/can_fire = TRUE + + // Bookkeeping variables; probably shouldn't mess with these. + var/last_fire = 0 //last world.time we called fire() + var/next_fire = 0 //scheduled world.time for next fire() + var/cost = 0 //average time to execute + var/tick_usage = 0 //average tick usage + var/state = SS_IDLE //tracks the current state of the ss, running, paused, etc. + var/paused_ticks = 0 //ticks this ss is taking to run right now. + var/paused_tick_usage //total tick_usage of all of our runs while pausing this run + var/ticks = 1 //how many ticks does this ss take to run on avg. + var/times_fired = 0 //number of times we have called fire() + var/queued_time = 0 //time we entered the queue, (for timing and priority reasons) + var/queued_priority //we keep a running total to make the math easier, if priority changes mid-fire that would break our running total, so we store it here + //linked list stuff for the queue + var/datum/controller/subsystem/queue_next + var/datum/controller/subsystem/queue_prev + +// Used to initialize the subsystem BEFORE the map has loaded +/datum/controller/subsystem/New() + +//This is used so the mc knows when the subsystem sleeps. do not override. +/datum/controller/subsystem/proc/ignite(resumed = 0) + set waitfor = 0 + . = SS_SLEEPING + fire(resumed) + . = state + if (state == SS_SLEEPING) + state = SS_IDLE + if (state == SS_PAUSING) + var/QT = queued_time + enqueue() + state = SS_PAUSED + queued_time = QT + +//previously, this would have been named 'process()' but that name is used everywhere for different things! +//fire() seems more suitable. This is the procedure that gets called every 'wait' deciseconds. +//Sleeping in here prevents future fires until returned. +/datum/controller/subsystem/proc/fire(resumed = 0) + flags |= SS_NO_FIRE + throw EXCEPTION("Subsystem [src]([type]) does not fire() but did not set the SS_NO_FIRE flag. Please add the SS_NO_FIRE flag to any subsystem that doesn't fire so it doesn't get added to the processing list and waste cpu.") + +/datum/controller/subsystem/Destroy() + dequeue() + can_fire = 0 + flags |= SS_NO_FIRE + Master.subsystems -= src + + +//Queue it to run. +// (we loop thru a linked list until we get to the end or find the right point) +// (this lets us sort our run order correctly without having to re-sort the entire already sorted list) +/datum/controller/subsystem/proc/enqueue() + var/SS_priority = priority + var/SS_flags = flags + var/datum/controller/subsystem/queue_node + var/queue_node_priority + var/queue_node_flags + + for (queue_node = Master.queue_head; queue_node; queue_node = queue_node.queue_next) + queue_node_priority = queue_node.queued_priority + queue_node_flags = queue_node.flags + + if (queue_node_flags & SS_TICKER) + if (!(SS_flags & SS_TICKER)) + continue + if (queue_node_priority < SS_priority) + break + + else if (queue_node_flags & SS_BACKGROUND) + if (!(SS_flags & SS_BACKGROUND)) + break + if (queue_node_priority < SS_priority) + break + + else + if (SS_flags & SS_BACKGROUND) + continue + if (SS_flags & SS_TICKER) + break + if (queue_node_priority < SS_priority) + break + + queued_time = world.time + queued_priority = SS_priority + state = SS_QUEUED + if (SS_flags & SS_BACKGROUND) //update our running total + Master.queue_priority_count_bg += SS_priority + else + Master.queue_priority_count += SS_priority + + queue_next = queue_node + if (!queue_node)//we stopped at the end, add to tail + queue_prev = Master.queue_tail + if (Master.queue_tail) + Master.queue_tail.queue_next = src + else //empty queue, we also need to set the head + Master.queue_head = src + Master.queue_tail = src + + else if (queue_node == Master.queue_head)//insert at start of list + Master.queue_head.queue_prev = src + Master.queue_head = src + queue_prev = null + else + queue_node.queue_prev.queue_next = src + queue_prev = queue_node.queue_prev + queue_node.queue_prev = src + + +/datum/controller/subsystem/proc/dequeue() + if (queue_next) + queue_next.queue_prev = queue_prev + if (queue_prev) + queue_prev.queue_next = queue_next + if (src == Master.queue_tail) + Master.queue_tail = queue_prev + if (src == Master.queue_head) + Master.queue_head = queue_next + queued_time = 0 + if (state == SS_QUEUED) + state = SS_IDLE + + +/datum/controller/subsystem/proc/pause() + . = 1 + switch(state) + if(SS_RUNNING) + state = SS_PAUSED + if(SS_SLEEPING) + state = SS_PAUSING + + +//used to initialize the subsystem AFTER the map has loaded +/datum/controller/subsystem/Initialize(start_timeofday) + var/time = (REALTIMEOFDAY - start_timeofday) / 10 + var/msg = "Initialized [name] subsystem within [time] second[time == 1 ? "" : "s"]!" + to_chat(world, "[msg]") + log_world(msg) + return time + +//hook for printing stats to the "MC" statuspanel for admins to see performance and related stats etc. +/datum/controller/subsystem/stat_entry(msg) + if(!statclick) + statclick = new/obj/effect/statclick/debug(null, "Initializing...", src) + + + + if(can_fire && !(SS_NO_FIRE in flags)) + msg = "[round(cost,1)]ms|[round(tick_usage,1)]%|[round(ticks,0.1)]\t[msg]" + else + msg = "OFFLINE\t[msg]" + + var/title = name + if (can_fire) + title = "\[[state_letter()]][title]" + + stat(title, statclick.update(msg)) + +/datum/controller/subsystem/proc/state_letter() + switch (state) + if (SS_RUNNING) + . = "R" + if (SS_QUEUED) + . = "Q" + if (SS_PAUSED, SS_PAUSING) + . = "P" + if (SS_SLEEPING) + . = "S" + if (SS_IDLE) + . = " " + +//could be used to postpone a costly subsystem for (default one) var/cycles, cycles +//for instance, during cpu intensive operations like explosions +/datum/controller/subsystem/proc/postpone(cycles = 1) + if(next_fire - world.time < wait) + next_fire += (wait*cycles) + +//usually called via datum/controller/subsystem/New() when replacing a subsystem (i.e. due to a recurring crash) +//should attempt to salvage what it can from the old instance of subsystem +/datum/controller/subsystem/Recover() diff --git a/code/controllers/subsystems.dm b/code/controllers/subsystems.dm deleted file mode 100644 index 11025d8d53..0000000000 --- a/code/controllers/subsystems.dm +++ /dev/null @@ -1,46 +0,0 @@ -#define NEW_SS_GLOBAL(varname) if(varname != src){if(istype(varname)){Recover();qdel(varname);}varname = src;} - -/datum/subsystem - //things you will want to define - var/name //name of the subsystem - var/priority = 0 //priority affects order of initialization. Higher priorities are initialized first, lower priorities later. Can be decimal and negative values. - var/wait = 20 //time to wait (in deciseconds) between each call to fire(). Must be a positive integer. - - //things you will probably want to leave alone - var/can_fire = 0 //prevent fire() calls - var/last_fire = 0 //last world.time we called fire() - var/next_fire = 0 //scheduled world.time for next fire() - var/cpu = 0 //cpu-usage stats (somewhat vague) - var/cost = 0 //average time to execute - var/times_fired = 0 //number of times we have called fire() - -//used to initialize the subsystem BEFORE the map has loaded -/datum/subsystem/New() - -//previously, this would have been named 'process()' but that name is used everywhere for different things! -//fire() seems more suitable. This is the procedure that gets called every 'wait' deciseconds. -//fire(), and the procs it calls, SHOULD NOT HAVE ANY SLEEP OPERATIONS in them! -//YE BE WARNED! -/datum/subsystem/proc/fire() - can_fire = 0 - -//used to initialize the subsystem AFTER the map has loaded -/datum/subsystem/proc/Initialize(start_timeofday) - var/time = (world.timeofday - start_timeofday) / 10 - var/msg = "Initialized [name] SubSystem within [time] seconds" - world << "[msg]" - world.log << msg - -//hook for printing stats to the "MC" statuspanel for admins to see performance and related stats etc. -/datum/subsystem/proc/stat_entry() - stat(name, "[round(cost,0.001)]ds\t(CPU:[round(cpu,1)]%)") - -//could be used to postpone a costly subsystem for one cycle -//for instance, during cpu intensive operations like explosions -/datum/subsystem/proc/postpone() - if(next_fire - world.time < wait) - next_fire += wait - -//usually called via datum/subsystem/New() when replacing a subsystem (i.e. due to a recurring crash) -//should attempt to salvage what it can from the old instance of subsystem -/datum/subsystem/proc/Recover() diff --git a/code/controllers/verbs.dm b/code/controllers/verbs.dm index f0d3cc3341..fcf239c88f 100644 --- a/code/controllers/verbs.dm +++ b/code/controllers/verbs.dm @@ -1,19 +1,57 @@ //TODO: rewrite and standardise all controller datums to the datum/controller type //TODO: allow all controllers to be deleted for clean restarts (see WIP master controller stuff) - MC done - lighting done -/client/proc/restart_controller(controller in list("Supply")) + +// Clickable stat() button. +/obj/effect/statclick + name = "Initializing..." + var/target + +/obj/effect/statclick/New(loc, text, target) //Don't port this to Initialize it's too critical + ..() + name = text + src.target = target + +/obj/effect/statclick/proc/update(text) + name = text + return src + +/obj/effect/statclick/debug + var/class + +/obj/effect/statclick/debug/Click() + if(!usr.client.holder || !target) + return + if(!class) + if(istype(target, /datum/controller/subsystem)) + class = "subsystem" + else if(istype(target, /datum/controller)) + class = "controller" + else if(istype(target, /datum)) + class = "datum" + else + class = "unknown" + + usr.client.debug_variables(target) + message_admins("Admin [key_name_admin(usr)] is debugging the [target] [class].") + + +// Debug verbs. +/client/proc/restart_controller(controller in list("Master", "Failsafe")) set category = "Debug" set name = "Restart Controller" set desc = "Restart one of the various periodic loop controllers for the game (be careful!)" - if(!holder) return - usr = null - src = null + if(!holder) + return switch(controller) - if("Supply") - supply_controller.process() - feedback_add_details("admin_verb","RSupply") + if("Master") + Recreate_MC() + feedback_add_details("admin_verb","RMC") + if("Failsafe") + new /datum/controller/failsafe() + feedback_add_details("admin_verb","RFailsafe") + message_admins("Admin [key_name_admin(usr)] has restarted the [controller] controller.") - return /client/proc/debug_antagonist_template(antag_type in all_antag_types) set category = "Debug" diff --git a/polaris.dme b/polaris.dme index 9f075b1d76..a60f9ef6e8 100644 --- a/polaris.dme +++ b/polaris.dme @@ -30,7 +30,9 @@ #include "code\__defines\lighting.dm" #include "code\__defines\machinery.dm" #include "code\__defines\map.dm" +#include "code\__defines\math.dm" #include "code\__defines\math_physics.dm" +#include "code\__defines\MC.dm" #include "code\__defines\misc.dm" #include "code\__defines\mobs.dm" #include "code\__defines\planets.dm" @@ -38,6 +40,7 @@ #include "code\__defines\research.dm" #include "code\__defines\species_languages.dm" #include "code\__defines\targeting.dm" +#include "code\__defines\tick.dm" #include "code\__defines\turfs.dm" #include "code\__defines\unit_tests.dm" #include "code\__defines\xenoarcheaology.dm" @@ -66,6 +69,9 @@ #include "code\_helpers\type2type.dm" #include "code\_helpers\unsorted.dm" #include "code\_helpers\vector.dm" +#include "code\_helpers\sorts\__main.dm" +#include "code\_helpers\sorts\comparators.dm" +#include "code\_helpers\sorts\TimSort.dm" #include "code\_onclick\_defines.dm" #include "code\_onclick\adjacent.dm" #include "code\_onclick\ai.dm" @@ -128,12 +134,15 @@ #include "code\controllers\autotransfer.dm" #include "code\controllers\communications.dm" #include "code\controllers\configuration.dm" +#include "code\controllers\controller.dm" #include "code\controllers\emergency_shuttle_controller.dm" +#include "code\controllers\failsafe.dm" #include "code\controllers\hooks-defs.dm" #include "code\controllers\hooks.dm" +#include "code\controllers\master.dm" #include "code\controllers\master_controller.dm" #include "code\controllers\shuttle_controller.dm" -#include "code\controllers\subsystems.dm" +#include "code\controllers\subsystem.dm" #include "code\controllers\verbs.dm" #include "code\controllers\voting.dm" #include "code\controllers\observer_listener\atom\observer.dm"