• 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 #ifndef GRPC_SRC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H
20 #define GRPC_SRC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H
21 
22 #include <grpc/support/port_platform.h>
23 #include <grpc/support/time.h>
24 #include <inttypes.h>
25 
26 #include <string>
27 
28 #include "absl/log/check.h"
29 #include "absl/log/log.h"
30 #include "absl/strings/string_view.h"
31 #include "src/core/lib/debug/trace.h"
32 #include "src/core/util/time.h"
33 
34 namespace grpc_core {
35 
36 class BdpEstimator {
37  public:
38   explicit BdpEstimator(absl::string_view name);
~BdpEstimator()39   ~BdpEstimator() {}
40 
EstimateBdp()41   int64_t EstimateBdp() const { return estimate_; }
EstimateBandwidth()42   double EstimateBandwidth() const { return bw_est_; }
43 
AddIncomingBytes(int64_t num_bytes)44   void AddIncomingBytes(int64_t num_bytes) { accumulator_ += num_bytes; }
45 
46   // Schedule a ping: call in response to receiving a true from
47   // grpc_bdp_estimator_add_incoming_bytes once a ping has been scheduled by a
48   // transport (but not necessarily started)
SchedulePing()49   void SchedulePing() {
50     GRPC_TRACE_LOG(bdp_estimator, INFO)
51         << "bdp[" << name_ << "]:sched acc=" << accumulator_
52         << " est=" << estimate_;
53     CHECK(ping_state_ == PingState::UNSCHEDULED);
54     ping_state_ = PingState::SCHEDULED;
55     accumulator_ = 0;
56   }
57 
58   // Start a ping: call after calling grpc_bdp_estimator_schedule_ping and
59   // once
60   // the ping is on the wire
StartPing()61   void StartPing() {
62     GRPC_TRACE_LOG(bdp_estimator, INFO)
63         << "bdp[" << name_ << "]:start acc=" << accumulator_
64         << " est=" << estimate_;
65     CHECK(ping_state_ == PingState::SCHEDULED);
66     ping_state_ = PingState::STARTED;
67     ping_start_time_ = gpr_now(GPR_CLOCK_MONOTONIC);
68   }
69 
70   // Completes a previously started ping, returns when to schedule the next one
71   Timestamp CompletePing();
72 
accumulator()73   int64_t accumulator() const { return accumulator_; }
74 
75  private:
76   enum class PingState { UNSCHEDULED, SCHEDULED, STARTED };
77 
78   int64_t accumulator_;
79   int64_t estimate_;
80   // when was the current ping started?
81   gpr_timespec ping_start_time_;
82   Duration inter_ping_delay_;
83   int stable_estimate_count_;
84   PingState ping_state_;
85   double bw_est_;
86   absl::string_view name_;
87 };
88 
89 }  // namespace grpc_core
90 
91 #endif  // GRPC_SRC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H
92