Complete code (no tests yet)
diff --git a/src/core/lib/transport/bdp_estimator.c b/src/core/lib/transport/bdp_estimator.c
index 038054d..f397440 100644
--- a/src/core/lib/transport/bdp_estimator.c
+++ b/src/core/lib/transport/bdp_estimator.c
@@ -33,6 +33,9 @@
#include "src/core/lib/transport/bdp_estimator.h"
+#include <stdlib.h>
+
+#include <grpc/support/log.h>
#include <grpc/support/useful.h>
void grpc_bdp_estimator_init(grpc_bdp_estimator *estimator) {
@@ -60,6 +63,43 @@
qsort(samples, estimator->num_samples, sizeof(*samples), compare_samples);
if (estimator->num_samples & 1) {
+ *estimate = samples[estimator->num_samples / 2];
} else {
+ *estimate = (samples[estimator->num_samples / 2] +
+ samples[estimator->num_samples / 2 + 1]) /
+ 2;
}
+ return true;
+}
+
+static int64_t *sampling(grpc_bdp_estimator *estimator) {
+ return &estimator
+ ->samples[(estimator->first_sample_idx + estimator->num_samples) %
+ GRPC_BDP_SAMPLES];
+}
+
+bool grpc_bdp_estimator_add_incoming_bytes(grpc_bdp_estimator *estimator,
+ int64_t num_bytes) {
+ if (estimator->sampling) {
+ *sampling(estimator) += num_bytes;
+ return false;
+ } else {
+ return true;
+ }
+}
+
+void grpc_bdp_estimator_start_ping(grpc_bdp_estimator *estimator) {
+ GPR_ASSERT(!estimator->sampling);
+ estimator->sampling = true;
+ if (estimator->num_samples == GRPC_BDP_SAMPLES) {
+ estimator->first_sample_idx++;
+ estimator->num_samples--;
+ }
+ *sampling(estimator) = 0;
+}
+
+void grpc_bdp_estimator_complete_ping(grpc_bdp_estimator *estimator) {
+ GPR_ASSERT(estimator->sampling);
+ estimator->num_samples++;
+ estimator->sampling = false;
}
diff --git a/src/core/lib/transport/bdp_estimator.h b/src/core/lib/transport/bdp_estimator.h
index 9bbb3ff..3149486 100644
--- a/src/core/lib/transport/bdp_estimator.h
+++ b/src/core/lib/transport/bdp_estimator.h
@@ -41,10 +41,10 @@
#define GRPC_BDP_MIN_SAMPLES_FOR_ESTIMATE 3
typedef struct grpc_bdp_estimator {
- int64_t samples[GRPC_BDP_SAMPLES];
uint8_t num_samples;
uint8_t first_sample_idx;
bool sampling;
+ int64_t samples[GRPC_BDP_SAMPLES];
} grpc_bdp_estimator;
void grpc_bdp_estimator_init(grpc_bdp_estimator *estimator);
@@ -56,6 +56,8 @@
// Returns true if the user should start a ping
bool grpc_bdp_estimator_add_incoming_bytes(grpc_bdp_estimator *estimator,
int64_t num_bytes);
+// Note that a ping is starting
+void grpc_bdp_estimator_start_ping(grpc_bdp_estimator *estimator);
// Completes a previously started ping
void grpc_bdp_estimator_complete_ping(grpc_bdp_estimator *estimator);