Skip to content

Commit 6a8e586

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 <rusty@rustcorp.com.au> Changelog-Fixed: lightingd: trimmed overhead of tracing infrastructure.
1 parent b4dcf4e commit 6a8e586

File tree

5 files changed

+43
-52
lines changed

5 files changed

+43
-52
lines changed

common/test/Makefile

Lines changed: 1 addition & 0 deletions
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

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "config.h"
22
#include <assert.h>
3+
#include <ccan/endian/endian.h>
34
#include <ccan/err/err.h>
45
#include <ccan/htable/htable.h>
56
#include <ccan/str/hex/hex.h>
@@ -8,9 +9,9 @@
89
#include <ccan/time/time.h>
910
#include <common/json_stream.h>
1011
#include <common/memleak.h>
12+
#include <common/pseudorand.h>
1113
#include <common/trace.h>
1214
#include <fcntl.h>
13-
#include <sodium/randombytes.h>
1415
#include <stdio.h>
1516
#include <unistd.h>
1617

@@ -19,9 +20,6 @@
1920

2021
#define MAX_ACTIVE_SPANS 128
2122

22-
#define HEX_SPAN_ID_SIZE (2*SPAN_ID_SIZE+1)
23-
#define HEX_TRACE_ID_SIZE (2 * TRACE_ID_SIZE + 1)
24-
2523
/* The traceperent format is defined in W3C Trace Context RFC[1].
2624
* Its format is defined as
2725
*
@@ -56,13 +54,13 @@ struct span_tag {
5654

5755
struct span {
5856
/* Our own id */
59-
u8 id[SPAN_ID_SIZE];
57+
u64 id;
6058

6159
/* 0 if we have no parent. */
62-
u8 parent_id[SPAN_ID_SIZE];
60+
u64 parent_id;
6361

6462
/* The trace_id for this span and all its children. */
65-
u8 trace_id[TRACE_ID_SIZE];
63+
u64 trace_id_hi, trace_id_lo;
6664

6765
u64 start_time;
6866
u64 end_time;
@@ -94,7 +92,7 @@ static void init_span(struct span *s,
9492
struct timeabs now = time_now();
9593

9694
s->key = key;
97-
randombytes_buf(s->id, SPAN_ID_SIZE);
95+
s->id = pseudorand_u64();
9896
s->start_time = (now.ts.tv_sec * 1000000) + now.ts.tv_nsec / 1000;
9997
s->parent = parent;
10098
s->name = name;
@@ -103,10 +101,12 @@ static void init_span(struct span *s,
103101
/* If this is a new root span we also need to associate a new
104102
* trace_id with it. */
105103
if (!s->parent) {
106-
randombytes_buf(s->trace_id, TRACE_ID_SIZE);
104+
s->trace_id_hi = pseudorand_u64();
105+
s->trace_id_lo = pseudorand_u64();
107106
} else {
108-
memcpy(s->parent_id, parent->id, SPAN_ID_SIZE);
109-
memcpy(s->trace_id, parent->trace_id, TRACE_ID_SIZE);
107+
s->parent_id = current->id;
108+
s->trace_id_hi = current->trace_id_hi;
109+
s->trace_id_lo = current->trace_id_lo;
110110
}
111111
}
112112

@@ -122,7 +122,9 @@ static void trace_span_clear(struct span *s);
122122
* own parent. */
123123
static void trace_inject_traceparent(void)
124124
{
125-
char *traceparent;
125+
const char *traceparent;
126+
be64 trace_hi, trace_lo, span;
127+
126128
traceparent = getenv("CLN_TRACEPARENT");
127129
if (!traceparent)
128130
return;
@@ -134,14 +136,18 @@ static void trace_inject_traceparent(void)
134136
init_span(current, trace_key(active_spans), "", NULL);
135137
current->remote = true;
136138
assert(current && !current->parent);
137-
if (!hex_decode(traceparent + 3, 2*TRACE_ID_SIZE, current->trace_id,
138-
TRACE_ID_SIZE) ||
139-
!hex_decode(traceparent + 36, 2*SPAN_ID_SIZE, current->id,
140-
SPAN_ID_SIZE)) {
139+
140+
if (!hex_decode(traceparent + 3, 16, &trace_hi, sizeof(trace_hi))
141+
|| !hex_decode(traceparent + 3 + 16, 16, &trace_lo, sizeof(trace_lo))
142+
|| !hex_decode(traceparent + 3 + 16 + 16 + 1, 16, &span, sizeof(span))) {
141143
/* We failed to parse the traceparent, abandon. */
142144
fprintf(stderr, "Failed!");
143145
trace_span_clear(current);
144146
current = NULL;
147+
} else {
148+
current->trace_id_hi = be64_to_cpu(trace_hi);
149+
current->trace_id_lo = be64_to_cpu(trace_lo);
150+
current->id = be64_to_cpu(span);
145151
}
146152
}
147153

@@ -261,9 +267,7 @@ static struct span *trace_span_slot(void)
261267

262268
static void trace_emit(struct span *s)
263269
{
264-
char span_id[HEX_SPAN_ID_SIZE];
265-
char trace_id[HEX_TRACE_ID_SIZE];
266-
char parent_span_id[HEX_SPAN_ID_SIZE];
270+
char span_id[hex_str_size(sizeof(s->id))];
267271
char buffer[MAX_BUF_SIZE + 1];
268272
size_t len;
269273

@@ -273,12 +277,7 @@ static void trace_emit(struct span *s)
273277
if (s->remote)
274278
return;
275279

276-
hex_encode(s->id, SPAN_ID_SIZE, span_id, HEX_SPAN_ID_SIZE);
277-
hex_encode(s->trace_id, TRACE_ID_SIZE, trace_id, HEX_TRACE_ID_SIZE);
278-
279-
if (s->parent)
280-
hex_encode(s->parent_id, SPAN_ID_SIZE, parent_span_id, HEX_SPAN_ID_SIZE);
281-
280+
snprintf(span_id, sizeof(span_id), "%016"PRIx64, s->id);
282281
len = snprintf(buffer, MAX_BUF_SIZE,
283282
"[{\"id\":\"%s\",\"name\":\"%s\","
284283
"\"timestamp\":%"PRIu64",\"duration\":%"PRIu64","
@@ -287,8 +286,8 @@ static void trace_emit(struct span *s)
287286

288287
if (s->parent != NULL) {
289288
len += snprintf(buffer + len, MAX_BUF_SIZE - len,
290-
"\"parentId\":\"%s\",",
291-
parent_span_id);
289+
"\"parentId\":\"%016"PRIx64"\",",
290+
s->parent_id);
292291
if (len > MAX_BUF_SIZE)
293292
len = MAX_BUF_SIZE;
294293
}
@@ -310,10 +309,12 @@ static void trace_emit(struct span *s)
310309
}
311310

312311
len += snprintf(buffer + len, MAX_BUF_SIZE - len,
313-
"},\"traceId\":\"%s\"}]", trace_id);
312+
"},\"traceId\":\"%016"PRIx64"%016"PRIx64"\"}]",
313+
s->trace_id_hi, s->trace_id_lo);
314314
if (len > MAX_BUF_SIZE)
315315
len = MAX_BUF_SIZE;
316316
buffer[len] = '\0';
317+
/* FIXME: span_id here is in hex, could be u64? */
317318
DTRACE_PROBE2(lightningd, span_emit, span_id, buffer);
318319
if (trace_to_file) {
319320
fprintf(trace_to_file, "span_emit %s %s\n", span_id, buffer);
@@ -326,14 +327,7 @@ static void trace_emit(struct span *s)
326327
*/
327328
static void trace_span_clear(struct span *s)
328329
{
329-
s->key = 0;
330-
memset(s->id, 0, SPAN_ID_SIZE);
331-
memset(s->trace_id, 0, TRACE_ID_SIZE);
332-
333-
s->parent = NULL;
334-
s->name = NULL;
335-
for (size_t i = 0; i < SPAN_MAX_TAGS; i++)
336-
s->tags[i].name = NULL;
330+
memset(s, 0, sizeof(*s));
337331
}
338332

339333
void trace_span_start_(const char *name, const void *key)
@@ -354,14 +348,12 @@ void trace_span_start_(const char *name, const void *key)
354348
trace_check_tree();
355349
DTRACE_PROBE1(lightningd, span_start, s->id);
356350
if (trace_to_file) {
357-
char span_id[HEX_SPAN_ID_SIZE];
358-
hex_encode(s->id, SPAN_ID_SIZE, span_id, HEX_SPAN_ID_SIZE);
359-
fprintf(trace_to_file, "span_start %s\n", span_id);
351+
fprintf(trace_to_file, "span_start %016"PRIx64"\n", s->id);
360352
fflush(trace_to_file);
361353
}
362354
}
363355

364-
void trace_span_remote(u8 trace_id[TRACE_ID_SIZE], u8 span_id[SPAN_ID_SIZE])
356+
void trace_span_remote(u64 trace_id_hi, u64 trade_id_lo, u64 span_id)
365357
{
366358
abort();
367359
}
@@ -382,9 +374,7 @@ void trace_span_end(const void *key)
382374
s->end_time = (now.ts.tv_sec * 1000000) + now.ts.tv_nsec / 1000;
383375
DTRACE_PROBE1(lightningd, span_end, s->id);
384376
if (trace_to_file) {
385-
char span_id[HEX_SPAN_ID_SIZE];
386-
hex_encode(s->id, SPAN_ID_SIZE, span_id, HEX_SPAN_ID_SIZE);
387-
fprintf(trace_to_file, "span_end %s\n", span_id);
377+
fprintf(trace_to_file, "span_end %016"PRIx64"\n", s->id);
388378
fflush(trace_to_file);
389379
}
390380
trace_emit(s);
@@ -439,9 +429,7 @@ void trace_span_suspend_(const void *key, const char *lbl)
439429
current = current->parent;
440430
DTRACE_PROBE1(lightningd, span_suspend, span->id);
441431
if (trace_to_file) {
442-
char span_id[HEX_SPAN_ID_SIZE];
443-
hex_encode(span->id, SPAN_ID_SIZE, span_id, HEX_SPAN_ID_SIZE);
444-
fprintf(trace_to_file, "span_suspend %s\n", span_id);
432+
fprintf(trace_to_file, "span_suspend %016"PRIx64"\n", span->id);
445433
fflush(trace_to_file);
446434
}
447435
}
@@ -482,9 +470,7 @@ void trace_span_resume_(const void *key, const char *lbl)
482470
TRACE_DBG("Resuming span %s (%zu)\n", current->name, current->key);
483471
DTRACE_PROBE1(lightningd, span_resume, current->id);
484472
if (trace_to_file) {
485-
char span_id[HEX_SPAN_ID_SIZE];
486-
hex_encode(current->id, SPAN_ID_SIZE, span_id, HEX_SPAN_ID_SIZE);
487-
fprintf(trace_to_file, "span_resume %s\n", span_id);
473+
fprintf(trace_to_file, "span_resume %016"PRIx64"\n", current->id);
488474
fflush(trace_to_file);
489475
}
490476
}

common/trace.h

Lines changed: 1 addition & 3 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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)