]> git.sesse.net Git - nageru/blob - nageru/queue_length_policy.h
Move Jitterhistory / QueueLengthPolicy into a separate header file.
[nageru] / nageru / queue_length_policy.h
1 #ifndef _QUEUE_LENGTH_POLICY_H
2 #define _QUEUE_LENGTH_POLICY_H 1
3
4 #include <atomic>
5 #include <string>
6 #include <utility>
7 #include <vector>
8
9 // A class to estimate the future jitter. Used in QueueLengthPolicy (see below).
10 //
11 // There are many ways to estimate jitter; I've tested a few ones (and also
12 // some algorithms that don't explicitly model jitter) with different
13 // parameters on some real-life data in experiments/queue_drop_policy.cpp.
14 // This is one based on simple order statistics where I've added some margin in
15 // the number of starvation events; I believe that about one every hour would
16 // probably be acceptable, but this one typically goes lower than that, at the
17 // cost of 2–3 ms extra latency. (If the queue is hard-limited to one frame, it's
18 // possible to get ~10 ms further down, but this would mean framedrops every
19 // second or so.) The general strategy is: Take the 99.9-percentile jitter over
20 // last 5000 frames, multiply by two, and that's our worst-case jitter
21 // estimate. The fact that we're not using the max value means that we could
22 // actually even throw away very late frames immediately, which means we only
23 // get one user-visible event instead of seeing something both when the frame
24 // arrives late (duplicate frame) and then again when we drop.
25 class JitterHistory {
26 private:
27         static constexpr size_t history_length = 5000;
28         static constexpr double percentile = 0.999;
29         static constexpr double multiplier = 2.0;
30
31 public:
32         void register_metrics(const std::vector<std::pair<std::string, std::string>> &labels);
33         void unregister_metrics(const std::vector<std::pair<std::string, std::string>> &labels);
34
35         void clear() {
36                 history.clear();
37                 orders.clear();
38         }
39         void frame_arrived(std::chrono::steady_clock::time_point now, int64_t frame_duration, size_t dropped_frames);
40         std::chrono::steady_clock::time_point get_expected_next_frame() const { return expected_timestamp; }
41         double estimate_max_jitter() const;
42
43 private:
44         // A simple O(k) based algorithm for getting the k-th largest or
45         // smallest element from our window; we simply keep the multiset
46         // ordered (insertions and deletions are O(n) as always) and then
47         // iterate from one of the sides. If we had larger values of k,
48         // we could go for a more complicated setup with two sets or heaps
49         // (one increasing and one decreasing) that we keep balanced around
50         // the point, or it is possible to reimplement std::set with
51         // counts in each node. However, since k=5, we don't need this.
52         std::multiset<double> orders;
53         std::deque<std::multiset<double>::iterator> history;
54
55         std::chrono::steady_clock::time_point expected_timestamp = std::chrono::steady_clock::time_point::min();
56
57         // Metrics. There are no direct summaries for jitter, since we already have latency summaries.
58         std::atomic<int64_t> metric_input_underestimated_jitter_frames{0};
59         std::atomic<double> metric_input_estimated_max_jitter_seconds{0.0 / 0.0};
60 };
61
62 // For any card that's not the master (where we pick out the frames as they
63 // come, as fast as we can process), there's going to be a queue. The question
64 // is when we should drop frames from that queue (apart from the obvious
65 // dropping if the 16-frame queue should become full), especially given that
66 // the frame rate could be lower or higher than the master (either subtly or
67 // dramatically). We have two (conflicting) demands:
68 //
69 //   1. We want to avoid starving the queue.
70 //   2. We don't want to add more delay than is needed.
71 //
72 // Our general strategy is to drop as many frames as we can (helping for #2)
73 // that we think is safe for #1 given jitter. To this end, we measure the
74 // deviation from the expected arrival time for all cards, and use that for
75 // continuous jitter estimation.
76 //
77 // We then drop everything from the queue that we're sure we won't need to
78 // serve the output in the time before the next frame arrives. Typically,
79 // this means the queue will contain 0 or 1 frames, although more is also
80 // possible if the jitter is very high.
81 class QueueLengthPolicy {
82 public:
83         QueueLengthPolicy() {}
84
85         void register_metrics(const std::vector<std::pair<std::string, std::string>> &labels);
86         void unregister_metrics(const std::vector<std::pair<std::string, std::string>> &labels);
87
88         // Call after picking out a frame, so 0 means starvation.
89         // Note that the policy has no memory; everything is given in as parameters.
90         void update_policy(std::chrono::steady_clock::time_point now,
91                            std::chrono::steady_clock::time_point expected_next_frame,
92                            int64_t input_frame_duration,
93                            int64_t master_frame_duration,
94                            double max_input_card_jitter_seconds,
95                            double max_master_card_jitter_seconds);
96         unsigned get_safe_queue_length() const { return safe_queue_length; }
97
98 private:
99         unsigned safe_queue_length = 0;  // Can never go below zero.
100
101         // Metrics.
102         std::atomic<int64_t> metric_input_queue_safe_length_frames{1};
103 };
104
105 #endif  // !defined(_QUEUE_LENGTH_POLICY_H)