|
1 | | -// |
2 | | -// Defines used for advanced performance profiling of subsystems. |
3 | | -// Currently used only by SSoverlays (2018-02-24 ~Leshana) |
4 | | -// |
5 | 1 | #define STAT_ENTRY_TIME 1 |
6 | 2 | #define STAT_ENTRY_COUNT 2 |
7 | 3 | #define STAT_ENTRY_LENGTH 2 |
8 | 4 |
|
| 5 | + |
9 | 6 | #define STAT_START_STOPWATCH var/STAT_STOP_WATCH = TICK_USAGE |
10 | 7 | #define STAT_STOP_STOPWATCH var/STAT_TIME = TICK_USAGE_TO_MS(STAT_STOP_WATCH) |
11 | 8 | #define STAT_LOG_ENTRY(entrylist, entryname) \ |
12 | 9 | var/list/STAT_ENTRY = entrylist[entryname] || (entrylist[entryname] = new /list(STAT_ENTRY_LENGTH));\ |
13 | 10 | STAT_ENTRY[STAT_ENTRY_TIME] += STAT_TIME;\ |
14 | | - var/STAT_INCR_AMOUNT = min(1, 2**round((STAT_ENTRY[STAT_ENTRY_COUNT] || 0)/SHORT_REAL_LIMIT));\ |
15 | | - if (STAT_INCR_AMOUNT == 1 || prob(100/STAT_INCR_AMOUNT)) {\ |
16 | | - STAT_ENTRY[STAT_ENTRY_COUNT] += STAT_INCR_AMOUNT;\ |
17 | | - };\ |
| 11 | + STAT_ENTRY[STAT_ENTRY_COUNT] += 1; |
| 12 | + |
| 13 | +// Cost tracking macros, to be used in one proc. If you're using this raw you'll want to use global lists |
| 14 | +// If you don't you'll need another way of reading it |
| 15 | +#define INIT_COST(costs, counting) \ |
| 16 | + var/list/_costs = costs; \ |
| 17 | + var/list/_counting = counting; \ |
| 18 | + var/_usage = TICK_USAGE; |
| 19 | + |
| 20 | +// STATIC cost tracking macro. Uses static lists instead of the normal global ones |
| 21 | +// Good for debug stuff, and for running before globals init |
| 22 | +#define INIT_COST_STATIC(...) \ |
| 23 | + var/static/list/hidden_static_list_for_fun1 = list(); \ |
| 24 | + var/static/list/hidden_static_list_for_fun2 = list(); \ |
| 25 | + INIT_COST(hidden_static_list_for_fun1, hidden_static_list_for_fun2) |
| 26 | + |
| 27 | +// Cost tracking macro for global lists, prevents erroring if GLOB has not yet been initialized |
| 28 | +#define INIT_COST_GLOBAL(costs, counting) \ |
| 29 | + INIT_COST_STATIC() \ |
| 30 | + if(GLOB){\ |
| 31 | + costs = hidden_static_list_for_fun1; \ |
| 32 | + counting = hidden_static_list_for_fun2 ; \ |
| 33 | + } \ |
| 34 | + _usage = TICK_USAGE; |
| 35 | + |
| 36 | + |
| 37 | +#define SET_COST(category) \ |
| 38 | + do { \ |
| 39 | + var/_cost = TICK_USAGE; \ |
| 40 | + _costs[category] += TICK_DELTA_TO_MS(_cost - _usage);\ |
| 41 | + _counting[category] += 1; \ |
| 42 | + } while(FALSE); \ |
| 43 | + _usage = TICK_USAGE; |
| 44 | + |
| 45 | +#define SET_COST_LINE(...) SET_COST("[__LINE__]") |
| 46 | + |
| 47 | +/// A quick helper for running the code as a statement and profiling its cost. |
| 48 | +/// For example, `SET_COST_STMT(var/x = do_work())` |
| 49 | +#define SET_COST_STMT(code...) ##code; SET_COST("[__LINE__] - [#code]") |
| 50 | + |
| 51 | +#define EXPORT_STATS_TO_JSON_LATER(filename, costs, counts) EXPORT_STATS_TO_FILE_LATER(filename, costs, counts, stat_tracking_export_to_json_later) |
| 52 | +#define EXPORT_STATS_TO_CSV_LATER(filename, costs, counts) EXPORT_STATS_TO_FILE_LATER(filename, costs, counts, stat_tracking_export_to_csv_later) |
| 53 | + |
| 54 | +#define EXPORT_STATS_TO_FILE_LATER(filename, costs, counts, proc) \ |
| 55 | + do { \ |
| 56 | + var/static/last_export = 0; \ |
| 57 | + /* Need to always run if we haven't yet, since this code can be placed ANYWHERE */ \ |
| 58 | + if (world.time - last_export > 1.1 SECONDS || (last_export == 0)) { \ |
| 59 | + last_export = world.time; \ |
| 60 | + /* spawn() is used here because this is often used to track init times, where timers act oddly. */ \ |
| 61 | + /* I was making timers and even after init times were complete, the timers didn't run :shrug: */ \ |
| 62 | + spawn (1 SECONDS) { \ |
| 63 | + ##proc(filename, costs, counts); \ |
| 64 | + } \ |
| 65 | + } \ |
| 66 | + } while (FALSE); \ |
| 67 | + _usage = TICK_USAGE; |
0 commit comments