blob: f397440cab8b9de6a645c2e949ff147a6150fae3 [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#include "src/core/lib/transport/bdp_estimator.h"
35
Craig Tiller56331f72016-10-20 13:43:07 -070036#include <stdlib.h>
37
38#include <grpc/support/log.h>
Craig Tiller064db442016-10-20 09:34:58 -070039#include <grpc/support/useful.h>
40
41void 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
47static int compare_samples(const void *a, const void *b) {
48 return GPR_ICMP(*(int64_t *)a, *(int64_t *)b);
49}
50
51bool 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 Tiller56331f72016-10-20 13:43:07 -070066 *estimate = samples[estimator->num_samples / 2];
Craig Tiller064db442016-10-20 09:34:58 -070067 } else {
Craig Tiller56331f72016-10-20 13:43:07 -070068 *estimate = (samples[estimator->num_samples / 2] +
69 samples[estimator->num_samples / 2 + 1]) /
70 2;
Craig Tiller064db442016-10-20 09:34:58 -070071 }
Craig Tiller56331f72016-10-20 13:43:07 -070072 return true;
73}
74
75static 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
81bool 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
91void 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
101void grpc_bdp_estimator_complete_ping(grpc_bdp_estimator *estimator) {
102 GPR_ASSERT(estimator->sampling);
103 estimator->num_samples++;
104 estimator->sampling = false;
Craig Tiller064db442016-10-20 09:34:58 -0700105}