Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2016 gRPC authors. |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * 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 Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * 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 Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #ifndef GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H |
| 20 | #define GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H |
| 21 | |
Craig Tiller | 42ab2b1 | 2017-04-19 12:08:58 -0700 | [diff] [blame] | 22 | #include <grpc/support/time.h> |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 23 | #include <stdbool.h> |
| 24 | #include <stdint.h> |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 25 | #include "src/core/lib/debug/trace.h" |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 26 | |
| 27 | #define GRPC_BDP_SAMPLES 16 |
| 28 | #define GRPC_BDP_MIN_SAMPLES_FOR_ESTIMATE 3 |
| 29 | |
Yash Tibrewal | a7e6d65 | 2017-09-20 18:56:37 -0700 | [diff] [blame] | 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame] | 34 | extern grpc_tracer_flag grpc_bdp_estimator_trace; |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame] | 35 | |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 36 | typedef enum { |
| 37 | GRPC_BDP_PING_UNSCHEDULED, |
| 38 | GRPC_BDP_PING_SCHEDULED, |
| 39 | GRPC_BDP_PING_STARTED |
| 40 | } grpc_bdp_estimator_ping_state; |
| 41 | |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 42 | typedef struct grpc_bdp_estimator { |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 43 | grpc_bdp_estimator_ping_state ping_state; |
Craig Tiller | 91f77ea | 2016-12-29 16:59:43 -0800 | [diff] [blame] | 44 | int64_t accumulator; |
| 45 | int64_t estimate; |
Craig Tiller | 42ab2b1 | 2017-04-19 12:08:58 -0700 | [diff] [blame] | 46 | gpr_timespec ping_start_time; |
| 47 | double bw_est; |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame] | 48 | const char *name; |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 49 | } grpc_bdp_estimator; |
| 50 | |
Craig Tiller | efbd7c2 | 2017-01-27 14:07:44 -0800 | [diff] [blame] | 51 | void grpc_bdp_estimator_init(grpc_bdp_estimator *estimator, const char *name); |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 52 | |
| 53 | // Returns true if a reasonable estimate could be obtained |
ncteisen | 41ba268 | 2017-07-14 16:15:10 -0700 | [diff] [blame] | 54 | bool grpc_bdp_estimator_get_estimate(const grpc_bdp_estimator *estimator, |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 55 | int64_t *estimate); |
ncteisen | 41ba268 | 2017-07-14 16:15:10 -0700 | [diff] [blame] | 56 | // Tracks new bytes read. |
| 57 | bool grpc_bdp_estimator_get_bw(const grpc_bdp_estimator *estimator, double *bw); |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 58 | // Returns true if the user should schedule a ping |
ncteisen | 41ba268 | 2017-07-14 16:15:10 -0700 | [diff] [blame] | 59 | void grpc_bdp_estimator_add_incoming_bytes(grpc_bdp_estimator *estimator, |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 60 | int64_t num_bytes); |
ncteisen | 41ba268 | 2017-07-14 16:15:10 -0700 | [diff] [blame] | 61 | // Returns true if the user should schedule a ping |
| 62 | bool grpc_bdp_estimator_need_ping(const grpc_bdp_estimator *estimator); |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 63 | // 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 Tiller | ad2a11f | 2016-12-28 13:05:00 -0800 | [diff] [blame] | 66 | void grpc_bdp_estimator_schedule_ping(grpc_bdp_estimator *estimator); |
Craig Tiller | c0118b4 | 2016-12-29 12:17:57 -0800 | [diff] [blame] | 67 | // Start a ping: call after calling grpc_bdp_estimator_schedule_ping and once |
| 68 | // the ping is on the wire |
Craig Tiller | 56331f7 | 2016-10-20 13:43:07 -0700 | [diff] [blame] | 69 | void grpc_bdp_estimator_start_ping(grpc_bdp_estimator *estimator); |
Craig Tiller | 064db44 | 2016-10-20 09:34:58 -0700 | [diff] [blame] | 70 | // Completes a previously started ping |
| 71 | void grpc_bdp_estimator_complete_ping(grpc_bdp_estimator *estimator); |
| 72 | |
Yash Tibrewal | a7e6d65 | 2017-09-20 18:56:37 -0700 | [diff] [blame] | 73 | #ifdef __cplusplus |
| 74 | } |
| 75 | #endif |
| 76 | |
| 77 | #endif /* GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H */ |