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