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 | |
Yash Tibrewal | f058915 | 2017-10-13 09:55:27 -0700 | [diff] [blame] | 22 | #include <grpc/support/port_platform.h> |
| 23 | |
Craig Tiller | 9fc49c9 | 2017-10-12 15:53:36 -0700 | [diff] [blame] | 24 | #include <inttypes.h> |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 25 | #include <stdbool.h> |
| 26 | #include <stdint.h> |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 27 | |
| 28 | #include <grpc/support/log.h> |
| 29 | #include <grpc/support/time.h> |
| 30 | |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 31 | #include "src/core/lib/debug/trace.h" |
Craig Tiller | 43c1b5f | 2017-10-02 14:42:49 -0700 | [diff] [blame] | 32 | #include "src/core/lib/iomgr/exec_ctx.h" |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 33 | |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 34 | extern grpc_tracer_flag grpc_bdp_estimator_trace; |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame] | 35 | |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 36 | namespace grpc_core; { |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 37 | |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 38 | class BdpEstimator { |
| 39 | public: |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 40 | explicit BdpEstimator(const char* name); |
Craig Tiller | 3be5e1e | 2017-10-08 21:48:13 -0700 | [diff] [blame] | 41 | ~BdpEstimator() {} |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 42 | |
Craig Tiller | 96582b7 | 2017-10-18 12:19:15 -0700 | [diff] [blame] | 43 | int64_t EstimateBdp() const { return estimate_; } |
| 44 | double EstimateBandwidth() const { return bw_est_; } |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 45 | |
| 46 | void AddIncomingBytes(int64_t num_bytes) { accumulator_ += num_bytes; } |
| 47 | |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 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() { |
| 52 | if (GRPC_TRACER_ON(grpc_bdp_estimator_trace)) { |
| 53 | gpr_log(GPR_DEBUG, "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() { |
| 65 | if (GRPC_TRACER_ON(grpc_bdp_estimator_trace)) { |
| 66 | gpr_log(GPR_DEBUG, "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 | |
Craig Tiller | 00c2076 | 2017-10-08 21:50:33 -0700 | [diff] [blame] | 75 | // Completes a previously started ping, returns when to schedule the next one |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 76 | grpc_millis CompletePing(grpc_exec_ctx* exec_ctx); |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 77 | |
| 78 | private: |
| 79 | enum class PingState { UNSCHEDULED, SCHEDULED, STARTED }; |
| 80 | |
| 81 | PingState ping_state_; |
| 82 | int64_t accumulator_; |
| 83 | int64_t estimate_; |
Craig Tiller | 43c1b5f | 2017-10-02 14:42:49 -0700 | [diff] [blame] | 84 | // when was the current ping started? |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 85 | gpr_timespec ping_start_time_; |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 86 | int inter_ping_delay_; |
| 87 | int stable_estimate_count_; |
| 88 | double bw_est_; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 89 | const char* name_; |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 90 | }; |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 91 | |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 92 | } // namespace grpc_core |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 93 | |
Craig Tiller | 9222606 | 2017-10-08 21:16:12 -0700 | [diff] [blame] | 94 | #endif /* GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H */ |