blob: 6b1ebb5f67e219f5a0260fe3fae465fc8f4e86fb [file] [log] [blame]
Craig Tiller064db442016-10-20 09:34:58 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2016 gRPC authors.
Craig Tiller064db442016-10-20 09:34:58 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Craig Tiller064db442016-10-20 09:34:58 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller064db442016-10-20 09:34:58 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Craig Tiller064db442016-10-20 09:34:58 -070016 *
17 */
18
19#ifndef GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H
20#define GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H
21
22#include <stdbool.h>
23#include <stdint.h>
Craig Tiller92226062017-10-08 21:16:12 -070024
25#include <grpc/support/log.h>
26#include <grpc/support/time.h>
27
Craig Tiller84f75d42017-05-03 13:06:35 -070028#include "src/core/lib/debug/trace.h"
Craig Tiller43c1b5f2017-10-02 14:42:49 -070029#include "src/core/lib/iomgr/exec_ctx.h"
Craig Tiller064db442016-10-20 09:34:58 -070030
Craig Tiller84f75d42017-05-03 13:06:35 -070031extern grpc_tracer_flag grpc_bdp_estimator_trace;
Craig Tillerefbd7c22017-01-27 14:07:44 -080032
Craig Tiller92226062017-10-08 21:16:12 -070033namespace grpc_core {
Craig Tillerc0118b42016-12-29 12:17:57 -080034
Craig Tiller92226062017-10-08 21:16:12 -070035class BdpEstimator {
36 public:
37 explicit BdpEstimator(const char *name);
38 ~BdpEstimator();
39
40 // Returns true if a reasonable estimate could be obtained
41 bool EstimateBdp(int64_t *estimate_out) {
42 *estimate_out = estimate_;
43 return true;
44 }
45 bool EstimateBandwidth(double *bw_out) {
46 *bw_out = bw_est_;
47 return true;
48 }
49
50 void AddIncomingBytes(int64_t num_bytes) { accumulator_ += num_bytes; }
51
52 // Returns true if the user should schedule a ping
53 bool NeedPing(grpc_exec_ctx *exec_ctx) {
54 switch (ping_state_) {
55 case PingState::UNSCHEDULED:
56 return grpc_exec_ctx_now(exec_ctx) >= next_ping_scheduled_;
57 case PingState::SCHEDULED:
58 case PingState::STARTED:
59 return false;
60 }
61 GPR_UNREACHABLE_CODE(return false);
62 }
63
64 // Schedule a ping: call in response to receiving a true from
65 // grpc_bdp_estimator_add_incoming_bytes once a ping has been scheduled by a
66 // transport (but not necessarily started)
67 void SchedulePing() {
68 if (GRPC_TRACER_ON(grpc_bdp_estimator_trace)) {
69 gpr_log(GPR_DEBUG, "bdp[%s]:sched acc=%" PRId64 " est=%" PRId64, name_,
70 accumulator_, estimate_);
71 }
72 GPR_ASSERT(ping_state_ == PingState::UNSCHEDULED);
73 ping_state_ = PingState::SCHEDULED;
74 accumulator_ = 0;
75 }
76
77 // Start a ping: call after calling grpc_bdp_estimator_schedule_ping and
78 // once
79 // the ping is on the wire
80 void StartPing() {
81 if (GRPC_TRACER_ON(grpc_bdp_estimator_trace)) {
82 gpr_log(GPR_DEBUG, "bdp[%s]:start acc=%" PRId64 " est=%" PRId64, name_,
83 accumulator_, estimate_);
84 }
85 GPR_ASSERT(ping_state_ == PingState::SCHEDULED);
86 ping_state_ = PingState::STARTED;
87 accumulator_ = 0;
88 ping_start_time_ = gpr_now(GPR_CLOCK_MONOTONIC);
89 }
90
91 // Completes a previously started ping
92 void CompletePing(grpc_exec_ctx *exec_ctx);
93
94 private:
95 enum class PingState { UNSCHEDULED, SCHEDULED, STARTED };
96
97 PingState ping_state_;
98 int64_t accumulator_;
99 int64_t estimate_;
Craig Tiller43c1b5f2017-10-02 14:42:49 -0700100 // when was the current ping started?
Craig Tiller92226062017-10-08 21:16:12 -0700101 gpr_timespec ping_start_time_;
Craig Tiller43c1b5f2017-10-02 14:42:49 -0700102 // when should the next ping start?
Craig Tiller92226062017-10-08 21:16:12 -0700103 grpc_millis next_ping_scheduled_;
104 int inter_ping_delay_;
105 int stable_estimate_count_;
106 double bw_est_;
107 const char *name_;
108};
Craig Tiller064db442016-10-20 09:34:58 -0700109
Craig Tiller92226062017-10-08 21:16:12 -0700110} // namespace grpc_core
Craig Tiller064db442016-10-20 09:34:58 -0700111
Craig Tiller92226062017-10-08 21:16:12 -0700112#endif /* GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H */