blob: 90e4332023778b7b92d931b85a97a99ba6890faa [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
41void grpc_bdp_estimator_init(grpc_bdp_estimator *estimator) {
Craig Tiller91f77ea2016-12-29 16:59:43 -080042 estimator->estimate = 65536;
Craig Tillerc0118b42016-12-29 12:17:57 -080043 estimator->ping_state = GRPC_BDP_PING_UNSCHEDULED;
Craig Tiller064db442016-10-20 09:34:58 -070044}
45
Craig Tiller064db442016-10-20 09:34:58 -070046bool grpc_bdp_estimator_get_estimate(grpc_bdp_estimator *estimator,
47 int64_t *estimate) {
Craig Tiller91f77ea2016-12-29 16:59:43 -080048 *estimate = estimator->estimate;
Craig Tiller56331f72016-10-20 13:43:07 -070049 return true;
50}
51
Craig Tiller56331f72016-10-20 13:43:07 -070052bool grpc_bdp_estimator_add_incoming_bytes(grpc_bdp_estimator *estimator,
53 int64_t num_bytes) {
Craig Tillerc0118b42016-12-29 12:17:57 -080054 switch (estimator->ping_state) {
55 case GRPC_BDP_PING_UNSCHEDULED:
56 return true;
57 case GRPC_BDP_PING_SCHEDULED:
58 return false;
59 case GRPC_BDP_PING_STARTED:
Craig Tiller91f77ea2016-12-29 16:59:43 -080060 estimator->accumulator += num_bytes;
Craig Tillerc0118b42016-12-29 12:17:57 -080061 return false;
Craig Tiller56331f72016-10-20 13:43:07 -070062 }
Craig Tillerc0118b42016-12-29 12:17:57 -080063 GPR_UNREACHABLE_CODE(return false);
64}
65
66void grpc_bdp_estimator_schedule_ping(grpc_bdp_estimator *estimator) {
67 GPR_ASSERT(estimator->ping_state == GRPC_BDP_PING_UNSCHEDULED);
68 estimator->ping_state = GRPC_BDP_PING_SCHEDULED;
Craig Tiller56331f72016-10-20 13:43:07 -070069}
70
71void grpc_bdp_estimator_start_ping(grpc_bdp_estimator *estimator) {
Craig Tillerc0118b42016-12-29 12:17:57 -080072 GPR_ASSERT(estimator->ping_state == GRPC_BDP_PING_SCHEDULED);
73 estimator->ping_state = GRPC_BDP_PING_STARTED;
Craig Tiller91f77ea2016-12-29 16:59:43 -080074 estimator->accumulator = 0;
Craig Tiller56331f72016-10-20 13:43:07 -070075}
76
77void grpc_bdp_estimator_complete_ping(grpc_bdp_estimator *estimator) {
Craig Tillerc0118b42016-12-29 12:17:57 -080078 GPR_ASSERT(estimator->ping_state == GRPC_BDP_PING_STARTED);
Craig Tiller91f77ea2016-12-29 16:59:43 -080079 if (estimator->accumulator > 2 * estimator->estimate / 3) {
80 estimator->estimate *= 2;
81 gpr_log(GPR_DEBUG, "est --> %" PRId64, estimator->estimate);
82 }
Craig Tillerc0118b42016-12-29 12:17:57 -080083 estimator->ping_state = GRPC_BDP_PING_UNSCHEDULED;
Craig Tiller064db442016-10-20 09:34:58 -070084}