GRPC Core  9.0.0
bdp_estimator.h
Go to the documentation of this file.
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_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H
20 #define GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H
21 
23 
24 #include <inttypes.h>
25 #include <stdbool.h>
26 #include <stdint.h>
27 
28 #include <grpc/support/log.h>
29 #include <grpc/support/time.h>
30 
33 
35 
36 namespace grpc_core {
37 
38 class BdpEstimator {
39  public:
40  explicit BdpEstimator(const char* name);
42 
43  int64_t EstimateBdp() const { return estimate_; }
44  double EstimateBandwidth() const { return bw_est_; }
45 
46  void AddIncomingBytes(int64_t num_bytes) { accumulator_ += num_bytes; }
47 
48  // Schedule a ping: call in response to receiving a true from
49  // grpc_bdp_estimator_add_incoming_bytes once a ping has been scheduled by a
50  // transport (but not necessarily started)
51  void SchedulePing() {
53  gpr_log(GPR_INFO, "bdp[%s]:sched acc=%" PRId64 " est=%" PRId64, name_,
54  accumulator_, estimate_);
55  }
56  GPR_ASSERT(ping_state_ == PingState::UNSCHEDULED);
57  ping_state_ = PingState::SCHEDULED;
58  accumulator_ = 0;
59  }
60 
61  // Start a ping: call after calling grpc_bdp_estimator_schedule_ping and
62  // once
63  // the ping is on the wire
64  void StartPing() {
66  gpr_log(GPR_INFO, "bdp[%s]:start acc=%" PRId64 " est=%" PRId64, name_,
67  accumulator_, estimate_);
68  }
69  GPR_ASSERT(ping_state_ == PingState::SCHEDULED);
70  ping_state_ = PingState::STARTED;
71  accumulator_ = 0;
72  ping_start_time_ = gpr_now(GPR_CLOCK_MONOTONIC);
73  }
74 
75  // Completes a previously started ping, returns when to schedule the next one
77 
78  private:
79  enum class PingState { UNSCHEDULED, SCHEDULED, STARTED };
80 
81  PingState ping_state_;
82  int64_t accumulator_;
83  int64_t estimate_;
84  // when was the current ping started?
85  gpr_timespec ping_start_time_;
86  int inter_ping_delay_;
87  int stable_estimate_count_;
88  double bw_est_;
89  const char* name_;
90 };
91 
92 } // namespace grpc_core
93 
94 #endif /* GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H */
grpc_core::TraceFlag grpc_bdp_estimator_trace
Definition: bdp_estimator.h:38
BdpEstimator(const char *name)
Definition: bdp_estimator.cc:32
double EstimateBandwidth() const
Definition: bdp_estimator.h:44
~BdpEstimator()
Definition: bdp_estimator.h:41
int64_t EstimateBdp() const
Definition: bdp_estimator.h:43
void SchedulePing()
Definition: bdp_estimator.h:51
grpc_millis CompletePing()
Definition: bdp_estimator.cc:42
void StartPing()
Definition: bdp_estimator.h:64
void AddIncomingBytes(int64_t num_bytes)
Definition: bdp_estimator.h:46
Definition: trace.h:61
int64_t grpc_millis
Definition: exec_ctx.h:35
@ GPR_CLOCK_MONOTONIC
Monotonic clock.
Definition: gpr_types.h:33
#define GPR_ASSERT(x)
abort() the process if x is zero, having written a line to the log.
Definition: log.h:94
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
Log a message.
#define GPR_INFO
Definition: log.h:56
Round Robin Policy.
Definition: backend_metric.cc:24
Analogous to struct timespec.
Definition: gpr_types.h:47
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
Return the current time measured from the given clocks epoch.
#define GRPC_TRACE_FLAG_ENABLED(f)
Definition: trace.h:112