| 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 |
|
| 22 |
#include <grpc/support/port_platform.h> |
| 23 |
|
| 24 |
#include <inttypes.h> |
| 25 |
|
| 26 |
#include <grpc/impl/codegen/gpr_types.h> |
| 27 |
#include <grpc/support/log.h> |
| 28 |
#include <grpc/support/time.h> |
| 29 |
|
| 30 |
#include "src/core/lib/debug/trace.h" |
| 31 |
#include "src/core/lib/gprpp/time.h" |
| 32 |
|
| 33 |
extern grpc_core::TraceFlag grpc_bdp_estimator_trace; |
| 34 |
|
| 35 |
namespace grpc_core { |
| 36 |
|
| 37 |
class BdpEstimator { |
| 38 |
public: |
| 39 |
explicit BdpEstimator(const char* name); |
| 40 |
~BdpEstimator() {} |
| 41 |
|
| 42 |
int64_t EstimateBdp() const { return estimate_; } |
| 43 |
double EstimateBandwidth() const { return bw_est_; } |
| 44 |
|
| 45 |
void AddIncomingBytes(int64_t num_bytes) { accumulator_ += num_bytes; } |
| 46 |
|
| 47 |
// Schedule a ping: call in response to receiving a true from |
| 48 |
// grpc_bdp_estimator_add_incoming_bytes once a ping has been scheduled by a |
| 49 |
// transport (but not necessarily started) |
| 50 |
void SchedulePing() { |
| 51 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_bdp_estimator_trace)) { |
| 52 |
gpr_log(GPR_INFO, "bdp[%s]:sched acc=%" PRId64 " est=%" PRId64, name_, |
| 53 |
accumulator_, estimate_); |
| 54 |
} |
| 55 |
GPR_ASSERT(ping_state_ == PingState::UNSCHEDULED); |
| 56 |
ping_state_ = PingState::SCHEDULED; |
| 57 |
accumulator_ = 0; |
| 58 |
} |
| 59 |
|
| 60 |
// Start a ping: call after calling grpc_bdp_estimator_schedule_ping and |
| 61 |
// once |
| 62 |
// the ping is on the wire |
| 63 |
void StartPing() { |
| 64 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_bdp_estimator_trace)) { |
| 65 |
gpr_log(GPR_INFO, "bdp[%s]:start acc=%" PRId64 " est=%" PRId64, name_, |
| 66 |
accumulator_, estimate_); |
| 67 |
} |
| 68 |
GPR_ASSERT(ping_state_ == PingState::SCHEDULED); |
| 69 |
ping_state_ = PingState::STARTED; |
| 70 |
ping_start_time_ = gpr_now(GPR_CLOCK_MONOTONIC); |
| 71 |
} |
| 72 |
|
| 73 |
// Completes a previously started ping, returns when to schedule the next one |
| 74 |
Timestamp CompletePing(); |
| 75 |
|
| 76 |
int64_t accumulator() { return accumulator_; } |
| 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 |
Duration 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 */ |