Debian Codegraph @

src/utils/statistics.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3
4 /// Rendering statistics
5 ///
6 /// Tracks how long it takes to render a frame, for measuring performance, and for pacing
7 /// the frames.
8
9 #include <stdlib.h>
10
11 #include "log.h"
12 #include "misc.h"
13 #include "statistics.h"
14
15 void rolling_window_destroy(struct rolling_window *rw) {
16 free(rw->elem);
17 rw->elem = NULL;
18 }
19
20 void rolling_window_reset(struct rolling_window *rw) {
21 rw->nelem = 0;
22 rw->elem_head = 0;
23 }
24
25 void rolling_window_init(struct rolling_window *rw, int size) {
26 rw->elem = ccalloc(size, int);
27 rw->window_size = size;
28 rolling_window_reset(rw);
29 }
30
31 int rolling_window_pop_front(struct rolling_window *rw) {
32 assert(rw->nelem > 0);
33 auto ret = rw->elem[rw->elem_head];
34 rw->elem_head = (rw->elem_head + 1) % rw->window_size;
35 rw->nelem--;
36 return ret;
37 }
38
39 bool rolling_window_push_back(struct rolling_window *rw, int val, int *front) {
40 bool full = rw->nelem == rw->window_size;
41 if (full) {
42 *front = rolling_window_pop_front(rw);
43 }
44 rw->elem[(rw->elem_head + rw->nelem) % rw->window_size] = val;
45 rw->nelem++;
46 return full;
47 }
48
49 /// Track the maximum member of a FIFO queue of integers. Integers are pushed to the back
50 /// and popped from the front, the maximum of the current members in the queue is
51 /// tracked.
52 struct rolling_max {
53 /// A priority queue holding the indices of the maximum element candidates.
54 /// The head of the queue is the index of the maximum element.
55 /// The indices in the queue are the "original" indices.
56 ///
57 /// There are only `capacity` elements in `elem`, all previous elements are
58 /// discarded. But the discarded elements' indices are not forgotten, that's why
59 /// it's called the "original" indices.
60 int *p;
61 int p_head, np;
62 /// The maximum number of in flight elements.
63 int capacity;
64 };
65
66 void rolling_max_destroy(struct rolling_max *rm) {
67 free(rm->p);
68 free(rm);
69 }
70
71 struct rolling_max *rolling_max_new(int capacity) {
72 auto rm = ccalloc(1, struct rolling_max);
73 if (!rm) {
74 return NULL;
75 }
76
77 rm->p = ccalloc(capacity, int);
78 if (!rm->p) {
79 goto err;
80 }
81 rm->capacity = capacity;
82
83 return rm;
84
85 err:
86 rolling_max_destroy(rm);
87 return NULL;
88 }
89
90 void rolling_max_reset(struct rolling_max *rm) {
91 rm->p_head = 0;
92 rm->np = 0;
93 }
94
95 #define IDX(n) ((n) % rm->capacity)
96 /// Remove the oldest element in the window. The caller must maintain the list of elements
97 /// themselves, i.e. the behavior is undefined if `front` does not 1match the oldest
98 /// element.
99 void rolling_max_pop_front(struct rolling_max *rm, int front) {
100 if (rm->p[rm->p_head] == front) {
101 // rm->p.pop_front()
102 rm->p_head = IDX(rm->p_head + 1);
103 rm->np--;
104 }
105 }
106
107 void rolling_max_push_back(struct rolling_max *rm, int val) {
108 // Update the priority queue.
109 // Remove all elements smaller than the new element from the queue. Because
110 // the new element will become the maximum element before them, and since they
111 // come before the new element, they will have been popped before the new
112 // element, so they will never become the maximum element.
113 while (rm->np) {
114 int p_tail = IDX(rm->p_head + rm->np - 1);
115 if (rm->p[p_tail] > val) {
116 break;
117 }
118 // rm->p.pop_back()
119 rm->np--;
120 }
121 // Add the new element to the end of the queue.
122 // rm->p.push_back(rm->start_index + rm->nelem - 1)
123 assert(rm->np < rm->capacity);
124 rm->p[IDX(rm->p_head + rm->np)] = val;
125 rm->np++;
126 }
127 #undef IDX
128
129 int rolling_max_get_max(struct rolling_max *rm) {
130 if (rm->np == 0) {
131 return INT_MIN;
132 }
133 return rm->p[rm->p_head];
134 }
135
136 TEST_CASE(rolling_max_test) {
137 #define NELEM 15
138 struct rolling_window queue;
139 rolling_window_init(&queue, 3);
140 auto rm = rolling_max_new(3);
141 const int data[NELEM] = {1, 2, 3, 1, 4, 5, 2, 3, 6, 5, 4, 3, 2, 0, 0};
142 const int expected_max[NELEM] = {1, 2, 3, 3, 4, 5, 5, 5, 6, 6, 6, 5, 4, 3, 2};
143 int max[NELEM] = {0};
144 for (int i = 0; i < NELEM; i++) {
145 int front;
146 bool full = rolling_window_push_back(&queue, data[i], &front);
147 if (full) {
148 rolling_max_pop_front(rm, front);
149 }
150 rolling_max_push_back(rm, data[i]);
151 max[i] = rolling_max_get_max(rm);
152 }
153 rolling_window_destroy(&queue);
154 rolling_max_destroy(rm);
155 TEST_TRUE(memcmp(max, expected_max, sizeof(max)) == 0);
156 #undef NELEM
157 }
158
159 void rolling_quantile_init(struct rolling_quantile *rq, int capacity, int mink, int maxk) {
160 *rq = (struct rolling_quantile){0};
161 rq->tmp_buffer = malloc(sizeof(int) * (size_t)capacity);
162 rq->capacity = capacity;
163 rq->min_target_rank = mink;
164 rq->max_target_rank = maxk;
165 }
166
167 void rolling_quantile_init_with_tolerance(struct rolling_quantile *rq, int window_size,
168 double target, double tolerance) {
169 rolling_quantile_init(rq, window_size, (int)((target - tolerance) * window_size),
170 (int)((target + tolerance) * window_size));
171 }
172
173 void rolling_quantile_reset(struct rolling_quantile *rq) {
174 rq->current_rank = 0;
175 rq->estimate = 0;
176 }
177
178 void rolling_quantile_destroy(struct rolling_quantile *rq) {
179 free(rq->tmp_buffer);
180 }
181
182 int rolling_quantile_estimate(struct rolling_quantile *rq, struct rolling_window *elements) {
183 if (rq->current_rank < rq->min_target_rank || rq->current_rank > rq->max_target_rank) {
184 if (elements->nelem != elements->window_size) {
185 return INT_MIN;
186 }
187 // Re-estimate the quantile.
188 assert(elements->nelem <= rq->capacity);
189 rolling_window_copy_to_array(elements, rq->tmp_buffer);
190 const int target_rank =
191 rq->min_target_rank + (rq->max_target_rank - rq->min_target_rank) / 2;
192 rq->estimate = quickselect(rq->tmp_buffer, elements->nelem, target_rank);
193 rq->current_rank = target_rank;
194 }
195 return rq->estimate;
196 }
197
198 void rolling_quantile_push_back(struct rolling_quantile *rq, int x) {
199 if (x <= rq->estimate) {
200 rq->current_rank++;
201 }
202 }
203
204 void rolling_quantile_pop_front(struct rolling_quantile *rq, int x) {
205 if (x <= rq->estimate) {
206 rq->current_rank--;
207 }
208 }
209
210 void render_statistics_init(struct render_statistics *rs, int window_size) {
211 *rs = (struct render_statistics){0};
212
213 rolling_window_init(&rs->render_times, window_size);
214 rolling_quantile_init_with_tolerance(&rs->render_time_quantile, window_size,
215 /* q */ 0.98, /* tolerance */ 0.01);
216 }
217
218 void render_statistics_add_vblank_time_sample(struct render_statistics *rs, int time_us) {
219 auto sample_sd = sqrt(cumulative_mean_and_var_get_var(&rs->vblank_time_us));
220 auto current_estimate = render_statistics_get_vblank_time(rs);
221 if (current_estimate != 0 && fabs((double)time_us - current_estimate) > sample_sd * 3) {
222 // Deviated from the mean by more than 3 sigma (p < 0.003)
223 log_debug("vblank time outlier: %d %f %f", time_us, rs->vblank_time_us.mean,
224 cumulative_mean_and_var_get_var(&rs->vblank_time_us));
225 // An outlier sample, this could mean things like refresh rate changes, so
226 // we reset the statistics. This could also be benign, but we like to be
227 // cautious.
228 cumulative_mean_and_var_init(&rs->vblank_time_us);
229 }
230
231 if (rs->vblank_time_us.mean != 0) {
232 auto nframes_in_10_seconds =
233 (unsigned int)(10. * 1000000. / rs->vblank_time_us.mean);
234 if (rs->vblank_time_us.n > 20 && rs->vblank_time_us.n > nframes_in_10_seconds) {
235 // We collected 10 seconds worth of samples, we assume the
236 // estimated refresh rate is stable. We will still reset the
237 // statistics if we get an outlier sample though, see above.
238 return;
239 }
240 }
241 cumulative_mean_and_var_update(&rs->vblank_time_us, time_us);
242 }
243
244 void render_statistics_add_render_time_sample(struct render_statistics *rs, int time_us) {
245 int oldest;
246 if (rolling_window_push_back(&rs->render_times, time_us, &oldest)) {
247 rolling_quantile_pop_front(&rs->render_time_quantile, oldest);
248 }
249
250 rolling_quantile_push_back(&rs->render_time_quantile, time_us);
251 }
252
253 /// How much time budget we should give to the backend for rendering, in microseconds.
254 unsigned int render_statistics_get_budget(struct render_statistics *rs) {
255 if (rs->render_times.nelem < rs->render_times.window_size) {
256 // No valid render time estimates yet. Assume maximum budget.
257 return UINT_MAX;
258 }
259
260 // N-th percentile of render times, see render_statistics_init for N.
261 auto render_time_percentile =
262 rolling_quantile_estimate(&rs->render_time_quantile, &rs->render_times);
263 return (unsigned int)render_time_percentile;
264 }
265
266 unsigned int render_statistics_get_vblank_time(struct render_statistics *rs) {
267 if (rs->vblank_time_us.n <= 20 || rs->vblank_time_us.mean < 100) {
268 // Not enough samples yet, or the vblank time is too short to be
269 // meaningful. Assume maximum budget.
270 return 0;
271 }
272 return (unsigned int)rs->vblank_time_us.mean;
273 }
274
275 void render_statistics_reset(struct render_statistics *rs) {
276 rolling_window_reset(&rs->render_times);
277 rolling_quantile_reset(&rs->render_time_quantile);
278 rs->vblank_time_us = (struct cumulative_mean_and_var){0};
279 }
280
281 void render_statistics_destroy(struct render_statistics *rs) {
282 render_statistics_reset(rs);
283 rolling_window_destroy(&rs->render_times);
284 rolling_quantile_destroy(&rs->render_time_quantile);
285 }