Debian Codegraph @

src/utils/statistics.h

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
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 /// Copy the contents of the rolling window to an array. The array is assumed to
27 /// have enough space to hold the contents of the rolling window.
28 static inline void attr_unused rolling_window_copy_to_array(struct rolling_window *rw,
29 int *arr) {
30 // The length from head to the end of the array
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 /// Estimate the mean and variance of random variable X using Welford's online
51 /// algorithm.
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 // We have too many elements, let's keep the mean and variance.
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 /// A naive quantile estimator.
84 ///
85 /// Estimates the N-th percentile of a random variable X in a sliding window.
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 /// Rolling window of rendering times (in us) and the tiers they belong to.
105 /// We keep track of the tiers because the vblank time estimate can change over
106 /// time.
107 struct rolling_window render_times;
108 /// Estimate the 95-th percentile of rendering times
109 struct rolling_quantile render_time_quantile;
110 /// Time between each vblanks
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 /// How much time budget we should give to the backend for rendering, in microseconds.
122 unsigned int render_statistics_get_budget(struct render_statistics *rs);
123
124 /// Return the measured vblank interval in microseconds. Returns 0 if not enough
125 /// samples have been collected yet.
126 unsigned int render_statistics_get_vblank_time(struct render_statistics *rs);