grpc
src
core
lib
transport
bdp_estimator.cc
Go to the documentation of this file.
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
#include <
grpc/support/port_platform.h
>
20
21
#include "
src/core/lib/transport/bdp_estimator.h
"
22
23
#include <inttypes.h>
24
#include <stdlib.h>
25
26
#include <algorithm>
27
28
#include "
src/core/lib/iomgr/exec_ctx.h
"
29
30
grpc_core::TraceFlag
grpc_bdp_estimator_trace
(
false
,
"bdp_estimator"
);
31
32
namespace
grpc_core
{
33
34
BdpEstimator::BdpEstimator
(
const
char
*
name
)
35
: ping_state_(
PingState
::UNSCHEDULED),
36
accumulator_(0),
37
estimate_(65536),
38
ping_start_time_(
gpr_time_0
(
GPR_CLOCK_MONOTONIC
)),
39
inter_ping_delay_(
Duration
::
Milliseconds
(100)),
// start at 100ms
40
stable_estimate_count_(0),
41
bw_est_(0),
42
name_
(
name
) {}
43
44
Timestamp
BdpEstimator::CompletePing
() {
45
gpr_timespec
now
=
gpr_now
(
GPR_CLOCK_MONOTONIC
);
46
gpr_timespec
dt_ts =
gpr_time_sub
(
now
,
ping_start_time_
);
47
double
dt =
static_cast<
double
>
(dt_ts.
tv_sec
) +
48
1e-9 *
static_cast<
double
>
(dt_ts.
tv_nsec
);
49
double
bw = dt > 0 ? (
static_cast<
double
>
(
accumulator_
) / dt) : 0;
50
Duration
start_inter_ping_delay =
inter_ping_delay_
;
51
if
(
GRPC_TRACE_FLAG_ENABLED
(
grpc_bdp_estimator_trace
)) {
52
gpr_log
(
GPR_INFO
,
53
"bdp[%s]:complete acc=%"
PRId64
" est=%"
PRId64
54
" dt=%lf bw=%lfMbs bw_est=%lfMbs"
,
55
name_
,
accumulator_
,
estimate_
, dt, bw / 125000.0,
56
bw_est_
/ 125000.0);
57
}
58
GPR_ASSERT
(
ping_state_
==
PingState::STARTED
);
59
if
(
accumulator_
> 2 *
estimate_
/ 3 && bw >
bw_est_
) {
60
estimate_
=
std::max
(
accumulator_
,
estimate_
* 2);
61
bw_est_
= bw;
62
if
(
GRPC_TRACE_FLAG_ENABLED
(
grpc_bdp_estimator_trace
)) {
63
gpr_log
(
GPR_INFO
,
"bdp[%s]: estimate increased to %"
PRId64,
name_
,
64
estimate_
);
65
}
66
inter_ping_delay_
/= 2;
// if the ping estimate changes,
67
// exponentially get faster at probing
68
}
else
if
(
inter_ping_delay_
<
Duration::Seconds
(10)) {
69
stable_estimate_count_
++;
70
if
(
stable_estimate_count_
>= 2) {
71
// if the ping estimate is steady, slowly ramp down the probe time
72
inter_ping_delay_
+=
Duration::Milliseconds
(
73
100 +
static_cast<
int
>
(rand() * 100.0 / RAND_MAX));
74
}
75
}
76
if
(start_inter_ping_delay !=
inter_ping_delay_
) {
77
stable_estimate_count_
= 0;
78
if
(
GRPC_TRACE_FLAG_ENABLED
(
grpc_bdp_estimator_trace
)) {
79
gpr_log
(
GPR_INFO
,
"bdp[%s]:update_inter_time to %"
PRId64
"ms"
,
name_
,
80
inter_ping_delay_
.
millis
());
81
}
82
}
83
ping_state_
=
PingState::UNSCHEDULED
;
84
accumulator_
= 0;
85
return
ExecCtx::Get
()->
Now
() +
inter_ping_delay_
;
86
}
87
88
}
// namespace grpc_core
gpr_timespec::tv_nsec
int32_t tv_nsec
Definition:
gpr_types.h:52
gpr_timespec::tv_sec
int64_t tv_sec
Definition:
gpr_types.h:51
GPR_INFO
#define GPR_INFO
Definition:
include/grpc/impl/codegen/log.h:56
now
static double now(void)
Definition:
test/core/fling/client.cc:130
grpc_core::BdpEstimator::PingState::STARTED
@ STARTED
grpc_core::BdpEstimator::PingState
PingState
Definition:
bdp_estimator.h:79
gpr_time_0
GPRAPI gpr_timespec gpr_time_0(gpr_clock_type type)
Definition:
src/core/lib/gpr/time.cc:47
grpc_core
Definition:
call_metric_recorder.h:31
grpc_core::Timestamp
Definition:
src/core/lib/gprpp/time.h:62
grpc_core::BdpEstimator::estimate_
int64_t estimate_
Definition:
bdp_estimator.h:83
setup.name
name
Definition:
setup.py:542
grpc_core::BdpEstimator::name_
const char * name_
Definition:
bdp_estimator.h:89
name_
const std::string name_
Definition:
priority.cc:233
GRPC_TRACE_FLAG_ENABLED
#define GRPC_TRACE_FLAG_ENABLED(f)
Definition:
debug/trace.h:114
grpc_core::BdpEstimator::ping_start_time_
gpr_timespec ping_start_time_
Definition:
bdp_estimator.h:85
absl::Milliseconds
constexpr Duration Milliseconds(T n)
Definition:
third_party/abseil-cpp/absl/time/time.h:415
grpc_core::BdpEstimator::BdpEstimator
BdpEstimator(const char *name)
Definition:
bdp_estimator.cc:34
GPR_ASSERT
#define GPR_ASSERT(x)
Definition:
include/grpc/impl/codegen/log.h:94
max
int max
Definition:
bloaty/third_party/zlib/examples/enough.c:170
gpr_time_sub
GPRAPI gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b)
Definition:
src/core/lib/gpr/time.cc:168
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition:
gpr_types.h:36
grpc_core::BdpEstimator::bw_est_
double bw_est_
Definition:
bdp_estimator.h:88
grpc_core::BdpEstimator::stable_estimate_count_
int stable_estimate_count_
Definition:
bdp_estimator.h:87
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
bdp_estimator.h
grpc_core::TraceFlag
Definition:
debug/trace.h:63
grpc_core::Duration::Milliseconds
static constexpr Duration Milliseconds(int64_t millis)
Definition:
src/core/lib/gprpp/time.h:155
grpc_core::Duration::millis
constexpr int64_t millis() const
Definition:
src/core/lib/gprpp/time.h:208
grpc_core::BdpEstimator::PingState::UNSCHEDULED
@ UNSCHEDULED
grpc_core::BdpEstimator::ping_state_
PingState ping_state_
Definition:
bdp_estimator.h:81
grpc_core::BdpEstimator::accumulator_
int64_t accumulator_
Definition:
bdp_estimator.h:82
grpc_core::BdpEstimator::CompletePing
Timestamp CompletePing()
Definition:
bdp_estimator.cc:44
grpc_core::Duration::Seconds
static constexpr Duration Seconds(int64_t seconds)
Definition:
src/core/lib/gprpp/time.h:151
grpc_core::BdpEstimator::inter_ping_delay_
Duration inter_ping_delay_
Definition:
bdp_estimator.h:86
exec_ctx.h
grpc_core::ExecCtx::Now
Timestamp Now()
Definition:
exec_ctx.cc:90
gpr_timespec
Definition:
gpr_types.h:50
grpc_bdp_estimator_trace
grpc_core::TraceFlag grpc_bdp_estimator_trace(false, "bdp_estimator")
grpc_core::Duration
Definition:
src/core/lib/gprpp/time.h:122
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition:
exec_ctx.h:205
port_platform.h
grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:45