1010#include <common/trace.h>
1111#include <sodium/randombytes.h>
1212#include <stdio.h>
13+ #include <unistd.h>
1314
1415#if HAVE_USDT
15- #include <sys/sdt.h>
16+ #include <sys/sdt.h>
1617
1718#define MAX_ACTIVE_SPANS 128
1819
4041#endif
4142
4243const char * trace_service_name = "lightningd" ;
44+ static bool disable_trace = false;
4345
4446struct span_tag {
4547 char * name , * value ;
@@ -176,6 +178,9 @@ static struct span *trace_span_find(size_t key)
176178 return NULL ;
177179}
178180
181+ /* FIXME: Forward declaration for minimal patch size */
182+ static void trace_span_clear (struct span * s );
183+
179184/**
180185 * Find an empty slot for a new span.
181186 */
@@ -187,7 +192,13 @@ static struct span *trace_span_slot(void)
187192
188193 /* Might end up here if we have more than MAX_ACTIVE_SPANS
189194 * concurrent spans. */
190- assert (s );
195+ if (!s ) {
196+ fprintf (stderr , "%u: out of spans, disabling tracing\n" , getpid ());
197+ for (size_t i = 0 ; i < MAX_ACTIVE_SPANS ; i ++ )
198+ trace_span_clear (& active_spans [i ]);
199+ disable_trace = true;
200+ return NULL ;
201+ }
191202 assert (s -> parent == NULL );
192203
193204 /* Be extra careful not to create cycles. If we return the
@@ -260,11 +271,15 @@ void trace_span_start(const char *name, const void *key)
260271 size_t numkey = trace_key (key );
261272 struct timeabs now = time_now ();
262273
274+ if (disable_trace )
275+ return ;
263276 trace_init ();
264277 trace_check_tree ();
265278
266279 assert (trace_span_find (numkey ) == NULL );
267280 struct span * s = trace_span_slot ();
281+ if (!s )
282+ return ;
268283 s -> key = numkey ;
269284 randombytes_buf (s -> id , SPAN_ID_SIZE );
270285 s -> start_time = (now .ts .tv_sec * 1000000 ) + now .ts .tv_nsec / 1000 ;
@@ -293,6 +308,9 @@ void trace_span_remote(u8 trace_id[TRACE_ID_SIZE], u8 span_id[SPAN_ID_SIZE])
293308
294309void trace_span_end (const void * key )
295310{
311+ if (disable_trace )
312+ return ;
313+
296314 size_t numkey = trace_key (key );
297315 struct span * s = trace_span_find (numkey );
298316 assert (s && "Span to end not found" );
@@ -323,6 +341,9 @@ void trace_span_end(const void *key)
323341
324342void trace_span_tag (const void * key , const char * name , const char * value )
325343{
344+ if (disable_trace )
345+ return ;
346+
326347 size_t numkey = trace_key (key );
327348 struct span * span = trace_span_find (numkey );
328349 assert (span );
@@ -341,6 +362,9 @@ void trace_span_tag(const void *key, const char *name, const char *value)
341362
342363void trace_span_suspend_ (const void * key , const char * lbl )
343364{
365+ if (disable_trace )
366+ return ;
367+
344368 size_t numkey = trace_key (key );
345369 struct span * span = trace_span_find (numkey );
346370 TRACE_DBG ("Suspending span %s (%zu)\n" , current -> name , current -> key );
@@ -349,8 +373,33 @@ void trace_span_suspend_(const void *key, const char *lbl)
349373 DTRACE_PROBE1 (lightningd , span_suspend , span -> id );
350374}
351375
376+ static void destroy_trace_span (const void * key )
377+ {
378+ size_t numkey = trace_key (key );
379+ struct span * span = trace_span_find (numkey );
380+
381+ /* It's usually ended normally. */
382+ if (!span )
383+ return ;
384+
385+ /* Otherwise resume so we can terminate it */
386+ trace_span_resume (key );
387+ trace_span_end (key );
388+ }
389+
390+ void trace_span_suspend_may_free_ (const void * key , const char * lbl )
391+ {
392+ if (disable_trace )
393+ return ;
394+ trace_span_suspend_ (key , lbl );
395+ tal_add_destructor (key , destroy_trace_span );
396+ }
397+
352398void trace_span_resume_ (const void * key , const char * lbl )
353399{
400+ if (disable_trace )
401+ return ;
402+
354403 size_t numkey = trace_key (key );
355404 current = trace_span_find (numkey );
356405 TRACE_DBG ("Resuming span %s (%zu)\n" , current -> name , current -> key );
@@ -368,6 +417,7 @@ void trace_cleanup(void)
368417void trace_span_start (const char * name , const void * key ) {}
369418void trace_span_end (const void * key ) {}
370419void trace_span_suspend_ (const void * key , const char * lbl ) {}
420+ void trace_span_suspend_may_free_ (const void * key , const char * lbl ) {}
371421void trace_span_resume_ (const void * key , const char * lbl ) {}
372422void trace_span_tag (const void * key , const char * name , const char * value ) {}
373423void trace_cleanup (void ) {}
0 commit comments