Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2016 gRPC authors. |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #ifndef GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H |
| 20 | #define GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H |
| 21 | |
Craig Tiller | 9fc49c9 | 2017-10-12 15:53:36 -0700 | [diff] [blame^] | 22 | #include <inttypes.h> |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 23 | #include <stdbool.h> |
| 24 | #include <stdint.h> |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 25 | |
| 26 | #include <grpc/support/log.h> |
| 27 | #include <grpc/support/time.h> |
| 28 | |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 29 | #include "src/core/lib/debug/trace.h" |
Craig Tiller | 43c1b5f | 2017-10-02 14:42:49 -0700 | [diff] [blame] | 30 | #include "src/core/lib/iomgr/exec_ctx.h" |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 31 | |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 32 | extern grpc_tracer_flag grpc_bdp_estimator_trace; |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame] | 33 | |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 34 | namespace grpc_core { |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 35 | |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 36 | class BdpEstimator { |
| 37 | public: |
| 38 | explicit BdpEstimator(const char *name); |
Craig Tiller | 3be5e1e | 2017-10-08 21:48:13 -0700 | [diff] [blame] | 39 | ~BdpEstimator() {} |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 40 | |
| 41 | // Returns true if a reasonable estimate could be obtained |
Craig Tiller | 0cdfb87 | 2017-10-08 21:19:38 -0700 | [diff] [blame] | 42 | bool EstimateBdp(int64_t *estimate_out) const { |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 43 | *estimate_out = estimate_; |
| 44 | return true; |
| 45 | } |
Craig Tiller | 0cdfb87 | 2017-10-08 21:19:38 -0700 | [diff] [blame] | 46 | bool EstimateBandwidth(double *bw_out) const { |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 47 | *bw_out = bw_est_; |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | void AddIncomingBytes(int64_t num_bytes) { accumulator_ += num_bytes; } |
| 52 | |
| 53 | // Returns true if the user should schedule a ping |
Craig Tiller | 0cdfb87 | 2017-10-08 21:19:38 -0700 | [diff] [blame] | 54 | bool NeedPing(grpc_exec_ctx *exec_ctx) const { |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 55 | switch (ping_state_) { |
| 56 | case PingState::UNSCHEDULED: |
| 57 | return grpc_exec_ctx_now(exec_ctx) >= next_ping_scheduled_; |
| 58 | case PingState::SCHEDULED: |
| 59 | case PingState::STARTED: |
| 60 | return false; |
| 61 | } |
| 62 | GPR_UNREACHABLE_CODE(return false); |
| 63 | } |
| 64 | |
| 65 | // Schedule a ping: call in response to receiving a true from |
| 66 | // grpc_bdp_estimator_add_incoming_bytes once a ping has been scheduled by a |
| 67 | // transport (but not necessarily started) |
| 68 | void SchedulePing() { |
| 69 | if (GRPC_TRACER_ON(grpc_bdp_estimator_trace)) { |
| 70 | gpr_log(GPR_DEBUG, "bdp[%s]:sched acc=%" PRId64 " est=%" PRId64, name_, |
| 71 | accumulator_, estimate_); |
| 72 | } |
| 73 | GPR_ASSERT(ping_state_ == PingState::UNSCHEDULED); |
| 74 | ping_state_ = PingState::SCHEDULED; |
| 75 | accumulator_ = 0; |
| 76 | } |
| 77 | |
| 78 | // Start a ping: call after calling grpc_bdp_estimator_schedule_ping and |
| 79 | // once |
| 80 | // the ping is on the wire |
| 81 | void StartPing() { |
| 82 | if (GRPC_TRACER_ON(grpc_bdp_estimator_trace)) { |
| 83 | gpr_log(GPR_DEBUG, "bdp[%s]:start acc=%" PRId64 " est=%" PRId64, name_, |
| 84 | accumulator_, estimate_); |
| 85 | } |
| 86 | GPR_ASSERT(ping_state_ == PingState::SCHEDULED); |
| 87 | ping_state_ = PingState::STARTED; |
| 88 | accumulator_ = 0; |
| 89 | ping_start_time_ = gpr_now(GPR_CLOCK_MONOTONIC); |
| 90 | } |
| 91 | |
| 92 | // Completes a previously started ping |
| 93 | void CompletePing(grpc_exec_ctx *exec_ctx); |
| 94 | |
| 95 | private: |
| 96 | enum class PingState { UNSCHEDULED, SCHEDULED, STARTED }; |
| 97 | |
| 98 | PingState ping_state_; |
| 99 | int64_t accumulator_; |
| 100 | int64_t estimate_; |
Craig Tiller | 43c1b5f | 2017-10-02 14:42:49 -0700 | [diff] [blame] | 101 | // when was the current ping started? |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 102 | gpr_timespec ping_start_time_; |
Craig Tiller | 43c1b5f | 2017-10-02 14:42:49 -0700 | [diff] [blame] | 103 | // when should the next ping start? |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 104 | grpc_millis next_ping_scheduled_; |
| 105 | int inter_ping_delay_; |
| 106 | int stable_estimate_count_; |
| 107 | double bw_est_; |
| 108 | const char *name_; |
| 109 | }; |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 110 | |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 111 | } // namespace grpc_core |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 112 | |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 113 | #endif /* GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H */ |