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 | #ifndef GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H |
| 35 | #define GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H |
| 36 | |
| 37 | #include <stdbool.h> |
| 38 | #include <stdint.h> |
| 39 | |
| 40 | #define GRPC_BDP_SAMPLES 16 |
| 41 | #define GRPC_BDP_MIN_SAMPLES_FOR_ESTIMATE 3 |
| 42 | |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame^] | 43 | typedef enum { |
| 44 | GRPC_BDP_PING_UNSCHEDULED, |
| 45 | GRPC_BDP_PING_SCHEDULED, |
| 46 | GRPC_BDP_PING_STARTED |
| 47 | } grpc_bdp_estimator_ping_state; |
| 48 | |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 49 | typedef struct grpc_bdp_estimator { |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 50 | uint8_t num_samples; |
| 51 | uint8_t first_sample_idx; |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame^] | 52 | grpc_bdp_estimator_ping_state ping_state; |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 53 | int64_t samples[GRPC_BDP_SAMPLES]; |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 54 | } grpc_bdp_estimator; |
| 55 | |
| 56 | void grpc_bdp_estimator_init(grpc_bdp_estimator *estimator); |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 57 | |
| 58 | // Returns true if a reasonable estimate could be obtained |
| 59 | bool grpc_bdp_estimator_get_estimate(grpc_bdp_estimator *estimator, |
| 60 | int64_t *estimate); |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame^] | 61 | // Returns true if the user should schedule a ping |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 62 | bool grpc_bdp_estimator_add_incoming_bytes(grpc_bdp_estimator *estimator, |
| 63 | int64_t num_bytes); |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame^] | 64 | // Schedule a ping: call in response to receiving a true from |
| 65 | // grpc_bdp_estimator_add_incoming_bytes once a ping has been scheduled by a |
| 66 | // transport (but not necessarily started) |
Craig Tiller | ad2a11f | 2016-12-28 13:05:00 -0800 | [diff] [blame] | 67 | void grpc_bdp_estimator_schedule_ping(grpc_bdp_estimator *estimator); |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame^] | 68 | // Start a ping: call after calling grpc_bdp_estimator_schedule_ping and once |
| 69 | // the ping is on the wire |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 70 | void grpc_bdp_estimator_start_ping(grpc_bdp_estimator *estimator); |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 71 | // Completes a previously started ping |
| 72 | void grpc_bdp_estimator_complete_ping(grpc_bdp_estimator *estimator); |
| 73 | |
| 74 | #endif |