libe3 0.6.1
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
latrec.c
Go to the documentation of this file.
1/**
2 * @file latrec.c
3 * @brief Per-thread ring storage and sequence allocator for latrec.
4 *
5 * latrec.h is usable on its own with an explicitly owned latrec_t. This file
6 * backs the convenience API on top of it: one ring per calling thread, opened
7 * on first stamp and named `<prefix>.<tid>`, plus the process-wide sequence
8 * counter. Keeping the storage in one translation unit gives a single ring per
9 * thread rather than one per includer.
10 *
11 * SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
12 * SPDX-License-Identifier: Apache-2.0
13 */
14
15#include "libe3/latrec.h"
16
17#include <errno.h> /* program_invocation_short_name */
18#include <pthread.h>
19#include <sys/syscall.h>
20
21/** Capacity of a per-thread ring, as a power of two. The mapping is pre-faulted
22 * at open, so the ring is resident and a stamp costs the same at any size; the
23 * size sets the footprint and how long a run fits before wrapping. It is
24 * file-backed, so that footprint is page cache and disk, not anonymous memory.
25 *
26 * Every thread that stamps in a LIBE3_ENABLE_LATREC build opens a ring, with
27 * no per-run opt-out, so this default is what an ordinary run of an enabled
28 * build costs per thread. It is deliberately modest for that reason: a long
29 * capture raises it per role (see entries_log2() below and docs/utilities/latrec.md)
30 * rather than every test and example paying for a capacity only a benchmark
31 * needs. A ring that wrapped says so: rec_count keeps counting past the
32 * capacity. */
33#ifndef LATREC_TLS_ENTRIES_LOG2
34#define LATREC_TLS_ENTRIES_LOG2 18 /**< 256 K records = 8 MiB per thread */
35#endif
36#define LATREC_ENTRIES_LOG2_MIN 12 /**< Smallest ring capacity accepted from the environment, as a power of two */
37#define LATREC_ENTRIES_LOG2_MAX 28 /**< Largest ring capacity accepted from the environment, as a power of two */
38#define LATREC_MAX_RINGS 64 /**< Per-thread rings tracked for the flush at exit */
39
40/** Threads refer here until they open a ring of their own. It stays zeroed, so
41 * enabled == 0 and a stamp from a thread that never called latrec_tls_open()
42 * is discarded at the same check that disables tracing globally. */
44
46
47static latrec_t* open_rings[LATREC_MAX_RINGS]; /**< rings to flush at exit */
48static int n_open_rings; /**< entries used in open_rings */
49static pthread_mutex_t rings_mu = PTHREAD_MUTEX_INITIALIZER; /**< guards open_rings and output_dir */
50static pthread_once_t atexit_once = PTHREAD_ONCE_INIT; /**< installs the exit handler once */
51
52/** Threads do not close their rings, so rec_count and the closing clock pair
53 * are refreshed at exit. Records are valid without this (t_ns != 0); it only
54 * fills in the header counts. */
55static void flush_open_rings(void)
56{
57 pthread_mutex_lock(&rings_mu);
58 for (int i = 0; i < n_open_rings; i++) latrec_heartbeat(open_rings[i]);
59 pthread_mutex_unlock(&rings_mu);
60}
61
62/** Register flush_open_rings() to run at process exit. */
63static void install_atexit(void) { atexit(flush_open_rings); }
64
65/** LATREC_ENTRIES_LOG2 overrides the compiled-in capacity; out-of-range and
66 * unparseable values fall back to it.
67 * @param name environment variable to read
68 * @return the capacity as a power of two, 0 when the variable is unset or invalid */
69static unsigned entries_log2_from(const char* name)
70{
71 const char* e = getenv(name);
72 if (e && *e) {
73 char* end = NULL;
74 long v = strtol(e, &end, 10);
75 if (end && !*end && v >= LATREC_ENTRIES_LOG2_MIN && v <= LATREC_ENTRIES_LOG2_MAX)
76 return (unsigned)v;
77 fprintf(stderr, "[latrec] ignoring %s=%s (expected %d..%d)\n",
79 }
80 return 0;
81}
82
83/** Ring capacity for a role: LATREC_ENTRIES_LOG2_<ROLE> if set, else
84 * LATREC_ENTRIES_LOG2, else the compiled-in default. The per-role name is the
85 * role uppercased with every non-alphanumeric character replaced by '_', so
86 * role "oai.mac" reads LATREC_ENTRIES_LOG2_OAI_MAC. Roles differ by orders of
87 * magnitude in stamp rate, so sizing them together either wastes memory or
88 * wraps the busiest ring.
89 * @param role ring role, NULL or empty for no per-role override
90 * @return the capacity as a power of two */
91static unsigned entries_log2(const char* role)
92{
93 if (role && *role) {
94 char name[128];
95 int n = snprintf(name, sizeof(name), "LATREC_ENTRIES_LOG2_%s", role);
96 if (n > 0 && (size_t)n < sizeof(name)) {
97 for (char* c = name + sizeof("LATREC_ENTRIES_LOG2_") - 1; *c; c++)
98 *c = (*c >= 'a' && *c <= 'z') ? (char)(*c - 'a' + 'A')
99 : ((*c >= 'A' && *c <= 'Z') || (*c >= '0' && *c <= '9')) ? *c : '_';
100 const unsigned v = entries_log2_from(name);
101 if (v)
102 return v;
103 }
104 }
105 const unsigned v = entries_log2_from("LATREC_ENTRIES_LOG2");
106 return v ? v : LATREC_TLS_ENTRIES_LOG2;
107}
108
109/** Process-wide ring directory. Written once at start-up by whoever owns the
110 * process, read by every thread that opens a ring; the mutex is only there to
111 * make that publication well-defined, since ring opens are not on a hot path. */
112static char output_dir[512];
113
114void latrec_set_output_dir(const char* dir)
115{
116 pthread_mutex_lock(&rings_mu);
117 if (dir && *dir) snprintf(output_dir, sizeof(output_dir), "%s", dir);
118 else output_dir[0] = '\0';
119 pthread_mutex_unlock(&rings_mu);
120}
121
122latrec_t* latrec_tls_open_as(const char* role)
123{
124 if (latrec_tls != &disabled_ring) return latrec_tls; /* already opened */
125
126 /* Heap, not TLS: flush_open_rings() and the mapping are still live at
127 * process exit, while a thread's TLS is released when it exits. Not
128 * freed, like the mapping. */
129 latrec_t* r = (latrec_t*)calloc(1, sizeof(*r));
130 if (!r) return latrec_tls; /* stays disabled */
131
132 if (!role || !*role) {
133#ifdef __GLIBC__
134 role = program_invocation_short_name;
135#else
136 role = "latrec";
137#endif
138 }
139 char name[64];
140 snprintf(name, sizeof(name), "%s.%ld", role, (long)syscall(SYS_gettid));
141
142 char dir[sizeof(output_dir)];
143 pthread_mutex_lock(&rings_mu);
144 memcpy(dir, output_dir, sizeof(dir));
145 pthread_mutex_unlock(&rings_mu);
146
147 /* A failed open leaves a disabled ring, which every later latrec_stamp()
148 * skips at the enabled check. */
149 latrec_open_in(r, dir, name, entries_log2(role));
150 latrec_tls = r;
151
152 if (r->enabled) {
153 pthread_once(&atexit_once, install_atexit);
154 pthread_mutex_lock(&rings_mu);
155 /* Beyond the cap a ring still records; it gets no exit flush. */
157 pthread_mutex_unlock(&rings_mu);
158 }
159 return latrec_tls;
160}
161
163
164uint64_t latrec_seq_next(void)
165{
166 static uint64_t counter;
167 return __atomic_add_fetch(&counter, 1, __ATOMIC_RELAXED);
168}
static pthread_mutex_t rings_mu
Definition latrec.c:49
static void install_atexit(void)
Definition latrec.c:63
static void flush_open_rings(void)
Definition latrec.c:55
latrec_t * latrec_tls_open(void)
Definition latrec.c:162
void latrec_set_output_dir(const char *dir)
Definition latrec.c:114
#define LATREC_MAX_RINGS
Definition latrec.c:38
static unsigned entries_log2(const char *role)
Definition latrec.c:91
static unsigned entries_log2_from(const char *name)
Definition latrec.c:69
uint64_t latrec_seq_next(void)
Definition latrec.c:164
latrec_t * latrec_tls_open_as(const char *role)
Definition latrec.c:122
static int n_open_rings
Definition latrec.c:48
static latrec_t disabled_ring
Definition latrec.c:43
#define LATREC_ENTRIES_LOG2_MAX
Definition latrec.c:37
static pthread_once_t atexit_once
Definition latrec.c:50
static char output_dir[512]
Definition latrec.c:112
#define LATREC_TLS_ENTRIES_LOG2
Definition latrec.c:34
static latrec_t * open_rings[64]
Definition latrec.c:47
#define LATREC_ENTRIES_LOG2_MIN
Definition latrec.c:36
Lock-free per-thread latency stamp recorder (header-only, C99).
static int latrec_open_in(latrec_t *r, const char *dir, const char *name, unsigned entries_log2)
Definition latrec.h:514
static void latrec_heartbeat(latrec_t *r)
Definition latrec.h:638
__thread latrec_t * latrec_tls
Definition latrec.c:45
int enabled
Definition latrec.h:441