blob: 21c27ec6afd955c8ca99b64def5469dcaea6ffc0 [file] [log] [blame]
Craig Tiller064db442016-10-20 09:34:58 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2016 gRPC authors.
Craig Tiller064db442016-10-20 09:34:58 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Craig Tiller064db442016-10-20 09:34:58 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller064db442016-10-20 09:34:58 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Craig Tiller064db442016-10-20 09:34:58 -070016 *
17 */
18
19#ifndef GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H
20#define GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H
21
Craig Tiller42ab2b12017-04-19 12:08:58 -070022#include <grpc/support/time.h>
Craig Tiller064db442016-10-20 09:34:58 -070023#include <stdbool.h>
24#include <stdint.h>
Craig Tiller84f75d42017-05-03 13:06:35 -070025#include "src/core/lib/debug/trace.h"
Craig Tiller064db442016-10-20 09:34:58 -070026
27#define GRPC_BDP_SAMPLES 16
28#define GRPC_BDP_MIN_SAMPLES_FOR_ESTIMATE 3
29
Yash Tibrewala7e6d652017-09-20 18:56:37 -070030#ifdef __cplusplus
31extern "C" {
32#endif
33
Craig Tiller84f75d42017-05-03 13:06:35 -070034extern grpc_tracer_flag grpc_bdp_estimator_trace;
Craig Tillerefbd7c22017-01-27 14:07:44 -080035
Craig Tillerc0118b42016-12-29 12:17:57 -080036typedef enum {
37 GRPC_BDP_PING_UNSCHEDULED,
38 GRPC_BDP_PING_SCHEDULED,
39 GRPC_BDP_PING_STARTED
40} grpc_bdp_estimator_ping_state;
41
Craig Tiller064db442016-10-20 09:34:58 -070042typedef struct grpc_bdp_estimator {
Craig Tillerc0118b42016-12-29 12:17:57 -080043 grpc_bdp_estimator_ping_state ping_state;
Craig Tiller91f77ea2016-12-29 16:59:43 -080044 int64_t accumulator;
45 int64_t estimate;
Craig Tiller42ab2b12017-04-19 12:08:58 -070046 gpr_timespec ping_start_time;
47 double bw_est;
Craig Tillerefbd7c22017-01-27 14:07:44 -080048 const char *name;
Craig Tiller064db442016-10-20 09:34:58 -070049} grpc_bdp_estimator;
50
Craig Tillerefbd7c22017-01-27 14:07:44 -080051void grpc_bdp_estimator_init(grpc_bdp_estimator *estimator, const char *name);
Craig Tiller064db442016-10-20 09:34:58 -070052
53// Returns true if a reasonable estimate could be obtained
ncteisen41ba2682017-07-14 16:15:10 -070054bool grpc_bdp_estimator_get_estimate(const grpc_bdp_estimator *estimator,
Craig Tiller064db442016-10-20 09:34:58 -070055 int64_t *estimate);
ncteisen41ba2682017-07-14 16:15:10 -070056// Tracks new bytes read.
57bool grpc_bdp_estimator_get_bw(const grpc_bdp_estimator *estimator, double *bw);
Craig Tillerc0118b42016-12-29 12:17:57 -080058// Returns true if the user should schedule a ping
ncteisen41ba2682017-07-14 16:15:10 -070059void grpc_bdp_estimator_add_incoming_bytes(grpc_bdp_estimator *estimator,
Craig Tiller064db442016-10-20 09:34:58 -070060 int64_t num_bytes);
ncteisen41ba2682017-07-14 16:15:10 -070061// Returns true if the user should schedule a ping
62bool grpc_bdp_estimator_need_ping(const grpc_bdp_estimator *estimator);
Craig Tillerc0118b42016-12-29 12:17:57 -080063// Schedule a ping: call in response to receiving a true from
64// grpc_bdp_estimator_add_incoming_bytes once a ping has been scheduled by a
65// transport (but not necessarily started)
Craig Tillerad2a11f2016-12-28 13:05:00 -080066void grpc_bdp_estimator_schedule_ping(grpc_bdp_estimator *estimator);
Craig Tillerc0118b42016-12-29 12:17:57 -080067// Start a ping: call after calling grpc_bdp_estimator_schedule_ping and once
68// the ping is on the wire
Craig Tiller56331f72016-10-20 13:43:07 -070069void grpc_bdp_estimator_start_ping(grpc_bdp_estimator *estimator);
Craig Tiller064db442016-10-20 09:34:58 -070070// Completes a previously started ping
71void grpc_bdp_estimator_complete_ping(grpc_bdp_estimator *estimator);
72
Yash Tibrewala7e6d652017-09-20 18:56:37 -070073#ifdef __cplusplus
74}
75#endif
76
77#endif /* GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H */