blob: d812f90a357ba727a23d285461b4d4f9b9ec740f [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#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 Tillerc0118b42016-12-29 12:17:57 -080043typedef enum {
44 GRPC_BDP_PING_UNSCHEDULED,
45 GRPC_BDP_PING_SCHEDULED,
46 GRPC_BDP_PING_STARTED
47} grpc_bdp_estimator_ping_state;
48
Craig Tiller064db442016-10-20 09:34:58 -070049typedef struct grpc_bdp_estimator {
Craig Tiller064db442016-10-20 09:34:58 -070050 uint8_t num_samples;
51 uint8_t first_sample_idx;
Craig Tillerc0118b42016-12-29 12:17:57 -080052 grpc_bdp_estimator_ping_state ping_state;
Craig Tiller56331f72016-10-20 13:43:07 -070053 int64_t samples[GRPC_BDP_SAMPLES];
Craig Tiller064db442016-10-20 09:34:58 -070054} grpc_bdp_estimator;
55
56void grpc_bdp_estimator_init(grpc_bdp_estimator *estimator);
Craig Tiller064db442016-10-20 09:34:58 -070057
58// Returns true if a reasonable estimate could be obtained
59bool grpc_bdp_estimator_get_estimate(grpc_bdp_estimator *estimator,
60 int64_t *estimate);
Craig Tillerc0118b42016-12-29 12:17:57 -080061// Returns true if the user should schedule a ping
Craig Tiller064db442016-10-20 09:34:58 -070062bool grpc_bdp_estimator_add_incoming_bytes(grpc_bdp_estimator *estimator,
63 int64_t num_bytes);
Craig Tillerc0118b42016-12-29 12:17:57 -080064// 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 Tillerad2a11f2016-12-28 13:05:00 -080067void grpc_bdp_estimator_schedule_ping(grpc_bdp_estimator *estimator);
Craig Tillerc0118b42016-12-29 12:17:57 -080068// Start a ping: call after calling grpc_bdp_estimator_schedule_ping and once
69// the ping is on the wire
Craig Tiller56331f72016-10-20 13:43:07 -070070void grpc_bdp_estimator_start_ping(grpc_bdp_estimator *estimator);
Craig Tiller064db442016-10-20 09:34:58 -070071// Completes a previously started ping
72void grpc_bdp_estimator_complete_ping(grpc_bdp_estimator *estimator);
73
74#endif