diff options
Diffstat (limited to 'pkfuncinstr.h')
| -rw-r--r-- | pkfuncinstr.h | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/pkfuncinstr.h b/pkfuncinstr.h index 151c60d..be7e59a 100644 --- a/pkfuncinstr.h +++ b/pkfuncinstr.h @@ -5,8 +5,6 @@ #include <stdio.h> -#define PK_FUNCINSTR_CHILDREN_INCREMENT_COUNT 8 - struct pk_funcinstr; struct pk_funcinstr { void *fn; @@ -60,11 +58,8 @@ void __cyg_profile_func_exit(void* this_fn, void* call_site); #include <threads.h> #include <string.h> -// TODO 2025-06-02 JCB -// There's some speed improvements that can be made here by growing faster. -// OR use some type of bucket -// - might be a good chance to isolate some of the pkmem logic - +#define PK_FUNCINSTR_CHILDREN_START_COUNT 8 +#define PK_FUNCINSTR_CHILDREN_GROW_RATIO 2.0 #define PK_FUNCINSTR_BKT_START_COUNT 64 #define PK_FUNCINSTR_BKT_GROW_RATIO 2.0 #define PK_FUNCINSTR_BKT_DATA_COUNT 0xFFFF @@ -104,7 +99,6 @@ void pk_funcinstr_init() { thrd_mstr.err = stderr; thrd_mstr.r_buckets = PK_FUNCINSTR_BKT_START_COUNT; thrd_mstr.buckets = (struct pk_funcinstr_bkt**)aligned_alloc(alignof(struct pk_funcinstr_bkt *), (sizeof(struct pk_funcinstr_bkt *) * PK_FUNCINSTR_BKT_START_COUNT)); - memset(thrd_mstr.buckets, 0, (sizeof(struct pk_funcinstr_bkt *) * PK_FUNCINSTR_BKT_START_COUNT)); clock_gettime(PK_TMR_CLOCK, &thrd_mstr.reset_time); } @@ -213,7 +207,6 @@ void pk_funcinstr_detect_and_handle_reset() { thrd_mstr.r_buckets *= PK_FUNCINSTR_BKT_GROW_RATIO; struct pk_funcinstr_bkt **buckets = (struct pk_funcinstr_bkt**)aligned_alloc(alignof(void *), sizeof(void *) * thrd_mstr.r_buckets); memcpy(buckets, thrd_mstr.buckets, sizeof(void *) * (thrd_mstr.n_buckets)); - memset((char*)buckets + (sizeof(void *) * (thrd_mstr.n_buckets)), 0, (sizeof(void *) * thrd_mstr.r_buckets) - sizeof(void *) * (thrd_mstr.n_buckets)); free(thrd_mstr.buckets); thrd_mstr.buckets = buckets; mtx_unlock(&thrd_mstr.mtx); @@ -247,11 +240,11 @@ __attribute__((no_instrument_function)) struct pk_funcinstr *pk_funcinstr_create_funcinstr(void *this_fn) { struct pk_funcinstr *funcinstr = &pk_funcinstr_thrd_bkt->data[pk_funcinstr_thrd_bkt->used_count]; pk_funcinstr_thrd_bkt->used_count++; - pk_tmr_start(funcinstr->tmr); funcinstr->fn = this_fn; + pk_tmr_start(funcinstr->tmr); funcinstr->parent = pk_funcinstr_thrd_instr; - funcinstr->children = NULL; funcinstr->first_child = NULL; + funcinstr->children = NULL; funcinstr->n_children = 0; funcinstr->r_children = 0; @@ -261,9 +254,12 @@ struct pk_funcinstr *pk_funcinstr_create_funcinstr(void *this_fn) { pk_funcinstr_thrd_instr->first_child = funcinstr; } else { if (pk_funcinstr_thrd_instr->n_children == pk_funcinstr_thrd_instr->r_children) { - pk_funcinstr_thrd_instr->r_children += PK_FUNCINSTR_CHILDREN_INCREMENT_COUNT; + if (pk_funcinstr_thrd_instr->r_children == 0) { + pk_funcinstr_thrd_instr->r_children = PK_FUNCINSTR_CHILDREN_START_COUNT; + } else { + pk_funcinstr_thrd_instr->r_children *= PK_FUNCINSTR_CHILDREN_GROW_RATIO; + } struct pk_funcinstr **children = (struct pk_funcinstr **)aligned_alloc(alignof(void *), sizeof(void *) * pk_funcinstr_thrd_instr->r_children); - memset((char*)children + (sizeof(void *) * (pk_funcinstr_thrd_instr->n_children)), 0, (sizeof(void *) * pk_funcinstr_thrd_instr->r_children) - sizeof(void *) * (pk_funcinstr_thrd_instr->n_children)); if (pk_funcinstr_thrd_instr->children != NULL) { memcpy(children, pk_funcinstr_thrd_instr->children, sizeof(void *) * pk_funcinstr_thrd_instr->n_children); free(pk_funcinstr_thrd_instr->children); @@ -303,7 +299,9 @@ void __cyg_profile_func_exit(void* this_fn, void* call_site) { if (pk_funcinstr_thrd_instr == NULL) return; // exit called before enter? pk_funcinstr_thrd_bkt->guard_exit++; +#ifdef PK_FUNCINSTR_PRINT Dl_info info; +#endif /* PK_FUNCINSTR_PRINT */ if (this_fn != pk_funcinstr_thrd_instr->fn) { int64_t i = (int64_t)pk_funcinstr_thrd_bkt->used_count - 1; @@ -345,6 +343,7 @@ void __cyg_profile_func_exit(void* this_fn, void* call_site) { } pk_tmr_stop(pk_funcinstr_thrd_instr->tmr); +#ifdef PK_FUNCINSTR_PRINT if (dladdr(this_fn, &info) != 0) { int depth = 0; // TODO track depth in a better way @@ -359,16 +358,15 @@ void __cyg_profile_func_exit(void* this_fn, void* call_site) { demangled = abi::__cxa_demangle(info.dli_sname, NULL, NULL, NULL); #endif } -#ifdef PK_FUNCINSTR_PRINT fprintf(thrd_mstr.out, "[pkfuncinstr] %p %*s %s took %.6f ms\n" ,this_fn ,depth, "" ,demangled != NULL ? demangled : info.dli_sname != NULL ? info.dli_sname : "???" ,pk_tmr_duration_dbl_mili(pk_funcinstr_thrd_instr->tmr) ); -#endif /* PK_FUNCINSTR_PRINT */ if (demangled != NULL) free(demangled); } +#endif /* PK_FUNCINSTR_PRINT */ pk_funcinstr_thrd_bkt->guard_exit=0; pk_funcinstr_thrd_instr = pk_funcinstr_thrd_instr->parent; } |
