]> git.sesse.net Git - nageru/blob - print_latency.cpp
Pre-serialize only the labels for metrics; the ordering constraints did not really...
[nageru] / print_latency.cpp
1 #include "print_latency.h"
2
3 #include "flags.h"
4 #include "metrics.h"
5
6 #include <stdio.h>
7 #include <chrono>
8 #include <string>
9
10 using namespace std;
11 using namespace std::chrono;
12
13 ReceivedTimestamps find_received_timestamp(const vector<RefCountedFrame> &input_frames)
14 {
15         // Find min and max timestamp of all input frames that have a timestamp.
16         steady_clock::time_point min_ts = steady_clock::time_point::max(), max_ts = steady_clock::time_point::min();
17         for (const RefCountedFrame &input_frame : input_frames) {
18                 if (input_frame && input_frame->received_timestamp > steady_clock::time_point::min()) {
19                         min_ts = min(min_ts, input_frame->received_timestamp);
20                         max_ts = max(max_ts, input_frame->received_timestamp);
21                 }
22         }
23         return { min_ts, max_ts };
24 }
25
26 void LatencyHistogram::init(const string &measuring_point)
27 {
28         histogram_lowest_latency_input.init_geometric(0.001, 10.0, 30);
29         histogram_highest_latency_input.init_geometric(0.001, 10.0, 30);
30         histogram_lowest_latency_input_bframe.init_geometric(0.001, 10.0, 30);
31         histogram_highest_latency_input_bframe.init_geometric(0.001, 10.0, 30);
32
33         global_metrics.add("latency_seconds",
34                 {{ "measuring_point", measuring_point }, { "input", "lowest_latency" }, { "frame_type", "i/p" }},
35                 &histogram_lowest_latency_input);
36         global_metrics.add("latency_seconds",
37                 {{ "measuring_point", measuring_point }, { "input", "highest_latency" }, { "frame_type", "i/p" }},
38                 &histogram_highest_latency_input);
39         global_metrics.add("latency_seconds",
40                 {{ "measuring_point", measuring_point }, { "input", "lowest_latency" }, { "frame_type", "b" }},
41                 &histogram_lowest_latency_input_bframe);
42         global_metrics.add("latency_seconds",
43                 {{ "measuring_point", measuring_point }, { "input", "highest_latency" }, { "frame_type", "b" }},
44                 &histogram_highest_latency_input_bframe);
45 }
46
47 void print_latency(const string &header, const ReceivedTimestamps &received_ts, bool is_b_frame, int *frameno, LatencyHistogram *histogram)
48 {
49         if (received_ts.max_ts == steady_clock::time_point::min())
50                 return;
51
52         const steady_clock::time_point now = steady_clock::now();
53         duration<double> lowest_latency = now - received_ts.max_ts;
54         duration<double> highest_latency = now - received_ts.min_ts;
55
56         if (is_b_frame) {
57                 histogram->histogram_lowest_latency_input_bframe.count_event(lowest_latency.count());
58                 histogram->histogram_highest_latency_input_bframe.count_event(highest_latency.count());
59         } else {
60                 histogram->histogram_lowest_latency_input.count_event(lowest_latency.count());
61                 histogram->histogram_highest_latency_input.count_event(highest_latency.count());
62         }
63
64         // 101 is chosen so that it's prime, which is unlikely to get the same frame type every time.
65         if (global_flags.print_video_latency && (++*frameno % 101) == 0) {
66                 printf("%-60s %4.0f ms (lowest-latency input), %4.0f ms (highest-latency input)",
67                         header.c_str(), 1e3 * lowest_latency.count(), 1e3 * highest_latency.count());
68
69                 if (is_b_frame) {
70                         printf("  [on B-frame; potential extra latency]\n");
71                 } else {
72                         printf("\n");
73                 }
74         }
75 }