libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_trace.h
1/*-
2 * Copyright (c) 2026, The XTC Project
3 * Use of this source code is governed by the ISC License.
4 *
5 * src/inc/xtc_trace.h
6 * Causal message tracing -- libxtc's seq_trace. An opt-in,
7 * bounded ring of trace events (message sends and receives,
8 * process spawns and exits), each stamped with a hybrid logical
9 * clock (HLC) so the true happens-before of a request can be
10 * reconstructed across procs and cores even when wall-clock
11 * arrival order lies.
12 *
13 * The HLC is a 64-bit stamp: the high 48 bits are a monotonic
14 * physical-time component (microseconds) and the low 16 bits a
15 * logical counter. Every traced event ticks one global clock, so
16 * a send's stamp is always less than the stamp of the receive it
17 * causes; a RECV record also carries the originating SEND's stamp
18 * in `cause`, so the causal edge is explicit. (A per-shard HLC for
19 * the MVCC data path is a separate, later
20 * use of the same idea.)
21 *
22 * Tracing is OFF by default and costs a single relaxed atomic load
23 * on the message hot path when disabled. When enabled it serializes
24 * ring writes under a lock; enable it to debug, as with recon/dbg
25 * in the BEAM, not as an always-on production tax.
26 *
27 * See docs/guide/debugging.md.
28 */
29
30#ifndef XTC_TRACE_H
31#define XTC_TRACE_H
32
33#include <stddef.h>
34#include <stdint.h>
35
36#include "xtc.h"
37#include "xtc_proc.h"
38
39enum xtc_trace_kind {
40 XTC_TRACE_SEND = 0, /* self sent a message to peer */
41 XTC_TRACE_RECV = 1, /* self received a message from peer */
42 XTC_TRACE_SPAWN = 2, /* self (parent) spawned peer (child) */
43 XTC_TRACE_EXIT = 3 /* self exited; detail = reason */
44};
45
46typedef struct xtc_trace_rec {
47 uint64_t hlc; /* this event's HLC stamp */
48 uint64_t cause; /* RECV: the originating send's HLC; else 0 */
49 int kind; /* enum xtc_trace_kind */
50 xtc_pid_t self; /* the proc the event happened in */
51 xtc_pid_t peer; /* the other proc (dest / source / child) */
52 uint32_t detail; /* SEND/RECV: payload bytes; EXIT: reason */
54
55/* Visit callback: return 0 to continue, nonzero to stop early. */
56typedef int (*xtc_trace_fn)(const xtc_trace_rec_t *rec, void *user);
57
58/*
59 * PUBLIC: int xtc_trace_enable __P((int));
60 * PUBLIC: int xtc_trace_reset __P((void));
61 * PUBLIC: int xtc_trace_dump __P((xtc_trace_fn, void *));
62 * PUBLIC: uint64_t xtc_hlc_now __P((void));
63 */
64
65/* Turn tracing on (on != 0) or off. Returns the previous state. */
66int xtc_trace_enable(int on);
67
68/* Drop all buffered trace records. Returns XTC_OK. */
69int xtc_trace_reset(void);
70
71/* Visit buffered records in causal (HLC-ascending) order. Returns the
72 * number visited, or a negative XTC_E_* on error. */
73int xtc_trace_dump(xtc_trace_fn cb, void *user);
74
75/* The current global HLC value (for tests and display). */
76uint64_t xtc_hlc_now(void);
77
78#endif /* XTC_TRACE_H */