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 | |
| 41 | void grpc_bdp_estimator_init(grpc_bdp_estimator *estimator) { |
Craig Tiller | 91f77ea | 2016-12-29 16:59:43 -0800 | [diff] [blame] | 42 | estimator->estimate = 65536; |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 43 | estimator->ping_state = GRPC_BDP_PING_UNSCHEDULED; |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 46 | bool grpc_bdp_estimator_get_estimate(grpc_bdp_estimator *estimator, |
| 47 | int64_t *estimate) { |
Craig Tiller | 91f77ea | 2016-12-29 16:59:43 -0800 | [diff] [blame] | 48 | *estimate = estimator->estimate; |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 49 | return true; |
| 50 | } |
| 51 | |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 52 | bool grpc_bdp_estimator_add_incoming_bytes(grpc_bdp_estimator *estimator, |
| 53 | int64_t num_bytes) { |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 54 | 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 Tiller | 91f77ea | 2016-12-29 16:59:43 -0800 | [diff] [blame] | 60 | estimator->accumulator += num_bytes; |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 61 | return false; |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 62 | } |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 63 | GPR_UNREACHABLE_CODE(return false); |
| 64 | } |
| 65 | |
| 66 | void 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 Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void grpc_bdp_estimator_start_ping(grpc_bdp_estimator *estimator) { |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 72 | GPR_ASSERT(estimator->ping_state == GRPC_BDP_PING_SCHEDULED); |
| 73 | estimator->ping_state = GRPC_BDP_PING_STARTED; |
Craig Tiller | 91f77ea | 2016-12-29 16:59:43 -0800 | [diff] [blame] | 74 | estimator->accumulator = 0; |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void grpc_bdp_estimator_complete_ping(grpc_bdp_estimator *estimator) { |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 78 | GPR_ASSERT(estimator->ping_state == GRPC_BDP_PING_STARTED); |
Craig Tiller | 91f77ea | 2016-12-29 16:59:43 -0800 | [diff] [blame] | 79 | if (estimator->accumulator > 2 * estimator->estimate / 3) { |
| 80 | estimator->estimate *= 2; |
| 81 | gpr_log(GPR_DEBUG, "est --> %" PRId64, estimator->estimate); |
| 82 | } |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 83 | estimator->ping_state = GRPC_BDP_PING_UNSCHEDULED; |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 84 | } |