| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <limits.h> |
| 7 |
#include <stdbool.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include "compiler.h" |
| 11 |
|
| 12 |
#define NTIERS (3) |
| 13 |
|
| 14 |
struct rolling_window { |
| 15 |
int *elem; |
| 16 |
int elem_head, nelem; |
| 17 |
int window_size; |
| 18 |
}; |
| 19 |
|
| 20 |
void rolling_window_destroy(struct rolling_window *rw); |
| 21 |
void rolling_window_reset(struct rolling_window *rw); |
| 22 |
void rolling_window_init(struct rolling_window *rw, int size); |
| 23 |
int rolling_window_pop_front(struct rolling_window *rw); |
| 24 |
bool rolling_window_push_back(struct rolling_window *rw, int val, int *front); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
static inline void attr_unused rolling_window_copy_to_array(struct rolling_window *rw, |
| 29 |
int *arr) { |
| 30 |
|
| 31 |
auto head_len = (size_t)(rw->window_size - rw->elem_head); |
| 32 |
if (head_len >= (size_t)rw->nelem) { |
| 33 |
memcpy(arr, rw->elem + rw->elem_head, sizeof(int) * (size_t)rw->nelem); |
| 34 |
} else { |
| 35 |
auto tail_len = (size_t)((size_t)rw->nelem - head_len); |
| 36 |
memcpy(arr, rw->elem + rw->elem_head, sizeof(int) * head_len); |
| 37 |
memcpy(arr + head_len, rw->elem, sizeof(int) * tail_len); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
struct rolling_max; |
| 42 |
|
| 43 |
struct rolling_max *rolling_max_new(int capacity); |
| 44 |
void rolling_max_destroy(struct rolling_max *rm); |
| 45 |
void rolling_max_reset(struct rolling_max *rm); |
| 46 |
void rolling_max_pop_front(struct rolling_max *rm, int front); |
| 47 |
void rolling_max_push_back(struct rolling_max *rm, int val); |
| 48 |
int rolling_max_get_max(struct rolling_max *rm); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
struct cumulative_mean_and_var { |
| 53 |
double mean; |
| 54 |
double m2; |
| 55 |
unsigned int n; |
| 56 |
}; |
| 57 |
|
| 58 |
static inline attr_unused void |
| 59 |
cumulative_mean_and_var_init(struct cumulative_mean_and_var *cmv) { |
| 60 |
*cmv = (struct cumulative_mean_and_var){0}; |
| 61 |
} |
| 62 |
|
| 63 |
static inline attr_unused void |
| 64 |
cumulative_mean_and_var_update(struct cumulative_mean_and_var *cmv, double x) { |
| 65 |
if (cmv->n == UINT_MAX) { |
| 66 |
|
| 67 |
return; |
| 68 |
} |
| 69 |
cmv->n++; |
| 70 |
double delta = x - cmv->mean; |
| 71 |
cmv->mean += delta / (double)cmv->n; |
| 72 |
cmv->m2 += delta * (x - cmv->mean); |
| 73 |
} |
| 74 |
|
| 75 |
static inline attr_unused double |
| 76 |
cumulative_mean_and_var_get_var(struct cumulative_mean_and_var *cmv) { |
| 77 |
if (cmv->n < 2) { |
| 78 |
return 0; |
| 79 |
} |
| 80 |
return cmv->m2 / (double)(cmv->n - 1); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
struct rolling_quantile { |
| 87 |
int current_rank; |
| 88 |
int min_target_rank, max_target_rank; |
| 89 |
int estimate; |
| 90 |
int capacity; |
| 91 |
int *tmp_buffer; |
| 92 |
}; |
| 93 |
|
| 94 |
void rolling_quantile_init(struct rolling_quantile *rq, int capacity, int mink, int maxk); |
| 95 |
void rolling_quantile_init_with_tolerance(struct rolling_quantile *rq, int window_size, |
| 96 |
double target, double tolerance); |
| 97 |
void rolling_quantile_reset(struct rolling_quantile *rq); |
| 98 |
void rolling_quantile_destroy(struct rolling_quantile *rq); |
| 99 |
int rolling_quantile_estimate(struct rolling_quantile *rq, struct rolling_window *elements); |
| 100 |
void rolling_quantile_push_back(struct rolling_quantile *rq, int x); |
| 101 |
void rolling_quantile_pop_front(struct rolling_quantile *rq, int x); |
| 102 |
|
| 103 |
struct render_statistics { |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
struct rolling_window render_times; |
| 108 |
|
| 109 |
struct rolling_quantile render_time_quantile; |
| 110 |
|
| 111 |
struct cumulative_mean_and_var vblank_time_us; |
| 112 |
}; |
| 113 |
|
| 114 |
void render_statistics_init(struct render_statistics *rs, int window_size); |
| 115 |
void render_statistics_reset(struct render_statistics *rs); |
| 116 |
void render_statistics_destroy(struct render_statistics *rs); |
| 117 |
|
| 118 |
void render_statistics_add_vblank_time_sample(struct render_statistics *rs, int time_us); |
| 119 |
void render_statistics_add_render_time_sample(struct render_statistics *rs, int time_us); |
| 120 |
|
| 121 |
|
| 122 |
unsigned int render_statistics_get_budget(struct render_statistics *rs); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
unsigned int render_statistics_get_vblank_time(struct render_statistics *rs); |