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

Per-thread ring storage and sequence allocator for latrec. More...

#include "libe3/latrec.h"
#include <errno.h>
#include <pthread.h>
#include <sys/syscall.h>

Go to the source code of this file.

Macros

#define LATREC_TLS_ENTRIES_LOG2   18
#define LATREC_ENTRIES_LOG2_MIN   12
#define LATREC_ENTRIES_LOG2_MAX   28
#define LATREC_MAX_RINGS   64

Functions

static void flush_open_rings (void)
static void install_atexit (void)
static unsigned entries_log2_from (const char *name)
static unsigned entries_log2 (const char *role)
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)

Variables

static latrec_t disabled_ring
__thread latrec_t * latrec_tls = &disabled_ring
static latrec_t * open_rings [64]
static int n_open_rings
static pthread_mutex_t rings_mu = PTHREAD_MUTEX_INITIALIZER
static pthread_once_t atexit_once = PTHREAD_ONCE_INIT
static char output_dir [512]

Detailed Description

Per-thread ring storage and sequence allocator for latrec.

latrec.h is usable on its own with an explicitly owned latrec_t. This file backs the convenience API on top of it: one ring per calling thread, opened on first stamp and named <prefix>.<tid>, plus the process-wide sequence counter. Keeping the storage in one translation unit gives a single ring per thread rather than one per includer.

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

Definition in file latrec.c.

Macro Definition Documentation

◆ LATREC_ENTRIES_LOG2_MAX

#define LATREC_ENTRIES_LOG2_MAX   28

Largest ring capacity accepted from the environment, as a power of two

Definition at line 37 of file latrec.c.

◆ LATREC_ENTRIES_LOG2_MIN

#define LATREC_ENTRIES_LOG2_MIN   12

Smallest ring capacity accepted from the environment, as a power of two

Definition at line 36 of file latrec.c.

◆ LATREC_MAX_RINGS

#define LATREC_MAX_RINGS   64

Per-thread rings tracked for the flush at exit

Definition at line 38 of file latrec.c.

◆ LATREC_TLS_ENTRIES_LOG2

#define LATREC_TLS_ENTRIES_LOG2   18

Capacity of a per-thread ring, as a power of two. The mapping is pre-faulted at open, so the ring is resident and a stamp costs the same at any size; the size sets the footprint and how long a run fits before wrapping. It is file-backed, so that footprint is page cache and disk, not anonymous memory.

Every thread that stamps in a LIBE3_ENABLE_LATREC build opens a ring, with no per-run opt-out, so this default is what an ordinary run of an enabled build costs per thread. It is deliberately modest for that reason: a long capture raises it per role (see entries_log2() below and docs/utilities/latrec.md) rather than every test and example paying for a capacity only a benchmark needs. A ring that wrapped says so: rec_count keeps counting past the capacity. 256 K records = 8 MiB per thread

Definition at line 34 of file latrec.c.

Function Documentation

◆ entries_log2()

unsigned entries_log2 ( const char * role)
static

Ring capacity for a role: LATREC_ENTRIES_LOG2_<ROLE> if set, else LATREC_ENTRIES_LOG2, else the compiled-in default. The per-role name is the role uppercased with every non-alphanumeric character replaced by '_', so role "oai.mac" reads LATREC_ENTRIES_LOG2_OAI_MAC. Roles differ by orders of magnitude in stamp rate, so sizing them together either wastes memory or wraps the busiest ring.

Parameters
rolering role, NULL or empty for no per-role override
Returns
the capacity as a power of two

< 256 K records = 8 MiB per thread

Definition at line 91 of file latrec.c.

◆ entries_log2_from()

unsigned entries_log2_from ( const char * name)
static

LATREC_ENTRIES_LOG2 overrides the compiled-in capacity; out-of-range and unparseable values fall back to it.

Parameters
nameenvironment variable to read
Returns
the capacity as a power of two, 0 when the variable is unset or invalid

< Smallest ring capacity accepted from the environment, as a power of two

< Largest ring capacity accepted from the environment, as a power of two

< Smallest ring capacity accepted from the environment, as a power of two

< Largest ring capacity accepted from the environment, as a power of two

Definition at line 69 of file latrec.c.

◆ flush_open_rings()

void flush_open_rings ( void )
static

Threads do not close their rings, so rec_count and the closing clock pair are refreshed at exit. Records are valid without this (t_ns != 0); it only fills in the header counts.

Definition at line 55 of file latrec.c.

◆ install_atexit()

void install_atexit ( void )
static

Register flush_open_rings() to run at process exit.

Definition at line 63 of file latrec.c.

◆ 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_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.

Variable Documentation

◆ atexit_once

pthread_once_t atexit_once = PTHREAD_ONCE_INIT
static

installs the exit handler once

Definition at line 50 of file latrec.c.

◆ disabled_ring

latrec_t disabled_ring
static

Threads refer here until they open a ring of their own. It stays zeroed, so enabled == 0 and a stamp from a thread that never called latrec_tls_open() is discarded at the same check that disables tracing globally.

Definition at line 43 of file latrec.c.

◆ latrec_tls

__thread latrec_t* latrec_tls = &disabled_ring

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.

◆ n_open_rings

int n_open_rings
static

entries used in open_rings

Definition at line 48 of file latrec.c.

◆ open_rings

latrec_t* open_rings[64]
static

rings to flush at exit

Definition at line 47 of file latrec.c.

◆ output_dir

char output_dir[512]
static

Process-wide ring directory. Written once at start-up by whoever owns the process, read by every thread that opens a ring; the mutex is only there to make that publication well-defined, since ring opens are not on a hot path.

Definition at line 112 of file latrec.c.

◆ rings_mu

pthread_mutex_t rings_mu = PTHREAD_MUTEX_INITIALIZER
static

guards open_rings and output_dir

Definition at line 49 of file latrec.c.