2010-02-11 02:37:56 +08:00
|
|
|
#define JEMALLOC_PROF_C_
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
2010-02-11 02:37:56 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_PROF_LIBUNWIND
|
|
|
|
#define UNW_LOCAL_ONLY
|
|
|
|
#include <libunwind.h>
|
|
|
|
#endif
|
|
|
|
|
2011-03-16 13:23:12 +08:00
|
|
|
#ifdef JEMALLOC_PROF_LIBGCC
|
|
|
|
#include <unwind.h>
|
|
|
|
#endif
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
|
|
|
bool opt_prof = false;
|
2010-04-01 09:43:24 +08:00
|
|
|
bool opt_prof_active = true;
|
2014-10-04 14:25:30 +08:00
|
|
|
bool opt_prof_thread_active_init = true;
|
2010-03-02 12:15:26 +08:00
|
|
|
size_t opt_lg_prof_sample = LG_PROF_SAMPLE_DEFAULT;
|
2010-04-01 08:35:51 +08:00
|
|
|
ssize_t opt_lg_prof_interval = LG_PROF_INTERVAL_DEFAULT;
|
2010-10-24 09:37:06 +08:00
|
|
|
bool opt_prof_gdump = false;
|
2014-10-09 08:57:19 +08:00
|
|
|
bool opt_prof_final = false;
|
2010-02-11 02:37:56 +08:00
|
|
|
bool opt_prof_leak = false;
|
2012-04-18 07:39:33 +08:00
|
|
|
bool opt_prof_accum = false;
|
2014-01-17 05:23:56 +08:00
|
|
|
char opt_prof_prefix[
|
|
|
|
/* Minimize memory bloat for non-prof builds. */
|
|
|
|
#ifdef JEMALLOC_PROF
|
|
|
|
PATH_MAX +
|
|
|
|
#endif
|
2014-01-17 10:04:30 +08:00
|
|
|
1];
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
/*
|
|
|
|
* Initialized as opt_prof_active, and accessed via
|
|
|
|
* prof_active_[gs]et{_unlocked,}().
|
|
|
|
*/
|
|
|
|
bool prof_active;
|
|
|
|
static malloc_mutex_t prof_active_mtx;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialized as opt_prof_thread_active_init, and accessed via
|
|
|
|
* prof_thread_active_init_[gs]et().
|
|
|
|
*/
|
|
|
|
static bool prof_thread_active_init;
|
|
|
|
static malloc_mutex_t prof_thread_active_init_mtx;
|
|
|
|
|
2015-01-26 13:16:57 +08:00
|
|
|
/*
|
|
|
|
* Initialized as opt_prof_gdump, and accessed via
|
|
|
|
* prof_gdump_[gs]et{_unlocked,}().
|
|
|
|
*/
|
|
|
|
bool prof_gdump_val;
|
|
|
|
static malloc_mutex_t prof_gdump_mtx;
|
|
|
|
|
2012-11-14 04:56:27 +08:00
|
|
|
uint64_t prof_interval = 0;
|
2010-02-12 05:19:21 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
size_t lg_prof_sample;
|
|
|
|
|
2012-03-24 09:05:51 +08:00
|
|
|
/*
|
2014-08-19 07:22:13 +08:00
|
|
|
* Table of mutexes that are shared among gctx's. These are leaf locks, so
|
|
|
|
* there is no problem with using them for more than one gctx at the same time.
|
|
|
|
* The primary motivation for this sharing though is that gctx's are ephemeral,
|
2012-03-24 09:05:51 +08:00
|
|
|
* and destroying mutexes causes complications for systems that allocate when
|
|
|
|
* creating/destroying mutexes.
|
|
|
|
*/
|
2014-08-19 07:22:13 +08:00
|
|
|
static malloc_mutex_t *gctx_locks;
|
|
|
|
static unsigned cum_gctxs; /* Atomic counter. */
|
2012-03-24 09:05:51 +08:00
|
|
|
|
2010-10-03 13:38:14 +08:00
|
|
|
/*
|
2014-08-19 07:22:13 +08:00
|
|
|
* Table of mutexes that are shared among tdata's. No operations require
|
|
|
|
* holding multiple tdata locks, so there is no problem with using them for more
|
|
|
|
* than one tdata at the same time, even though a gctx lock may be acquired
|
|
|
|
* while holding a tdata lock.
|
|
|
|
*/
|
|
|
|
static malloc_mutex_t *tdata_locks;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Global hash of (prof_bt_t *)-->(prof_gctx_t *). This is the master data
|
2010-10-21 10:05:59 +08:00
|
|
|
* structure that knows about all backtraces currently captured.
|
2010-10-03 13:38:14 +08:00
|
|
|
*/
|
2014-08-19 07:22:13 +08:00
|
|
|
static ckh_t bt2gctx;
|
|
|
|
static malloc_mutex_t bt2gctx_mtx;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tree of all extant prof_tdata_t structures, regardless of state,
|
|
|
|
* {attached,detached,expired}.
|
|
|
|
*/
|
|
|
|
static prof_tdata_tree_t tdatas;
|
|
|
|
static malloc_mutex_t tdatas_mtx;
|
|
|
|
|
|
|
|
static uint64_t next_thr_uid;
|
2014-09-12 09:06:30 +08:00
|
|
|
static malloc_mutex_t next_thr_uid_mtx;
|
2010-03-02 12:15:26 +08:00
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
static malloc_mutex_t prof_dump_seq_mtx;
|
|
|
|
static uint64_t prof_dump_seq;
|
|
|
|
static uint64_t prof_dump_iseq;
|
|
|
|
static uint64_t prof_dump_mseq;
|
|
|
|
static uint64_t prof_dump_useq;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This buffer is rather large for stack allocation, so use a single buffer for
|
2014-01-17 05:23:56 +08:00
|
|
|
* all profile dumps.
|
2010-02-11 02:37:56 +08:00
|
|
|
*/
|
2014-01-17 05:23:56 +08:00
|
|
|
static malloc_mutex_t prof_dump_mtx;
|
|
|
|
static char prof_dump_buf[
|
|
|
|
/* Minimize memory bloat for non-prof builds. */
|
|
|
|
#ifdef JEMALLOC_PROF
|
|
|
|
PROF_DUMP_BUFSIZE
|
|
|
|
#else
|
|
|
|
1
|
|
|
|
#endif
|
|
|
|
];
|
2010-02-11 02:37:56 +08:00
|
|
|
static unsigned prof_dump_buf_end;
|
|
|
|
static int prof_dump_fd;
|
|
|
|
|
|
|
|
/* Do not dump any profiles until bootstrapping is complete. */
|
|
|
|
static bool prof_booted = false;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-08-19 07:22:13 +08:00
|
|
|
/*
|
|
|
|
* Function prototypes for static functions that are referenced prior to
|
|
|
|
* definition.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool prof_tctx_should_destroy(prof_tctx_t *tctx);
|
2014-09-23 12:09:23 +08:00
|
|
|
static void prof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx);
|
2014-10-05 06:03:49 +08:00
|
|
|
static bool prof_tdata_should_destroy(prof_tdata_t *tdata,
|
|
|
|
bool even_if_attached);
|
|
|
|
static void prof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata,
|
|
|
|
bool even_if_attached);
|
2014-10-04 14:25:30 +08:00
|
|
|
static char *prof_thread_name_alloc(tsd_t *tsd, const char *thread_name);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Red-black trees. */
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-08-17 03:58:55 +08:00
|
|
|
JEMALLOC_INLINE_C int
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tctx_comp(const prof_tctx_t *a, const prof_tctx_t *b)
|
2014-08-17 03:58:55 +08:00
|
|
|
{
|
2015-03-17 06:11:06 +08:00
|
|
|
uint64_t a_thr_uid = a->thr_uid;
|
|
|
|
uint64_t b_thr_uid = b->thr_uid;
|
|
|
|
int ret = (a_thr_uid > b_thr_uid) - (a_thr_uid < b_thr_uid);
|
2015-03-13 07:25:18 +08:00
|
|
|
if (ret == 0) {
|
2015-03-17 06:11:06 +08:00
|
|
|
uint64_t a_tctx_uid = a->tctx_uid;
|
|
|
|
uint64_t b_tctx_uid = b->tctx_uid;
|
|
|
|
ret = (a_tctx_uid > b_tctx_uid) - (a_tctx_uid < b_tctx_uid);
|
2015-03-13 07:25:18 +08:00
|
|
|
}
|
|
|
|
return (ret);
|
2014-08-17 03:58:55 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
rb_gen(static UNUSED, tctx_tree_, prof_tctx_tree_t, prof_tctx_t,
|
|
|
|
tctx_link, prof_tctx_comp)
|
2014-08-17 03:58:55 +08:00
|
|
|
|
|
|
|
JEMALLOC_INLINE_C int
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_gctx_comp(const prof_gctx_t *a, const prof_gctx_t *b)
|
2014-08-17 03:58:55 +08:00
|
|
|
{
|
|
|
|
unsigned a_len = a->bt.len;
|
|
|
|
unsigned b_len = b->bt.len;
|
|
|
|
unsigned comp_len = (a_len < b_len) ? a_len : b_len;
|
|
|
|
int ret = memcmp(a->bt.vec, b->bt.vec, comp_len * sizeof(void *));
|
|
|
|
if (ret == 0)
|
|
|
|
ret = (a_len > b_len) - (a_len < b_len);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
rb_gen(static UNUSED, gctx_tree_, prof_gctx_tree_t, prof_gctx_t, dump_link,
|
|
|
|
prof_gctx_comp)
|
|
|
|
|
|
|
|
JEMALLOC_INLINE_C int
|
|
|
|
prof_tdata_comp(const prof_tdata_t *a, const prof_tdata_t *b)
|
|
|
|
{
|
2014-10-03 14:01:10 +08:00
|
|
|
int ret;
|
2014-08-19 07:22:13 +08:00
|
|
|
uint64_t a_uid = a->thr_uid;
|
|
|
|
uint64_t b_uid = b->thr_uid;
|
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
ret = ((a_uid > b_uid) - (a_uid < b_uid));
|
|
|
|
if (ret == 0) {
|
|
|
|
uint64_t a_discrim = a->thr_discrim;
|
|
|
|
uint64_t b_discrim = b->thr_discrim;
|
|
|
|
|
|
|
|
ret = ((a_discrim > b_discrim) - (a_discrim < b_discrim));
|
|
|
|
}
|
|
|
|
return (ret);
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_gen(static UNUSED, tdata_tree_, prof_tdata_tree_t, prof_tdata_t, tdata_link,
|
|
|
|
prof_tdata_comp)
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-09-10 10:37:26 +08:00
|
|
|
void
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_alloc_rollback(tsd_t *tsd, prof_tctx_t *tctx, bool updated)
|
2014-09-10 10:37:26 +08:00
|
|
|
{
|
|
|
|
prof_tdata_t *tdata;
|
|
|
|
|
|
|
|
cassert(config_prof);
|
|
|
|
|
|
|
|
if (updated) {
|
|
|
|
/*
|
|
|
|
* Compute a new sample threshold. This isn't very important in
|
|
|
|
* practice, because this function is rarely executed, so the
|
|
|
|
* potential for sample bias is minimal except in contrived
|
|
|
|
* programs.
|
|
|
|
*/
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = prof_tdata_get(tsd, true);
|
|
|
|
if (tdata != NULL)
|
2014-09-10 10:37:26 +08:00
|
|
|
prof_sample_threshold_update(tctx->tdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((uintptr_t)tctx > (uintptr_t)1U) {
|
|
|
|
malloc_mutex_lock(tctx->tdata->lock);
|
|
|
|
tctx->prepared = false;
|
|
|
|
if (prof_tctx_should_destroy(tctx))
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_tctx_destroy(tsd, tctx);
|
2014-09-10 10:37:26 +08:00
|
|
|
else
|
|
|
|
malloc_mutex_unlock(tctx->tdata->lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
void
|
2014-10-31 14:18:45 +08:00
|
|
|
prof_malloc_sample_object(const void *ptr, size_t usize, prof_tctx_t *tctx)
|
|
|
|
{
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tctx_set(ptr, tctx);
|
|
|
|
|
|
|
|
malloc_mutex_lock(tctx->tdata->lock);
|
|
|
|
tctx->cnts.curobjs++;
|
|
|
|
tctx->cnts.curbytes += usize;
|
|
|
|
if (opt_prof_accum) {
|
|
|
|
tctx->cnts.accumobjs++;
|
|
|
|
tctx->cnts.accumbytes += usize;
|
|
|
|
}
|
2014-09-10 10:37:26 +08:00
|
|
|
tctx->prepared = false;
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_unlock(tctx->tdata->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_free_sampled_object(tsd_t *tsd, size_t usize, prof_tctx_t *tctx)
|
2014-08-19 07:22:13 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_lock(tctx->tdata->lock);
|
|
|
|
assert(tctx->cnts.curobjs > 0);
|
|
|
|
assert(tctx->cnts.curbytes >= usize);
|
|
|
|
tctx->cnts.curobjs--;
|
|
|
|
tctx->cnts.curbytes -= usize;
|
|
|
|
|
|
|
|
if (prof_tctx_should_destroy(tctx))
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_tctx_destroy(tsd, tctx);
|
2014-08-19 07:22:13 +08:00
|
|
|
else
|
|
|
|
malloc_mutex_unlock(tctx->tdata->lock);
|
|
|
|
}
|
2014-08-17 03:58:55 +08:00
|
|
|
|
2010-10-21 10:05:59 +08:00
|
|
|
void
|
2010-02-11 02:37:56 +08:00
|
|
|
bt_init(prof_bt_t *bt, void **vec)
|
|
|
|
{
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
bt->vec = vec;
|
|
|
|
bt->len = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-31 07:38:08 +08:00
|
|
|
JEMALLOC_INLINE_C void
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_enter(tsd_t *tsd, prof_tdata_t *tdata)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
2014-10-31 07:50:33 +08:00
|
|
|
assert(tdata == prof_tdata_get(tsd, false));
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2014-11-01 15:20:28 +08:00
|
|
|
if (tdata != NULL) {
|
|
|
|
assert(!tdata->enq);
|
|
|
|
tdata->enq = true;
|
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_lock(&bt2gctx_mtx);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2014-10-31 07:38:08 +08:00
|
|
|
JEMALLOC_INLINE_C void
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_leave(tsd_t *tsd, prof_tdata_t *tdata)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
2014-10-31 07:50:33 +08:00
|
|
|
assert(tdata == prof_tdata_get(tsd, false));
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_unlock(&bt2gctx_mtx);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-11-01 15:20:28 +08:00
|
|
|
if (tdata != NULL) {
|
|
|
|
bool idump, gdump;
|
|
|
|
|
|
|
|
assert(tdata->enq);
|
|
|
|
tdata->enq = false;
|
|
|
|
idump = tdata->enq_idump;
|
|
|
|
tdata->enq_idump = false;
|
|
|
|
gdump = tdata->enq_gdump;
|
|
|
|
tdata->enq_gdump = false;
|
|
|
|
|
|
|
|
if (idump)
|
|
|
|
prof_idump();
|
|
|
|
if (gdump)
|
|
|
|
prof_gdump();
|
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2011-03-16 13:23:12 +08:00
|
|
|
#ifdef JEMALLOC_PROF_LIBUNWIND
|
2010-10-21 10:05:59 +08:00
|
|
|
void
|
2014-04-23 09:41:15 +08:00
|
|
|
prof_backtrace(prof_bt_t *bt)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2014-04-23 09:41:15 +08:00
|
|
|
int nframes;
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
2010-02-11 02:37:56 +08:00
|
|
|
assert(bt->len == 0);
|
|
|
|
assert(bt->vec != NULL);
|
|
|
|
|
2014-04-23 09:41:15 +08:00
|
|
|
nframes = unw_backtrace(bt->vec, PROF_BT_MAX);
|
|
|
|
if (nframes <= 0)
|
2014-04-22 11:52:35 +08:00
|
|
|
return;
|
2014-04-23 09:41:15 +08:00
|
|
|
bt->len = nframes;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
2012-02-11 12:22:09 +08:00
|
|
|
#elif (defined(JEMALLOC_PROF_LIBGCC))
|
2011-03-16 13:23:12 +08:00
|
|
|
static _Unwind_Reason_Code
|
|
|
|
prof_unwind_init_callback(struct _Unwind_Context *context, void *arg)
|
|
|
|
{
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2011-03-16 13:23:12 +08:00
|
|
|
return (_URC_NO_REASON);
|
|
|
|
}
|
|
|
|
|
|
|
|
static _Unwind_Reason_Code
|
|
|
|
prof_unwind_callback(struct _Unwind_Context *context, void *arg)
|
|
|
|
{
|
|
|
|
prof_unwind_data_t *data = (prof_unwind_data_t *)arg;
|
2014-04-23 09:41:15 +08:00
|
|
|
void *ip;
|
2011-03-16 13:23:12 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2014-04-23 09:41:15 +08:00
|
|
|
ip = (void *)_Unwind_GetIP(context);
|
|
|
|
if (ip == NULL)
|
|
|
|
return (_URC_END_OF_STACK);
|
|
|
|
data->bt->vec[data->bt->len] = ip;
|
|
|
|
data->bt->len++;
|
|
|
|
if (data->bt->len == data->max)
|
|
|
|
return (_URC_END_OF_STACK);
|
2011-03-16 13:23:12 +08:00
|
|
|
|
|
|
|
return (_URC_NO_REASON);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-04-23 09:41:15 +08:00
|
|
|
prof_backtrace(prof_bt_t *bt)
|
2011-03-16 13:23:12 +08:00
|
|
|
{
|
2014-04-23 09:41:15 +08:00
|
|
|
prof_unwind_data_t data = {bt, PROF_BT_MAX};
|
2011-03-16 13:23:12 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2011-03-16 13:23:12 +08:00
|
|
|
_Unwind_Backtrace(prof_unwind_callback, &data);
|
|
|
|
}
|
2012-02-11 12:22:09 +08:00
|
|
|
#elif (defined(JEMALLOC_PROF_GCC))
|
2010-10-21 10:05:59 +08:00
|
|
|
void
|
2014-04-23 09:41:15 +08:00
|
|
|
prof_backtrace(prof_bt_t *bt)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
#define BT_FRAME(i) \
|
2014-04-23 09:41:15 +08:00
|
|
|
if ((i) < PROF_BT_MAX) { \
|
2010-02-11 02:37:56 +08:00
|
|
|
void *p; \
|
|
|
|
if (__builtin_frame_address(i) == 0) \
|
2010-02-11 10:15:53 +08:00
|
|
|
return; \
|
2010-02-11 02:37:56 +08:00
|
|
|
p = __builtin_return_address(i); \
|
|
|
|
if (p == NULL) \
|
2010-02-11 10:15:53 +08:00
|
|
|
return; \
|
2014-04-23 09:41:15 +08:00
|
|
|
bt->vec[(i)] = p; \
|
|
|
|
bt->len = (i) + 1; \
|
2010-02-11 02:37:56 +08:00
|
|
|
} else \
|
2010-02-11 10:15:53 +08:00
|
|
|
return;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
BT_FRAME(0)
|
|
|
|
BT_FRAME(1)
|
|
|
|
BT_FRAME(2)
|
|
|
|
BT_FRAME(3)
|
|
|
|
BT_FRAME(4)
|
|
|
|
BT_FRAME(5)
|
|
|
|
BT_FRAME(6)
|
|
|
|
BT_FRAME(7)
|
|
|
|
BT_FRAME(8)
|
|
|
|
BT_FRAME(9)
|
|
|
|
|
|
|
|
BT_FRAME(10)
|
|
|
|
BT_FRAME(11)
|
|
|
|
BT_FRAME(12)
|
|
|
|
BT_FRAME(13)
|
|
|
|
BT_FRAME(14)
|
|
|
|
BT_FRAME(15)
|
|
|
|
BT_FRAME(16)
|
|
|
|
BT_FRAME(17)
|
|
|
|
BT_FRAME(18)
|
|
|
|
BT_FRAME(19)
|
|
|
|
|
|
|
|
BT_FRAME(20)
|
|
|
|
BT_FRAME(21)
|
|
|
|
BT_FRAME(22)
|
|
|
|
BT_FRAME(23)
|
|
|
|
BT_FRAME(24)
|
|
|
|
BT_FRAME(25)
|
|
|
|
BT_FRAME(26)
|
|
|
|
BT_FRAME(27)
|
|
|
|
BT_FRAME(28)
|
|
|
|
BT_FRAME(29)
|
|
|
|
|
|
|
|
BT_FRAME(30)
|
|
|
|
BT_FRAME(31)
|
|
|
|
BT_FRAME(32)
|
|
|
|
BT_FRAME(33)
|
|
|
|
BT_FRAME(34)
|
|
|
|
BT_FRAME(35)
|
|
|
|
BT_FRAME(36)
|
|
|
|
BT_FRAME(37)
|
|
|
|
BT_FRAME(38)
|
|
|
|
BT_FRAME(39)
|
|
|
|
|
|
|
|
BT_FRAME(40)
|
|
|
|
BT_FRAME(41)
|
|
|
|
BT_FRAME(42)
|
|
|
|
BT_FRAME(43)
|
|
|
|
BT_FRAME(44)
|
|
|
|
BT_FRAME(45)
|
|
|
|
BT_FRAME(46)
|
|
|
|
BT_FRAME(47)
|
|
|
|
BT_FRAME(48)
|
|
|
|
BT_FRAME(49)
|
|
|
|
|
|
|
|
BT_FRAME(50)
|
|
|
|
BT_FRAME(51)
|
|
|
|
BT_FRAME(52)
|
|
|
|
BT_FRAME(53)
|
|
|
|
BT_FRAME(54)
|
|
|
|
BT_FRAME(55)
|
|
|
|
BT_FRAME(56)
|
|
|
|
BT_FRAME(57)
|
|
|
|
BT_FRAME(58)
|
|
|
|
BT_FRAME(59)
|
|
|
|
|
|
|
|
BT_FRAME(60)
|
|
|
|
BT_FRAME(61)
|
|
|
|
BT_FRAME(62)
|
|
|
|
BT_FRAME(63)
|
|
|
|
BT_FRAME(64)
|
|
|
|
BT_FRAME(65)
|
|
|
|
BT_FRAME(66)
|
|
|
|
BT_FRAME(67)
|
|
|
|
BT_FRAME(68)
|
|
|
|
BT_FRAME(69)
|
|
|
|
|
|
|
|
BT_FRAME(70)
|
|
|
|
BT_FRAME(71)
|
|
|
|
BT_FRAME(72)
|
|
|
|
BT_FRAME(73)
|
|
|
|
BT_FRAME(74)
|
|
|
|
BT_FRAME(75)
|
|
|
|
BT_FRAME(76)
|
|
|
|
BT_FRAME(77)
|
|
|
|
BT_FRAME(78)
|
|
|
|
BT_FRAME(79)
|
|
|
|
|
|
|
|
BT_FRAME(80)
|
|
|
|
BT_FRAME(81)
|
|
|
|
BT_FRAME(82)
|
|
|
|
BT_FRAME(83)
|
|
|
|
BT_FRAME(84)
|
|
|
|
BT_FRAME(85)
|
|
|
|
BT_FRAME(86)
|
|
|
|
BT_FRAME(87)
|
|
|
|
BT_FRAME(88)
|
|
|
|
BT_FRAME(89)
|
|
|
|
|
|
|
|
BT_FRAME(90)
|
|
|
|
BT_FRAME(91)
|
|
|
|
BT_FRAME(92)
|
|
|
|
BT_FRAME(93)
|
|
|
|
BT_FRAME(94)
|
|
|
|
BT_FRAME(95)
|
|
|
|
BT_FRAME(96)
|
|
|
|
BT_FRAME(97)
|
|
|
|
BT_FRAME(98)
|
|
|
|
BT_FRAME(99)
|
|
|
|
|
|
|
|
BT_FRAME(100)
|
|
|
|
BT_FRAME(101)
|
|
|
|
BT_FRAME(102)
|
|
|
|
BT_FRAME(103)
|
|
|
|
BT_FRAME(104)
|
|
|
|
BT_FRAME(105)
|
|
|
|
BT_FRAME(106)
|
|
|
|
BT_FRAME(107)
|
|
|
|
BT_FRAME(108)
|
|
|
|
BT_FRAME(109)
|
|
|
|
|
|
|
|
BT_FRAME(110)
|
|
|
|
BT_FRAME(111)
|
|
|
|
BT_FRAME(112)
|
|
|
|
BT_FRAME(113)
|
|
|
|
BT_FRAME(114)
|
|
|
|
BT_FRAME(115)
|
|
|
|
BT_FRAME(116)
|
|
|
|
BT_FRAME(117)
|
|
|
|
BT_FRAME(118)
|
|
|
|
BT_FRAME(119)
|
|
|
|
|
|
|
|
BT_FRAME(120)
|
|
|
|
BT_FRAME(121)
|
|
|
|
BT_FRAME(122)
|
|
|
|
BT_FRAME(123)
|
|
|
|
BT_FRAME(124)
|
|
|
|
BT_FRAME(125)
|
|
|
|
BT_FRAME(126)
|
|
|
|
BT_FRAME(127)
|
|
|
|
#undef BT_FRAME
|
|
|
|
}
|
2012-02-11 12:22:09 +08:00
|
|
|
#else
|
|
|
|
void
|
2014-04-23 09:41:15 +08:00
|
|
|
prof_backtrace(prof_bt_t *bt)
|
2012-02-11 12:22:09 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
cassert(config_prof);
|
2013-10-22 05:56:27 +08:00
|
|
|
not_reached();
|
2012-02-11 12:22:09 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
#endif
|
|
|
|
|
2014-01-17 05:23:56 +08:00
|
|
|
static malloc_mutex_t *
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_gctx_mutex_choose(void)
|
|
|
|
{
|
|
|
|
unsigned ngctxs = atomic_add_u(&cum_gctxs, 1);
|
|
|
|
|
|
|
|
return (&gctx_locks[(ngctxs - 1) % PROF_NCTX_LOCKS]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static malloc_mutex_t *
|
|
|
|
prof_tdata_mutex_choose(uint64_t thr_uid)
|
2014-01-17 05:23:56 +08:00
|
|
|
{
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
return (&tdata_locks[thr_uid % PROF_NTDATA_LOCKS]);
|
2014-01-17 05:23:56 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
static prof_gctx_t *
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_gctx_create(tsd_t *tsd, prof_bt_t *bt)
|
2014-01-17 05:23:56 +08:00
|
|
|
{
|
2014-08-16 06:05:12 +08:00
|
|
|
/*
|
|
|
|
* Create a single allocation that has space for vec of length bt->len.
|
|
|
|
*/
|
2014-11-28 03:22:36 +08:00
|
|
|
prof_gctx_t *gctx = (prof_gctx_t *)iallocztm(tsd, offsetof(prof_gctx_t,
|
2015-01-30 07:30:47 +08:00
|
|
|
vec) + (bt->len * sizeof(void *)), false, tcache_get(tsd, true),
|
|
|
|
true, NULL);
|
2014-08-19 07:22:13 +08:00
|
|
|
if (gctx == NULL)
|
2014-08-16 06:05:12 +08:00
|
|
|
return (NULL);
|
2014-08-19 07:22:13 +08:00
|
|
|
gctx->lock = prof_gctx_mutex_choose();
|
2014-01-17 05:23:56 +08:00
|
|
|
/*
|
|
|
|
* Set nlimbo to 1, in order to avoid a race condition with
|
2014-10-03 14:01:10 +08:00
|
|
|
* prof_tctx_destroy()/prof_gctx_try_destroy().
|
2014-01-17 05:23:56 +08:00
|
|
|
*/
|
2014-08-19 07:22:13 +08:00
|
|
|
gctx->nlimbo = 1;
|
|
|
|
tctx_tree_new(&gctx->tctxs);
|
2014-08-16 06:05:12 +08:00
|
|
|
/* Duplicate bt. */
|
2014-08-19 07:22:13 +08:00
|
|
|
memcpy(gctx->vec, bt->vec, bt->len * sizeof(void *));
|
|
|
|
gctx->bt.vec = gctx->vec;
|
|
|
|
gctx->bt.len = bt->len;
|
|
|
|
return (gctx);
|
2014-01-17 05:23:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_gctx_try_destroy(tsd_t *tsd, prof_tdata_t *tdata_self, prof_gctx_t *gctx,
|
|
|
|
prof_tdata_t *tdata)
|
2014-01-17 05:23:56 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
cassert(config_prof);
|
|
|
|
|
|
|
|
/*
|
2014-08-19 07:22:13 +08:00
|
|
|
* Check that gctx is still unused by any thread cache before destroying
|
|
|
|
* it. prof_lookup() increments gctx->nlimbo in order to avoid a race
|
|
|
|
* condition with this function, as does prof_tctx_destroy() in order to
|
|
|
|
* avoid a race between the main body of prof_tctx_destroy() and entry
|
2014-01-17 05:23:56 +08:00
|
|
|
* into this function.
|
|
|
|
*/
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_enter(tsd, tdata_self);
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_lock(gctx->lock);
|
2014-10-03 14:01:10 +08:00
|
|
|
assert(gctx->nlimbo != 0);
|
2014-08-19 07:22:13 +08:00
|
|
|
if (tctx_tree_empty(&gctx->tctxs) && gctx->nlimbo == 1) {
|
|
|
|
/* Remove gctx from bt2gctx. */
|
2014-09-23 12:09:23 +08:00
|
|
|
if (ckh_remove(tsd, &bt2gctx, &gctx->bt, NULL, NULL))
|
2014-01-17 05:23:56 +08:00
|
|
|
not_reached();
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_leave(tsd, tdata_self);
|
2014-08-19 07:22:13 +08:00
|
|
|
/* Destroy gctx. */
|
|
|
|
malloc_mutex_unlock(gctx->lock);
|
2015-01-30 07:30:47 +08:00
|
|
|
idalloctm(tsd, gctx, tcache_get(tsd, false), true);
|
2014-01-17 05:23:56 +08:00
|
|
|
} else {
|
|
|
|
/*
|
2014-08-19 07:22:13 +08:00
|
|
|
* Compensate for increment in prof_tctx_destroy() or
|
2014-01-17 05:23:56 +08:00
|
|
|
* prof_lookup().
|
|
|
|
*/
|
2014-08-19 07:22:13 +08:00
|
|
|
gctx->nlimbo--;
|
|
|
|
malloc_mutex_unlock(gctx->lock);
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_leave(tsd, tdata_self);
|
2014-01-17 05:23:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
/* tctx->tdata->lock must be held. */
|
|
|
|
static bool
|
|
|
|
prof_tctx_should_destroy(prof_tctx_t *tctx)
|
2014-01-17 05:23:56 +08:00
|
|
|
{
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
if (opt_prof_accum)
|
|
|
|
return (false);
|
|
|
|
if (tctx->cnts.curobjs != 0)
|
|
|
|
return (false);
|
2014-09-10 10:37:26 +08:00
|
|
|
if (tctx->prepared)
|
|
|
|
return (false);
|
2014-08-19 07:22:13 +08:00
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
prof_gctx_should_destroy(prof_gctx_t *gctx)
|
|
|
|
{
|
2014-01-17 05:23:56 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
if (opt_prof_accum)
|
|
|
|
return (false);
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!tctx_tree_empty(&gctx->tctxs))
|
2014-08-19 07:22:13 +08:00
|
|
|
return (false);
|
|
|
|
if (gctx->nlimbo != 0)
|
|
|
|
return (false);
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tctx->tdata->lock is held upon entry, and released before return. */
|
|
|
|
static void
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx)
|
2014-08-19 07:22:13 +08:00
|
|
|
{
|
2014-09-10 03:45:53 +08:00
|
|
|
prof_tdata_t *tdata = tctx->tdata;
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_gctx_t *gctx = tctx->gctx;
|
2014-10-07 07:35:11 +08:00
|
|
|
bool destroy_tdata, destroy_tctx, destroy_gctx;
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
assert(tctx->cnts.curobjs == 0);
|
|
|
|
assert(tctx->cnts.curbytes == 0);
|
2014-10-04 01:16:09 +08:00
|
|
|
assert(!opt_prof_accum);
|
2014-08-19 07:22:13 +08:00
|
|
|
assert(tctx->cnts.accumobjs == 0);
|
|
|
|
assert(tctx->cnts.accumbytes == 0);
|
|
|
|
|
2014-09-23 12:09:23 +08:00
|
|
|
ckh_remove(tsd, &tdata->bt2tctx, &gctx->bt, NULL, NULL);
|
2014-10-05 06:03:49 +08:00
|
|
|
destroy_tdata = prof_tdata_should_destroy(tdata, false);
|
2014-09-10 03:45:53 +08:00
|
|
|
malloc_mutex_unlock(tdata->lock);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
malloc_mutex_lock(gctx->lock);
|
2015-03-15 05:01:35 +08:00
|
|
|
switch (tctx->state) {
|
2015-03-17 06:11:06 +08:00
|
|
|
case prof_tctx_state_nominal:
|
2014-10-07 07:35:11 +08:00
|
|
|
tctx_tree_remove(&gctx->tctxs, tctx);
|
|
|
|
destroy_tctx = true;
|
|
|
|
if (prof_gctx_should_destroy(gctx)) {
|
|
|
|
/*
|
|
|
|
* Increment gctx->nlimbo in order to keep another
|
|
|
|
* thread from winning the race to destroy gctx while
|
|
|
|
* this one has gctx->lock dropped. Without this, it
|
|
|
|
* would be possible for another thread to:
|
|
|
|
*
|
|
|
|
* 1) Sample an allocation associated with gctx.
|
|
|
|
* 2) Deallocate the sampled object.
|
|
|
|
* 3) Successfully prof_gctx_try_destroy(gctx).
|
|
|
|
*
|
|
|
|
* The result would be that gctx no longer exists by the
|
|
|
|
* time this thread accesses it in
|
|
|
|
* prof_gctx_try_destroy().
|
|
|
|
*/
|
|
|
|
gctx->nlimbo++;
|
|
|
|
destroy_gctx = true;
|
|
|
|
} else
|
|
|
|
destroy_gctx = false;
|
2015-03-15 05:01:35 +08:00
|
|
|
break;
|
|
|
|
case prof_tctx_state_dumping:
|
2014-01-17 05:23:56 +08:00
|
|
|
/*
|
2014-10-07 07:35:11 +08:00
|
|
|
* A dumping thread needs tctx to remain valid until dumping
|
|
|
|
* has finished. Change state such that the dumping thread will
|
|
|
|
* complete destruction during a late dump iteration phase.
|
2014-01-17 05:23:56 +08:00
|
|
|
*/
|
2014-10-07 07:35:11 +08:00
|
|
|
tctx->state = prof_tctx_state_purgatory;
|
|
|
|
destroy_tctx = false;
|
2014-08-19 07:22:13 +08:00
|
|
|
destroy_gctx = false;
|
2015-03-15 05:01:35 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
not_reached();
|
2015-03-15 05:34:16 +08:00
|
|
|
destroy_tctx = false;
|
|
|
|
destroy_gctx = false;
|
2014-10-07 07:35:11 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_unlock(gctx->lock);
|
2014-10-31 07:50:33 +08:00
|
|
|
if (destroy_gctx) {
|
|
|
|
prof_gctx_try_destroy(tsd, prof_tdata_get(tsd, false), gctx,
|
|
|
|
tdata);
|
|
|
|
}
|
2014-09-10 03:45:53 +08:00
|
|
|
|
|
|
|
if (destroy_tdata)
|
2014-10-05 06:03:49 +08:00
|
|
|
prof_tdata_destroy(tsd, tdata, false);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2014-10-07 07:35:11 +08:00
|
|
|
if (destroy_tctx)
|
2015-01-30 07:30:47 +08:00
|
|
|
idalloctm(tsd, tctx, tcache_get(tsd, false), true);
|
2014-01-17 05:23:56 +08:00
|
|
|
}
|
|
|
|
|
2014-01-15 09:04:34 +08:00
|
|
|
static bool
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_lookup_global(tsd_t *tsd, prof_bt_t *bt, prof_tdata_t *tdata,
|
|
|
|
void **p_btkey, prof_gctx_t **p_gctx, bool *p_new_gctx)
|
2014-01-15 09:04:34 +08:00
|
|
|
{
|
|
|
|
union {
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_gctx_t *p;
|
2014-01-15 09:04:34 +08:00
|
|
|
void *v;
|
2014-08-19 07:22:13 +08:00
|
|
|
} gctx;
|
2014-01-15 09:04:34 +08:00
|
|
|
union {
|
|
|
|
prof_bt_t *p;
|
|
|
|
void *v;
|
|
|
|
} btkey;
|
2014-08-19 07:22:13 +08:00
|
|
|
bool new_gctx;
|
2014-01-15 09:04:34 +08:00
|
|
|
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_enter(tsd, tdata);
|
2014-08-19 07:22:13 +08:00
|
|
|
if (ckh_search(&bt2gctx, bt, &btkey.v, &gctx.v)) {
|
2014-01-15 09:04:34 +08:00
|
|
|
/* bt has never been seen before. Insert it. */
|
2014-09-23 12:09:23 +08:00
|
|
|
gctx.p = prof_gctx_create(tsd, bt);
|
2014-08-19 07:22:13 +08:00
|
|
|
if (gctx.v == NULL) {
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_leave(tsd, tdata);
|
2014-01-15 09:04:34 +08:00
|
|
|
return (true);
|
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
btkey.p = &gctx.p->bt;
|
2014-09-23 12:09:23 +08:00
|
|
|
if (ckh_insert(tsd, &bt2gctx, btkey.v, gctx.v)) {
|
2014-01-15 09:04:34 +08:00
|
|
|
/* OOM. */
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_leave(tsd, tdata);
|
2015-01-30 07:30:47 +08:00
|
|
|
idalloctm(tsd, gctx.v, tcache_get(tsd, false), true);
|
2014-01-15 09:04:34 +08:00
|
|
|
return (true);
|
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
new_gctx = true;
|
2014-01-15 09:04:34 +08:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Increment nlimbo, in order to avoid a race condition with
|
2014-10-03 14:01:10 +08:00
|
|
|
* prof_tctx_destroy()/prof_gctx_try_destroy().
|
2014-01-15 09:04:34 +08:00
|
|
|
*/
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_lock(gctx.p->lock);
|
|
|
|
gctx.p->nlimbo++;
|
|
|
|
malloc_mutex_unlock(gctx.p->lock);
|
|
|
|
new_gctx = false;
|
2014-01-15 09:04:34 +08:00
|
|
|
}
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_leave(tsd, tdata);
|
2014-01-15 09:04:34 +08:00
|
|
|
|
|
|
|
*p_btkey = btkey.v;
|
2014-08-19 07:22:13 +08:00
|
|
|
*p_gctx = gctx.p;
|
|
|
|
*p_new_gctx = new_gctx;
|
2014-01-15 09:04:34 +08:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tctx_t *
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_lookup(tsd_t *tsd, prof_bt_t *bt)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2010-09-21 10:53:25 +08:00
|
|
|
union {
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tctx_t *p;
|
2010-09-21 10:53:25 +08:00
|
|
|
void *v;
|
|
|
|
} ret;
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tdata_t *tdata;
|
|
|
|
bool not_found;
|
2010-10-03 06:18:50 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = prof_tdata_get(tsd, false);
|
|
|
|
if (tdata == NULL)
|
2012-04-23 07:00:11 +08:00
|
|
|
return (NULL);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_lock(tdata->lock);
|
|
|
|
not_found = ckh_search(&tdata->bt2tctx, bt, NULL, &ret.v);
|
2014-09-10 10:37:26 +08:00
|
|
|
if (!not_found) /* Note double negative! */
|
|
|
|
ret.p->prepared = true;
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_unlock(tdata->lock);
|
|
|
|
if (not_found) {
|
2015-01-30 07:30:47 +08:00
|
|
|
tcache_t *tcache;
|
2014-01-15 09:04:34 +08:00
|
|
|
void *btkey;
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_gctx_t *gctx;
|
|
|
|
bool new_gctx, error;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This thread's cache lacks bt. Look for it in the global
|
|
|
|
* cache.
|
|
|
|
*/
|
2014-09-23 12:09:23 +08:00
|
|
|
if (prof_lookup_global(tsd, bt, tdata, &btkey, &gctx,
|
2014-08-19 07:22:13 +08:00
|
|
|
&new_gctx))
|
2014-01-15 09:04:34 +08:00
|
|
|
return (NULL);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
/* Link a prof_tctx_t into gctx for this thread. */
|
2015-01-30 07:30:47 +08:00
|
|
|
tcache = tcache_get(tsd, true);
|
|
|
|
ret.v = iallocztm(tsd, sizeof(prof_tctx_t), false, tcache, true,
|
2014-11-28 03:22:36 +08:00
|
|
|
NULL);
|
2014-08-16 06:01:15 +08:00
|
|
|
if (ret.p == NULL) {
|
2014-08-19 07:22:13 +08:00
|
|
|
if (new_gctx)
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_gctx_try_destroy(tsd, tdata, gctx, tdata);
|
2014-08-16 06:01:15 +08:00
|
|
|
return (NULL);
|
2010-10-03 06:18:50 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
ret.p->tdata = tdata;
|
2014-10-13 04:03:20 +08:00
|
|
|
ret.p->thr_uid = tdata->thr_uid;
|
2010-09-21 10:53:25 +08:00
|
|
|
memset(&ret.p->cnts, 0, sizeof(prof_cnt_t));
|
2014-08-19 07:22:13 +08:00
|
|
|
ret.p->gctx = gctx;
|
2015-03-17 06:11:06 +08:00
|
|
|
ret.p->tctx_uid = tdata->tctx_uid_next++;
|
2014-09-10 10:37:26 +08:00
|
|
|
ret.p->prepared = true;
|
2014-09-25 13:14:21 +08:00
|
|
|
ret.p->state = prof_tctx_state_initializing;
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_lock(tdata->lock);
|
2014-09-23 12:09:23 +08:00
|
|
|
error = ckh_insert(tsd, &tdata->bt2tctx, btkey, ret.v);
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_unlock(tdata->lock);
|
|
|
|
if (error) {
|
|
|
|
if (new_gctx)
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_gctx_try_destroy(tsd, tdata, gctx, tdata);
|
2015-01-30 07:30:47 +08:00
|
|
|
idalloctm(tsd, ret.v, tcache, true);
|
2010-02-11 02:37:56 +08:00
|
|
|
return (NULL);
|
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_lock(gctx->lock);
|
2014-09-25 13:14:21 +08:00
|
|
|
ret.p->state = prof_tctx_state_nominal;
|
2014-08-19 07:22:13 +08:00
|
|
|
tctx_tree_insert(&gctx->tctxs, ret.p);
|
|
|
|
gctx->nlimbo--;
|
|
|
|
malloc_mutex_unlock(gctx->lock);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2010-09-21 10:53:25 +08:00
|
|
|
return (ret.p);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2014-04-16 04:47:13 +08:00
|
|
|
void
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_sample_threshold_update(prof_tdata_t *tdata)
|
2014-04-16 04:47:13 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The body of this function is compiled out unless heap profiling is
|
|
|
|
* enabled, so that it is possible to compile jemalloc with floating
|
|
|
|
* point support completely disabled. Avoiding floating point code is
|
|
|
|
* important on memory-constrained systems, but it also enables a
|
|
|
|
* workaround for versions of glibc that don't properly save/restore
|
|
|
|
* floating point registers during dynamic lazy symbol loading (which
|
|
|
|
* internally calls into whatever malloc implementation happens to be
|
|
|
|
* integrated into the application). Note that some compilers (e.g.
|
|
|
|
* gcc 4.8) may use floating point registers for fast memory moves, so
|
|
|
|
* jemalloc must be compiled with such optimizations disabled (e.g.
|
|
|
|
* -mno-sse) in order for the workaround to be complete.
|
|
|
|
*/
|
|
|
|
#ifdef JEMALLOC_PROF
|
|
|
|
uint64_t r;
|
|
|
|
double u;
|
|
|
|
|
|
|
|
if (!config_prof)
|
|
|
|
return;
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
if (lg_prof_sample == 0) {
|
|
|
|
tdata->bytes_until_sample = 0;
|
2014-04-16 04:47:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-08-19 07:22:13 +08:00
|
|
|
* Compute sample interval as a geometrically distributed random
|
|
|
|
* variable with mean (2^lg_prof_sample).
|
2014-04-16 04:47:13 +08:00
|
|
|
*
|
2014-08-19 07:22:13 +08:00
|
|
|
* __ __
|
|
|
|
* | log(u) | 1
|
|
|
|
* tdata->bytes_until_sample = | -------- |, where p = ---------------
|
|
|
|
* | log(1-p) | lg_prof_sample
|
|
|
|
* 2
|
2014-04-16 04:47:13 +08:00
|
|
|
*
|
|
|
|
* For more information on the math, see:
|
|
|
|
*
|
|
|
|
* Non-Uniform Random Variate Generation
|
|
|
|
* Luc Devroye
|
|
|
|
* Springer-Verlag, New York, 1986
|
|
|
|
* pp 500
|
|
|
|
* (http://luc.devroye.org/rnbookindex.html)
|
|
|
|
*/
|
2014-08-19 07:22:13 +08:00
|
|
|
prng64(r, 53, tdata->prng_state, UINT64_C(6364136223846793005),
|
|
|
|
UINT64_C(1442695040888963407));
|
2014-04-16 04:47:13 +08:00
|
|
|
u = (double)r * (1.0/9007199254740992.0L);
|
2014-08-19 07:22:13 +08:00
|
|
|
tdata->bytes_until_sample = (uint64_t)(log(u) /
|
|
|
|
log(1.0 - (1.0 / (double)((uint64_t)1U << lg_prof_sample))))
|
2014-04-16 04:47:13 +08:00
|
|
|
+ (uint64_t)1U;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
static prof_tdata_t *
|
|
|
|
prof_tdata_count_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata, void *arg)
|
|
|
|
{
|
|
|
|
size_t *tdata_count = (size_t *)arg;
|
|
|
|
|
|
|
|
(*tdata_count)++;
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
prof_tdata_count(void)
|
|
|
|
{
|
|
|
|
size_t tdata_count = 0;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&tdatas_mtx);
|
|
|
|
tdata_tree_iter(&tdatas, NULL, prof_tdata_count_iter,
|
|
|
|
(void *)&tdata_count);
|
|
|
|
malloc_mutex_unlock(&tdatas_mtx);
|
|
|
|
|
|
|
|
return (tdata_count);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-18 07:40:52 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
size_t
|
|
|
|
prof_bt_count(void)
|
|
|
|
{
|
|
|
|
size_t bt_count;
|
2014-09-23 12:09:23 +08:00
|
|
|
tsd_t *tsd;
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tdata_t *tdata;
|
2014-01-18 07:40:52 +08:00
|
|
|
|
2014-10-05 02:12:53 +08:00
|
|
|
tsd = tsd_fetch();
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = prof_tdata_get(tsd, false);
|
|
|
|
if (tdata == NULL)
|
2014-01-18 07:40:52 +08:00
|
|
|
return (0);
|
|
|
|
|
2014-10-31 07:50:33 +08:00
|
|
|
malloc_mutex_lock(&bt2gctx_mtx);
|
2014-08-19 07:22:13 +08:00
|
|
|
bt_count = ckh_count(&bt2gctx);
|
2014-10-31 07:50:33 +08:00
|
|
|
malloc_mutex_unlock(&bt2gctx_mtx);
|
2014-01-18 07:40:52 +08:00
|
|
|
|
|
|
|
return (bt_count);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef prof_dump_open
|
|
|
|
#define prof_dump_open JEMALLOC_N(prof_dump_open_impl)
|
|
|
|
#endif
|
|
|
|
static int
|
2014-01-17 05:23:56 +08:00
|
|
|
prof_dump_open(bool propagate_err, const char *filename)
|
|
|
|
{
|
2014-01-18 07:40:52 +08:00
|
|
|
int fd;
|
2014-01-17 05:23:56 +08:00
|
|
|
|
2014-01-18 07:40:52 +08:00
|
|
|
fd = creat(filename, 0644);
|
2014-10-04 01:16:09 +08:00
|
|
|
if (fd == -1 && !propagate_err) {
|
2014-01-18 07:40:52 +08:00
|
|
|
malloc_printf("<jemalloc>: creat(\"%s\"), 0644) failed\n",
|
|
|
|
filename);
|
|
|
|
if (opt_abort)
|
|
|
|
abort();
|
2014-01-17 05:23:56 +08:00
|
|
|
}
|
|
|
|
|
2014-01-18 07:40:52 +08:00
|
|
|
return (fd);
|
2014-01-17 05:23:56 +08:00
|
|
|
}
|
2014-01-18 07:40:52 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef prof_dump_open
|
|
|
|
#define prof_dump_open JEMALLOC_N(prof_dump_open)
|
|
|
|
prof_dump_open_t *prof_dump_open = JEMALLOC_N(prof_dump_open_impl);
|
|
|
|
#endif
|
2014-01-17 05:23:56 +08:00
|
|
|
|
|
|
|
static bool
|
|
|
|
prof_dump_flush(bool propagate_err)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2010-03-03 03:57:30 +08:00
|
|
|
bool ret = false;
|
2010-02-11 02:37:56 +08:00
|
|
|
ssize_t err;
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
err = write(prof_dump_fd, prof_dump_buf, prof_dump_buf_end);
|
|
|
|
if (err == -1) {
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!propagate_err) {
|
2010-03-04 09:45:38 +08:00
|
|
|
malloc_write("<jemalloc>: write() failed during heap "
|
|
|
|
"profile flush\n");
|
2010-03-03 03:57:30 +08:00
|
|
|
if (opt_abort)
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
ret = true;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
prof_dump_buf_end = 0;
|
2010-03-03 03:57:30 +08:00
|
|
|
|
|
|
|
return (ret);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2010-03-03 03:57:30 +08:00
|
|
|
static bool
|
2014-01-17 05:23:56 +08:00
|
|
|
prof_dump_close(bool propagate_err)
|
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
assert(prof_dump_fd != -1);
|
|
|
|
ret = prof_dump_flush(propagate_err);
|
|
|
|
close(prof_dump_fd);
|
|
|
|
prof_dump_fd = -1;
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
prof_dump_write(bool propagate_err, const char *s)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
unsigned i, slen, n;
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
i = 0;
|
|
|
|
slen = strlen(s);
|
|
|
|
while (i < slen) {
|
|
|
|
/* Flush the buffer if it is full. */
|
2012-03-22 09:33:03 +08:00
|
|
|
if (prof_dump_buf_end == PROF_DUMP_BUFSIZE)
|
2014-01-17 05:23:56 +08:00
|
|
|
if (prof_dump_flush(propagate_err) && propagate_err)
|
2010-03-03 03:57:30 +08:00
|
|
|
return (true);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
if (prof_dump_buf_end + slen <= PROF_DUMP_BUFSIZE) {
|
2010-02-11 02:37:56 +08:00
|
|
|
/* Finish writing. */
|
|
|
|
n = slen - i;
|
|
|
|
} else {
|
|
|
|
/* Write as much of s as will fit. */
|
2012-03-22 09:33:03 +08:00
|
|
|
n = PROF_DUMP_BUFSIZE - prof_dump_buf_end;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
memcpy(&prof_dump_buf[prof_dump_buf_end], &s[i], n);
|
|
|
|
prof_dump_buf_end += n;
|
|
|
|
i += n;
|
|
|
|
}
|
2010-03-03 03:57:30 +08:00
|
|
|
|
|
|
|
return (false);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2012-03-07 06:57:45 +08:00
|
|
|
JEMALLOC_ATTR(format(printf, 2, 3))
|
|
|
|
static bool
|
2014-01-17 05:23:56 +08:00
|
|
|
prof_dump_printf(bool propagate_err, const char *format, ...)
|
2012-03-07 06:57:45 +08:00
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
va_list ap;
|
2012-03-22 09:33:03 +08:00
|
|
|
char buf[PROF_PRINTF_BUFSIZE];
|
2012-03-07 06:57:45 +08:00
|
|
|
|
|
|
|
va_start(ap, format);
|
2012-03-24 09:05:51 +08:00
|
|
|
malloc_vsnprintf(buf, sizeof(buf), format, ap);
|
2012-03-07 06:57:45 +08:00
|
|
|
va_end(ap);
|
2014-01-17 05:23:56 +08:00
|
|
|
ret = prof_dump_write(propagate_err, buf);
|
2012-03-07 06:57:45 +08:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
/* tctx->tdata->lock is held. */
|
|
|
|
static void
|
|
|
|
prof_tctx_merge_tdata(prof_tctx_t *tctx, prof_tdata_t *tdata)
|
2014-08-17 03:58:55 +08:00
|
|
|
{
|
|
|
|
|
2014-09-25 13:14:21 +08:00
|
|
|
malloc_mutex_lock(tctx->gctx->lock);
|
2015-03-15 05:01:35 +08:00
|
|
|
|
|
|
|
switch (tctx->state) {
|
|
|
|
case prof_tctx_state_initializing:
|
2014-09-25 13:14:21 +08:00
|
|
|
malloc_mutex_unlock(tctx->gctx->lock);
|
|
|
|
return;
|
2015-03-15 05:01:35 +08:00
|
|
|
case prof_tctx_state_nominal:
|
|
|
|
tctx->state = prof_tctx_state_dumping;
|
|
|
|
malloc_mutex_unlock(tctx->gctx->lock);
|
2014-09-25 13:14:21 +08:00
|
|
|
|
2015-03-15 05:01:35 +08:00
|
|
|
memcpy(&tctx->dump_cnts, &tctx->cnts, sizeof(prof_cnt_t));
|
2014-08-17 03:58:55 +08:00
|
|
|
|
2015-03-15 05:01:35 +08:00
|
|
|
tdata->cnt_summed.curobjs += tctx->dump_cnts.curobjs;
|
|
|
|
tdata->cnt_summed.curbytes += tctx->dump_cnts.curbytes;
|
|
|
|
if (opt_prof_accum) {
|
|
|
|
tdata->cnt_summed.accumobjs +=
|
|
|
|
tctx->dump_cnts.accumobjs;
|
|
|
|
tdata->cnt_summed.accumbytes +=
|
|
|
|
tctx->dump_cnts.accumbytes;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case prof_tctx_state_dumping:
|
|
|
|
case prof_tctx_state_purgatory:
|
|
|
|
not_reached();
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-17 03:58:55 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
/* gctx->lock is held. */
|
|
|
|
static void
|
|
|
|
prof_tctx_merge_gctx(prof_tctx_t *tctx, prof_gctx_t *gctx)
|
|
|
|
{
|
2014-08-17 03:58:55 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
gctx->cnt_summed.curobjs += tctx->dump_cnts.curobjs;
|
|
|
|
gctx->cnt_summed.curbytes += tctx->dump_cnts.curbytes;
|
|
|
|
if (opt_prof_accum) {
|
|
|
|
gctx->cnt_summed.accumobjs += tctx->dump_cnts.accumobjs;
|
|
|
|
gctx->cnt_summed.accumbytes += tctx->dump_cnts.accumbytes;
|
2014-08-17 03:58:55 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
2014-08-17 03:58:55 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
/* tctx->gctx is held. */
|
|
|
|
static prof_tctx_t *
|
|
|
|
prof_tctx_merge_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (tctx->state) {
|
|
|
|
case prof_tctx_state_nominal:
|
|
|
|
/* New since dumping started; ignore. */
|
|
|
|
break;
|
|
|
|
case prof_tctx_state_dumping:
|
|
|
|
case prof_tctx_state_purgatory:
|
|
|
|
prof_tctx_merge_gctx(tctx, tctx->gctx);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
not_reached();
|
2014-08-17 03:58:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
/* gctx->lock is held. */
|
|
|
|
static prof_tctx_t *
|
|
|
|
prof_tctx_dump_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg)
|
|
|
|
{
|
|
|
|
bool propagate_err = *(bool *)arg;
|
|
|
|
|
|
|
|
if (prof_dump_printf(propagate_err,
|
|
|
|
" t%"PRIu64": %"PRIu64": %"PRIu64" [%"PRIu64": %"PRIu64"]\n",
|
2014-10-13 04:03:20 +08:00
|
|
|
tctx->thr_uid, tctx->dump_cnts.curobjs, tctx->dump_cnts.curbytes,
|
|
|
|
tctx->dump_cnts.accumobjs, tctx->dump_cnts.accumbytes))
|
2014-08-19 07:22:13 +08:00
|
|
|
return (tctx);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tctx->gctx is held. */
|
|
|
|
static prof_tctx_t *
|
|
|
|
prof_tctx_finish_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg)
|
|
|
|
{
|
|
|
|
prof_tctx_t *ret;
|
|
|
|
|
|
|
|
switch (tctx->state) {
|
|
|
|
case prof_tctx_state_nominal:
|
|
|
|
/* New since dumping started; ignore. */
|
|
|
|
break;
|
|
|
|
case prof_tctx_state_dumping:
|
|
|
|
tctx->state = prof_tctx_state_nominal;
|
|
|
|
break;
|
|
|
|
case prof_tctx_state_purgatory:
|
2014-10-03 14:01:10 +08:00
|
|
|
ret = tctx;
|
2014-08-19 07:22:13 +08:00
|
|
|
goto label_return;
|
|
|
|
default:
|
|
|
|
not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = NULL;
|
|
|
|
label_return:
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
static void
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_dump_gctx_prep(prof_gctx_t *gctx, prof_gctx_tree_t *gctxs)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_lock(gctx->lock);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-01-17 05:23:56 +08:00
|
|
|
/*
|
2014-08-19 07:22:13 +08:00
|
|
|
* Increment nlimbo so that gctx won't go away before dump.
|
|
|
|
* Additionally, link gctx into the dump list so that it is included in
|
2014-01-17 05:23:56 +08:00
|
|
|
* prof_dump()'s second pass.
|
|
|
|
*/
|
2014-08-19 07:22:13 +08:00
|
|
|
gctx->nlimbo++;
|
|
|
|
gctx_tree_insert(gctxs, gctx);
|
2014-01-17 05:23:56 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
memset(&gctx->cnt_summed, 0, sizeof(prof_cnt_t));
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_unlock(gctx->lock);
|
|
|
|
}
|
2010-10-03 13:39:59 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
static prof_gctx_t *
|
|
|
|
prof_gctx_merge_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *arg)
|
|
|
|
{
|
|
|
|
size_t *leak_ngctx = (size_t *)arg;
|
2010-10-03 06:18:50 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_lock(gctx->lock);
|
|
|
|
tctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_merge_iter, NULL);
|
|
|
|
if (gctx->cnt_summed.curobjs != 0)
|
|
|
|
(*leak_ngctx)++;
|
|
|
|
malloc_mutex_unlock(gctx->lock);
|
|
|
|
|
|
|
|
return (NULL);
|
2010-10-03 06:18:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
static void
|
|
|
|
prof_gctx_finish(tsd_t *tsd, prof_gctx_tree_t *gctxs)
|
2010-10-03 06:18:50 +08:00
|
|
|
{
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_tdata_t *tdata = prof_tdata_get(tsd, false);
|
2014-10-03 14:01:10 +08:00
|
|
|
prof_gctx_t *gctx;
|
2014-01-17 05:23:56 +08:00
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
/*
|
|
|
|
* Standard tree iteration won't work here, because as soon as we
|
|
|
|
* decrement gctx->nlimbo and unlock gctx, another thread can
|
|
|
|
* concurrently destroy it, which will corrupt the tree. Therefore,
|
|
|
|
* tear down the tree one node at a time during iteration.
|
|
|
|
*/
|
|
|
|
while ((gctx = gctx_tree_first(gctxs)) != NULL) {
|
|
|
|
gctx_tree_remove(gctxs, gctx);
|
|
|
|
malloc_mutex_lock(gctx->lock);
|
|
|
|
{
|
|
|
|
prof_tctx_t *next;
|
|
|
|
|
|
|
|
next = NULL;
|
|
|
|
do {
|
|
|
|
prof_tctx_t *to_destroy =
|
|
|
|
tctx_tree_iter(&gctx->tctxs, next,
|
|
|
|
prof_tctx_finish_iter, NULL);
|
|
|
|
if (to_destroy != NULL) {
|
|
|
|
next = tctx_tree_next(&gctx->tctxs,
|
|
|
|
to_destroy);
|
|
|
|
tctx_tree_remove(&gctx->tctxs,
|
|
|
|
to_destroy);
|
2015-01-30 07:30:47 +08:00
|
|
|
idalloctm(tsd, to_destroy,
|
|
|
|
tcache_get(tsd, false), true);
|
2014-10-03 14:01:10 +08:00
|
|
|
} else
|
|
|
|
next = NULL;
|
|
|
|
} while (next != NULL);
|
|
|
|
}
|
|
|
|
gctx->nlimbo--;
|
|
|
|
if (prof_gctx_should_destroy(gctx)) {
|
|
|
|
gctx->nlimbo++;
|
|
|
|
malloc_mutex_unlock(gctx->lock);
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_gctx_try_destroy(tsd, tdata, gctx, tdata);
|
2014-10-03 14:01:10 +08:00
|
|
|
} else
|
|
|
|
malloc_mutex_unlock(gctx->lock);
|
|
|
|
}
|
2010-10-03 06:18:50 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
static prof_tdata_t *
|
|
|
|
prof_tdata_merge_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata, void *arg)
|
2010-10-03 06:18:50 +08:00
|
|
|
{
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_cnt_t *cnt_all = (prof_cnt_t *)arg;
|
|
|
|
|
|
|
|
malloc_mutex_lock(tdata->lock);
|
2014-10-03 14:01:10 +08:00
|
|
|
if (!tdata->expired) {
|
2014-08-19 07:22:13 +08:00
|
|
|
size_t tabind;
|
|
|
|
union {
|
|
|
|
prof_tctx_t *p;
|
|
|
|
void *v;
|
|
|
|
} tctx;
|
|
|
|
|
|
|
|
tdata->dumping = true;
|
|
|
|
memset(&tdata->cnt_summed, 0, sizeof(prof_cnt_t));
|
2014-10-04 01:16:09 +08:00
|
|
|
for (tabind = 0; !ckh_iter(&tdata->bt2tctx, &tabind, NULL,
|
|
|
|
&tctx.v);)
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tctx_merge_tdata(tctx.p, tdata);
|
|
|
|
|
|
|
|
cnt_all->curobjs += tdata->cnt_summed.curobjs;
|
|
|
|
cnt_all->curbytes += tdata->cnt_summed.curbytes;
|
|
|
|
if (opt_prof_accum) {
|
|
|
|
cnt_all->accumobjs += tdata->cnt_summed.accumobjs;
|
|
|
|
cnt_all->accumbytes += tdata->cnt_summed.accumbytes;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
tdata->dumping = false;
|
|
|
|
malloc_mutex_unlock(tdata->lock);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
return (NULL);
|
2014-01-17 05:23:56 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
static prof_tdata_t *
|
|
|
|
prof_tdata_dump_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata, void *arg)
|
|
|
|
{
|
|
|
|
bool propagate_err = *(bool *)arg;
|
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!tdata->dumping)
|
2014-08-19 07:22:13 +08:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (prof_dump_printf(propagate_err,
|
|
|
|
" t%"PRIu64": %"PRIu64": %"PRIu64" [%"PRIu64": %"PRIu64"]%s%s\n",
|
|
|
|
tdata->thr_uid, tdata->cnt_summed.curobjs,
|
|
|
|
tdata->cnt_summed.curbytes, tdata->cnt_summed.accumobjs,
|
|
|
|
tdata->cnt_summed.accumbytes,
|
|
|
|
(tdata->thread_name != NULL) ? " " : "",
|
|
|
|
(tdata->thread_name != NULL) ? tdata->thread_name : ""))
|
|
|
|
return (tdata);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef prof_dump_header
|
|
|
|
#define prof_dump_header JEMALLOC_N(prof_dump_header_impl)
|
|
|
|
#endif
|
2014-08-19 07:22:13 +08:00
|
|
|
static bool
|
|
|
|
prof_dump_header(bool propagate_err, const prof_cnt_t *cnt_all)
|
2014-01-17 05:23:56 +08:00
|
|
|
{
|
2014-08-19 07:22:13 +08:00
|
|
|
bool ret;
|
|
|
|
|
|
|
|
if (prof_dump_printf(propagate_err,
|
|
|
|
"heap_v2/%"PRIu64"\n"
|
|
|
|
" t*: %"PRIu64": %"PRIu64" [%"PRIu64": %"PRIu64"]\n",
|
|
|
|
((uint64_t)1U << lg_prof_sample), cnt_all->curobjs,
|
|
|
|
cnt_all->curbytes, cnt_all->accumobjs, cnt_all->accumbytes))
|
|
|
|
return (true);
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_lock(&tdatas_mtx);
|
|
|
|
ret = (tdata_tree_iter(&tdatas, NULL, prof_tdata_dump_iter,
|
|
|
|
(void *)&propagate_err) != NULL);
|
|
|
|
malloc_mutex_unlock(&tdatas_mtx);
|
|
|
|
return (ret);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
2014-10-03 14:01:10 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef prof_dump_header
|
|
|
|
#define prof_dump_header JEMALLOC_N(prof_dump_header)
|
|
|
|
prof_dump_header_t *prof_dump_header = JEMALLOC_N(prof_dump_header_impl);
|
|
|
|
#endif
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
/* gctx->lock is held. */
|
2010-03-03 03:57:30 +08:00
|
|
|
static bool
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_dump_gctx(bool propagate_err, prof_gctx_t *gctx, const prof_bt_t *bt,
|
|
|
|
prof_gctx_tree_t *gctxs)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2014-01-17 05:23:56 +08:00
|
|
|
bool ret;
|
2010-02-11 02:37:56 +08:00
|
|
|
unsigned i;
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
/* Avoid dumping such gctx's that have no useful data. */
|
2014-10-04 01:16:09 +08:00
|
|
|
if ((!opt_prof_accum && gctx->cnt_summed.curobjs == 0) ||
|
2014-08-19 07:22:13 +08:00
|
|
|
(opt_prof_accum && gctx->cnt_summed.accumobjs == 0)) {
|
|
|
|
assert(gctx->cnt_summed.curobjs == 0);
|
|
|
|
assert(gctx->cnt_summed.curbytes == 0);
|
|
|
|
assert(gctx->cnt_summed.accumobjs == 0);
|
|
|
|
assert(gctx->cnt_summed.accumbytes == 0);
|
2014-01-17 05:23:56 +08:00
|
|
|
ret = false;
|
|
|
|
goto label_return;
|
2010-10-03 06:18:50 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
if (prof_dump_printf(propagate_err, "@")) {
|
2014-01-17 05:23:56 +08:00
|
|
|
ret = true;
|
|
|
|
goto label_return;
|
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
for (i = 0; i < bt->len; i++) {
|
2014-01-17 05:23:56 +08:00
|
|
|
if (prof_dump_printf(propagate_err, " %#"PRIxPTR,
|
|
|
|
(uintptr_t)bt->vec[i])) {
|
|
|
|
ret = true;
|
|
|
|
goto label_return;
|
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
if (prof_dump_printf(propagate_err,
|
|
|
|
"\n"
|
|
|
|
" t*: %"PRIu64": %"PRIu64" [%"PRIu64": %"PRIu64"]\n",
|
|
|
|
gctx->cnt_summed.curobjs, gctx->cnt_summed.curbytes,
|
|
|
|
gctx->cnt_summed.accumobjs, gctx->cnt_summed.accumbytes)) {
|
|
|
|
ret = true;
|
|
|
|
goto label_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_dump_iter,
|
|
|
|
(void *)&propagate_err) != NULL) {
|
2014-01-17 05:23:56 +08:00
|
|
|
ret = true;
|
|
|
|
goto label_return;
|
|
|
|
}
|
2010-03-03 03:57:30 +08:00
|
|
|
|
2014-01-18 07:40:52 +08:00
|
|
|
ret = false;
|
2014-01-17 05:23:56 +08:00
|
|
|
label_return:
|
|
|
|
return (ret);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2015-05-02 00:03:20 +08:00
|
|
|
JEMALLOC_ATTR(format(printf, 1, 2))
|
|
|
|
static int
|
|
|
|
prof_open_maps(const char *format, ...)
|
|
|
|
{
|
|
|
|
int mfd;
|
|
|
|
va_list ap;
|
|
|
|
char filename[PATH_MAX + 1];
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
malloc_vsnprintf(filename, sizeof(filename), format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
mfd = open(filename, O_RDONLY);
|
|
|
|
|
|
|
|
return (mfd);
|
|
|
|
}
|
|
|
|
|
2010-03-03 03:57:30 +08:00
|
|
|
static bool
|
|
|
|
prof_dump_maps(bool propagate_err)
|
2010-02-12 01:25:56 +08:00
|
|
|
{
|
2013-10-22 06:07:40 +08:00
|
|
|
bool ret;
|
2010-02-12 01:25:56 +08:00
|
|
|
int mfd;
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
2014-03-18 15:00:14 +08:00
|
|
|
#ifdef __FreeBSD__
|
2015-05-02 00:03:20 +08:00
|
|
|
mfd = prof_open_maps("/proc/curproc/map");
|
2014-03-18 15:00:14 +08:00
|
|
|
#else
|
2015-05-02 00:03:20 +08:00
|
|
|
{
|
|
|
|
int pid = getpid();
|
|
|
|
|
|
|
|
mfd = prof_open_maps("/proc/%d/task/%d/maps", pid, pid);
|
|
|
|
if (mfd == -1)
|
|
|
|
mfd = prof_open_maps("/proc/%d/maps", pid);
|
|
|
|
}
|
2014-03-18 15:00:14 +08:00
|
|
|
#endif
|
2010-02-12 01:25:56 +08:00
|
|
|
if (mfd != -1) {
|
|
|
|
ssize_t nread;
|
|
|
|
|
2014-01-17 05:23:56 +08:00
|
|
|
if (prof_dump_write(propagate_err, "\nMAPPED_LIBRARIES:\n") &&
|
2013-10-22 06:07:40 +08:00
|
|
|
propagate_err) {
|
|
|
|
ret = true;
|
|
|
|
goto label_return;
|
|
|
|
}
|
2010-02-12 01:25:56 +08:00
|
|
|
nread = 0;
|
|
|
|
do {
|
|
|
|
prof_dump_buf_end += nread;
|
2012-03-22 09:33:03 +08:00
|
|
|
if (prof_dump_buf_end == PROF_DUMP_BUFSIZE) {
|
2010-02-12 01:25:56 +08:00
|
|
|
/* Make space in prof_dump_buf before read(). */
|
2014-01-17 05:23:56 +08:00
|
|
|
if (prof_dump_flush(propagate_err) &&
|
2013-10-22 06:07:40 +08:00
|
|
|
propagate_err) {
|
|
|
|
ret = true;
|
|
|
|
goto label_return;
|
|
|
|
}
|
2010-02-12 01:25:56 +08:00
|
|
|
}
|
|
|
|
nread = read(mfd, &prof_dump_buf[prof_dump_buf_end],
|
2012-03-22 09:33:03 +08:00
|
|
|
PROF_DUMP_BUFSIZE - prof_dump_buf_end);
|
2010-02-12 01:25:56 +08:00
|
|
|
} while (nread > 0);
|
2013-10-22 06:07:40 +08:00
|
|
|
} else {
|
|
|
|
ret = true;
|
|
|
|
goto label_return;
|
|
|
|
}
|
2010-03-03 03:57:30 +08:00
|
|
|
|
2013-10-22 06:07:40 +08:00
|
|
|
ret = false;
|
|
|
|
label_return:
|
|
|
|
if (mfd != -1)
|
|
|
|
close(mfd);
|
|
|
|
return (ret);
|
2010-02-12 01:25:56 +08:00
|
|
|
}
|
|
|
|
|
2014-01-17 05:23:56 +08:00
|
|
|
static void
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_leakcheck(const prof_cnt_t *cnt_all, size_t leak_ngctx,
|
2014-01-17 05:23:56 +08:00
|
|
|
const char *filename)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (cnt_all->curbytes != 0) {
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_printf("<jemalloc>: Leak summary: %"PRIu64" byte%s, %"
|
2015-07-08 04:12:05 +08:00
|
|
|
PRIu64" object%s, %"PRIzu" context%s\n",
|
2014-01-17 05:23:56 +08:00
|
|
|
cnt_all->curbytes, (cnt_all->curbytes != 1) ? "s" : "",
|
|
|
|
cnt_all->curobjs, (cnt_all->curobjs != 1) ? "s" : "",
|
2014-08-19 07:22:13 +08:00
|
|
|
leak_ngctx, (leak_ngctx != 1) ? "s" : "");
|
2014-01-17 05:23:56 +08:00
|
|
|
malloc_printf(
|
2015-05-02 03:31:12 +08:00
|
|
|
"<jemalloc>: Run jeprof on \"%s\" for leak detail\n",
|
2014-01-17 05:23:56 +08:00
|
|
|
filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
static prof_gctx_t *
|
|
|
|
prof_gctx_dump_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *arg)
|
2014-08-17 03:58:55 +08:00
|
|
|
{
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_gctx_t *ret;
|
2014-08-17 03:58:55 +08:00
|
|
|
bool propagate_err = *(bool *)arg;
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_lock(gctx->lock);
|
2014-08-17 03:58:55 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
if (prof_dump_gctx(propagate_err, gctx, &gctx->bt, gctxs)) {
|
2014-10-03 14:01:10 +08:00
|
|
|
ret = gctx;
|
2014-08-19 07:22:13 +08:00
|
|
|
goto label_return;
|
|
|
|
}
|
2014-08-17 03:58:55 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
ret = NULL;
|
|
|
|
label_return:
|
|
|
|
malloc_mutex_unlock(gctx->lock);
|
|
|
|
return (ret);
|
2014-08-17 03:58:55 +08:00
|
|
|
}
|
|
|
|
|
2010-03-03 03:57:30 +08:00
|
|
|
static bool
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_dump(tsd_t *tsd, bool propagate_err, const char *filename, bool leakcheck)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tdata_t *tdata;
|
2010-02-11 02:37:56 +08:00
|
|
|
prof_cnt_t cnt_all;
|
|
|
|
size_t tabind;
|
2010-09-21 10:53:25 +08:00
|
|
|
union {
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_gctx_t *p;
|
2010-09-21 10:53:25 +08:00
|
|
|
void *v;
|
2014-08-19 07:22:13 +08:00
|
|
|
} gctx;
|
|
|
|
size_t leak_ngctx;
|
|
|
|
prof_gctx_tree_t gctxs;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
tdata = prof_tdata_get(tsd, true);
|
2014-09-23 12:09:23 +08:00
|
|
|
if (tdata == NULL)
|
2012-04-23 07:00:11 +08:00
|
|
|
return (true);
|
2014-01-17 05:23:56 +08:00
|
|
|
|
|
|
|
malloc_mutex_lock(&prof_dump_mtx);
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_enter(tsd, tdata);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
/*
|
|
|
|
* Put gctx's in limbo and clear their counters in preparation for
|
|
|
|
* summing.
|
|
|
|
*/
|
|
|
|
gctx_tree_new(&gctxs);
|
2014-10-04 01:16:09 +08:00
|
|
|
for (tabind = 0; !ckh_iter(&bt2gctx, &tabind, NULL, &gctx.v);)
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_dump_gctx_prep(gctx.p, &gctxs);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate over tdatas, and for the non-expired ones snapshot their tctx
|
|
|
|
* stats and merge them into the associated gctx's.
|
|
|
|
*/
|
2010-02-11 02:37:56 +08:00
|
|
|
memset(&cnt_all, 0, sizeof(prof_cnt_t));
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_lock(&tdatas_mtx);
|
|
|
|
tdata_tree_iter(&tdatas, NULL, prof_tdata_merge_iter, (void *)&cnt_all);
|
|
|
|
malloc_mutex_unlock(&tdatas_mtx);
|
|
|
|
|
|
|
|
/* Merge tctx stats into gctx's. */
|
|
|
|
leak_ngctx = 0;
|
|
|
|
gctx_tree_iter(&gctxs, NULL, prof_gctx_merge_iter, (void *)&leak_ngctx);
|
|
|
|
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_leave(tsd, tdata);
|
2014-01-17 05:23:56 +08:00
|
|
|
|
|
|
|
/* Create dump file. */
|
2014-01-18 07:40:52 +08:00
|
|
|
if ((prof_dump_fd = prof_dump_open(propagate_err, filename)) == -1)
|
2014-01-17 05:23:56 +08:00
|
|
|
goto label_open_close_error;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/* Dump profile header. */
|
2014-01-17 05:23:56 +08:00
|
|
|
if (prof_dump_header(propagate_err, &cnt_all))
|
|
|
|
goto label_write_error;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
/* Dump per gctx profile stats. */
|
|
|
|
if (gctx_tree_iter(&gctxs, NULL, prof_gctx_dump_iter,
|
|
|
|
(void *)&propagate_err) != NULL)
|
2014-08-17 03:58:55 +08:00
|
|
|
goto label_write_error;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2010-02-12 01:25:56 +08:00
|
|
|
/* Dump /proc/<pid>/maps if possible. */
|
2010-03-03 03:57:30 +08:00
|
|
|
if (prof_dump_maps(propagate_err))
|
2014-01-17 05:23:56 +08:00
|
|
|
goto label_write_error;
|
2010-02-12 01:25:56 +08:00
|
|
|
|
2014-01-17 05:23:56 +08:00
|
|
|
if (prof_dump_close(propagate_err))
|
|
|
|
goto label_open_close_error;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
prof_gctx_finish(tsd, &gctxs);
|
2014-01-17 05:23:56 +08:00
|
|
|
malloc_mutex_unlock(&prof_dump_mtx);
|
|
|
|
|
|
|
|
if (leakcheck)
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_leakcheck(&cnt_all, leak_ngctx, filename);
|
2010-03-03 03:57:30 +08:00
|
|
|
|
|
|
|
return (false);
|
2014-01-17 05:23:56 +08:00
|
|
|
label_write_error:
|
|
|
|
prof_dump_close(propagate_err);
|
|
|
|
label_open_close_error:
|
2014-10-03 14:01:10 +08:00
|
|
|
prof_gctx_finish(tsd, &gctxs);
|
2014-01-17 05:23:56 +08:00
|
|
|
malloc_mutex_unlock(&prof_dump_mtx);
|
2010-03-03 03:57:30 +08:00
|
|
|
return (true);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2012-03-07 06:57:45 +08:00
|
|
|
#define DUMP_FILENAME_BUFSIZE (PATH_MAX + 1)
|
2014-01-17 05:23:56 +08:00
|
|
|
#define VSEQ_INVALID UINT64_C(0xffffffffffffffff)
|
2010-02-11 02:37:56 +08:00
|
|
|
static void
|
2014-05-29 10:04:06 +08:00
|
|
|
prof_dump_filename(char *filename, char v, uint64_t vseq)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2014-01-17 05:23:56 +08:00
|
|
|
if (vseq != VSEQ_INVALID) {
|
2012-03-07 06:57:45 +08:00
|
|
|
/* "<prefix>.<pid>.<seq>.v<vseq>.heap" */
|
|
|
|
malloc_snprintf(filename, DUMP_FILENAME_BUFSIZE,
|
2014-05-29 10:04:06 +08:00
|
|
|
"%s.%d.%"PRIu64".%c%"PRIu64".heap",
|
2012-03-07 06:57:45 +08:00
|
|
|
opt_prof_prefix, (int)getpid(), prof_dump_seq, v, vseq);
|
|
|
|
} else {
|
|
|
|
/* "<prefix>.<pid>.<seq>.<v>.heap" */
|
|
|
|
malloc_snprintf(filename, DUMP_FILENAME_BUFSIZE,
|
|
|
|
"%s.%d.%"PRIu64".%c.heap",
|
|
|
|
opt_prof_prefix, (int)getpid(), prof_dump_seq, v);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
2012-04-23 07:00:11 +08:00
|
|
|
prof_dump_seq++;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prof_fdump(void)
|
|
|
|
{
|
2014-09-23 12:09:23 +08:00
|
|
|
tsd_t *tsd;
|
2010-02-11 02:37:56 +08:00
|
|
|
char filename[DUMP_FILENAME_BUFSIZE];
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
2014-10-09 08:57:19 +08:00
|
|
|
assert(opt_prof_final);
|
|
|
|
assert(opt_prof_prefix[0] != '\0');
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!prof_booted)
|
2010-02-11 02:37:56 +08:00
|
|
|
return;
|
2014-10-05 02:12:53 +08:00
|
|
|
tsd = tsd_fetch();
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-10-09 08:57:19 +08:00
|
|
|
malloc_mutex_lock(&prof_dump_seq_mtx);
|
|
|
|
prof_dump_filename(filename, 'f', VSEQ_INVALID);
|
|
|
|
malloc_mutex_unlock(&prof_dump_seq_mtx);
|
|
|
|
prof_dump(tsd, false, filename, opt_prof_leak);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prof_idump(void)
|
|
|
|
{
|
2014-09-23 12:09:23 +08:00
|
|
|
tsd_t *tsd;
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tdata_t *tdata;
|
2012-03-07 06:57:45 +08:00
|
|
|
char filename[PATH_MAX + 1];
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!prof_booted)
|
2010-02-11 02:37:56 +08:00
|
|
|
return;
|
2014-10-05 02:12:53 +08:00
|
|
|
tsd = tsd_fetch();
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = prof_tdata_get(tsd, false);
|
|
|
|
if (tdata == NULL)
|
2012-04-23 07:00:11 +08:00
|
|
|
return;
|
2014-08-19 07:22:13 +08:00
|
|
|
if (tdata->enq) {
|
|
|
|
tdata->enq_idump = true;
|
2010-02-12 05:19:21 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2010-10-24 09:37:06 +08:00
|
|
|
if (opt_prof_prefix[0] != '\0') {
|
|
|
|
malloc_mutex_lock(&prof_dump_seq_mtx);
|
|
|
|
prof_dump_filename(filename, 'i', prof_dump_iseq);
|
|
|
|
prof_dump_iseq++;
|
|
|
|
malloc_mutex_unlock(&prof_dump_seq_mtx);
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_dump(tsd, false, filename, false);
|
2010-10-24 09:37:06 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2010-03-03 03:57:30 +08:00
|
|
|
bool
|
|
|
|
prof_mdump(const char *filename)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2014-09-23 12:09:23 +08:00
|
|
|
tsd_t *tsd;
|
2010-03-03 03:57:30 +08:00
|
|
|
char filename_buf[DUMP_FILENAME_BUFSIZE];
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!opt_prof || !prof_booted)
|
2010-03-03 03:57:30 +08:00
|
|
|
return (true);
|
2014-10-05 02:12:53 +08:00
|
|
|
tsd = tsd_fetch();
|
2010-03-03 03:57:30 +08:00
|
|
|
|
|
|
|
if (filename == NULL) {
|
|
|
|
/* No filename specified, so automatically generate one. */
|
2010-10-24 09:37:06 +08:00
|
|
|
if (opt_prof_prefix[0] == '\0')
|
|
|
|
return (true);
|
2010-03-03 03:57:30 +08:00
|
|
|
malloc_mutex_lock(&prof_dump_seq_mtx);
|
|
|
|
prof_dump_filename(filename_buf, 'm', prof_dump_mseq);
|
|
|
|
prof_dump_mseq++;
|
|
|
|
malloc_mutex_unlock(&prof_dump_seq_mtx);
|
|
|
|
filename = filename_buf;
|
|
|
|
}
|
2014-09-23 12:09:23 +08:00
|
|
|
return (prof_dump(tsd, true, filename, false));
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-10-24 09:37:06 +08:00
|
|
|
prof_gdump(void)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2014-09-23 12:09:23 +08:00
|
|
|
tsd_t *tsd;
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tdata_t *tdata;
|
2010-02-11 02:37:56 +08:00
|
|
|
char filename[DUMP_FILENAME_BUFSIZE];
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!prof_booted)
|
2010-02-11 02:37:56 +08:00
|
|
|
return;
|
2014-10-05 02:12:53 +08:00
|
|
|
tsd = tsd_fetch();
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = prof_tdata_get(tsd, false);
|
|
|
|
if (tdata == NULL)
|
2012-04-23 07:00:11 +08:00
|
|
|
return;
|
2014-08-19 07:22:13 +08:00
|
|
|
if (tdata->enq) {
|
|
|
|
tdata->enq_gdump = true;
|
2010-02-11 02:37:56 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-24 09:37:06 +08:00
|
|
|
if (opt_prof_prefix[0] != '\0') {
|
|
|
|
malloc_mutex_lock(&prof_dump_seq_mtx);
|
|
|
|
prof_dump_filename(filename, 'u', prof_dump_useq);
|
|
|
|
prof_dump_useq++;
|
|
|
|
malloc_mutex_unlock(&prof_dump_seq_mtx);
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_dump(tsd, false, filename, false);
|
2010-10-24 09:37:06 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-01-23 04:02:08 +08:00
|
|
|
prof_bt_hash(const void *key, size_t r_hash[2])
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
prof_bt_t *bt = (prof_bt_t *)key;
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2013-01-23 04:02:08 +08:00
|
|
|
hash(bt->vec, bt->len * sizeof(void *), 0x94122f33U, r_hash);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
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;
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
if (bt1->len != bt2->len)
|
|
|
|
return (false);
|
|
|
|
return (memcmp(bt1->vec, bt2->vec, bt1->len * sizeof(void *)) == 0);
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
JEMALLOC_INLINE_C uint64_t
|
|
|
|
prof_thr_uid_alloc(void)
|
|
|
|
{
|
2014-09-12 09:06:30 +08:00
|
|
|
uint64_t thr_uid;
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2014-09-12 09:06:30 +08:00
|
|
|
malloc_mutex_lock(&next_thr_uid_mtx);
|
|
|
|
thr_uid = next_thr_uid;
|
|
|
|
next_thr_uid++;
|
|
|
|
malloc_mutex_unlock(&next_thr_uid_mtx);
|
|
|
|
|
|
|
|
return (thr_uid);
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static prof_tdata_t *
|
2014-10-04 14:25:30 +08:00
|
|
|
prof_tdata_init_impl(tsd_t *tsd, uint64_t thr_uid, uint64_t thr_discrim,
|
|
|
|
char *thread_name, bool active)
|
2010-10-21 10:05:59 +08:00
|
|
|
{
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tdata_t *tdata;
|
2015-01-30 07:30:47 +08:00
|
|
|
tcache_t *tcache;
|
2010-10-21 10:05:59 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2010-10-21 10:05:59 +08:00
|
|
|
/* Initialize an empty cache for this thread. */
|
2015-01-30 07:30:47 +08:00
|
|
|
tcache = tcache_get(tsd, true);
|
2014-11-28 03:22:36 +08:00
|
|
|
tdata = (prof_tdata_t *)iallocztm(tsd, sizeof(prof_tdata_t), false,
|
2015-01-30 07:30:47 +08:00
|
|
|
tcache, true, NULL);
|
2014-08-19 07:22:13 +08:00
|
|
|
if (tdata == NULL)
|
2010-10-21 10:05:59 +08:00
|
|
|
return (NULL);
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
tdata->lock = prof_tdata_mutex_choose(thr_uid);
|
|
|
|
tdata->thr_uid = thr_uid;
|
2014-10-03 14:01:10 +08:00
|
|
|
tdata->thr_discrim = thr_discrim;
|
2014-10-04 14:25:30 +08:00
|
|
|
tdata->thread_name = thread_name;
|
2014-10-03 14:01:10 +08:00
|
|
|
tdata->attached = true;
|
|
|
|
tdata->expired = false;
|
2015-03-17 06:11:06 +08:00
|
|
|
tdata->tctx_uid_next = 0;
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2014-09-23 12:09:23 +08:00
|
|
|
if (ckh_new(tsd, &tdata->bt2tctx, PROF_CKH_MINITEMS,
|
2010-10-21 10:05:59 +08:00
|
|
|
prof_bt_hash, prof_bt_keycomp)) {
|
2015-01-30 07:30:47 +08:00
|
|
|
idalloctm(tsd, tdata, tcache, true);
|
2010-10-21 10:05:59 +08:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
tdata->prng_state = (uint64_t)(uintptr_t)tdata;
|
|
|
|
prof_sample_threshold_update(tdata);
|
|
|
|
|
|
|
|
tdata->enq = false;
|
|
|
|
tdata->enq_idump = false;
|
|
|
|
tdata->enq_gdump = false;
|
|
|
|
|
|
|
|
tdata->dumping = false;
|
2014-10-04 14:25:30 +08:00
|
|
|
tdata->active = active;
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
malloc_mutex_lock(&tdatas_mtx);
|
|
|
|
tdata_tree_insert(&tdatas, tdata);
|
|
|
|
malloc_mutex_unlock(&tdatas_mtx);
|
|
|
|
|
|
|
|
return (tdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
prof_tdata_t *
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_tdata_init(tsd_t *tsd)
|
2014-08-19 07:22:13 +08:00
|
|
|
{
|
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
return (prof_tdata_init_impl(tsd, prof_thr_uid_alloc(), 0, NULL,
|
|
|
|
prof_thread_active_init_get()));
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* tdata->lock must be held. */
|
|
|
|
static bool
|
2014-10-05 06:03:49 +08:00
|
|
|
prof_tdata_should_destroy(prof_tdata_t *tdata, bool even_if_attached)
|
2014-08-19 07:22:13 +08:00
|
|
|
{
|
|
|
|
|
2014-10-05 06:03:49 +08:00
|
|
|
if (tdata->attached && !even_if_attached)
|
2014-08-19 07:22:13 +08:00
|
|
|
return (false);
|
|
|
|
if (ckh_count(&tdata->bt2tctx) != 0)
|
|
|
|
return (false);
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
/* tdatas_mtx must be held. */
|
2014-08-19 07:22:13 +08:00
|
|
|
static void
|
2014-10-05 06:03:49 +08:00
|
|
|
prof_tdata_destroy_locked(tsd_t *tsd, prof_tdata_t *tdata,
|
|
|
|
bool even_if_attached)
|
2014-08-19 07:22:13 +08:00
|
|
|
{
|
2015-01-30 07:30:47 +08:00
|
|
|
tcache_t *tcache;
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2014-10-05 06:03:49 +08:00
|
|
|
assert(prof_tdata_should_destroy(tdata, even_if_attached));
|
2014-10-05 02:12:53 +08:00
|
|
|
assert(tsd_prof_tdata_get(tsd) != tdata);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
tdata_tree_remove(&tdatas, tdata);
|
|
|
|
|
2015-01-30 07:30:47 +08:00
|
|
|
tcache = tcache_get(tsd, false);
|
2014-08-19 07:22:13 +08:00
|
|
|
if (tdata->thread_name != NULL)
|
2015-01-30 07:30:47 +08:00
|
|
|
idalloctm(tsd, tdata->thread_name, tcache, true);
|
2014-09-23 12:09:23 +08:00
|
|
|
ckh_delete(tsd, &tdata->bt2tctx);
|
2015-01-30 07:30:47 +08:00
|
|
|
idalloctm(tsd, tdata, tcache, true);
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-10-05 06:03:49 +08:00
|
|
|
prof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata, bool even_if_attached)
|
2014-10-03 14:01:10 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_lock(&tdatas_mtx);
|
2014-10-05 06:03:49 +08:00
|
|
|
prof_tdata_destroy_locked(tsd, tdata, even_if_attached);
|
2014-10-03 14:01:10 +08:00
|
|
|
malloc_mutex_unlock(&tdatas_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prof_tdata_detach(tsd_t *tsd, prof_tdata_t *tdata)
|
2014-08-19 07:22:13 +08:00
|
|
|
{
|
|
|
|
bool destroy_tdata;
|
|
|
|
|
|
|
|
malloc_mutex_lock(tdata->lock);
|
2014-10-03 14:01:10 +08:00
|
|
|
if (tdata->attached) {
|
2014-10-05 06:03:49 +08:00
|
|
|
destroy_tdata = prof_tdata_should_destroy(tdata, true);
|
|
|
|
/*
|
|
|
|
* Only detach if !destroy_tdata, because detaching would allow
|
|
|
|
* another thread to win the race to destroy tdata.
|
|
|
|
*/
|
|
|
|
if (!destroy_tdata)
|
|
|
|
tdata->attached = false;
|
2014-10-05 02:12:53 +08:00
|
|
|
tsd_prof_tdata_set(tsd, NULL);
|
2014-08-19 07:22:13 +08:00
|
|
|
} else
|
|
|
|
destroy_tdata = false;
|
|
|
|
malloc_mutex_unlock(tdata->lock);
|
|
|
|
if (destroy_tdata)
|
2014-10-05 06:03:49 +08:00
|
|
|
prof_tdata_destroy(tsd, tdata, true);
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
2010-10-21 10:05:59 +08:00
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
prof_tdata_t *
|
|
|
|
prof_tdata_reinit(tsd_t *tsd, prof_tdata_t *tdata)
|
2014-08-19 07:22:13 +08:00
|
|
|
{
|
2014-10-03 14:01:10 +08:00
|
|
|
uint64_t thr_uid = tdata->thr_uid;
|
|
|
|
uint64_t thr_discrim = tdata->thr_discrim + 1;
|
2014-10-04 14:25:30 +08:00
|
|
|
char *thread_name = (tdata->thread_name != NULL) ?
|
|
|
|
prof_thread_name_alloc(tsd, tdata->thread_name) : NULL;
|
|
|
|
bool active = tdata->active;
|
2012-04-23 07:00:11 +08:00
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
prof_tdata_detach(tsd, tdata);
|
2014-10-04 14:25:30 +08:00
|
|
|
return (prof_tdata_init_impl(tsd, thr_uid, thr_discrim, thread_name,
|
|
|
|
active));
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
2010-10-21 10:05:59 +08:00
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
static bool
|
|
|
|
prof_tdata_expire(prof_tdata_t *tdata)
|
2014-08-19 07:22:13 +08:00
|
|
|
{
|
2014-10-03 14:01:10 +08:00
|
|
|
bool destroy_tdata;
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
malloc_mutex_lock(tdata->lock);
|
|
|
|
if (!tdata->expired) {
|
|
|
|
tdata->expired = true;
|
|
|
|
destroy_tdata = tdata->attached ? false :
|
2014-10-05 06:03:49 +08:00
|
|
|
prof_tdata_should_destroy(tdata, false);
|
2014-10-03 14:01:10 +08:00
|
|
|
} else
|
|
|
|
destroy_tdata = false;
|
|
|
|
malloc_mutex_unlock(tdata->lock);
|
|
|
|
|
|
|
|
return (destroy_tdata);
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static prof_tdata_t *
|
|
|
|
prof_tdata_reset_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata, void *arg)
|
|
|
|
{
|
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
return (prof_tdata_expire(tdata) ? tdata : NULL);
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_reset(tsd_t *tsd, size_t lg_sample)
|
2014-08-19 07:22:13 +08:00
|
|
|
{
|
2014-10-03 14:01:10 +08:00
|
|
|
prof_tdata_t *next;
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
assert(lg_sample < (sizeof(uint64_t) << 3));
|
|
|
|
|
|
|
|
malloc_mutex_lock(&prof_dump_mtx);
|
|
|
|
malloc_mutex_lock(&tdatas_mtx);
|
|
|
|
|
|
|
|
lg_prof_sample = lg_sample;
|
2014-10-03 14:01:10 +08:00
|
|
|
|
|
|
|
next = NULL;
|
|
|
|
do {
|
|
|
|
prof_tdata_t *to_destroy = tdata_tree_iter(&tdatas, next,
|
|
|
|
prof_tdata_reset_iter, NULL);
|
|
|
|
if (to_destroy != NULL) {
|
|
|
|
next = tdata_tree_next(&tdatas, to_destroy);
|
2014-10-05 06:03:49 +08:00
|
|
|
prof_tdata_destroy_locked(tsd, to_destroy, false);
|
2014-10-03 14:01:10 +08:00
|
|
|
} else
|
|
|
|
next = NULL;
|
|
|
|
} while (next != NULL);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
malloc_mutex_unlock(&tdatas_mtx);
|
|
|
|
malloc_mutex_unlock(&prof_dump_mtx);
|
2010-10-21 10:05:59 +08:00
|
|
|
}
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
void
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_tdata_cleanup(tsd_t *tsd)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_tdata_t *tdata;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-09-23 12:09:23 +08:00
|
|
|
if (!config_prof)
|
|
|
|
return;
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = tsd_prof_tdata_get(tsd);
|
|
|
|
if (tdata != NULL)
|
|
|
|
prof_tdata_detach(tsd, tdata);
|
2010-10-03 13:38:14 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
bool
|
|
|
|
prof_active_get(void)
|
|
|
|
{
|
|
|
|
bool prof_active_current;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&prof_active_mtx);
|
|
|
|
prof_active_current = prof_active;
|
|
|
|
malloc_mutex_unlock(&prof_active_mtx);
|
|
|
|
return (prof_active_current);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
prof_active_set(bool active)
|
|
|
|
{
|
|
|
|
bool prof_active_old;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&prof_active_mtx);
|
|
|
|
prof_active_old = prof_active;
|
|
|
|
prof_active = active;
|
|
|
|
malloc_mutex_unlock(&prof_active_mtx);
|
|
|
|
return (prof_active_old);
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
const char *
|
|
|
|
prof_thread_name_get(void)
|
|
|
|
{
|
2014-09-23 12:09:23 +08:00
|
|
|
tsd_t *tsd;
|
|
|
|
prof_tdata_t *tdata;
|
|
|
|
|
2014-10-05 02:12:53 +08:00
|
|
|
tsd = tsd_fetch();
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = prof_tdata_get(tsd, true);
|
|
|
|
if (tdata == NULL)
|
2014-10-04 14:25:30 +08:00
|
|
|
return ("");
|
|
|
|
return (tdata->thread_name != NULL ? tdata->thread_name : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
prof_thread_name_alloc(tsd_t *tsd, const char *thread_name)
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
if (thread_name == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
size = strlen(thread_name) + 1;
|
|
|
|
if (size == 1)
|
|
|
|
return ("");
|
|
|
|
|
2015-01-30 07:30:47 +08:00
|
|
|
ret = iallocztm(tsd, size, false, tcache_get(tsd, true), true, NULL);
|
2014-10-04 14:25:30 +08:00
|
|
|
if (ret == NULL)
|
2014-08-19 07:22:13 +08:00
|
|
|
return (NULL);
|
2014-10-04 14:25:30 +08:00
|
|
|
memcpy(ret, thread_name, size);
|
|
|
|
return (ret);
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
int
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_thread_name_set(tsd_t *tsd, const char *thread_name)
|
2014-08-19 07:22:13 +08:00
|
|
|
{
|
|
|
|
prof_tdata_t *tdata;
|
2014-10-04 14:25:30 +08:00
|
|
|
unsigned i;
|
2014-08-19 07:22:13 +08:00
|
|
|
char *s;
|
|
|
|
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = prof_tdata_get(tsd, true);
|
|
|
|
if (tdata == NULL)
|
2014-10-04 14:25:30 +08:00
|
|
|
return (EAGAIN);
|
|
|
|
|
|
|
|
/* Validate input. */
|
|
|
|
if (thread_name == NULL)
|
|
|
|
return (EFAULT);
|
|
|
|
for (i = 0; thread_name[i] != '\0'; i++) {
|
|
|
|
char c = thread_name[i];
|
|
|
|
if (!isgraph(c) && !isblank(c))
|
|
|
|
return (EFAULT);
|
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
s = prof_thread_name_alloc(tsd, thread_name);
|
2014-08-19 07:22:13 +08:00
|
|
|
if (s == NULL)
|
2014-10-04 14:25:30 +08:00
|
|
|
return (EAGAIN);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
if (tdata->thread_name != NULL) {
|
2015-01-30 07:30:47 +08:00
|
|
|
idalloctm(tsd, tdata->thread_name, tcache_get(tsd, false),
|
|
|
|
true);
|
2014-10-04 14:25:30 +08:00
|
|
|
tdata->thread_name = NULL;
|
|
|
|
}
|
|
|
|
if (strlen(s) > 0)
|
|
|
|
tdata->thread_name = s;
|
|
|
|
return (0);
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
prof_thread_active_get(void)
|
|
|
|
{
|
2014-09-23 12:09:23 +08:00
|
|
|
tsd_t *tsd;
|
|
|
|
prof_tdata_t *tdata;
|
|
|
|
|
2014-10-05 02:12:53 +08:00
|
|
|
tsd = tsd_fetch();
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = prof_tdata_get(tsd, true);
|
|
|
|
if (tdata == NULL)
|
2014-08-19 07:22:13 +08:00
|
|
|
return (false);
|
|
|
|
return (tdata->active);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
prof_thread_active_set(bool active)
|
|
|
|
{
|
2014-09-23 12:09:23 +08:00
|
|
|
tsd_t *tsd;
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tdata_t *tdata;
|
|
|
|
|
2014-10-05 02:12:53 +08:00
|
|
|
tsd = tsd_fetch();
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = prof_tdata_get(tsd, true);
|
|
|
|
if (tdata == NULL)
|
2014-08-19 07:22:13 +08:00
|
|
|
return (true);
|
|
|
|
tdata->active = active;
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
bool
|
|
|
|
prof_thread_active_init_get(void)
|
|
|
|
{
|
|
|
|
bool active_init;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&prof_thread_active_init_mtx);
|
|
|
|
active_init = prof_thread_active_init;
|
|
|
|
malloc_mutex_unlock(&prof_thread_active_init_mtx);
|
|
|
|
return (active_init);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
prof_thread_active_init_set(bool active_init)
|
|
|
|
{
|
|
|
|
bool active_init_old;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&prof_thread_active_init_mtx);
|
|
|
|
active_init_old = prof_thread_active_init;
|
|
|
|
prof_thread_active_init = active_init;
|
|
|
|
malloc_mutex_unlock(&prof_thread_active_init_mtx);
|
|
|
|
return (active_init_old);
|
|
|
|
}
|
|
|
|
|
2015-01-26 13:16:57 +08:00
|
|
|
bool
|
|
|
|
prof_gdump_get(void)
|
|
|
|
{
|
|
|
|
bool prof_gdump_current;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&prof_gdump_mtx);
|
|
|
|
prof_gdump_current = prof_gdump_val;
|
|
|
|
malloc_mutex_unlock(&prof_gdump_mtx);
|
|
|
|
return (prof_gdump_current);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
prof_gdump_set(bool gdump)
|
|
|
|
{
|
|
|
|
bool prof_gdump_old;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&prof_gdump_mtx);
|
|
|
|
prof_gdump_old = prof_gdump_val;
|
|
|
|
prof_gdump_val = gdump;
|
|
|
|
malloc_mutex_unlock(&prof_gdump_mtx);
|
|
|
|
return (prof_gdump_old);
|
|
|
|
}
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
void
|
|
|
|
prof_boot0(void)
|
2010-10-24 09:37:06 +08:00
|
|
|
{
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2010-10-24 09:37:06 +08:00
|
|
|
memcpy(opt_prof_prefix, PROF_PREFIX_DEFAULT,
|
|
|
|
sizeof(PROF_PREFIX_DEFAULT));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prof_boot1(void)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
/*
|
2014-04-12 05:24:51 +08:00
|
|
|
* opt_prof must be in its final state before any arenas are
|
|
|
|
* initialized, so this function must be executed early.
|
2010-02-11 02:37:56 +08:00
|
|
|
*/
|
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (opt_prof_leak && !opt_prof) {
|
2010-02-11 02:37:56 +08:00
|
|
|
/*
|
|
|
|
* Enable opt_prof, but in such a way that profiles are never
|
|
|
|
* automatically dumped.
|
|
|
|
*/
|
|
|
|
opt_prof = true;
|
2010-10-24 09:37:06 +08:00
|
|
|
opt_prof_gdump = false;
|
2010-04-01 08:35:51 +08:00
|
|
|
} else if (opt_prof) {
|
|
|
|
if (opt_lg_prof_interval >= 0) {
|
|
|
|
prof_interval = (((uint64_t)1U) <<
|
|
|
|
opt_lg_prof_interval);
|
2012-11-14 04:56:27 +08:00
|
|
|
}
|
2010-04-01 08:35:51 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2010-10-24 09:37:06 +08:00
|
|
|
prof_boot2(void)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
if (opt_prof) {
|
2014-09-23 12:09:23 +08:00
|
|
|
tsd_t *tsd;
|
2012-03-24 09:05:51 +08:00
|
|
|
unsigned i;
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
lg_prof_sample = opt_lg_prof_sample;
|
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
prof_active = opt_prof_active;
|
|
|
|
if (malloc_mutex_init(&prof_active_mtx))
|
|
|
|
return (true);
|
|
|
|
|
2015-01-26 13:16:57 +08:00
|
|
|
prof_gdump_val = opt_prof_gdump;
|
|
|
|
if (malloc_mutex_init(&prof_gdump_mtx))
|
|
|
|
return (true);
|
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
prof_thread_active_init = opt_prof_thread_active_init;
|
|
|
|
if (malloc_mutex_init(&prof_thread_active_init_mtx))
|
|
|
|
return (true);
|
|
|
|
|
2014-10-05 02:12:53 +08:00
|
|
|
tsd = tsd_fetch();
|
2014-09-23 12:09:23 +08:00
|
|
|
if (ckh_new(tsd, &bt2gctx, PROF_CKH_MINITEMS, prof_bt_hash,
|
2010-02-11 02:37:56 +08:00
|
|
|
prof_bt_keycomp))
|
|
|
|
return (true);
|
2014-08-19 07:22:13 +08:00
|
|
|
if (malloc_mutex_init(&bt2gctx_mtx))
|
2010-02-11 02:37:56 +08:00
|
|
|
return (true);
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
tdata_tree_new(&tdatas);
|
|
|
|
if (malloc_mutex_init(&tdatas_mtx))
|
|
|
|
return (true);
|
|
|
|
|
|
|
|
next_thr_uid = 0;
|
2014-09-12 09:06:30 +08:00
|
|
|
if (malloc_mutex_init(&next_thr_uid_mtx))
|
|
|
|
return (true);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
if (malloc_mutex_init(&prof_dump_seq_mtx))
|
|
|
|
return (true);
|
2014-01-17 05:23:56 +08:00
|
|
|
if (malloc_mutex_init(&prof_dump_mtx))
|
|
|
|
return (true);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-10-09 08:57:19 +08:00
|
|
|
if (opt_prof_final && opt_prof_prefix[0] != '\0' &&
|
|
|
|
atexit(prof_fdump) != 0) {
|
2010-03-04 09:45:38 +08:00
|
|
|
malloc_write("<jemalloc>: Error in atexit()\n");
|
2010-02-11 02:37:56 +08:00
|
|
|
if (opt_abort)
|
|
|
|
abort();
|
|
|
|
}
|
2012-03-24 09:05:51 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
gctx_locks = (malloc_mutex_t *)base_alloc(PROF_NCTX_LOCKS *
|
2012-03-24 09:05:51 +08:00
|
|
|
sizeof(malloc_mutex_t));
|
2014-08-19 07:22:13 +08:00
|
|
|
if (gctx_locks == NULL)
|
2012-03-24 09:05:51 +08:00
|
|
|
return (true);
|
|
|
|
for (i = 0; i < PROF_NCTX_LOCKS; i++) {
|
2014-08-19 07:22:13 +08:00
|
|
|
if (malloc_mutex_init(&gctx_locks[i]))
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
tdata_locks = (malloc_mutex_t *)base_alloc(PROF_NTDATA_LOCKS *
|
|
|
|
sizeof(malloc_mutex_t));
|
|
|
|
if (tdata_locks == NULL)
|
|
|
|
return (true);
|
|
|
|
for (i = 0; i < PROF_NTDATA_LOCKS; i++) {
|
|
|
|
if (malloc_mutex_init(&tdata_locks[i]))
|
2012-03-24 09:05:51 +08:00
|
|
|
return (true);
|
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2010-02-11 10:15:53 +08:00
|
|
|
#ifdef JEMALLOC_PROF_LIBGCC
|
|
|
|
/*
|
|
|
|
* Cause the backtracing machinery to allocate its internal state
|
|
|
|
* before enabling profiling.
|
|
|
|
*/
|
|
|
|
_Unwind_Backtrace(prof_unwind_init_callback, NULL);
|
|
|
|
#endif
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
prof_booted = true;
|
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2012-10-10 05:46:22 +08:00
|
|
|
void
|
|
|
|
prof_prefork(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (opt_prof) {
|
|
|
|
unsigned i;
|
|
|
|
|
2014-09-12 09:06:30 +08:00
|
|
|
malloc_mutex_prefork(&tdatas_mtx);
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_prefork(&bt2gctx_mtx);
|
2014-09-12 09:06:30 +08:00
|
|
|
malloc_mutex_prefork(&next_thr_uid_mtx);
|
2013-10-22 05:59:10 +08:00
|
|
|
malloc_mutex_prefork(&prof_dump_seq_mtx);
|
2012-10-10 05:46:22 +08:00
|
|
|
for (i = 0; i < PROF_NCTX_LOCKS; i++)
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_prefork(&gctx_locks[i]);
|
2014-09-12 09:06:30 +08:00
|
|
|
for (i = 0; i < PROF_NTDATA_LOCKS; i++)
|
|
|
|
malloc_mutex_prefork(&tdata_locks[i]);
|
2012-10-10 05:46:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prof_postfork_parent(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (opt_prof) {
|
|
|
|
unsigned i;
|
|
|
|
|
2014-09-12 09:06:30 +08:00
|
|
|
for (i = 0; i < PROF_NTDATA_LOCKS; i++)
|
|
|
|
malloc_mutex_postfork_parent(&tdata_locks[i]);
|
2012-10-10 05:46:22 +08:00
|
|
|
for (i = 0; i < PROF_NCTX_LOCKS; i++)
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_postfork_parent(&gctx_locks[i]);
|
2012-10-10 05:46:22 +08:00
|
|
|
malloc_mutex_postfork_parent(&prof_dump_seq_mtx);
|
2014-09-12 09:06:30 +08:00
|
|
|
malloc_mutex_postfork_parent(&next_thr_uid_mtx);
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_postfork_parent(&bt2gctx_mtx);
|
2014-09-12 09:06:30 +08:00
|
|
|
malloc_mutex_postfork_parent(&tdatas_mtx);
|
2012-10-10 05:46:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prof_postfork_child(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (opt_prof) {
|
|
|
|
unsigned i;
|
|
|
|
|
2014-09-12 09:06:30 +08:00
|
|
|
for (i = 0; i < PROF_NTDATA_LOCKS; i++)
|
|
|
|
malloc_mutex_postfork_child(&tdata_locks[i]);
|
2012-10-10 05:46:22 +08:00
|
|
|
for (i = 0; i < PROF_NCTX_LOCKS; i++)
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_postfork_child(&gctx_locks[i]);
|
2012-10-10 05:46:22 +08:00
|
|
|
malloc_mutex_postfork_child(&prof_dump_seq_mtx);
|
2014-09-12 09:06:30 +08:00
|
|
|
malloc_mutex_postfork_child(&next_thr_uid_mtx);
|
2014-08-19 07:22:13 +08:00
|
|
|
malloc_mutex_postfork_child(&bt2gctx_mtx);
|
2014-09-12 09:06:30 +08:00
|
|
|
malloc_mutex_postfork_child(&tdatas_mtx);
|
2012-10-10 05:46:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
/******************************************************************************/
|