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) { |
| 42 | estimator->num_samples = 0; |
| 43 | estimator->first_sample_idx = 0; |
| 44 | estimator->sampling = false; |
| 45 | } |
| 46 | |
| 47 | static int compare_samples(const void *a, const void *b) { |
| 48 | return GPR_ICMP(*(int64_t *)a, *(int64_t *)b); |
| 49 | } |
| 50 | |
| 51 | bool grpc_bdp_estimator_get_estimate(grpc_bdp_estimator *estimator, |
| 52 | int64_t *estimate) { |
| 53 | if (estimator->num_samples < GRPC_BDP_MIN_SAMPLES_FOR_ESTIMATE) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | int64_t samples[GRPC_BDP_SAMPLES]; |
| 58 | for (uint8_t i = 0; i < estimator->num_samples; i++) { |
| 59 | samples[i] = |
| 60 | estimator |
| 61 | ->samples[(estimator->first_sample_idx + i) % GRPC_BDP_SAMPLES]; |
| 62 | } |
| 63 | qsort(samples, estimator->num_samples, sizeof(*samples), compare_samples); |
| 64 | |
| 65 | if (estimator->num_samples & 1) { |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame^] | 66 | *estimate = samples[estimator->num_samples / 2]; |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 67 | } else { |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame^] | 68 | *estimate = (samples[estimator->num_samples / 2] + |
| 69 | samples[estimator->num_samples / 2 + 1]) / |
| 70 | 2; |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 71 | } |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame^] | 72 | return true; |
| 73 | } |
| 74 | |
| 75 | static int64_t *sampling(grpc_bdp_estimator *estimator) { |
| 76 | return &estimator |
| 77 | ->samples[(estimator->first_sample_idx + estimator->num_samples) % |
| 78 | GRPC_BDP_SAMPLES]; |
| 79 | } |
| 80 | |
| 81 | bool grpc_bdp_estimator_add_incoming_bytes(grpc_bdp_estimator *estimator, |
| 82 | int64_t num_bytes) { |
| 83 | if (estimator->sampling) { |
| 84 | *sampling(estimator) += num_bytes; |
| 85 | return false; |
| 86 | } else { |
| 87 | return true; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void grpc_bdp_estimator_start_ping(grpc_bdp_estimator *estimator) { |
| 92 | GPR_ASSERT(!estimator->sampling); |
| 93 | estimator->sampling = true; |
| 94 | if (estimator->num_samples == GRPC_BDP_SAMPLES) { |
| 95 | estimator->first_sample_idx++; |
| 96 | estimator->num_samples--; |
| 97 | } |
| 98 | *sampling(estimator) = 0; |
| 99 | } |
| 100 | |
| 101 | void grpc_bdp_estimator_complete_ping(grpc_bdp_estimator *estimator) { |
| 102 | GPR_ASSERT(estimator->sampling); |
| 103 | estimator->num_samples++; |
| 104 | estimator->sampling = false; |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 105 | } |