From f2c82d7f2eb45ca8c2c1a01dca2e3f8a017ec0e3 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Tue, 6 Jun 2023 14:51:57 +0200 Subject: Remove limit on number of children in the profiling graph --- libcrystfel/src/profile.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'libcrystfel/src') diff --git a/libcrystfel/src/profile.c b/libcrystfel/src/profile.c index 82f0217f..75983af3 100644 --- a/libcrystfel/src/profile.c +++ b/libcrystfel/src/profile.c @@ -41,8 +41,6 @@ #define CLOCK_MONOTONIC_RAW (CLOCK_MONOTONIC) #endif -#define MAX_PROFILE_CHILDREN 256 - struct _profile_block { char *name; @@ -52,8 +50,9 @@ struct _profile_block double total_time; struct _profile_block *parent; - struct _profile_block *children[MAX_PROFILE_CHILDREN]; + struct _profile_block **children; int n_children; + int max_children; }; @@ -80,6 +79,8 @@ static struct _profile_block *start_profile_block(const char *name) return NULL; } b->n_children = 0; + b->max_children = 0; + b->children = NULL; #ifdef HAVE_CLOCK_GETTIME struct timespec tp; @@ -133,12 +134,16 @@ static char *format_profile_block(struct _profile_block *b) { int i; size_t total_len = 0; - char *subbufs[MAX_PROFILE_CHILDREN]; + char **subbufs; char *full_buf; + subbufs = malloc(b->n_children * sizeof(char *)); + if ( subbufs == NULL ) return NULL; + total_len = 32 + strlen(b->name); for ( i=0; in_children; i++ ) { subbufs[i] = format_profile_block(b->children[i]); + if ( subbufs[i] == NULL ) return NULL; total_len += 1 + strlen(subbufs[i]); } @@ -151,6 +156,8 @@ static char *format_profile_block(struct _profile_block *b) } strcat(full_buf, ")"); + free(subbufs); + return full_buf; } @@ -207,12 +214,16 @@ void profile_start(const char *name) if ( pd == NULL ) return; - if ( pd->current->n_children >= MAX_PROFILE_CHILDREN ) { - fprintf(stderr, "Too many profile children " - "(opening %s inside %s).\n", - pd->current->name, name); - fflush(stderr); - abort(); + if ( pd->current->n_children >= pd->current->max_children ) { + struct _profile_block **nblock; + int nmax = pd->current->n_children + 64; + nblock = realloc(pd->current->children, nmax*sizeof(struct _profile_block)); + if ( nblock == NULL ) { + fprintf(stderr, "Failed to allocate profiling record. Try again without --profile.\n"); + abort(); + } + pd->current->children = nblock; + pd->current->max_children = nmax; } b = start_profile_block(name); -- cgit v1.2.3