38 static constexpr int max_percentage = 50;
58 std::int64_t second {-1};
59 std::uint64_t sent {0};
60 std::uint64_t missing {0};
62 std::int64_t covered_ms {0};
78 int percentage(std::uint64_t sent_total, std::uint64_t missing_total, std::uint64_t reported_total, std::int64_t now_ms,
int configured,
int ceiling) {
79 if (reported_total == 0) {
91 const auto second = now_ms / 1000;
93 if (last_second < 0) {
95 last_sent = sent_total;
96 last_missing = missing_total;
98 last_rollover_ms = now_ms;
104 if (second != last_second) {
105 const auto delta_sent = sent_total - last_sent;
106 const auto delta_missing = missing_total - last_missing;
107 const auto covered_ms = now_ms - last_rollover_ms;
108 last_sent = sent_total;
109 last_missing = missing_total;
110 last_rollover_ms = now_ms;
115 auto &bucket = buckets[
static_cast<std::size_t
>(second %
window_seconds)];
116 bucket.second = second;
117 bucket.sent = delta_sent;
118 bucket.missing = delta_missing;
119 bucket.covered_ms = covered_ms;
120 last_second = second;
139 if (last_lowered_ms < 0) {
142 last_lowered_ms = now_ms;
145 if (*required > current) {
149 last_lowered_ms = now_ms;
154 last_lowered_ms = now_ms;
161 for (
const auto &bucket : buckets) {
172 for (
const auto &bucket : buckets) {
173 if (!
trusted(bucket, second)) {
176 const auto ratio =
static_cast<double>(bucket.missing) /
static_cast<double>(bucket.sent);
177 worst = std::max(worst, ratio);
189 static bool trusted(
const bucket_t &bucket, std::int64_t second) {
196 return bucket.second <= second && second - bucket.second <
window_seconds;
205 const auto bound = std::clamp(ceiling,
min_percentage, max_percentage);
210 for (
auto parity = 1; parity <= bound; ++parity) {
213 const auto percentage =
static_cast<int>(std::ceil(100.0 * parity / n));
222 const auto total = data + parity;
223 const auto p = std::clamp(loss, 0.0, 1.0);
226 for (
auto lost = parity + 1; lost <= total; ++lost) {
227 tail += binomial(total, lost) * std::pow(p, lost) * std::pow(1 - p, total - lost);
233 static double binomial(
int n,
int k) {
235 for (
auto i = 1; i <= k; ++i) {
236 result = result * (n - k + i) / i;
241 std::array<bucket_t, window_seconds> buckets {};
242 std::uint64_t last_sent {0};
243 std::uint64_t last_missing {0};
244 std::int64_t last_second {-1};
245 std::int64_t last_rollover_ms {0};
247 std::int64_t last_lowered_ms {-1};
249 std::optional<int> required;
static constexpr std::uint64_t min_packets_per_bucket
A second needs at least this many sent data packets before its ratio is trusted.
bool has_sample(std::int64_t second) const
True once some second in the window carried enough packets to judge.
static constexpr std::int64_t min_coverage_ms
static bool trusted(const bucket_t &bucket, std::int64_t second)
static constexpr int min_percentage
Never ask for less than this, and never more than the configured ceiling.
double worst_loss(std::int64_t second) const
The worst packet-loss ratio in the window, 0 when no bucket is trustworthy.
int percentage(std::uint64_t sent_total, std::uint64_t missing_total, std::uint64_t reported_total, std::int64_t now_ms, int configured, int ceiling)
static constexpr int window_seconds
Seconds of history the estimate looks at.
static constexpr int representative_data_shards
Data shards assumed per block when turning a target into a percentage.
static constexpr std::int64_t lower_cooldown_ms
How long a lower estimate must hold before it is applied.
static double unrecoverable_chance(int data, int parity, double loss)
P(more than parity packets of an (n + parity) block are lost).
static constexpr std::int64_t bucket_span_ms
A bucket must cover this much time before its ratio is trusted.
static constexpr double target_unrecoverable
Target chance that any one FEC block cannot be rebuilt.
static int percentage_for(double loss, int ceiling)