2
2
#include <assert.h>
3
3
#include <ccan/endian/endian.h>
4
4
#include <ccan/err/err.h>
5
- #include <ccan/htable/htable.h>
6
5
#include <ccan/str/hex/hex.h>
7
6
#include <ccan/tal/str/str.h>
8
7
#include <ccan/tal/tal.h>
18
17
#if HAVE_USDT
19
18
#include <sys/sdt.h>
20
19
21
- #define MAX_ACTIVE_SPANS 128
22
-
23
20
/* The traceperent format is defined in W3C Trace Context RFC[1].
24
21
* Its format is defined as
25
22
*
@@ -123,7 +120,7 @@ static void trace_inject_traceparent(void)
123
120
current = trace_span_slot ();
124
121
assert (current );
125
122
126
- init_span (current , trace_key (active_spans ), "" , NULL );
123
+ init_span (current , trace_key (& active_spans ), "" , NULL );
127
124
assert (current && !current -> parent );
128
125
129
126
if (!hex_decode (traceparent + 3 , 16 , & trace_hi , sizeof (trace_hi ))
@@ -145,7 +142,7 @@ static void trace_inject_traceparent(void)
145
142
/** Quickly print out the entries in the `active_spans`. */
146
143
static void trace_spans_print (void )
147
144
{
148
- for (size_t j = 0 ; j < MAX_ACTIVE_SPANS ; j ++ ) {
145
+ for (size_t j = 0 ; j < tal_count ( active_spans ) ; j ++ ) {
149
146
struct span * s = & active_spans [j ], * parent = s -> parent ;
150
147
TRACE_DBG (" > %zu: %s (key=%zu, parent=%s, "
151
148
"parent_key=%zu)\n" ,
@@ -156,17 +153,17 @@ static void trace_spans_print(void)
156
153
157
154
/** Small helper to check for consistency in the linking. The idea is
158
155
* that we should be able to reach the root (a span without a
159
- * `parent`) in less than `MAX_ACTIVE_SPANS` steps . */
156
+ * `parent`) in less than the number of spans . */
160
157
static void trace_check_tree (void )
161
158
{
162
159
/* `current` is either NULL or a valid entry. */
163
160
164
161
/* Walk the tree structure from leaves to their roots. It
165
- * should not take more than `MAX_ACTIVE_SPANS` . */
162
+ * should not take more than the number of spans . */
166
163
struct span * c ;
167
- for (size_t i = 0 ; i < MAX_ACTIVE_SPANS ; i ++ ) {
164
+ for (size_t i = 0 ; i < tal_count ( active_spans ) ; i ++ ) {
168
165
c = & active_spans [i ];
169
- for (int j = 0 ; j < MAX_ACTIVE_SPANS ; j ++ )
166
+ for (int j = 0 ; j < tal_count ( active_spans ) ; j ++ )
170
167
if (c -> parent == NULL )
171
168
break ;
172
169
else
@@ -189,7 +186,8 @@ static void trace_init(void)
189
186
const char * dev_trace_file ;
190
187
if (active_spans )
191
188
return ;
192
- active_spans = calloc (MAX_ACTIVE_SPANS , sizeof (struct span ));
189
+
190
+ active_spans = notleak (tal_arrz (NULL , struct span , 1 ));
193
191
194
192
current = NULL ;
195
193
dev_trace_file = getenv ("CLN_DEV_TRACE_FILE" );
@@ -213,7 +211,7 @@ static size_t trace_key(const void *key)
213
211
214
212
static struct span * trace_span_find (size_t key )
215
213
{
216
- for (size_t i = 0 ; i < MAX_ACTIVE_SPANS ; i ++ )
214
+ for (size_t i = 0 ; i < tal_count ( active_spans ) ; i ++ )
217
215
if (active_spans [i ].key == key )
218
216
return & active_spans [i ];
219
217
@@ -232,14 +230,12 @@ static struct span *trace_span_slot(void)
232
230
* that, and we should get an empty slot. */
233
231
struct span * s = trace_span_find (0 );
234
232
235
- /* Might end up here if we have more than MAX_ACTIVE_SPANS
236
- * concurrent spans. */
233
+ /* In the unlikely case this fails, double it */
237
234
if (!s ) {
238
- fprintf (stderr , "%u: out of spans, disabling tracing\n" , getpid ());
239
- for (size_t i = 0 ; i < MAX_ACTIVE_SPANS ; i ++ )
240
- trace_span_clear (& active_spans [i ]);
241
- disable_trace = true;
242
- return NULL ;
235
+ TRACE_DBG ("%u: out of %zu spans, doubling!\n" ,
236
+ getpid (), tal_count (active_spans ));
237
+ tal_resizez (& active_spans , tal_count (active_spans ) * 2 );
238
+ s = trace_span_find (0 );
243
239
}
244
240
assert (s -> parent == NULL );
245
241
@@ -460,8 +456,7 @@ void trace_span_resume_(const void *key, const char *lbl)
460
456
461
457
void trace_cleanup (void )
462
458
{
463
- free (active_spans );
464
- active_spans = NULL ;
459
+ active_spans = tal_free (active_spans );
465
460
}
466
461
467
462
#else /* HAVE_USDT */
0 commit comments