Compromise on 10ms sampling time for rate estimation. Giving pretty accurate results on my tests, and not hitting the CPU too hard.

This commit is contained in:
Herbert Wolverson committed 2024-02-29 11:19:35 -06:00
1 parent 23f2cfb9d8
commit 370b65fa67
1 file changed
+6 -5
+6 -5
View File
@@ -9,7 +9,7 @@
#define SECOND_IN_NANOS 1000000000
#define MS_IN_NANOS 1000000
#define MS_IN_NANOS_T10 10000000
//#define TIMESTAMP_INTERVAL_NANOS 10000000
// Some helpers to make understanding direction easier
@@ -91,7 +91,7 @@ static __always_inline struct flow_data_t new_flow_data(
.packets_sent = { 0, 0 },
// Track flow rates at an MS scale rather than per-second
// to minimize rounding errors.
.next_count_time = { now + MS_IN_NANOS, now + MS_IN_NANOS },
.next_count_time = { now + MS_IN_NANOS_T10, now + MS_IN_NANOS_T10 },
.last_count_time = { now, now },
.next_count_bytes = { dissector->skb_len, dissector->skb_len },
.rate_estimate_bps = { 0, 0 },
@@ -151,11 +151,12 @@ static __always_inline void update_flow_rates(
if (now > data->next_count_time[rate_index]) {
// Calculate the rate estimate
__u64 bits = (data->bytes_sent[rate_index] - data->next_count_bytes[rate_index])*8;
__u64 time = (now - data->last_count_time[rate_index]) / 1000000; // Milliseconds
data->rate_estimate_bps[rate_index] = (bits/time) * 1000; // bits per second
data->next_count_time[rate_index] = now + MS_IN_NANOS;
__u64 time = (now - data->last_count_time[rate_index]) / 100000; // 10 Milliseconds
data->rate_estimate_bps[rate_index] = (bits/time); // bits per second
data->next_count_time[rate_index] = now + MS_IN_NANOS_T10;
data->next_count_bytes[rate_index] = data->bytes_sent[rate_index];
data->last_count_time[rate_index] = now;
bpf_debug("[FLOWS] Rate Estimate: %llu", data->rate_estimate_bps[rate_index]);
}
}