Skip to content

Commit b4dcf4e

Browse files
committed
trace: use a static buffer instead of tal_fmt().
There's an EBPF limit anyway, so stick with a 512-byte buffer. This brings us back to 621ns per trace: Before: real 0m13.441000-14.592000(14.2686+/-0.43)s user 0m11.265000-12.289000(11.9626+/-0.37)s sys 0m2.175000-2.381000(2.3048+/-0.072)s After: 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 Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 3973c35 commit b4dcf4e

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

common/trace.c

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,15 @@ static struct span *trace_span_slot(void)
257257
return s;
258258
}
259259

260+
#define MAX_BUF_SIZE 2048
261+
260262
static void trace_emit(struct span *s)
261263
{
262264
char span_id[HEX_SPAN_ID_SIZE];
263265
char trace_id[HEX_TRACE_ID_SIZE];
264266
char parent_span_id[HEX_SPAN_ID_SIZE];
267+
char buffer[MAX_BUF_SIZE + 1];
268+
size_t len;
265269

266270
/* If this is a remote span it's not up to us to emit it. Make
267271
* this a no-op. `trace_span_end` will take care of cleaning
@@ -275,36 +279,46 @@ static void trace_emit(struct span *s)
275279
if (s->parent)
276280
hex_encode(s->parent_id, SPAN_ID_SIZE, parent_span_id, HEX_SPAN_ID_SIZE);
277281

278-
char *res = tal_fmt(
279-
NULL,
280-
"[{\"id\": \"%s\", \"name\": \"%s\", "
281-
"\"timestamp\": %" PRIu64 ", \"duration\": %" PRIu64 ",",
282-
span_id, s->name, s->start_time, s->end_time - s->start_time);
283-
284-
tal_append_fmt(&res, "\"localEndpoint\": { \"serviceName\": \"%s\"}, ",
285-
trace_service_name);
282+
len = snprintf(buffer, MAX_BUF_SIZE,
283+
"[{\"id\":\"%s\",\"name\":\"%s\","
284+
"\"timestamp\":%"PRIu64",\"duration\":%"PRIu64","
285+
"\"localEndpoint\":{\"serviceName\":\"%s\"},",
286+
span_id, s->name, s->start_time, s->end_time - s->start_time, trace_service_name);
286287

287288
if (s->parent != NULL) {
288-
tal_append_fmt(&res, "\"parentId\": \"%s\",", parent_span_id);
289+
len += snprintf(buffer + len, MAX_BUF_SIZE - len,
290+
"\"parentId\":\"%s\",",
291+
parent_span_id);
292+
if (len > MAX_BUF_SIZE)
293+
len = MAX_BUF_SIZE;
289294
}
290295

291-
tal_append_fmt(&res, "\"tags\": {");
296+
len += snprintf(buffer + len, MAX_BUF_SIZE - len,
297+
"\"tags\":{");
298+
if (len > MAX_BUF_SIZE)
299+
len = MAX_BUF_SIZE;
292300
for (size_t i = 0; i < SPAN_MAX_TAGS; i++) {
293301
if (!s->tags[i].name)
294302
continue;
295-
tal_append_fmt(&res, "%s\"%s\": \"%.*s\"", i == 0 ? "" : ", ",
296-
s->tags[i].name,
297-
s->tags[i].valuelen,
298-
s->tags[i].valuestr);
303+
len += snprintf(buffer + len, MAX_BUF_SIZE - len,
304+
"%s\"%s\":\"%.*s\"", i == 0 ? "" : ", ",
305+
s->tags[i].name,
306+
s->tags[i].valuelen,
307+
s->tags[i].valuestr);
308+
if (len > MAX_BUF_SIZE)
309+
len = MAX_BUF_SIZE;
299310
}
300311

301-
tal_append_fmt(&res, "}, \"traceId\": \"%s\"}]", trace_id);
302-
DTRACE_PROBE2(lightningd, span_emit, span_id, res);
312+
len += snprintf(buffer + len, MAX_BUF_SIZE - len,
313+
"},\"traceId\":\"%s\"}]", trace_id);
314+
if (len > MAX_BUF_SIZE)
315+
len = MAX_BUF_SIZE;
316+
buffer[len] = '\0';
317+
DTRACE_PROBE2(lightningd, span_emit, span_id, buffer);
303318
if (trace_to_file) {
304-
fprintf(trace_to_file, "span_emit %s %s\n", span_id, res);
319+
fprintf(trace_to_file, "span_emit %s %s\n", span_id, buffer);
305320
fflush(trace_to_file);
306321
}
307-
tal_free(res);
308322
}
309323

310324
/**

0 commit comments

Comments
 (0)