mirror of
https://github.com/jemalloc/jemalloc.git
synced 2026-07-03 20:37:20 +03:00
Reduce cpp conditional logic complexity.
Convert configuration-related cpp conditional logic to use static
constant variables, e.g.:
#ifdef JEMALLOC_DEBUG
[...]
#endif
becomes:
if (config_debug) {
[...]
}
The advantage is clearer, more concise code. The main disadvantage is
that data structures no longer have conditionally defined fields, so
they pay the cost of all fields regardless of whether they are used. In
practice, this is only a minor concern; config_stats will go away in an
upcoming change, and config_prof is the only other major feature that
depends on more than a few special-purpose fields.
This commit is contained in:
parent
b3bd885090
commit
7372b15a31
27 changed files with 1194 additions and 1725 deletions
75
src/prof.c
75
src/prof.c
|
|
@ -1,6 +1,5 @@
|
|||
#define JEMALLOC_PROF_C_
|
||||
#include "jemalloc/internal/jemalloc_internal.h"
|
||||
#ifdef JEMALLOC_PROF
|
||||
/******************************************************************************/
|
||||
|
||||
#ifdef JEMALLOC_PROF_LIBUNWIND
|
||||
|
|
@ -102,6 +101,8 @@ void
|
|||
bt_init(prof_bt_t *bt, void **vec)
|
||||
{
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
bt->vec = vec;
|
||||
bt->len = 0;
|
||||
}
|
||||
|
|
@ -110,6 +111,8 @@ static void
|
|||
bt_destroy(prof_bt_t *bt)
|
||||
{
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
idalloc(bt);
|
||||
}
|
||||
|
||||
|
|
@ -118,6 +121,8 @@ bt_dup(prof_bt_t *bt)
|
|||
{
|
||||
prof_bt_t *ret;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
/*
|
||||
* Create a single allocation that has space for vec immediately
|
||||
* following the prof_bt_t structure. The backtraces that get
|
||||
|
|
@ -141,6 +146,8 @@ static inline void
|
|||
prof_enter(void)
|
||||
{
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
malloc_mutex_lock(&enq_mtx);
|
||||
enq = true;
|
||||
malloc_mutex_unlock(&enq_mtx);
|
||||
|
|
@ -153,6 +160,8 @@ prof_leave(void)
|
|||
{
|
||||
bool idump, gdump;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
malloc_mutex_unlock(&bt2ctx_mtx);
|
||||
|
||||
malloc_mutex_lock(&enq_mtx);
|
||||
|
|
@ -178,6 +187,7 @@ prof_backtrace(prof_bt_t *bt, unsigned nignore, unsigned max)
|
|||
unsigned i;
|
||||
int err;
|
||||
|
||||
cassert(config_prof);
|
||||
assert(bt->len == 0);
|
||||
assert(bt->vec != NULL);
|
||||
assert(max <= (1U << opt_lg_prof_bt_max));
|
||||
|
|
@ -204,12 +214,13 @@ prof_backtrace(prof_bt_t *bt, unsigned nignore, unsigned max)
|
|||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef JEMALLOC_PROF_LIBGCC
|
||||
#elif (defined(JEMALLOC_PROF_LIBGCC))
|
||||
static _Unwind_Reason_Code
|
||||
prof_unwind_init_callback(struct _Unwind_Context *context, void *arg)
|
||||
{
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
return (_URC_NO_REASON);
|
||||
}
|
||||
|
||||
|
|
@ -218,6 +229,8 @@ prof_unwind_callback(struct _Unwind_Context *context, void *arg)
|
|||
{
|
||||
prof_unwind_data_t *data = (prof_unwind_data_t *)arg;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
if (data->nignore > 0)
|
||||
data->nignore--;
|
||||
else {
|
||||
|
|
@ -235,10 +248,11 @@ prof_backtrace(prof_bt_t *bt, unsigned nignore, unsigned max)
|
|||
{
|
||||
prof_unwind_data_t data = {bt, nignore, max};
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
_Unwind_Backtrace(prof_unwind_callback, &data);
|
||||
}
|
||||
#endif
|
||||
#ifdef JEMALLOC_PROF_GCC
|
||||
#elif (defined(JEMALLOC_PROF_GCC))
|
||||
void
|
||||
prof_backtrace(prof_bt_t *bt, unsigned nignore, unsigned max)
|
||||
{
|
||||
|
|
@ -257,6 +271,7 @@ prof_backtrace(prof_bt_t *bt, unsigned nignore, unsigned max)
|
|||
} else \
|
||||
return;
|
||||
|
||||
cassert(config_prof);
|
||||
assert(nignore <= 3);
|
||||
assert(max <= (1U << opt_lg_prof_bt_max));
|
||||
|
||||
|
|
@ -407,6 +422,14 @@ prof_backtrace(prof_bt_t *bt, unsigned nignore, unsigned max)
|
|||
BT_FRAME(130)
|
||||
#undef BT_FRAME
|
||||
}
|
||||
#else
|
||||
void
|
||||
prof_backtrace(prof_bt_t *bt, unsigned nignore, unsigned max)
|
||||
{
|
||||
|
||||
cassert(config_prof);
|
||||
assert(false);
|
||||
}
|
||||
#endif
|
||||
|
||||
prof_thr_cnt_t *
|
||||
|
|
@ -418,6 +441,8 @@ prof_lookup(prof_bt_t *bt)
|
|||
} ret;
|
||||
prof_tdata_t *prof_tdata;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
prof_tdata = PROF_TCACHE_GET();
|
||||
if (prof_tdata == NULL) {
|
||||
prof_tdata = prof_tdata_init();
|
||||
|
|
@ -553,6 +578,8 @@ prof_flush(bool propagate_err)
|
|||
bool ret = false;
|
||||
ssize_t err;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
err = write(prof_dump_fd, prof_dump_buf, prof_dump_buf_end);
|
||||
if (err == -1) {
|
||||
if (propagate_err == false) {
|
||||
|
|
@ -573,6 +600,8 @@ prof_write(const char *s, bool propagate_err)
|
|||
{
|
||||
unsigned i, slen, n;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
i = 0;
|
||||
slen = strlen(s);
|
||||
while (i < slen) {
|
||||
|
|
@ -602,6 +631,8 @@ prof_ctx_sum(prof_ctx_t *ctx, prof_cnt_t *cnt_all, size_t *leak_nctx)
|
|||
prof_thr_cnt_t *thr_cnt;
|
||||
prof_cnt_t tcnt;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
malloc_mutex_lock(&ctx->lock);
|
||||
|
||||
memcpy(&ctx->cnt_summed, &ctx->cnt_merged, sizeof(prof_cnt_t));
|
||||
|
|
@ -648,6 +679,8 @@ static void
|
|||
prof_ctx_destroy(prof_ctx_t *ctx)
|
||||
{
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
/*
|
||||
* Check that ctx is still unused by any thread cache before destroying
|
||||
* it. prof_lookup() artificially raises ctx->cnt_merge.curobjs in
|
||||
|
|
@ -686,6 +719,8 @@ prof_ctx_merge(prof_ctx_t *ctx, prof_thr_cnt_t *cnt)
|
|||
{
|
||||
bool destroy;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
/* Merge cnt stats and detach from ctx. */
|
||||
malloc_mutex_lock(&ctx->lock);
|
||||
ctx->cnt_merged.curobjs += cnt->cnts.curobjs;
|
||||
|
|
@ -723,6 +758,8 @@ prof_dump_ctx(prof_ctx_t *ctx, prof_bt_t *bt, bool propagate_err)
|
|||
char buf[UMAX2S_BUFSIZE];
|
||||
unsigned i;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
if (opt_prof_accum == false && ctx->cnt_summed.curobjs == 0) {
|
||||
assert(ctx->cnt_summed.curbytes == 0);
|
||||
assert(ctx->cnt_summed.accumobjs == 0);
|
||||
|
|
@ -767,6 +804,8 @@ prof_dump_maps(bool propagate_err)
|
|||
char mpath[6 + UMAX2S_BUFSIZE
|
||||
+ 5 + 1];
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
i = 0;
|
||||
|
||||
s = "/proc/";
|
||||
|
|
@ -827,6 +866,8 @@ prof_dump(const char *filename, bool leakcheck, bool propagate_err)
|
|||
char buf[UMAX2S_BUFSIZE];
|
||||
size_t leak_nctx;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
prof_enter();
|
||||
prof_dump_fd = creat(filename, 0644);
|
||||
if (prof_dump_fd == -1) {
|
||||
|
|
@ -917,6 +958,8 @@ prof_dump_filename(char *filename, char v, int64_t vseq)
|
|||
char *s;
|
||||
unsigned i, slen;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
/*
|
||||
* Construct a filename of the form:
|
||||
*
|
||||
|
|
@ -979,6 +1022,8 @@ prof_fdump(void)
|
|||
{
|
||||
char filename[DUMP_FILENAME_BUFSIZE];
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
if (prof_booted == false)
|
||||
return;
|
||||
|
||||
|
|
@ -995,6 +1040,8 @@ prof_idump(void)
|
|||
{
|
||||
char filename[DUMP_FILENAME_BUFSIZE];
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
if (prof_booted == false)
|
||||
return;
|
||||
malloc_mutex_lock(&enq_mtx);
|
||||
|
|
@ -1019,6 +1066,8 @@ prof_mdump(const char *filename)
|
|||
{
|
||||
char filename_buf[DUMP_FILENAME_BUFSIZE];
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
if (opt_prof == false || prof_booted == false)
|
||||
return (true);
|
||||
|
||||
|
|
@ -1040,6 +1089,8 @@ prof_gdump(void)
|
|||
{
|
||||
char filename[DUMP_FILENAME_BUFSIZE];
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
if (prof_booted == false)
|
||||
return;
|
||||
malloc_mutex_lock(&enq_mtx);
|
||||
|
|
@ -1066,6 +1117,7 @@ prof_bt_hash(const void *key, unsigned minbits, size_t *hash1, size_t *hash2)
|
|||
uint64_t h;
|
||||
prof_bt_t *bt = (prof_bt_t *)key;
|
||||
|
||||
cassert(config_prof);
|
||||
assert(minbits <= 32 || (SIZEOF_PTR == 8 && minbits <= 64));
|
||||
assert(hash1 != NULL);
|
||||
assert(hash2 != NULL);
|
||||
|
|
@ -1094,6 +1146,8 @@ prof_bt_keycomp(const void *k1, const void *k2)
|
|||
const prof_bt_t *bt1 = (prof_bt_t *)k1;
|
||||
const prof_bt_t *bt2 = (prof_bt_t *)k2;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
if (bt1->len != bt2->len)
|
||||
return (false);
|
||||
return (memcmp(bt1->vec, bt2->vec, bt1->len * sizeof(void *)) == 0);
|
||||
|
|
@ -1104,6 +1158,8 @@ prof_tdata_init(void)
|
|||
{
|
||||
prof_tdata_t *prof_tdata;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
/* Initialize an empty cache for this thread. */
|
||||
prof_tdata = (prof_tdata_t *)imalloc(sizeof(prof_tdata_t));
|
||||
if (prof_tdata == NULL)
|
||||
|
|
@ -1138,6 +1194,8 @@ prof_tdata_cleanup(void *arg)
|
|||
prof_thr_cnt_t *cnt;
|
||||
prof_tdata_t *prof_tdata = (prof_tdata_t *)arg;
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
/*
|
||||
* Delete the hash table. All of its contents can still be iterated
|
||||
* over via the LRU.
|
||||
|
|
@ -1161,6 +1219,8 @@ void
|
|||
prof_boot0(void)
|
||||
{
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
memcpy(opt_prof_prefix, PROF_PREFIX_DEFAULT,
|
||||
sizeof(PROF_PREFIX_DEFAULT));
|
||||
}
|
||||
|
|
@ -1169,6 +1229,8 @@ void
|
|||
prof_boot1(void)
|
||||
{
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
/*
|
||||
* opt_prof and prof_promote must be in their final state before any
|
||||
* arenas are initialized, so this function must be executed early.
|
||||
|
|
@ -1197,6 +1259,8 @@ bool
|
|||
prof_boot2(void)
|
||||
{
|
||||
|
||||
cassert(config_prof);
|
||||
|
||||
if (opt_prof) {
|
||||
if (ckh_new(&bt2ctx, PROF_CKH_MINITEMS, prof_bt_hash,
|
||||
prof_bt_keycomp))
|
||||
|
|
@ -1241,4 +1305,3 @@ prof_boot2(void)
|
|||
}
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* JEMALLOC_PROF */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue