Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit ab532e9

Browse files
committed
Directly embed prof_ctx_t's bt.
1 parent b41ccdb commit ab532e9

File tree

2 files changed

+26
-56
lines changed

2 files changed

+26
-56
lines changed

include/jemalloc/internal/prof.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ struct prof_thr_cnt_s {
115115
};
116116

117117
struct prof_ctx_s {
118-
/* Associated backtrace. */
119-
prof_bt_t *bt;
120-
121118
/* Protects nlimbo, cnt_merged, and cnts_ql. */
122119
malloc_mutex_t *lock;
123120

@@ -146,6 +143,12 @@ struct prof_ctx_s {
146143

147144
/* Linkage for list of contexts to be dumped. */
148145
ql_elm(prof_ctx_t) dump_link;
146+
147+
/* Associated backtrace. */
148+
prof_bt_t bt;
149+
150+
/* Backtrace vector, variable size, referred to by bt. */
151+
void *vec[1];
149152
};
150153
typedef ql_head(prof_ctx_t) prof_ctx_list_t;
151154

@@ -425,7 +428,7 @@ prof_realloc(const void *ptr, size_t usize, prof_thr_cnt_t *cnt,
425428
}
426429

427430
if ((uintptr_t)old_ctx > (uintptr_t)1U) {
428-
told_cnt = prof_lookup(old_ctx->bt);
431+
told_cnt = prof_lookup(&old_ctx->bt);
429432
if (told_cnt == NULL) {
430433
/*
431434
* It's too late to propagate OOM for this realloc(),
@@ -483,7 +486,7 @@ prof_free(const void *ptr, size_t size)
483486
if ((uintptr_t)ctx > (uintptr_t)1) {
484487
prof_thr_cnt_t *tcnt;
485488
assert(size == isalloc(ptr, true));
486-
tcnt = prof_lookup(ctx->bt);
489+
tcnt = prof_lookup(&ctx->bt);
487490

488491
if (tcnt != NULL) {
489492
tcnt->epoch++;

src/prof.c

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -87,41 +87,6 @@ bt_init(prof_bt_t *bt, void **vec)
8787
bt->len = 0;
8888
}
8989

90-
static void
91-
bt_destroy(prof_bt_t *bt)
92-
{
93-
94-
cassert(config_prof);
95-
96-
idalloc(bt);
97-
}
98-
99-
static prof_bt_t *
100-
bt_dup(prof_bt_t *bt)
101-
{
102-
prof_bt_t *ret;
103-
104-
cassert(config_prof);
105-
106-
/*
107-
* Create a single allocation that has space for vec immediately
108-
* following the prof_bt_t structure. The backtraces that get
109-
* stored in the backtrace caches are copied from stack-allocated
110-
* temporary variables, so size is known at creation time. Making this
111-
* a contiguous object improves cache locality.
112-
*/
113-
ret = (prof_bt_t *)imalloc(QUANTUM_CEILING(sizeof(prof_bt_t)) +
114-
(bt->len * sizeof(void *)));
115-
if (ret == NULL)
116-
return (NULL);
117-
ret->vec = (void **)((uintptr_t)ret +
118-
QUANTUM_CEILING(sizeof(prof_bt_t)));
119-
memcpy(ret->vec, bt->vec, bt->len * sizeof(void *));
120-
ret->len = bt->len;
121-
122-
return (ret);
123-
}
124-
12590
static inline void
12691
prof_enter(prof_tdata_t *prof_tdata)
12792
{
@@ -388,11 +353,16 @@ prof_ctx_mutex_choose(void)
388353
return (&ctx_locks[(nctxs - 1) % PROF_NCTX_LOCKS]);
389354
}
390355

391-
static void
392-
prof_ctx_init(prof_ctx_t *ctx, prof_bt_t *bt)
356+
static prof_ctx_t *
357+
prof_ctx_create(prof_bt_t *bt)
393358
{
394-
395-
ctx->bt = bt;
359+
/*
360+
* Create a single allocation that has space for vec of length bt->len.
361+
*/
362+
prof_ctx_t *ctx = (prof_ctx_t *)imalloc(offsetof(prof_ctx_t, vec) +
363+
(bt->len * sizeof(void *)));
364+
if (ctx == NULL)
365+
return (NULL);
396366
ctx->lock = prof_ctx_mutex_choose();
397367
/*
398368
* Set nlimbo to 1, in order to avoid a race condition with
@@ -402,6 +372,11 @@ prof_ctx_init(prof_ctx_t *ctx, prof_bt_t *bt)
402372
ql_elm_new(ctx, dump_link);
403373
memset(&ctx->cnt_merged, 0, sizeof(prof_cnt_t));
404374
ql_new(&ctx->cnts_ql);
375+
/* Duplicate bt. */
376+
memcpy(ctx->vec, bt->vec, bt->len * sizeof(void *));
377+
ctx->bt.vec = ctx->vec;
378+
ctx->bt.len = bt->len;
379+
return (ctx);
405380
}
406381

407382
static void
@@ -428,12 +403,11 @@ prof_ctx_destroy(prof_ctx_t *ctx)
428403
assert(ctx->cnt_merged.accumobjs == 0);
429404
assert(ctx->cnt_merged.accumbytes == 0);
430405
/* Remove ctx from bt2ctx. */
431-
if (ckh_remove(&bt2ctx, ctx->bt, NULL, NULL))
406+
if (ckh_remove(&bt2ctx, &ctx->bt, NULL, NULL))
432407
not_reached();
433408
prof_leave(prof_tdata);
434409
/* Destroy ctx. */
435410
malloc_mutex_unlock(ctx->lock);
436-
bt_destroy(ctx->bt);
437411
idalloc(ctx);
438412
} else {
439413
/*
@@ -501,22 +475,15 @@ prof_lookup_global(prof_bt_t *bt, prof_tdata_t *prof_tdata, void **p_btkey,
501475
prof_enter(prof_tdata);
502476
if (ckh_search(&bt2ctx, bt, &btkey.v, &ctx.v)) {
503477
/* bt has never been seen before. Insert it. */
504-
ctx.v = imalloc(sizeof(prof_ctx_t));
478+
ctx.p = prof_ctx_create(bt);
505479
if (ctx.v == NULL) {
506480
prof_leave(prof_tdata);
507481
return (true);
508482
}
509-
btkey.p = bt_dup(bt);
510-
if (btkey.v == NULL) {
511-
prof_leave(prof_tdata);
512-
idalloc(ctx.v);
513-
return (true);
514-
}
515-
prof_ctx_init(ctx.p, btkey.p);
483+
btkey.p = &ctx.p->bt;
516484
if (ckh_insert(&bt2ctx, btkey.v, ctx.v)) {
517485
/* OOM. */
518486
prof_leave(prof_tdata);
519-
idalloc(btkey.v);
520487
idalloc(ctx.v);
521488
return (true);
522489
}
@@ -1039,7 +1006,7 @@ prof_dump(bool propagate_err, const char *filename, bool leakcheck)
10391006

10401007
/* Dump per ctx profile stats. */
10411008
while ((ctx.p = ql_first(&ctx_ql)) != NULL) {
1042-
if (prof_dump_ctx(propagate_err, ctx.p, ctx.p->bt, &ctx_ql))
1009+
if (prof_dump_ctx(propagate_err, ctx.p, &ctx.p->bt, &ctx_ql))
10431010
goto label_write_error;
10441011
}
10451012

0 commit comments

Comments
 (0)