• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/lib/transport/bdp_estimator.h"
20 
21 #include <grpc/support/port_platform.h>
22 #include <inttypes.h>
23 #include <stdlib.h>
24 
25 #include <algorithm>
26 
27 #include "absl/log/check.h"
28 #include "absl/log/log.h"
29 
30 namespace grpc_core {
31 
BdpEstimator(absl::string_view name)32 BdpEstimator::BdpEstimator(absl::string_view name)
33     : accumulator_(0),
34       estimate_(65536),
35       ping_start_time_(gpr_time_0(GPR_CLOCK_MONOTONIC)),
36       inter_ping_delay_(Duration::Milliseconds(100)),  // start at 100ms
37       stable_estimate_count_(0),
38       ping_state_(PingState::UNSCHEDULED),
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   GRPC_TRACE_LOG(bdp_estimator, INFO)
50       << "bdp[" << name_ << "]:complete acc=" << accumulator_
51       << " est=" << estimate_ << " dt=" << dt << " bw=" << bw / 125000.0
52       << "Mbs bw_est=" << bw_est_ / 125000.0 << "Mbs";
53   CHECK(ping_state_ == PingState::STARTED);
54   if (accumulator_ > 2 * estimate_ / 3 && bw > bw_est_) {
55     estimate_ = std::max(accumulator_, estimate_ * 2);
56     bw_est_ = bw;
57     GRPC_TRACE_LOG(bdp_estimator, INFO)
58         << "bdp[" << name_ << "]: estimate increased to " << estimate_;
59     inter_ping_delay_ /= 2;  // if the ping estimate changes,
60                              // exponentially get faster at probing
61   } else if (inter_ping_delay_ < Duration::Seconds(10)) {
62     stable_estimate_count_++;
63     if (stable_estimate_count_ >= 2) {
64       // if the ping estimate is steady, slowly ramp down the probe time
65       inter_ping_delay_ += Duration::Milliseconds(
66           100 + static_cast<int>(rand() * 100.0 / RAND_MAX));
67     }
68   }
69   if (start_inter_ping_delay != inter_ping_delay_) {
70     stable_estimate_count_ = 0;
71     GRPC_TRACE_LOG(bdp_estimator, INFO)
72         << "bdp[" << name_ << "]:update_inter_time to "
73         << inter_ping_delay_.millis() << "ms";
74   }
75   ping_state_ = PingState::UNSCHEDULED;
76   accumulator_ = 0;
77   return Timestamp::Now() + inter_ping_delay_;
78 }
79 
80 }  // namespace grpc_core
81