libe3 0.6.1
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
latrec.h File Reference

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"

Enumerations

enum  {
  LATREC_RECORD_BEGIN = 0x10 , LATREC_PROCESS_BEGIN = 0x11 , LATREC_ENCODE_E3SM_BEGIN = 0x18 , LATREC_ENCODE_E3SM_DONE = 0x19 ,
  LATREC_DECODE_E3SM_BEGIN = 0x1A , LATREC_DECODE_E3SM_DONE = 0x1B , LATREC_EMIT_ENTER = 0x20 , LATREC_ENQUEUE = 0x21 ,
  LATREC_DEQUEUE = 0x22 , LATREC_ENCODE_E3AP_DONE = 0x23 , LATREC_SEND_DONE = 0x24 , LATREC_RECV = 0x25 ,
  LATREC_DECODE_E3AP_DONE = 0x26 , LATREC_DELIVER_BEGIN = 0x27 , LATREC_DELIVER_DONE = 0x28 , LATREC_REPORT_QUEUED = 0x29 ,
  LATREC_REPORT_DONE = 0x2A , LATREC_SESSION_QUEUED = 0x2B , LATREC_SESSION_POLLED = 0x2C , LATREC_DROP = 0x2F ,
  LATREC_SETUP_BEGIN = 0x30 , LATREC_SETUP_DONE = 0x31 , LATREC_SUB_BEGIN = 0x32 , LATREC_SUB_DONE = 0x33 ,
  LATREC_SM_START = 0x34 , LATREC_SM_STOP = 0x35 , LATREC_SM_STATUS_BEGIN = 0x36 , LATREC_SM_STATUS_DONE = 0x37 ,
  LATREC_PROCESS_DONE = 0x40 , LATREC_CREATE_OUTPUT = 0x41 , LATREC_APPLY_POLICY_DONE = 0x42 , LATREC_ADMITTED = 0x43 ,
  LATREC_APPLY_CONTROL_DONE = 0x48 , LATREC_LIVE_ON_AIR = 0x49 , LATREC_ACK_SENT = 0x4A , LATREC_ACK_RECV = 0x4B ,
  LATREC_BRIDGE_IN = 0x50 , LATREC_BRIDGE_OUT = 0x51 , LATREC_ENCODE_E2AP_BEGIN = 0x58 , LATREC_ENCODE_E2AP_DONE = 0x59 ,
  LATREC_DECODE_E2AP_BEGIN = 0x5A , LATREC_DECODE_E2AP_DONE = 0x5B , LATREC_DECODE_E2SM_BEGIN = 0x60 , LATREC_DECODE_E2SM_DONE = 0x61 ,
  LATREC_XAPP_PROCESS_DONE = 0x62 , LATREC_ENCODE_E2SM_BEGIN = 0x63 , LATREC_ENCODE_E2SM_DONE = 0x64 , LATREC_WAIT_ENTER = 0x70 ,
  LATREC_SKIPPED = 0x71 , LATREC_CONTEXT = 0x72
}
enum  { LATREC_OUT_PROCESSED = 0 , LATREC_OUT_SHED_AGE = 1 , LATREC_OUT_SHED_LAG = 2 , LATREC_OUT_NO_RANGE = 3 }
enum  {
  LATREC_SKIP_NO_SUBSCRIBERS = 1 , LATREC_SKIP_ENCODE = 2 , LATREC_SKIP_COALESCED = 3 , LATREC_SKIP_NO_SHM = 4 ,
  LATREC_SKIP_RANGES_LOST = 5
}
enum  {
  LATREC_DROP_QUEUE_PUSH = 1 , LATREC_DROP_ENCODE = 2 , LATREC_DROP_SEND = 3 , LATREC_DROP_DECODE = 4 ,
  LATREC_DROP_REPORT_QUEUE = 5 , LATREC_DROP_NO_HANDLER = 6 , LATREC_DROP_SESSION_QUEUE = 7 , LATREC_DROP_FILTERED = 8 ,
  LATREC_DROP_SHUTDOWN = 9
}

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

Detailed Description

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.

See also
docs/utilities/latrec.md for the full guide: the clock model, ring naming/sizing, the stage catalog table, and the capture-to-CSV workflow.
docs/control-loops/path-a-e3-loop.md and docs/control-loops/path-b-e2-e3-loop.md for the loop boxes the stage catalog names.

SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University SPDX-License-Identifier: Apache-2.0

Definition in file latrec.h.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE   1

Selects the GNU feature set, defined only when the including file chose none.

Definition at line 57 of file latrec.h.

◆ LATREC_CAT

#define LATREC_CAT ( a,
b )
Value:
#define LATREC_CAT_(a, b)
Definition latrec.h:135

Pastes two tokens after expanding them.

Parameters
afirst token
bsecond token

Definition at line 139 of file latrec.h.

◆ LATREC_CAT_

#define LATREC_CAT_ ( a,
b )
Value:
a##b

Pastes two tokens; use LATREC_CAT so that both are expanded first.

Parameters
afirst token
bsecond token

Definition at line 135 of file latrec.h.

◆ LATREC_CLOCK_SLOW_NS

#define LATREC_CLOCK_SLOW_NS   100u

Measured timestamp cost above which latrec_open warns. A vDSO read is tens of ns; a syscall fallback is several hundred.

Definition at line 403 of file latrec.h.

◆ LATREC_CPUID_ON_STAMP

#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().

Definition at line 129 of file latrec.h.

◆ LATREC_DEFAULT_DIR

#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().

Definition at line 498 of file latrec.h.

◆ LATREC_HAVE_GETCPU

#define LATREC_HAVE_GETCPU   0

1 where sched_getcpu() is available (Linux), else 0.

Definition at line 120 of file latrec.h.

◆ LATREC_HDR_LEN

#define LATREC_HDR_LEN   4096

Bytes of file header, before the records

Definition at line 399 of file latrec.h.

◆ LATREC_LOAD_ACQUIRE

#define LATREC_LOAD_ACQUIRE ( p)
Value:
(*(p))

Load with acquire ordering.

Parameters
plocation to read

Definition at line 101 of file latrec.h.

◆ LATREC_MAGIC

#define LATREC_MAGIC   0x31524C41u

Magic number at the start of a ring file ("ALR1")

Definition at line 397 of file latrec.h.

◆ LATREC_PREFETCH_W

#define LATREC_PREFETCH_W ( p)
Value:
((void)(p))

Hint that the cache line at p is about to be written.

Parameters
paddress of the line

Definition at line 102 of file latrec.h.

◆ LATREC_STATIC_ASSERT

#define LATREC_STATIC_ASSERT ( c,
m )
Value:
typedef char LATREC_CAT(latrec_static_assert_, __LINE__)[(c) ? 1 : -1]
#define LATREC_CAT(a, b)
Definition latrec.h:139

Compile-time assertion for C and C++.

Parameters
ccondition that has to hold
mmessage shown when it does not (the pre-C11 fallback drops it)

Definition at line 148 of file latrec.h.

◆ LATREC_STORE_RELEASE

#define LATREC_STORE_RELEASE ( p,
v )
Value:
do { *(p) = (v); } while (0)

Store with release ordering, so earlier stores are visible first.

Parameters
plocation to store to
vvalue to store

Definition at line 100 of file latrec.h.

◆ LATREC_UNLIKELY

#define LATREC_UNLIKELY ( x)
Value:
(x)

Branch hint: x is expected to be false.

Parameters
xcondition

Definition at line 103 of file latrec.h.

◆ LATREC_VERSION

#define LATREC_VERSION   2u

Ring file layout version

Definition at line 398 of file latrec.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
LATREC_DROP_QUEUE_PUSH 
LATREC_DROP_ENCODE 
LATREC_DROP_SEND 
LATREC_DROP_DECODE 
LATREC_DROP_REPORT_QUEUE 
LATREC_DROP_NO_HANDLER 
LATREC_DROP_SESSION_QUEUE 
LATREC_DROP_FILTERED 
LATREC_DROP_SHUTDOWN 

Definition at line 378 of file latrec.h.

◆ anonymous enum

anonymous enum
Enumerator
LATREC_OUT_PROCESSED 
LATREC_OUT_SHED_AGE 
LATREC_OUT_SHED_LAG 
LATREC_OUT_NO_RANGE 

Definition at line 356 of file latrec.h.

◆ anonymous enum

anonymous enum
Enumerator
LATREC_RECORD_BEGIN 
LATREC_PROCESS_BEGIN 
LATREC_ENCODE_E3SM_BEGIN 
LATREC_ENCODE_E3SM_DONE 
LATREC_DECODE_E3SM_BEGIN 
LATREC_DECODE_E3SM_DONE 
LATREC_EMIT_ENTER 
LATREC_ENQUEUE 
LATREC_DEQUEUE 
LATREC_ENCODE_E3AP_DONE 
LATREC_SEND_DONE 
LATREC_RECV 
LATREC_DECODE_E3AP_DONE 
LATREC_DELIVER_BEGIN 
LATREC_DELIVER_DONE 
LATREC_REPORT_QUEUED 
LATREC_REPORT_DONE 
LATREC_SESSION_QUEUED 
LATREC_SESSION_POLLED 
LATREC_DROP 
LATREC_SETUP_BEGIN 
LATREC_SETUP_DONE 
LATREC_SUB_BEGIN 
LATREC_SUB_DONE 
LATREC_SM_START 
LATREC_SM_STOP 
LATREC_SM_STATUS_BEGIN 
LATREC_SM_STATUS_DONE 
LATREC_PROCESS_DONE 
LATREC_CREATE_OUTPUT 
LATREC_APPLY_POLICY_DONE 
LATREC_ADMITTED 
LATREC_APPLY_CONTROL_DONE 
LATREC_LIVE_ON_AIR 
LATREC_ACK_SENT 
LATREC_ACK_RECV 
LATREC_BRIDGE_IN 
LATREC_BRIDGE_OUT 
LATREC_ENCODE_E2AP_BEGIN 
LATREC_ENCODE_E2AP_DONE 
LATREC_DECODE_E2AP_BEGIN 
LATREC_DECODE_E2AP_DONE 
LATREC_DECODE_E2SM_BEGIN 
LATREC_DECODE_E2SM_DONE 
LATREC_XAPP_PROCESS_DONE 
LATREC_ENCODE_E2SM_BEGIN 
LATREC_ENCODE_E2SM_DONE 
LATREC_WAIT_ENTER 
LATREC_SKIPPED 
LATREC_CONTEXT 

Definition at line 172 of file latrec.h.

◆ anonymous enum

anonymous enum
Enumerator
LATREC_SKIP_NO_SUBSCRIBERS 
LATREC_SKIP_ENCODE 
LATREC_SKIP_COALESCED 
LATREC_SKIP_NO_SHM 
LATREC_SKIP_RANGES_LOST 

Definition at line 366 of file latrec.h.

Function Documentation

◆ latrec_close()

void latrec_close ( latrec_t * r)
inlinestatic

Record the final counts and clock pair, flush and unmap the ring, and disable it. Does nothing on a ring that is not open.

Parameters
rring to close

Definition at line 650 of file latrec.h.

◆ latrec_ctx()

uint64_t latrec_ctx ( void )
inlinestatic

The seq published by latrec_ctx_set() on this thread.

Returns
the published seq, 0 from a thread with no ring

Definition at line 751 of file latrec.h.

◆ latrec_ctx_set()

void latrec_ctx_set ( uint64_t seq)
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.

Parameters
seqsequence number of the message this thread is carrying

Definition at line 742 of file latrec.h.

◆ latrec_heartbeat()

void latrec_heartbeat ( latrec_t * r)
inlinestatic

Refresh rec_count and the closing clock pair without closing the ring. Call periodically so a ring that is never closed still carries both.

Parameters
rring to refresh

Definition at line 638 of file latrec.h.

◆ latrec_measure_clock_ns()

uint32_t latrec_measure_clock_ns ( void )
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.

Returns
mean nanoseconds per call, 0 when the measurement is unusable

Definition at line 482 of file latrec.h.

◆ latrec_now_ns()

uint64_t latrec_now_ns ( void )
inlinestatic

Current CLOCK_MONOTONIC reading.

Returns
nanoseconds on the monotonic clock

Definition at line 450 of file latrec.h.

◆ latrec_open()

int latrec_open ( latrec_t * r,
const char * name,
unsigned entries_log2 )
inlinestatic

Open a ring under the compiled-in default directory.

Parameters
rring to initialise
namering name, see latrec_open_in()
entries_log2capacity as a power of two
Returns
1 on success, -1 on error

Definition at line 572 of file latrec.h.

◆ latrec_open_in()

int latrec_open_in ( latrec_t * r,
const char * dir,
const char * name,
unsigned entries_log2 )
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.

Parameters
rring to initialise
dirdirectory for the file, NULL or empty for LATREC_DEFAULT_DIR
namering name, "process.thread"
entries_log2capacity as a power of two
Returns
1 on success, -1 on error

< 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

Definition at line 514 of file latrec.h.

◆ latrec_real_ns()

uint64_t latrec_real_ns ( void )
inlinestatic

Current CLOCK_REALTIME reading.

Returns
nanoseconds since the Unix epoch

Definition at line 459 of file latrec.h.

◆ latrec_refresh_cpu()

void latrec_refresh_cpu ( latrec_t * r)
inlinestatic

Re-read the cpu id into the ring. Call off the hot path: sched_getcpu() is a syscall on targets where LATREC_CPUID_ON_STAMP is 0.

Parameters
rring whose cached cpu id is refreshed

Definition at line 469 of file latrec.h.

◆ latrec_seq_next()

uint64_t latrec_seq_next ( void )

Next value of the process-wide sequence counter (starts at 1).

Returns
the next sequence number

Definition at line 164 of file latrec.c.

◆ latrec_set_output_dir()

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.

Parameters
dirdirectory for the rings, NULL or empty for LATREC_DEFAULT_DIR

Definition at line 114 of file latrec.c.

◆ latrec_stamp()

void latrec_stamp ( latrec_t * r,
uint64_t seq,
uint8_t stage,
uint64_t aux,
uint64_t aux2 )
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.

Parameters
rring to stamp
seqsequence number of the message, only the low 48 bits are kept
stagestage id from the catalog
auxstage-specific value
aux2second stage-specific value

Definition at line 628 of file latrec.h.

◆ latrec_stamp_at()

void latrec_stamp_at ( latrec_t * r,
uint64_t seq,
uint8_t stage,
uint64_t aux,
uint64_t aux2,
uint64_t t_ns )
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.

Parameters
rring to stamp
seqsequence number of the message, only the low 48 bits are kept
stagestage id from the catalog
auxstage-specific value
aux2second stage-specific value
t_nsCLOCK_MONOTONIC reading taken earlier

Definition at line 594 of file latrec.h.

◆ latrec_tls_open()

latrec_t * latrec_tls_open ( void )

Same as latrec_tls_open_as() with no role.

Returns
the calling thread's ring, a disabled one if the open failed

Definition at line 162 of file latrec.c.

◆ latrec_tls_open_as()

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.

Parameters
rolering name prefix, NULL or empty for the program name
Returns
the calling thread's ring, a disabled one if the open failed

< Per-thread rings tracked for the flush at exit

Definition at line 122 of file latrec.c.

◆ latrec_tnow()

uint64_t latrec_tnow ( void )
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.

Returns
a CLOCK_MONOTONIC reading, 0 when the recorder is compiled out

Definition at line 713 of file latrec.h.

◆ latrec_tstamp()

void latrec_tstamp ( uint64_t seq,
uint8_t stage,
uint64_t aux,
uint64_t aux2 )
inlinestatic

Stamp into the calling thread's ring.

Parameters
seqsequence number of the message
stagestage id from the catalog
auxstage-specific value
aux2second stage-specific value

Definition at line 720 of file latrec.h.

◆ latrec_tstamp_at()

void latrec_tstamp_at ( uint64_t seq,
uint8_t stage,
uint64_t aux,
uint64_t aux2,
uint64_t t_ns )
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.

Parameters
seqsequence number of the message
stagestage id from the catalog
auxstage-specific value
aux2second stage-specific value
t_nsreading from latrec_tnow()

Definition at line 702 of file latrec.h.

Variable Documentation

◆ latrec_tls

__thread latrec_t* latrec_tls
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.

Definition at line 45 of file latrec.c.