[PATCH v2 6/6] pcapng: improve performance of timestamping

Stephen Hemminger
2025-12-29T23:03:03Z

Avoid doing expensive divide operations when converting
timestamps from cycles (TSC) to pcapng scaled value (ns).
This logic was derived from the math used by Linux kernel
virtual system call with help from AI.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
lib/pcapng/rte_pcapng.c | 63 ++++++++++++++++++++++++++---------------
1 file changed, 40 insertions(+), 23 deletions(-)

diff --git a/lib/pcapng/rte_pcapng.c b/lib/pcapng/rte_pcapng.c
index 7c3c400c71..b12814e305 100644
--- a/lib/pcapng/rte_pcapng.c
+++ b/lib/pcapng/rte_pcapng.c
@@ -38,8 +38,14 @@
struct rte_pcapng {
int outfd; /* output file */
unsigned int ports; /* number of interfaces added */
- uint64_t offset_ns; /* ns since 1/1/1970 when initialized */
- uint64_t tsc_base; /* TSC when started */
+
+ struct pcapng_time_conv {
+ uint64_t tsc_base; /* TSC when started */
+ uint64_t ns_base; /* ns since 1/1/1970 when initialized */
+ uint64_t mult; /* scaling factor relative to TSC hz */
+ uint32_t shift; /* shift for scaling (24) */
+ uint64_t mask; /* mask of bits used (56) */
+ } tc;

/* DPDK port id to interface index in file */
uint32_t port_index[RTE_MAX_ETHPORTS];
@@ -95,21 +101,38 @@ static ssize_t writev(int fd, const struct iovec *iov, int iovcnt)
#define if_indextoname(ifindex, ifname) NULL
#endif

+/* Initialize time conversion based on logic similar to rte_cyclecounter */
+static void
+pcapng_timestamp_init(struct pcapng_time_conv *tc)
+{
+ struct timespec ts;
+ uint64_t cycles = rte_get_tsc_cycles();
+
+ /* record start time in ns since 1/1/1970 */
+ clock_gettime(CLOCK_REALTIME, &ts);
+
+ /* Compute baseline TSC which occurred during clock_gettime */
+ tc->tsc_base = (cycles + rte_get_tsc_cycles()) / 2;
+ tc->ns_base = (uint64_t)ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+
+ /* Set conversion factors for reasonable precision with no overflow */
+ uint64_t tsc_hz = rte_get_tsc_hz();
+ tc->shift = 24;
+ tc->mult = ((uint64_t)1000000000ULL << tc->shift) / tsc_hz;
+ tc->mask = RTE_BIT64(56) - 1;
+}
+
/* Convert from TSC (CPU cycles) to nanoseconds */
static uint64_t
-pcapng_timestamp(const rte_pcapng_t *self, uint64_t cycles)
+pcapng_timestamp(const struct pcapng_time_conv *tc, uint64_t cycles)
{
- uint64_t delta, rem, secs, ns;
- const uint64_t hz = rte_get_tsc_hz();
-
- delta = cycles - self->tsc_base;
+ /* Compute TSC delta with mask to avoid wraparound */
+ uint64_t delta = (cycles - tc->tsc_base) & tc->mask;

- /* Avoid numeric wraparound by computing seconds first */
- secs = delta / hz;
- rem = delta % hz;
- ns = (rem * NS_PER_S) / hz;
+ /* Convert TSC delta to nanoseconds (no division) */
+ uint64_t ns_delta = (delta * tc->mult) >> tc->shift;

- return secs * NS_PER_S + ns + self->offset_ns;
+ return tc->ns_base + ns_delta;
}

/* length of option including padding */
@@ -309,7 +332,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, uint16_t link_type,

opt = pcapng_add_option(opt, PCAPNG_OPT_END, NULL, 0);

- /* clone block_length after optionsa */
+ /* clone block_length after options */
memcpy(opt, &hdr->block_length, sizeof(uint32_t));

/* remember the file index */
@@ -329,7 +352,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id,
{
struct pcapng_statistics *hdr;
struct pcapng_option *opt;
- uint64_t start_time = self->offset_ns;
+ uint64_t start_time = self->tc.ns_base;
uint64_t sample_time;
uint32_t optlen, len;
uint32_t *buf;
@@ -379,7 +402,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id,
hdr->block_length = len;
hdr->interface_id = self->port_index[port_id];

- sample_time = pcapng_timestamp(self, rte_get_tsc_cycles());
+ sample_time = pcapng_timestamp(&self->tc, rte_get_tsc_cycles());
hdr->timestamp_hi = sample_time >> 32;
hdr->timestamp_lo = (uint32_t)sample_time;

@@ -658,7 +681,7 @@ rte_pcapng_write_packets(rte_pcapng_t *self,
/* adjust timestamp recorded in packet */
cycles = (uint64_t)epb->timestamp_hi << 32;
cycles += epb->timestamp_lo;
- timestamp = pcapng_timestamp(self, cycles);
+ timestamp = pcapng_timestamp(&self->tc, cycles);
epb->timestamp_hi = timestamp >> 32;
epb->timestamp_lo = (uint32_t)timestamp;

@@ -704,8 +727,6 @@ rte_pcapng_fdopen(int fd,
{
unsigned int i;
rte_pcapng_t *self;
- struct timespec ts;
- uint64_t cycles;

self = malloc(sizeof(*self));
if (!self) {
@@ -716,11 +737,7 @@ rte_pcapng_fdopen(int fd,
self->outfd = fd;
self->ports = 0;

- /* record start time in ns since 1/1/1970 */
- cycles = rte_get_tsc_cycles();
- clock_gettime(CLOCK_REALTIME, &ts);
- self->tsc_base = (cycles + rte_get_tsc_cycles()) / 2;
- self->offset_ns = rte_timespec_to_ns(&ts);
+ pcapng_timestamp_init(&self->tc);

for (i = 0; i < RTE_MAX_ETHPORTS; i++)
self->port_index[i] = UINT32_MAX;
--
2.51.0