From 73feea97d3c2c721a3e7f554fa63c79fc2685145 Mon Sep 17 00:00:00 2001 From: Jonathan Perry Date: Fri, 21 Feb 2025 14:42:56 +0000 Subject: [PATCH] fix large timestamp deltas at ebpf program start --- cmd/collector/task_counter.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/cmd/collector/task_counter.c b/cmd/collector/task_counter.c index 5d3f68c..3a9e1ad 100644 --- a/cmd/collector/task_counter.c +++ b/cmd/collector/task_counter.c @@ -226,8 +226,7 @@ int count_events(void *ctx) { e.rmid = args->rmid; - // Get current timestamp - __u64 now = bpf_ktime_get_ns(); + __u64 now; // Get previous counters __u32 zero = 0; @@ -260,13 +259,17 @@ int count_events(void *ctx) { } // Compute time delta and update timestamp - e.time_delta_ns = compute_delta(now, prev->timestamp); + now = bpf_ktime_get_ns(); + // if prev->timestamp is 0, this is the first event. We did not have the counter and timestamp values, + // so do not emit this event -- only use it to initialize the counters + if (prev->timestamp != 0) { + e.time_delta_ns = compute_delta(now, prev->timestamp); + e.timestamp = now; + // Submit the event to the perf event array + bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &e, sizeof(e)); + } prev->timestamp = now; - - // Submit the event to the perf event array - e.timestamp = now; - bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &e, sizeof(e)); - + increase_count(ctx); return 0;