|
libe3 0.6.1
C++ library for E3AP RAN vendor integration
|
Lock-free per-thread latency stamp recorder (header-only, C99). More...
#include <fcntl.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/mman.h>#include <sys/stat.h>#include <time.h>#include <unistd.h>Go to the source code of this file.
Classes | |
| struct | latrec_rec |
| struct | latrec_hdr |
| struct | latrec_t |
Macros | |
| #define | _GNU_SOURCE 1 |
| #define | LATREC_STORE_RELEASE(p, v) |
| #define | LATREC_LOAD_ACQUIRE(p) |
| #define | LATREC_PREFETCH_W(p) |
| #define | LATREC_UNLIKELY(x) |
| #define | LATREC_HAVE_GETCPU 0 |
| #define | LATREC_CPUID_ON_STAMP 0 |
| #define | LATREC_CAT_(a, b) |
| #define | LATREC_CAT(a, b) |
| #define | LATREC_STATIC_ASSERT(c, m) |
| #define | LATREC_MAGIC 0x31524C41u |
| #define | LATREC_VERSION 2u |
| #define | LATREC_HDR_LEN 4096 |
| #define | LATREC_CLOCK_SLOW_NS 100u |
| #define | LATREC_DEFAULT_DIR "/tmp/latrec" |
Functions | |
| static uint64_t | latrec_now_ns (void) |
| static uint64_t | latrec_real_ns (void) |
| static void | latrec_refresh_cpu (latrec_t *r) |
| static uint32_t | latrec_measure_clock_ns (void) |
| static int | latrec_open_in (latrec_t *r, const char *dir, const char *name, unsigned entries_log2) |
| static int | latrec_open (latrec_t *r, const char *name, unsigned entries_log2) |
| static void | latrec_stamp_at (latrec_t *r, uint64_t seq, uint8_t stage, uint64_t aux, uint64_t aux2, uint64_t t_ns) |
| static void | latrec_stamp (latrec_t *r, uint64_t seq, uint8_t stage, uint64_t aux, uint64_t aux2) |
| static void | latrec_heartbeat (latrec_t *r) |
| static void | latrec_close (latrec_t *r) |
| void | latrec_set_output_dir (const char *dir) |
| latrec_t * | latrec_tls_open_as (const char *role) |
| latrec_t * | latrec_tls_open (void) |
| uint64_t | latrec_seq_next (void) |
| static void | latrec_tstamp_at (uint64_t seq, uint8_t stage, uint64_t aux, uint64_t aux2, uint64_t t_ns) |
| static uint64_t | latrec_tnow (void) |
| static void | latrec_tstamp (uint64_t seq, uint8_t stage, uint64_t aux, uint64_t aux2) |
| static void | latrec_ctx_set (uint64_t seq) |
| static uint64_t | latrec_ctx (void) |
Variables | |
| __thread latrec_t * | latrec_tls |
Lock-free per-thread latency stamp recorder (header-only, C99).
One mmap-backed ring per writer thread, single producer, no locks: a stamp is one CLOCK_MONOTONIC read and four stores, with no syscall, allocation or formatting. Records are 32 bytes (seq:48 | cpu:8 | stage:8, t_ns, aux, aux2); seq is assigned once at the producer and joins the stages offline, aux/aux2 carry the per-stage payloads listed in the stage catalog below.
CLOCK_MONOTONIC shares an origin across processes, so RAN, dApp and xApp stamps join without calibration.
t_ns is stored last, with release ordering: a reader that acquire-loads a nonzero t_ns also observes that record's sc, aux and aux2. That makes one record self-consistent; it does not stop the slot being reused, so a reader of a ring that has wrapped may pair one write's t_ns with the next write's payload. A live reader must therefore keep up, or detect the wrap from rec_count and the single descent in t_ns.
Gated once, at compile time, by LIBE3_ENABLE_LATREC: off by default, so a normal build carries none of this, not even a disabled-ring branch. With it on, tracing runs – there is no second per-run opt-in. See the bottom of this file, and docs/utilities/latrec.md for where the rings are written.
File layout: a 4 KiB latrec_hdr followed by entries 32-byte records. v1 field offsets are frozen and v2 fields appended inside the header pad, so a v1 reader parses a v2 file. Readers detect a byte-swapped LATREC_MAGIC. 64-bit hosts only: a 32-bit host can tear the t_ns store.
Installed as <libe3/latrec.h>, so a component that links libe3 includes it from there. Components that do not link libe3 mirror the layout instead, and the stage catalog below carries their ids too, so it stays the one place a new id is checked for collisions.
SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University SPDX-License-Identifier: Apache-2.0
Definition in file latrec.h.
| #define _GNU_SOURCE 1 |
| #define LATREC_CAT | ( | a, | |
| b ) |
Pastes two tokens after expanding them.
| a | first token |
| b | second token |
| #define LATREC_CAT_ | ( | a, | |
| b ) |
| #define LATREC_CLOCK_SLOW_NS 100u |
| #define LATREC_CPUID_ON_STAMP 0 |
Read the cpu id in the stamp only where sched_getcpu() resolves in user space (x86-64 glibc goes through rseq). Elsewhere it can be a getcpu syscall, so the stamp uses the id cached by latrec_refresh_cpu().
| #define LATREC_DEFAULT_DIR "/tmp/latrec" |
Where rings go when the caller does not name a directory. A build-time setting, like the recorder itself: -DLATREC_DEFAULT_DIR=... at configure time. Runtime placement is chosen with latrec_open_in(), or process-wide with latrec_set_output_dir().
| #define LATREC_HAVE_GETCPU 0 |
| #define LATREC_HDR_LEN 4096 |
| #define LATREC_LOAD_ACQUIRE | ( | p | ) |
| #define LATREC_MAGIC 0x31524C41u |
| #define LATREC_PREFETCH_W | ( | p | ) |
| #define LATREC_STATIC_ASSERT | ( | c, | |
| m ) |
Compile-time assertion for C and C++.
| c | condition that has to hold |
| m | message shown when it does not (the pre-C11 fallback drops it) |
| #define LATREC_STORE_RELEASE | ( | p, | |
| v ) |
| #define LATREC_UNLIKELY | ( | x | ) |
| anonymous enum |
| anonymous enum |
| anonymous enum |
| anonymous enum |
|
inlinestatic |
|
inlinestatic |
The seq published by latrec_ctx_set() on this thread.
|
inlinestatic |
Publishes the seq of the message this thread is carrying, for code whose signature cannot receive it: the connector is handed a byte buffer, not a Pdu, libe3's emit entry points are handed neither, and an application's indication or control handler is handed the message alone. Caller and callee run on the same thread. latrec_ctx() reads back 0 from a thread with no ring.
The value is sticky until overwritten, so a producer that wants its records joined to libe3's outbound leg (via LATREC_EMIT_ENTER's aux) sets it once per message, immediately before emitting. Readers must tolerate a stale or zero value rather than treating it as authoritative.
Both directions use it. Outbound, the producer sets it and libe3 reads it into LATREC_EMIT_ENTER's aux. Inbound, libe3 sets it before invoking the application handler, so the handler's own stages can key on the same seq as the DELIVER_BEGIN/DELIVER_DONE pair around them; a dApp reads it with latrec_ctx() on entry rather than being passed it.
| seq | sequence number of the message this thread is carrying |
|
inlinestatic |
|
inlinestatic |
Mean cost of one latrec_now_ns() over 1000 calls, about 30 us in total. Called once per ring at open and recorded in the header.
|
inlinestatic |
|
inlinestatic |
Open a ring under the compiled-in default directory.
| r | ring to initialise |
| name | ring name, see latrec_open_in() |
| entries_log2 | capacity as a power of two |
|
inlinestatic |
Open a ring under dir. name ("process.thread") becomes <dir>/<name>.latrec and must be unique per writer thread across every process sharing the directory: the file is opened O_TRUNC, so a collision discards the other writer's ring. entries_log2 sets the capacity (22 = 4 M records = 128 MiB). dir is created if missing (one level only).
There is no "tracing disabled" return: in a build with LIBE3_ENABLE_LATREC the recorder records. Whether it exists at all is the build's decision.
| r | ring to initialise |
| dir | directory for the file, NULL or empty for LATREC_DEFAULT_DIR |
| name | ring name, "process.thread" |
| entries_log2 | capacity as a power of two |
< Bytes of file header, before the records
< Magic number at the start of a ring file ("ALR1")
< Ring file layout version
< Bytes of file header, before the records
|
inlinestatic |
|
inlinestatic |
| uint64_t latrec_seq_next | ( | void | ) |
| void latrec_set_output_dir | ( | const char * | dir | ) |
Directory the per-thread rings are written to, for the whole process. Call before the first latrec_tls_open*(); rings already open are unaffected. NULL or empty restores LATREC_DEFAULT_DIR. This chooses where, never whether: with LIBE3_ENABLE_LATREC on, the recorder records regardless. Two processes that should not share rings (concurrent tests, one benchmark per directory) each set their own.
| dir | directory for the rings, NULL or empty for LATREC_DEFAULT_DIR |
|
inlinestatic |
Hot path: one clock read and four stores. Single writer per ring.
The disabled-ring check is repeated here deliberately. latrec_now_ns() is an argument, so it would be evaluated before the call even on a thread with no ring, putting a clock read on a path whose whole point is to be free – tests/bench_latrec.cpp holds that path to a 10 ns ceiling and catches it.
| r | ring to stamp |
| seq | sequence number of the message, only the low 48 bits are kept |
| stage | stage id from the catalog |
| aux | stage-specific value |
| aux2 | second stage-specific value |
|
inlinestatic |
Stamp carrying a timestamp the caller already took, for the case where the moment being recorded is not the moment the stamp can be issued: handing an item to another thread is only known to have succeeded after the handoff, by which time the consumer may already have run and stamped its own stage. Reading the clock before the handoff and passing it here keeps the interval meaningful; issuing a fresh stamp afterwards can order the producer's record after the consumer's.
t_ns must be a CLOCK_MONOTONIC reading from latrec_now_ns(). Zero is treated as "not taken" and replaced with the current time, since zero is also how an unwritten slot reads.
| r | ring to stamp |
| seq | sequence number of the message, only the low 48 bits are kept |
| stage | stage id from the catalog |
| aux | stage-specific value |
| aux2 | second stage-specific value |
| t_ns | CLOCK_MONOTONIC reading taken earlier |
| latrec_t * latrec_tls_open | ( | void | ) |
Same as latrec_tls_open_as() with no role.
| latrec_t * latrec_tls_open_as | ( | const char * | role | ) |
Opens the calling thread's ring as <role>.<tid>, or <program>.<tid> when role is NULL or empty; idempotent. Capacity comes from LATREC_ENTRIES_LOG2_<ROLE> (role uppercased, non-alphanumerics as '_'), else LATREC_ENTRIES_LOG2, else the compiled-in default. The open pre-faults the whole mapping, which takes tens of ms; the stamp path does not call it.
| role | ring name prefix, NULL or empty for the program name |
< Per-thread rings tracked for the flush at exit
|
inlinestatic |
Take a timestamp for a later latrec_tstamp_at(). Pairing these two, rather than calling latrec_now_ns() directly, keeps the clock read out of a build with the recorder compiled out: this is the point where the pair collapses to nothing.
|
inlinestatic |
|
inlinestatic |
Stamp into the calling thread's ring, carrying a timestamp taken earlier. See latrec_stamp_at for when that is the correct thing to do.
| seq | sequence number of the message |
| stage | stage id from the catalog |
| aux | stage-specific value |
| aux2 | second stage-specific value |
| t_ns | reading from latrec_tnow() |
|
extern |
Ring of the calling thread. Never NULL: it refers to a disabled ring until the thread calls latrec_tls_open(), so a stamp before that is discarded.