libe3 0.2.5
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 /* "ALR1" */
#define LATREC_VERSION   2u
#define LATREC_HDR_LEN   4096
#define LATREC_CLOCK_SLOW_NS   100u
#define LATREC_DEFAULT_DIR   "/tmp/latrec"

Typedefs

typedef char latrec_static_assert___LINE__[(sizeof(latrec_rec)==32) ? 1 :-1]

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)
static void latrec_set_output_dir (const char *dir)
static latrec_t * latrec_tls_open_as (const char *role)
static latrec_t * latrec_tls_open (void)
static uint64_t latrec_seq_next (void)
static void latrec_tstamp (uint64_t seq, uint8_t stage, uint64_t aux, uint64_t aux2)
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_ctx_set (uint64_t seq)
static uint64_t latrec_ctx (void)

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/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/latrec.md for the full guide: the clock model, ring naming/sizing, the stage catalog table, and the capture-to-CSV workflow.
docs/path-a-e3-loop.md and docs/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

Definition at line 56 of file latrec.h.

◆ LATREC_CAT

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

Definition at line 117 of file latrec.h.

◆ LATREC_CAT_

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

Definition at line 116 of file latrec.h.

◆ LATREC_CLOCK_SLOW_NS

#define LATREC_CLOCK_SLOW_NS   100u

Definition at line 377 of file latrec.h.

◆ LATREC_CPUID_ON_STAMP

#define LATREC_CPUID_ON_STAMP   0

Definition at line 113 of file latrec.h.

◆ LATREC_DEFAULT_DIR

#define LATREC_DEFAULT_DIR   "/tmp/latrec"

Definition at line 463 of file latrec.h.

◆ LATREC_HAVE_GETCPU

#define LATREC_HAVE_GETCPU   0

Definition at line 104 of file latrec.h.

◆ LATREC_HDR_LEN

#define LATREC_HDR_LEN   4096

Definition at line 373 of file latrec.h.

◆ LATREC_LOAD_ACQUIRE

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

Definition at line 87 of file latrec.h.

◆ LATREC_MAGIC

#define LATREC_MAGIC   0x31524C41u /* "ALR1" */

Definition at line 371 of file latrec.h.

◆ LATREC_PREFETCH_W

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

Definition at line 88 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:117

Definition at line 123 of file latrec.h.

◆ LATREC_STORE_RELEASE

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

Definition at line 86 of file latrec.h.

◆ LATREC_UNLIKELY

#define LATREC_UNLIKELY ( x)
Value:
(x)

Definition at line 89 of file latrec.h.

◆ LATREC_VERSION

#define LATREC_VERSION   2u

Definition at line 372 of file latrec.h.

Typedef Documentation

◆ latrec_static_assert___LINE__

typedef char latrec_static_assert___LINE__[(sizeof(latrec_rec)==32) ? 1 :-1]

Definition at line 403 of file latrec.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
LATREC_OUT_PROCESSED 
LATREC_OUT_SHED_AGE 
LATREC_OUT_SHED_LAG 
LATREC_OUT_NO_RANGE 

Definition at line 330 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 340 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 147 of file latrec.h.

◆ 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 352 of file latrec.h.

Function Documentation

◆ latrec_close()

void latrec_close ( latrec_t * r)
inlinestatic

Definition at line 592 of file latrec.h.

◆ latrec_ctx()

uint64_t latrec_ctx ( void )
inlinestatic

Definition at line 697 of file latrec.h.

◆ latrec_ctx_set()

void latrec_ctx_set ( uint64_t seq)
inlinestatic

Definition at line 696 of file latrec.h.

◆ latrec_heartbeat()

void latrec_heartbeat ( latrec_t * r)
inlinestatic

Definition at line 583 of file latrec.h.

◆ latrec_measure_clock_ns()

uint32_t latrec_measure_clock_ns ( void )
inlinestatic

Definition at line 447 of file latrec.h.

◆ latrec_now_ns()

uint64_t latrec_now_ns ( void )
inlinestatic

Definition at line 419 of file latrec.h.

◆ latrec_open()

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

Definition at line 529 of file latrec.h.

◆ latrec_open_in()

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

Definition at line 475 of file latrec.h.

◆ latrec_real_ns()

uint64_t latrec_real_ns ( void )
inlinestatic

Definition at line 426 of file latrec.h.

◆ latrec_refresh_cpu()

void latrec_refresh_cpu ( latrec_t * r)
inlinestatic

Definition at line 435 of file latrec.h.

◆ latrec_seq_next()

uint64_t latrec_seq_next ( void )
inlinestatic

Definition at line 684 of file latrec.h.

◆ latrec_set_output_dir()

void latrec_set_output_dir ( const char * dir)
inlinestatic

Definition at line 681 of file latrec.h.

◆ latrec_stamp()

void latrec_stamp ( latrec_t * r,
uint64_t seq,
uint8_t stage,
uint64_t aux,
uint64_t aux2 )
inlinestatic

Definition at line 574 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

Definition at line 545 of file latrec.h.

◆ latrec_tls_open()

latrec_t * latrec_tls_open ( void )
inlinestatic

Definition at line 683 of file latrec.h.

◆ latrec_tls_open_as()

latrec_t * latrec_tls_open_as ( const char * role)
inlinestatic

Definition at line 682 of file latrec.h.

◆ latrec_tnow()

uint64_t latrec_tnow ( void )
inlinestatic

Definition at line 695 of file latrec.h.

◆ latrec_tstamp()

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

Definition at line 685 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

Definition at line 689 of file latrec.h.