blob: 984ba88ecb71c8c2e03884d3a5ed50ce2df2ef9a [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
Yash Tibrewalf0589152017-10-13 09:55:27 -070022#include <grpc/support/port_platform.h>
23
Craig Tiller9fc49c92017-10-12 15:53:36 -070024#include <inttypes.h>
Craig Tiller064db442016-10-20 09:34:58 -070025#include <stdbool.h>
26#include <stdint.h>
Craig Tiller92226062017-10-08 21:16:12 -070027
28#include <grpc/support/log.h>
29#include <grpc/support/time.h>
30
Craig Tiller84f75d42017-05-03 13:06:35 -070031#include "src/core/lib/debug/trace.h"
Craig Tiller43c1b5f2017-10-02 14:42:49 -070032#include "src/core/lib/iomgr/exec_ctx.h"
Craig Tiller064db442016-10-20 09:34:58 -070033
Craig Tiller84f75d42017-05-03 13:06:35 -070034extern grpc_tracer_flag grpc_bdp_estimator_trace;
Craig Tillerefbd7c22017-01-27 14:07:44 -080035
Craig Tiller4782d922017-11-10 09:53:21 -080036namespace grpc_core; {
Craig Tillerc0118b42016-12-29 12:17:57 -080037
Craig Tiller92226062017-10-08 21:16:12 -070038class BdpEstimator {
39 public:
Craig Tillerbaa14a92017-11-03 09:09:36 -070040 explicit BdpEstimator(const char* name);
Craig Tiller3be5e1e2017-10-08 21:48:13 -070041 ~BdpEstimator() {}
Craig Tiller92226062017-10-08 21:16:12 -070042
Craig Tiller96582b72017-10-18 12:19:15 -070043 int64_t EstimateBdp() const { return estimate_; }
44 double EstimateBandwidth() const { return bw_est_; }
Craig Tiller92226062017-10-08 21:16:12 -070045
46 void AddIncomingBytes(int64_t num_bytes) { accumulator_ += num_bytes; }
47
Craig Tiller92226062017-10-08 21:16:12 -070048 // 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 Tiller00c20762017-10-08 21:50:33 -070075 // Completes a previously started ping, returns when to schedule the next one
Craig Tillerbaa14a92017-11-03 09:09:36 -070076 grpc_millis CompletePing(grpc_exec_ctx* exec_ctx);
Craig Tiller92226062017-10-08 21:16:12 -070077
78 private:
79 enum class PingState { UNSCHEDULED, SCHEDULED, STARTED };
80
81 PingState ping_state_;
82 int64_t accumulator_;
83 int64_t estimate_;
Craig Tiller43c1b5f2017-10-02 14:42:49 -070084 // when was the current ping started?
Craig Tiller92226062017-10-08 21:16:12 -070085 gpr_timespec ping_start_time_;
Craig Tiller92226062017-10-08 21:16:12 -070086 int inter_ping_delay_;
87 int stable_estimate_count_;
88 double bw_est_;
Craig Tillerbaa14a92017-11-03 09:09:36 -070089 const char* name_;
Craig Tiller92226062017-10-08 21:16:12 -070090};
Craig Tiller064db442016-10-20 09:34:58 -070091
Craig Tiller92226062017-10-08 21:16:12 -070092} // namespace grpc_core
Craig Tiller064db442016-10-20 09:34:58 -070093
Craig Tiller92226062017-10-08 21:16:12 -070094#endif /* GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H */