Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 36 | #include <stdlib.h> |
| 37 | |
| 38 | #include <grpc/support/log.h> |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 39 | #include <grpc/support/useful.h> |
| 40 | |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame^] | 41 | int grpc_bdp_estimator_trace = 0; |
| 42 | |
| 43 | void grpc_bdp_estimator_init(grpc_bdp_estimator *estimator, const char *name) { |
Craig Tiller | 91f77ea | 2016-12-29 16:59:43 -0800 | [diff] [blame] | 44 | estimator->estimate = 65536; |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 45 | estimator->ping_state = GRPC_BDP_PING_UNSCHEDULED; |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame^] | 46 | estimator->name = name; |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 49 | bool grpc_bdp_estimator_get_estimate(grpc_bdp_estimator *estimator, |
| 50 | int64_t *estimate) { |
Craig Tiller | 91f77ea | 2016-12-29 16:59:43 -0800 | [diff] [blame] | 51 | *estimate = estimator->estimate; |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 55 | bool grpc_bdp_estimator_add_incoming_bytes(grpc_bdp_estimator *estimator, |
| 56 | int64_t num_bytes) { |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame^] | 57 | estimator->accumulator += num_bytes; |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 58 | 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 Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 64 | return false; |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 65 | } |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 66 | GPR_UNREACHABLE_CODE(return false); |
| 67 | } |
| 68 | |
| 69 | void grpc_bdp_estimator_schedule_ping(grpc_bdp_estimator *estimator) { |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame^] | 70 | 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 Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 74 | GPR_ASSERT(estimator->ping_state == GRPC_BDP_PING_UNSCHEDULED); |
| 75 | estimator->ping_state = GRPC_BDP_PING_SCHEDULED; |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame^] | 76 | estimator->accumulator = 0; |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void grpc_bdp_estimator_start_ping(grpc_bdp_estimator *estimator) { |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame^] | 80 | 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 Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 84 | GPR_ASSERT(estimator->ping_state == GRPC_BDP_PING_SCHEDULED); |
| 85 | estimator->ping_state = GRPC_BDP_PING_STARTED; |
Craig Tiller | 91f77ea | 2016-12-29 16:59:43 -0800 | [diff] [blame] | 86 | estimator->accumulator = 0; |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void grpc_bdp_estimator_complete_ping(grpc_bdp_estimator *estimator) { |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame^] | 90 | 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 Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 94 | GPR_ASSERT(estimator->ping_state == GRPC_BDP_PING_STARTED); |
Craig Tiller | 91f77ea | 2016-12-29 16:59:43 -0800 | [diff] [blame] | 95 | if (estimator->accumulator > 2 * estimator->estimate / 3) { |
| 96 | estimator->estimate *= 2; |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame^] | 97 | if (grpc_bdp_estimator_trace) { |
| 98 | gpr_log(GPR_DEBUG, "bdp[%s]: estimate increased to %" PRId64, |
| 99 | estimator->name, estimator->estimate); |
| 100 | } |
Craig Tiller | 91f77ea | 2016-12-29 16:59:43 -0800 | [diff] [blame] | 101 | } |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 102 | estimator->ping_state = GRPC_BDP_PING_UNSCHEDULED; |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame^] | 103 | estimator->accumulator = 0; |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 104 | } |