From 907084cfb2b0a4df0ca3f481bc73fd8b64c9f4d2 Mon Sep 17 00:00:00 2001
From: Tom Barbette <tom.barbette@uclouvain.be>
Date: Thu, 27 Jul 2023 22:52:34 +0200
Subject: [PATCH] RecordTimestamp: Add a sample parameter too

This allows to record only one packet out of N. And therefore need 1/N
of the space.

TimestampDiff should use the same parameter.
---
 elements/analysis/recordtimestamp.cc | 11 +++++++++--
 elements/analysis/recordtimestamp.hh |  3 +++
 elements/analysis/timestampdiff.cc   | 11 ++++++-----
 3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/elements/analysis/recordtimestamp.cc b/elements/analysis/recordtimestamp.cc
index db9c7c447..02f3e8692 100644
--- a/elements/analysis/recordtimestamp.cc
+++ b/elements/analysis/recordtimestamp.cc
@@ -39,8 +39,9 @@ int RecordTimestamp::configure(Vector<String> &conf, ErrorHandler *errh) {
             .read("COUNTER", e)
             .read("N", n)
             .read("OFFSET", _offset)
-            .read("DYNAMIC", _dynamic)
-            .read("NET_ORDER", _net_order)
+            .read_or_set("DYNAMIC", _dynamic, false)
+            .read_or_set("NET_ORDER", _net_order, false)
+            .read_or_set("SAMPLE", _sample, false)
             .complete() < 0)
         return -1;
 
@@ -64,6 +65,12 @@ RecordTimestamp::rmaction(Packet *p) {
     uint64_t i;
     if (_offset >= 0) {
         i = get_numberpacket(p, _offset, _net_order);
+        if (_sample > 1) {
+            if (i % _sample == 0)
+                i = i / _sample;
+            else
+                return;
+        }
         assert(i < ULLONG_MAX);
         while (i >= (unsigned)_timestamps.size()) {
             if (!_dynamic && i >= (unsigned)_timestamps.capacity()) {
diff --git a/elements/analysis/recordtimestamp.hh b/elements/analysis/recordtimestamp.hh
index 8d9e38349..1f2b2d37f 100644
--- a/elements/analysis/recordtimestamp.hh
+++ b/elements/analysis/recordtimestamp.hh
@@ -97,6 +97,7 @@ private:
     int _offset;
     bool _dynamic;
     bool _net_order;
+    uint32_t _sample;
     Vector<TimestampT> _timestamps;
     NumberPacket *_np;
 };
@@ -105,6 +106,8 @@ private:
 const TimestampT read_timestamp = TimestampUnread;
 
 inline TimestampT RecordTimestamp::get(uint64_t i) {
+    if (_sample > 1)
+        i = i / _sample;
     if (i >= (unsigned)_timestamps.size()) {
         click_chatter("%p{element}: Index %lu is out of range !", this, i);
         return TimestampT::uninitialized_t();
diff --git a/elements/analysis/timestampdiff.cc b/elements/analysis/timestampdiff.cc
index dc1130552..eadca0287 100644
--- a/elements/analysis/timestampdiff.cc
+++ b/elements/analysis/timestampdiff.cc
@@ -244,17 +244,18 @@ inline int TimestampDiff::smaction(Packet *p)
 {
     TimestampT now = TimestampT::now_steady();
     uint64_t i = NumberPacket::read_number_of_packet(p, _offset, _net_order);
+if (_sample != 1) {
+        if ((uint32_t)i % _sample != 0) {
+            return 0;
+        }
+    }
     TimestampT old = get_recordtimestamp_instance()->get(i);
 
     if (old == TimestampT::uninitialized_t()) {
         return 1;
     }
 
-    if (_sample != 1) {
-        if ((uint32_t)i % _sample != 0) {
-            return 0;
-        }
-    }
+
 
     TimestampT diff = now - old;
     uint32_t usec = _nano? diff.nsecval() : diff.usecval();
-- 
GitLab