1 //
2 //
3 // Copyright 2016 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/transport/bdp_estimator.h"
22 
23 #include <inttypes.h>
24 #include <stdlib.h>
25 
26 #include <algorithm>
27 
28 grpc_core::TraceFlag grpc_bdp_estimator_trace(false, "bdp_estimator");
29 
30 namespace grpc_core {
31 
BdpEstimator(absl::string_view name)32 BdpEstimator::BdpEstimator(absl::string_view name)
33     : ping_state_(PingState::UNSCHEDULED),
34       accumulator_(0),
35       estimate_(65536),
36       ping_start_time_(gpr_time_0(GPR_CLOCK_MONOTONIC)),
37       inter_ping_delay_(Duration::Milliseconds(100)),  // start at 100ms
38       stable_estimate_count_(0),
39       bw_est_(0),
40       name_(name) {}
41 
CompletePing()42 Timestamp BdpEstimator::CompletePing() {
43   gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC);
44   gpr_timespec dt_ts = gpr_time_sub(now, ping_start_time_);
45   double dt = static_cast<double>(dt_ts.tv_sec) +
46               1e-9 * static_cast<double>(dt_ts.tv_nsec);
47   double bw = dt > 0 ? (static_cast<double>(accumulator_) / dt) : 0;
48   Duration start_inter_ping_delay = inter_ping_delay_;
49   if (GRPC_TRACE_FLAG_ENABLED(grpc_bdp_estimator_trace)) {
50     gpr_log(GPR_INFO,
51             "bdp[%s]:complete acc=%" PRId64 " est=%" PRId64
52             " dt=%lf bw=%lfMbs bw_est=%lfMbs",
53             std::string(name_).c_str(), accumulator_, estimate_, dt,
54             bw / 125000.0, bw_est_ / 125000.0);
55   }
56   GPR_ASSERT(ping_state_ == PingState::STARTED);
57   if (accumulator_ > 2 * estimate_ / 3 && bw > bw_est_) {
58     estimate_ = std::max(accumulator_, estimate_ * 2);
59     bw_est_ = bw;
60     if (GRPC_TRACE_FLAG_ENABLED(grpc_bdp_estimator_trace)) {
61       gpr_log(GPR_INFO, "bdp[%s]: estimate increased to %" PRId64,
62               std::string(name_).c_str(), estimate_);
63     }
64     inter_ping_delay_ /= 2;  // if the ping estimate changes,
65                              // exponentially get faster at probing
66   } else if (inter_ping_delay_ < Duration::Seconds(10)) {
67     stable_estimate_count_++;
68     if (stable_estimate_count_ >= 2) {
69       // if the ping estimate is steady, slowly ramp down the probe time
70       inter_ping_delay_ += Duration::Milliseconds(
71           100 + static_cast<int>(rand() * 100.0 / RAND_MAX));
72     }
73   }
74   if (start_inter_ping_delay != inter_ping_delay_) {
75     stable_estimate_count_ = 0;
76     if (GRPC_TRACE_FLAG_ENABLED(grpc_bdp_estimator_trace)) {
77       gpr_log(GPR_INFO, "bdp[%s]:update_inter_time to %" PRId64 "ms",
78               std::string(name_).c_str(), inter_ping_delay_.millis());
79     }
80   }
81   ping_state_ = PingState::UNSCHEDULED;
82   accumulator_ = 0;
83   return Timestamp::Now() + inter_ping_delay_;
84 }
85 
86 }  // namespace grpc_core
87