1 /*
2 * Copyright 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10 #include "modules/congestion_controller/goog_cc/test/goog_cc_printer.h"
11
12 #include <math.h>
13
14 #include <utility>
15
16 #include "absl/types/optional.h"
17 #include "modules/congestion_controller/goog_cc/alr_detector.h"
18 #include "modules/congestion_controller/goog_cc/delay_based_bwe.h"
19 #include "modules/congestion_controller/goog_cc/trendline_estimator.h"
20 #include "modules/remote_bitrate_estimator/aimd_rate_control.h"
21 #include "rtc_base/checks.h"
22
23 namespace webrtc {
24 namespace {
WriteTypedValue(RtcEventLogOutput * out,int value)25 void WriteTypedValue(RtcEventLogOutput* out, int value) {
26 LogWriteFormat(out, "%i", value);
27 }
WriteTypedValue(RtcEventLogOutput * out,double value)28 void WriteTypedValue(RtcEventLogOutput* out, double value) {
29 LogWriteFormat(out, "%.6f", value);
30 }
WriteTypedValue(RtcEventLogOutput * out,absl::optional<DataRate> value)31 void WriteTypedValue(RtcEventLogOutput* out, absl::optional<DataRate> value) {
32 LogWriteFormat(out, "%.0f", value ? value->bytes_per_sec<double>() : NAN);
33 }
WriteTypedValue(RtcEventLogOutput * out,absl::optional<DataSize> value)34 void WriteTypedValue(RtcEventLogOutput* out, absl::optional<DataSize> value) {
35 LogWriteFormat(out, "%.0f", value ? value->bytes<double>() : NAN);
36 }
WriteTypedValue(RtcEventLogOutput * out,absl::optional<TimeDelta> value)37 void WriteTypedValue(RtcEventLogOutput* out, absl::optional<TimeDelta> value) {
38 LogWriteFormat(out, "%.3f", value ? value->seconds<double>() : NAN);
39 }
WriteTypedValue(RtcEventLogOutput * out,absl::optional<Timestamp> value)40 void WriteTypedValue(RtcEventLogOutput* out, absl::optional<Timestamp> value) {
41 LogWriteFormat(out, "%.3f", value ? value->seconds<double>() : NAN);
42 }
43
44 template <typename F>
45 class TypedFieldLogger : public FieldLogger {
46 public:
TypedFieldLogger(std::string name,F && getter)47 TypedFieldLogger(std::string name, F&& getter)
48 : name_(std::move(name)), getter_(std::forward<F>(getter)) {}
name() const49 const std::string& name() const override { return name_; }
WriteValue(RtcEventLogOutput * out)50 void WriteValue(RtcEventLogOutput* out) override {
51 WriteTypedValue(out, getter_());
52 }
53
54 private:
55 std::string name_;
56 F getter_;
57 };
58
59 template <typename F>
Log(std::string name,F && getter)60 FieldLogger* Log(std::string name, F&& getter) {
61 return new TypedFieldLogger<F>(std::move(name), std::forward<F>(getter));
62 }
63
64 } // namespace
GoogCcStatePrinter()65 GoogCcStatePrinter::GoogCcStatePrinter() {
66 for (auto* logger : CreateLoggers()) {
67 loggers_.emplace_back(logger);
68 }
69 }
70
CreateLoggers()71 std::deque<FieldLogger*> GoogCcStatePrinter::CreateLoggers() {
72 auto stable_estimate = [this] {
73 return DataRate::KilobitsPerSec(
74 controller_->delay_based_bwe_->rate_control_.link_capacity_
75 .estimate_kbps_.value_or(-INFINITY));
76 };
77 auto rate_control_state = [this] {
78 return static_cast<int>(
79 controller_->delay_based_bwe_->rate_control_.rate_control_state_);
80 };
81 auto trend = [this] {
82 return reinterpret_cast<TrendlineEstimator*>(
83 controller_->delay_based_bwe_->active_delay_detector_);
84 };
85 auto acknowledged_rate = [this] {
86 return controller_->acknowledged_bitrate_estimator_->bitrate();
87 };
88 auto loss_cont = [&] {
89 return &controller_->bandwidth_estimation_
90 ->loss_based_bandwidth_estimation_;
91 };
92 std::deque<FieldLogger*> loggers({
93 Log("time", [=] { return target_.at_time; }),
94 Log("rtt", [=] { return target_.network_estimate.round_trip_time; }),
95 Log("target", [=] { return target_.target_rate; }),
96 Log("stable_target", [=] { return target_.stable_target_rate; }),
97 Log("pacing", [=] { return pacing_.data_rate(); }),
98 Log("padding", [=] { return pacing_.pad_rate(); }),
99 Log("window", [=] { return congestion_window_; }),
100 Log("rate_control_state", [=] { return rate_control_state(); }),
101 Log("stable_estimate", [=] { return stable_estimate(); }),
102 Log("trendline", [=] { return trend()->prev_trend_; }),
103 Log("trendline_modified_offset",
104 [=] { return trend()->prev_modified_trend_; }),
105 Log("trendline_offset_threshold", [=] { return trend()->threshold_; }),
106 Log("acknowledged_rate", [=] { return acknowledged_rate(); }),
107 Log("est_capacity", [=] { return est_.link_capacity; }),
108 Log("est_capacity_dev", [=] { return est_.link_capacity_std_dev; }),
109 Log("est_capacity_min", [=] { return est_.link_capacity_min; }),
110 Log("est_cross_traffic", [=] { return est_.cross_traffic_ratio; }),
111 Log("est_cross_delay", [=] { return est_.cross_delay_rate; }),
112 Log("est_spike_delay", [=] { return est_.spike_delay_rate; }),
113 Log("est_pre_buffer", [=] { return est_.pre_link_buffer_delay; }),
114 Log("est_post_buffer", [=] { return est_.post_link_buffer_delay; }),
115 Log("est_propagation", [=] { return est_.propagation_delay; }),
116 Log("loss_ratio", [=] { return loss_cont()->last_loss_ratio_; }),
117 Log("loss_average", [=] { return loss_cont()->average_loss_; }),
118 Log("loss_average_max", [=] { return loss_cont()->average_loss_max_; }),
119 Log("loss_thres_inc",
120 [=] { return loss_cont()->loss_increase_threshold(); }),
121 Log("loss_thres_dec",
122 [=] { return loss_cont()->loss_decrease_threshold(); }),
123 Log("loss_dec_rate", [=] { return loss_cont()->decreased_bitrate(); }),
124 Log("loss_based_rate", [=] { return loss_cont()->loss_based_bitrate_; }),
125 Log("loss_ack_rate",
126 [=] { return loss_cont()->acknowledged_bitrate_max_; }),
127 Log("data_window", [=] { return controller_->current_data_window_; }),
128 Log("pushback_target",
129 [=] { return controller_->last_pushback_target_rate_; }),
130 });
131 return loggers;
132 }
133 GoogCcStatePrinter::~GoogCcStatePrinter() = default;
134
PrintHeaders(RtcEventLogOutput * log)135 void GoogCcStatePrinter::PrintHeaders(RtcEventLogOutput* log) {
136 int ix = 0;
137 for (const auto& logger : loggers_) {
138 if (ix++)
139 log->Write(" ");
140 log->Write(logger->name());
141 }
142 log->Write("\n");
143 log->Flush();
144 }
145
PrintState(RtcEventLogOutput * log,GoogCcNetworkController * controller,Timestamp at_time)146 void GoogCcStatePrinter::PrintState(RtcEventLogOutput* log,
147 GoogCcNetworkController* controller,
148 Timestamp at_time) {
149 controller_ = controller;
150 auto state_update = controller_->GetNetworkState(at_time);
151 target_ = state_update.target_rate.value();
152 pacing_ = state_update.pacer_config.value();
153 if (state_update.congestion_window)
154 congestion_window_ = *state_update.congestion_window;
155 if (controller_->network_estimator_) {
156 est_ = controller_->network_estimator_->GetCurrentEstimate().value_or(
157 NetworkStateEstimate());
158 }
159
160 int ix = 0;
161 for (const auto& logger : loggers_) {
162 if (ix++)
163 log->Write(" ");
164 logger->WriteValue(log);
165 }
166
167 log->Write("\n");
168 log->Flush();
169 }
170
GoogCcDebugFactory()171 GoogCcDebugFactory::GoogCcDebugFactory()
172 : GoogCcDebugFactory(GoogCcFactoryConfig()) {}
173
GoogCcDebugFactory(GoogCcFactoryConfig config)174 GoogCcDebugFactory::GoogCcDebugFactory(GoogCcFactoryConfig config)
175 : GoogCcNetworkControllerFactory(std::move(config)) {}
176
Create(NetworkControllerConfig config)177 std::unique_ptr<NetworkControllerInterface> GoogCcDebugFactory::Create(
178 NetworkControllerConfig config) {
179 RTC_CHECK(controller_ == nullptr);
180 auto controller = GoogCcNetworkControllerFactory::Create(config);
181 controller_ = static_cast<GoogCcNetworkController*>(controller.get());
182 return controller;
183 }
184
PrintState(const Timestamp at_time)185 void GoogCcDebugFactory::PrintState(const Timestamp at_time) {
186 if (controller_ && log_writer_) {
187 printer_.PrintState(log_writer_.get(), controller_, at_time);
188 }
189 }
190
AttachWriter(std::unique_ptr<RtcEventLogOutput> log_writer)191 void GoogCcDebugFactory::AttachWriter(
192 std::unique_ptr<RtcEventLogOutput> log_writer) {
193 if (log_writer) {
194 log_writer_ = std::move(log_writer);
195 printer_.PrintHeaders(log_writer_.get());
196 }
197 }
198
199 } // namespace webrtc
200