Skip to content

Commit c520ab1

Browse files
committed
trace: don't use randombytes_buf(), use pseudorand.
This is much faster to give 64 bits of data, and we don't need cryptographic randomness. This brings us back to 413ns per trace. Before: real 0m5.819000-6.472000(6.2064+/-0.26)s user 0m3.779000-4.101000(3.956+/-0.12)s sys 0m2.040000-2.431000(2.2496+/-0.15)s After: real 0m3.981000-4.247000(4.1276+/-0.11)s user 0m3.979000-4.245000(4.126+/-0.11)s sys 0m0.000000-0.002000(0.001+/-0.00063)s Signed-off-by: Rusty Russell <[email protected]> Changelog-Fixed: lightingd: trimmed overhead of tracing infrastructure.
1 parent 2d25462 commit c520ab1

File tree

5 files changed

+39
-40
lines changed

5 files changed

+39
-40
lines changed

common/test/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ common/test/run-splice_script: \
118118
common/test/run-trace: \
119119
common/amount.o \
120120
common/memleak.o \
121+
common/pseudorand.o \
121122
common/trace.o \
122123
wire/fromwire.o \
123124
wire/towire.o

common/trace.c

+31-37
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#include "config.h"
22
#include <assert.h>
3+
#include <ccan/endian/endian.h>
34
#include <ccan/htable/htable.h>
45
#include <ccan/str/hex/hex.h>
56
#include <ccan/tal/tal.h>
67
#include <ccan/time/time.h>
78
#include <common/json_stream.h>
89
#include <common/memleak.h>
10+
#include <common/pseudorand.h>
911
#include <common/trace.h>
10-
#include <sodium/randombytes.h>
1112
#include <stdio.h>
1213
#include <unistd.h>
1314

@@ -16,9 +17,6 @@
1617

1718
#define MAX_ACTIVE_SPANS 128
1819

19-
#define HEX_SPAN_ID_SIZE (2*SPAN_ID_SIZE+1)
20-
#define HEX_TRACE_ID_SIZE (2 * TRACE_ID_SIZE + 1)
21-
2220
/* The traceperent format is defined in W3C Trace Context RFC[1].
2321
* Its format is defined as
2422
*
@@ -52,13 +50,13 @@ struct span_tag {
5250

5351
struct span {
5452
/* Our own id */
55-
u8 id[SPAN_ID_SIZE];
53+
u64 id;
5654

5755
/* 0 if we have no parent. */
58-
u8 parent_id[SPAN_ID_SIZE];
56+
u64 parent_id;
5957

6058
/* The trace_id for this span and all its children. */
61-
u8 trace_id[TRACE_ID_SIZE];
59+
u64 trace_id_hi, trace_id_lo;
6260

6361
u64 start_time;
6462
u64 end_time;
@@ -88,7 +86,9 @@ static struct span *current;
8886
* own parent. */
8987
static void trace_inject_traceparent(void)
9088
{
91-
char *traceparent;
89+
const char *traceparent;
90+
be64 trace_hi, trace_lo, span;
91+
9292
traceparent = getenv("CLN_TRACEPARENT");
9393
if (!traceparent)
9494
return;
@@ -97,13 +97,17 @@ static void trace_inject_traceparent(void)
9797
trace_span_start("", active_spans);
9898
current->remote = true;
9999
assert(current && !current->parent);
100-
if (!hex_decode(traceparent + 3, 2*TRACE_ID_SIZE, current->trace_id,
101-
TRACE_ID_SIZE) ||
102-
!hex_decode(traceparent + 36, 2*SPAN_ID_SIZE, current->id,
103-
SPAN_ID_SIZE)) {
100+
101+
if (!hex_decode(traceparent + 3, 16, &trace_hi, sizeof(trace_hi))
102+
|| !hex_decode(traceparent + 3 + 16, 16, &trace_lo, sizeof(trace_lo))
103+
|| !hex_decode(traceparent + 3 + 16 + 16 + 1, 16, &span, sizeof(span))) {
104104
/* We failed to parse the traceparent, abandon. */
105105
fprintf(stderr, "Failed!");
106106
trace_span_end(active_spans);
107+
} else {
108+
current->trace_id_hi = be64_to_cpu(trace_hi);
109+
current->trace_id_lo = be64_to_cpu(trace_lo);
110+
current->id = be64_to_cpu(span);
107111
}
108112
}
109113

@@ -217,9 +221,7 @@ static struct span *trace_span_slot(void)
217221

218222
static void trace_emit(struct span *s)
219223
{
220-
char span_id[HEX_SPAN_ID_SIZE];
221-
char trace_id[HEX_TRACE_ID_SIZE];
222-
char parent_span_id[HEX_SPAN_ID_SIZE];
224+
char span_id[hex_str_size(sizeof(s->id))];
223225
char buffer[MAX_BUF_SIZE + 1];
224226
size_t len;
225227

@@ -229,12 +231,7 @@ static void trace_emit(struct span *s)
229231
if (s->remote)
230232
return;
231233

232-
hex_encode(s->id, SPAN_ID_SIZE, span_id, HEX_SPAN_ID_SIZE);
233-
hex_encode(s->trace_id, TRACE_ID_SIZE, trace_id, HEX_TRACE_ID_SIZE);
234-
235-
if (s->parent)
236-
hex_encode(s->parent_id, SPAN_ID_SIZE, parent_span_id, HEX_SPAN_ID_SIZE);
237-
234+
sprintf(span_id, "%016"PRIx64, s->id);
238235
len = snprintf(buffer, MAX_BUF_SIZE,
239236
"[{\"id\": \"%s\", \"name\": \"%s\", "
240237
"\"timestamp\": %" PRIu64 ", \"duration\": %" PRIu64 ","
@@ -243,8 +240,8 @@ static void trace_emit(struct span *s)
243240

244241
if (s->parent != NULL) {
245242
len += snprintf(buffer + len, MAX_BUF_SIZE - len,
246-
"\"parentId\": \"%s\",",
247-
parent_span_id);
243+
"\"parentId\": \"%016"PRIx64"\",",
244+
s->parent_id);
248245
if (len > MAX_BUF_SIZE)
249246
len = MAX_BUF_SIZE;
250247
}
@@ -265,10 +262,12 @@ static void trace_emit(struct span *s)
265262
len = MAX_BUF_SIZE;
266263
}
267264
len += snprintf(buffer + len, MAX_BUF_SIZE - len,
268-
"}, \"traceId\": \"%s\"}]", trace_id);
265+
"}, \"traceId\": \"%016"PRIx64"%016"PRIx64"\"}]",
266+
s->trace_id_hi, s->trace_id_lo);
269267
if (len > MAX_BUF_SIZE)
270268
len = MAX_BUF_SIZE;
271269
buffer[len] = '\0';
270+
/* FIXME: span_id here is in hex, could be u64? */
272271
DTRACE_PROBE2(lightningd, span_emit, span_id, buffer);
273272
}
274273

@@ -277,14 +276,7 @@ static void trace_emit(struct span *s)
277276
*/
278277
static void trace_span_clear(struct span *s)
279278
{
280-
s->key = 0;
281-
memset(s->id, 0, SPAN_ID_SIZE);
282-
memset(s->trace_id, 0, TRACE_ID_SIZE);
283-
284-
s->parent = NULL;
285-
s->name = NULL;
286-
for (size_t i = 0; i < SPAN_MAX_TAGS; i++)
287-
s->tags[i].name = NULL;
279+
memset(s, 0, sizeof(*s));
288280
}
289281

290282
void trace_span_start_(const char *name, const void *key)
@@ -302,26 +294,28 @@ void trace_span_start_(const char *name, const void *key)
302294
if (!s)
303295
return;
304296
s->key = numkey;
305-
randombytes_buf(s->id, SPAN_ID_SIZE);
297+
s->id = pseudorand_u64();
306298
s->start_time = (now.ts.tv_sec * 1000000) + now.ts.tv_nsec / 1000;
307299
s->parent = current;
308300
s->name = name;
309301

310302
/* If this is a new root span we also need to associate a new
311303
* trace_id with it. */
312304
if (!current) {
313-
randombytes_buf(s->trace_id, TRACE_ID_SIZE);
305+
s->trace_id_hi = pseudorand_u64();
306+
s->trace_id_lo = pseudorand_u64();
314307
} else {
315-
memcpy(s->parent_id, current->id, SPAN_ID_SIZE);
316-
memcpy(s->trace_id, current->trace_id, TRACE_ID_SIZE);
308+
s->parent_id = current->id;
309+
s->trace_id_hi = current->trace_id_hi;
310+
s->trace_id_lo = current->trace_id_lo;
317311
}
318312

319313
current = s;
320314
trace_check_tree();
321315
DTRACE_PROBE1(lightningd, span_start, s->id);
322316
}
323317

324-
void trace_span_remote(u8 trace_id[TRACE_ID_SIZE], u8 span_id[SPAN_ID_SIZE])
318+
void trace_span_remote(u64 trace_id_hi, u64 trade_id_lo, u64 span_id)
325319
{
326320
abort();
327321
}

common/trace.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include "config.h"
44
#include <ccan/short_types/short_types.h>
55

6-
#define SPAN_ID_SIZE 8
7-
#define TRACE_ID_SIZE 16
86
#undef TRACE_DEBUG
97

108
/* name must be a string constant */
@@ -13,7 +11,7 @@ void trace_span_start_(const char *name, const void *key);
1311
void trace_span_end(const void *key);
1412
void trace_span_tag(const void *key, const char *name, const char *value);
1513
void trace_cleanup(void);
16-
void trace_span_remote(u8 trace_id[TRACE_ID_SIZE], u8 span_id[SPAN_ID_SIZE]);
14+
void trace_span_remote(u64 trace_id_hi, u64 trade_id_lo, u64 span_id);
1715

1816
#define TRACE_LBL __FILE__ ":" stringify(__LINE__)
1917
void trace_span_suspend_(const void *key, const char *lbl);

plugins/bkpr/test/run-bkpr_db.c

+3
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ bool param_check(struct command *cmd UNNEEDED,
219219
const char *buffer UNNEEDED,
220220
const jsmntok_t tokens[] UNNEEDED, ...)
221221
{ fprintf(stderr, "param_check called!\n"); abort(); }
222+
/* Generated stub for pseudorand_u64 */
223+
uint64_t pseudorand_u64(void)
224+
{ fprintf(stderr, "pseudorand_u64 called!\n"); abort(); }
222225
/* Generated stub for toks_alloc */
223226
jsmntok_t *toks_alloc(const tal_t *ctx UNNEEDED)
224227
{ fprintf(stderr, "toks_alloc called!\n"); abort(); }

plugins/bkpr/test/run-recorder.c

+3
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ bool param_check(struct command *cmd UNNEEDED,
225225
const char *buffer UNNEEDED,
226226
const jsmntok_t tokens[] UNNEEDED, ...)
227227
{ fprintf(stderr, "param_check called!\n"); abort(); }
228+
/* Generated stub for pseudorand_u64 */
229+
uint64_t pseudorand_u64(void)
230+
{ fprintf(stderr, "pseudorand_u64 called!\n"); abort(); }
228231
/* Generated stub for toks_alloc */
229232
jsmntok_t *toks_alloc(const tal_t *ctx UNNEEDED)
230233
{ fprintf(stderr, "toks_alloc called!\n"); abort(); }

0 commit comments

Comments
 (0)