blob: e1483677fd307bb2fae5d28474fa7e46d0bd6b6d [file] [log] [blame]
Craig Tiller064db442016-10-20 09:34:58 -07001/*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "src/core/lib/transport/bdp_estimator.h"
35
Craig Tiller56331f72016-10-20 13:43:07 -070036#include <stdlib.h>
37
38#include <grpc/support/log.h>
Craig Tiller064db442016-10-20 09:34:58 -070039#include <grpc/support/useful.h>
40
Craig Tillerefbd7c22017-01-27 14:07:44 -080041int grpc_bdp_estimator_trace = 0;
42
43void grpc_bdp_estimator_init(grpc_bdp_estimator *estimator, const char *name) {
Craig Tiller91f77ea2016-12-29 16:59:43 -080044 estimator->estimate = 65536;
Craig Tillerc0118b42016-12-29 12:17:57 -080045 estimator->ping_state = GRPC_BDP_PING_UNSCHEDULED;
Craig Tillerefbd7c22017-01-27 14:07:44 -080046 estimator->name = name;
Craig Tiller064db442016-10-20 09:34:58 -070047}
48
Craig Tiller064db442016-10-20 09:34:58 -070049bool grpc_bdp_estimator_get_estimate(grpc_bdp_estimator *estimator,
50 int64_t *estimate) {
Craig Tiller91f77ea2016-12-29 16:59:43 -080051 *estimate = estimator->estimate;
Craig Tiller56331f72016-10-20 13:43:07 -070052 return true;
53}
54
Craig Tiller56331f72016-10-20 13:43:07 -070055bool grpc_bdp_estimator_add_incoming_bytes(grpc_bdp_estimator *estimator,
56 int64_t num_bytes) {
Craig Tillerefbd7c22017-01-27 14:07:44 -080057 estimator->accumulator += num_bytes;
Craig Tillerc0118b42016-12-29 12:17:57 -080058 switch (estimator->ping_state) {
59 case GRPC_BDP_PING_UNSCHEDULED:
60 return true;
61 case GRPC_BDP_PING_SCHEDULED:
62 return false;
63 case GRPC_BDP_PING_STARTED:
Craig Tillerc0118b42016-12-29 12:17:57 -080064 return false;
Craig Tiller56331f72016-10-20 13:43:07 -070065 }
Craig Tillerc0118b42016-12-29 12:17:57 -080066 GPR_UNREACHABLE_CODE(return false);
67}
68
69void grpc_bdp_estimator_schedule_ping(grpc_bdp_estimator *estimator) {
Craig Tillerefbd7c22017-01-27 14:07:44 -080070 if (grpc_bdp_estimator_trace) {
71 gpr_log(GPR_DEBUG, "bdp[%s]:sched acc=%" PRId64 " est=%" PRId64,
72 estimator->name, estimator->accumulator, estimator->estimate);
73 }
Craig Tillerc0118b42016-12-29 12:17:57 -080074 GPR_ASSERT(estimator->ping_state == GRPC_BDP_PING_UNSCHEDULED);
75 estimator->ping_state = GRPC_BDP_PING_SCHEDULED;
Craig Tillerefbd7c22017-01-27 14:07:44 -080076 estimator->accumulator = 0;
Craig Tiller56331f72016-10-20 13:43:07 -070077}
78
79void grpc_bdp_estimator_start_ping(grpc_bdp_estimator *estimator) {
Craig Tillerefbd7c22017-01-27 14:07:44 -080080 if (grpc_bdp_estimator_trace) {
81 gpr_log(GPR_DEBUG, "bdp[%s]:start acc=%" PRId64 " est=%" PRId64,
82 estimator->name, estimator->accumulator, estimator->estimate);
83 }
Craig Tillerc0118b42016-12-29 12:17:57 -080084 GPR_ASSERT(estimator->ping_state == GRPC_BDP_PING_SCHEDULED);
85 estimator->ping_state = GRPC_BDP_PING_STARTED;
Craig Tiller91f77ea2016-12-29 16:59:43 -080086 estimator->accumulator = 0;
Craig Tiller56331f72016-10-20 13:43:07 -070087}
88
89void grpc_bdp_estimator_complete_ping(grpc_bdp_estimator *estimator) {
Craig Tillerefbd7c22017-01-27 14:07:44 -080090 if (grpc_bdp_estimator_trace) {
91 gpr_log(GPR_DEBUG, "bdp[%s]:complete acc=%" PRId64 " est=%" PRId64,
92 estimator->name, estimator->accumulator, estimator->estimate);
93 }
Craig Tillerc0118b42016-12-29 12:17:57 -080094 GPR_ASSERT(estimator->ping_state == GRPC_BDP_PING_STARTED);
Craig Tiller91f77ea2016-12-29 16:59:43 -080095 if (estimator->accumulator > 2 * estimator->estimate / 3) {
96 estimator->estimate *= 2;
Craig Tillerefbd7c22017-01-27 14:07:44 -080097 if (grpc_bdp_estimator_trace) {
98 gpr_log(GPR_DEBUG, "bdp[%s]: estimate increased to %" PRId64,
99 estimator->name, estimator->estimate);
100 }
Craig Tiller91f77ea2016-12-29 16:59:43 -0800101 }
Craig Tillerc0118b42016-12-29 12:17:57 -0800102 estimator->ping_state = GRPC_BDP_PING_UNSCHEDULED;
Craig Tillerefbd7c22017-01-27 14:07:44 -0800103 estimator->accumulator = 0;
Craig Tiller064db442016-10-20 09:34:58 -0700104}