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. */
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
malloc_tsd_data(, prof_tdata, prof_tdata_t *, NULL)
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
bool opt_prof = false;
|
2010-04-01 09:43:24 +08:00
|
|
|
bool opt_prof_active = 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;
|
2012-04-18 07:39:33 +08:00
|
|
|
bool opt_prof_final = true;
|
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
|
|
|
|
2012-11-14 04:56:27 +08:00
|
|
|
uint64_t prof_interval = 0;
|
2010-02-12 05:19:21 +08:00
|
|
|
|
2012-03-24 09:05:51 +08:00
|
|
|
/*
|
|
|
|
* Table of mutexes that are shared among ctx's. These are leaf locks, so
|
|
|
|
* there is no problem with using them for more than one ctx at the same time.
|
|
|
|
* The primary motivation for this sharing though is that ctx's are ephemeral,
|
|
|
|
* and destroying mutexes causes complications for systems that allocate when
|
|
|
|
* creating/destroying mutexes.
|
|
|
|
*/
|
|
|
|
static malloc_mutex_t *ctx_locks;
|
|
|
|
static unsigned cum_ctxs; /* Atomic counter. */
|
|
|
|
|
2010-10-03 13:38:14 +08:00
|
|
|
/*
|
2010-10-21 10:05:59 +08:00
|
|
|
* Global hash of (prof_bt_t *)-->(prof_ctx_t *). This is the master data
|
|
|
|
* structure that knows about all backtraces currently captured.
|
2010-10-03 13:38:14 +08:00
|
|
|
*/
|
2010-10-21 10:05:59 +08:00
|
|
|
static ckh_t bt2ctx;
|
|
|
|
static malloc_mutex_t bt2ctx_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-17 03:58:55 +08:00
|
|
|
JEMALLOC_INLINE_C int
|
|
|
|
prof_thr_cnt_comp(const prof_thr_cnt_t *a, const prof_thr_cnt_t *b)
|
|
|
|
{
|
|
|
|
prof_thr_uid_t a_uid = a->thr_uid;
|
|
|
|
prof_thr_uid_t b_uid = b->thr_uid;
|
|
|
|
|
|
|
|
return ((a_uid > b_uid) - (a_uid < b_uid));
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_gen(static UNUSED, thr_cnt_tree_, prof_thr_cnt_tree_t, prof_thr_cnt_t,
|
|
|
|
thr_cnt_link, prof_thr_cnt_comp)
|
|
|
|
|
|
|
|
JEMALLOC_INLINE_C int
|
|
|
|
prof_ctx_comp(const prof_ctx_t *a, const prof_ctx_t *b)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_gen(static UNUSED, ctx_tree_, prof_ctx_tree_t, prof_ctx_t, dump_link,
|
|
|
|
prof_ctx_comp)
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2012-04-23 07:00:11 +08:00
|
|
|
prof_enter(prof_tdata_t *prof_tdata)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2012-04-23 07:00:11 +08:00
|
|
|
assert(prof_tdata->enq == false);
|
|
|
|
prof_tdata->enq = true;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
malloc_mutex_lock(&bt2ctx_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2012-04-23 07:00:11 +08:00
|
|
|
prof_leave(prof_tdata_t *prof_tdata)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2010-10-24 09:37:06 +08:00
|
|
|
bool idump, gdump;
|
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
|
|
|
malloc_mutex_unlock(&bt2ctx_mtx);
|
|
|
|
|
2012-04-23 07:00:11 +08:00
|
|
|
assert(prof_tdata->enq);
|
|
|
|
prof_tdata->enq = false;
|
|
|
|
idump = prof_tdata->enq_idump;
|
|
|
|
prof_tdata->enq_idump = false;
|
|
|
|
gdump = prof_tdata->enq_gdump;
|
|
|
|
prof_tdata->enq_gdump = false;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2010-02-12 05:19:21 +08:00
|
|
|
if (idump)
|
|
|
|
prof_idump();
|
2010-10-24 09:37:06 +08:00
|
|
|
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 *
|
|
|
|
prof_ctx_mutex_choose(void)
|
|
|
|
{
|
|
|
|
unsigned nctxs = atomic_add_u(&cum_ctxs, 1);
|
|
|
|
|
|
|
|
return (&ctx_locks[(nctxs - 1) % PROF_NCTX_LOCKS]);
|
|
|
|
}
|
|
|
|
|
2014-08-16 06:05:12 +08:00
|
|
|
static prof_ctx_t *
|
|
|
|
prof_ctx_create(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.
|
|
|
|
*/
|
|
|
|
prof_ctx_t *ctx = (prof_ctx_t *)imalloc(offsetof(prof_ctx_t, vec) +
|
|
|
|
(bt->len * sizeof(void *)));
|
|
|
|
if (ctx == NULL)
|
|
|
|
return (NULL);
|
2014-01-17 05:23:56 +08:00
|
|
|
ctx->lock = prof_ctx_mutex_choose();
|
|
|
|
/*
|
|
|
|
* Set nlimbo to 1, in order to avoid a race condition with
|
|
|
|
* prof_ctx_merge()/prof_ctx_destroy().
|
|
|
|
*/
|
|
|
|
ctx->nlimbo = 1;
|
|
|
|
memset(&ctx->cnt_merged, 0, sizeof(prof_cnt_t));
|
2014-08-17 03:58:55 +08:00
|
|
|
thr_cnt_tree_new(&ctx->thr_cnts);
|
2014-08-16 06:05:12 +08:00
|
|
|
/* Duplicate bt. */
|
|
|
|
memcpy(ctx->vec, bt->vec, bt->len * sizeof(void *));
|
|
|
|
ctx->bt.vec = ctx->vec;
|
|
|
|
ctx->bt.len = bt->len;
|
|
|
|
return (ctx);
|
2014-01-17 05:23:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prof_ctx_destroy(prof_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
prof_tdata_t *prof_tdata;
|
|
|
|
|
|
|
|
cassert(config_prof);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that ctx is still unused by any thread cache before destroying
|
|
|
|
* it. prof_lookup() increments ctx->nlimbo in order to avoid a race
|
|
|
|
* condition with this function, as does prof_ctx_merge() in order to
|
|
|
|
* avoid a race between the main body of prof_ctx_merge() and entry
|
|
|
|
* into this function.
|
|
|
|
*/
|
|
|
|
prof_tdata = prof_tdata_get(false);
|
|
|
|
assert((uintptr_t)prof_tdata > (uintptr_t)PROF_TDATA_STATE_MAX);
|
|
|
|
prof_enter(prof_tdata);
|
|
|
|
malloc_mutex_lock(ctx->lock);
|
2014-08-17 03:58:55 +08:00
|
|
|
if (thr_cnt_tree_first(&ctx->thr_cnts) == NULL &&
|
|
|
|
ctx->cnt_merged.curobjs == 0 && ctx->nlimbo == 1) {
|
2014-01-17 05:23:56 +08:00
|
|
|
assert(ctx->cnt_merged.curbytes == 0);
|
|
|
|
assert(ctx->cnt_merged.accumobjs == 0);
|
|
|
|
assert(ctx->cnt_merged.accumbytes == 0);
|
|
|
|
/* Remove ctx from bt2ctx. */
|
2014-08-16 06:05:12 +08:00
|
|
|
if (ckh_remove(&bt2ctx, &ctx->bt, NULL, NULL))
|
2014-01-17 05:23:56 +08:00
|
|
|
not_reached();
|
|
|
|
prof_leave(prof_tdata);
|
|
|
|
/* Destroy ctx. */
|
|
|
|
malloc_mutex_unlock(ctx->lock);
|
|
|
|
idalloc(ctx);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Compensate for increment in prof_ctx_merge() or
|
|
|
|
* prof_lookup().
|
|
|
|
*/
|
|
|
|
ctx->nlimbo--;
|
|
|
|
malloc_mutex_unlock(ctx->lock);
|
|
|
|
prof_leave(prof_tdata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prof_ctx_merge(prof_ctx_t *ctx, prof_thr_cnt_t *cnt)
|
|
|
|
{
|
|
|
|
bool destroy;
|
|
|
|
|
|
|
|
cassert(config_prof);
|
|
|
|
|
|
|
|
/* Merge cnt stats and detach from ctx. */
|
|
|
|
malloc_mutex_lock(ctx->lock);
|
|
|
|
ctx->cnt_merged.curobjs += cnt->cnts.curobjs;
|
|
|
|
ctx->cnt_merged.curbytes += cnt->cnts.curbytes;
|
|
|
|
ctx->cnt_merged.accumobjs += cnt->cnts.accumobjs;
|
|
|
|
ctx->cnt_merged.accumbytes += cnt->cnts.accumbytes;
|
2014-08-17 03:58:55 +08:00
|
|
|
thr_cnt_tree_remove(&ctx->thr_cnts, cnt);
|
|
|
|
if (opt_prof_accum == false && thr_cnt_tree_first(&ctx->thr_cnts) ==
|
|
|
|
NULL && ctx->cnt_merged.curobjs == 0 && ctx->nlimbo == 0) {
|
2014-01-17 05:23:56 +08:00
|
|
|
/*
|
|
|
|
* Increment ctx->nlimbo in order to keep another thread from
|
|
|
|
* winning the race to destroy ctx while this one has ctx->lock
|
|
|
|
* dropped. Without this, it would be possible for another
|
|
|
|
* thread to:
|
|
|
|
*
|
|
|
|
* 1) Sample an allocation associated with ctx.
|
|
|
|
* 2) Deallocate the sampled object.
|
|
|
|
* 3) Successfully prof_ctx_destroy(ctx).
|
|
|
|
*
|
|
|
|
* The result would be that ctx no longer exists by the time
|
|
|
|
* this thread accesses it in prof_ctx_destroy().
|
|
|
|
*/
|
|
|
|
ctx->nlimbo++;
|
|
|
|
destroy = true;
|
|
|
|
} else
|
|
|
|
destroy = false;
|
|
|
|
malloc_mutex_unlock(ctx->lock);
|
|
|
|
if (destroy)
|
|
|
|
prof_ctx_destroy(ctx);
|
|
|
|
}
|
|
|
|
|
2014-01-15 09:04:34 +08:00
|
|
|
static bool
|
|
|
|
prof_lookup_global(prof_bt_t *bt, prof_tdata_t *prof_tdata, void **p_btkey,
|
|
|
|
prof_ctx_t **p_ctx, bool *p_new_ctx)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
prof_ctx_t *p;
|
|
|
|
void *v;
|
|
|
|
} ctx;
|
|
|
|
union {
|
|
|
|
prof_bt_t *p;
|
|
|
|
void *v;
|
|
|
|
} btkey;
|
|
|
|
bool new_ctx;
|
|
|
|
|
|
|
|
prof_enter(prof_tdata);
|
|
|
|
if (ckh_search(&bt2ctx, bt, &btkey.v, &ctx.v)) {
|
|
|
|
/* bt has never been seen before. Insert it. */
|
2014-08-16 06:05:12 +08:00
|
|
|
ctx.p = prof_ctx_create(bt);
|
2014-01-15 09:04:34 +08:00
|
|
|
if (ctx.v == NULL) {
|
|
|
|
prof_leave(prof_tdata);
|
|
|
|
return (true);
|
|
|
|
}
|
2014-08-16 06:05:12 +08:00
|
|
|
btkey.p = &ctx.p->bt;
|
2014-01-15 09:04:34 +08:00
|
|
|
if (ckh_insert(&bt2ctx, btkey.v, ctx.v)) {
|
|
|
|
/* OOM. */
|
|
|
|
prof_leave(prof_tdata);
|
|
|
|
idalloc(ctx.v);
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
new_ctx = true;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Increment nlimbo, in order to avoid a race condition with
|
|
|
|
* prof_ctx_merge()/prof_ctx_destroy().
|
|
|
|
*/
|
|
|
|
malloc_mutex_lock(ctx.p->lock);
|
|
|
|
ctx.p->nlimbo++;
|
|
|
|
malloc_mutex_unlock(ctx.p->lock);
|
|
|
|
new_ctx = false;
|
|
|
|
}
|
|
|
|
prof_leave(prof_tdata);
|
|
|
|
|
|
|
|
*p_btkey = btkey.v;
|
|
|
|
*p_ctx = ctx.p;
|
|
|
|
*p_new_ctx = new_ctx;
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2010-10-21 10:05:59 +08:00
|
|
|
prof_thr_cnt_t *
|
2010-02-11 02:37:56 +08:00
|
|
|
prof_lookup(prof_bt_t *bt)
|
|
|
|
{
|
2010-09-21 10:53:25 +08:00
|
|
|
union {
|
|
|
|
prof_thr_cnt_t *p;
|
|
|
|
void *v;
|
|
|
|
} ret;
|
2010-10-21 10:05:59 +08:00
|
|
|
prof_tdata_t *prof_tdata;
|
2010-10-03 06:18:50 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2013-01-31 07:03:11 +08:00
|
|
|
prof_tdata = prof_tdata_get(false);
|
2012-04-29 14:27:13 +08:00
|
|
|
if ((uintptr_t)prof_tdata <= (uintptr_t)PROF_TDATA_STATE_MAX)
|
2012-04-23 07:00:11 +08:00
|
|
|
return (NULL);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2010-10-21 10:05:59 +08:00
|
|
|
if (ckh_search(&prof_tdata->bt2cnt, bt, NULL, &ret.v)) {
|
2014-01-15 09:04:34 +08:00
|
|
|
void *btkey;
|
|
|
|
prof_ctx_t *ctx;
|
2011-01-15 09:27:44 +08:00
|
|
|
bool new_ctx;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This thread's cache lacks bt. Look for it in the global
|
|
|
|
* cache.
|
|
|
|
*/
|
2014-01-15 09:04:34 +08:00
|
|
|
if (prof_lookup_global(bt, prof_tdata, &btkey, &ctx, &new_ctx))
|
|
|
|
return (NULL);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/* Link a prof_thd_cnt_t into ctx for this thread. */
|
2014-08-16 06:01:15 +08:00
|
|
|
ret.v = imalloc(sizeof(prof_thr_cnt_t));
|
|
|
|
if (ret.p == NULL) {
|
|
|
|
if (new_ctx)
|
|
|
|
prof_ctx_destroy(ctx);
|
|
|
|
return (NULL);
|
2010-10-03 06:18:50 +08:00
|
|
|
}
|
2014-01-15 09:04:34 +08:00
|
|
|
ret.p->ctx = ctx;
|
2010-09-21 10:53:25 +08:00
|
|
|
ret.p->epoch = 0;
|
|
|
|
memset(&ret.p->cnts, 0, sizeof(prof_cnt_t));
|
2014-01-15 09:04:34 +08:00
|
|
|
if (ckh_insert(&prof_tdata->bt2cnt, btkey, ret.v)) {
|
2011-08-10 10:06:06 +08:00
|
|
|
if (new_ctx)
|
2014-01-15 09:04:34 +08:00
|
|
|
prof_ctx_destroy(ctx);
|
2010-09-21 10:53:25 +08:00
|
|
|
idalloc(ret.v);
|
2010-02-11 02:37:56 +08:00
|
|
|
return (NULL);
|
|
|
|
}
|
2014-01-15 09:04:34 +08:00
|
|
|
malloc_mutex_lock(ctx->lock);
|
2014-08-17 03:58:55 +08:00
|
|
|
thr_cnt_tree_insert(&ctx->thr_cnts, ret.p);
|
2014-01-15 09:04:34 +08:00
|
|
|
ctx->nlimbo--;
|
|
|
|
malloc_mutex_unlock(ctx->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
|
|
|
|
prof_sample_threshold_update(prof_tdata_t *prof_tdata)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
if (prof_tdata == NULL)
|
|
|
|
prof_tdata = prof_tdata_get(false);
|
|
|
|
|
|
|
|
if (opt_lg_prof_sample == 0) {
|
|
|
|
prof_tdata->bytes_until_sample = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute sample threshold as a geometrically distributed random
|
|
|
|
* variable with mean (2^opt_lg_prof_sample).
|
|
|
|
*
|
|
|
|
* __ __
|
|
|
|
* | log(u) | 1
|
|
|
|
* prof_tdata->threshold = | -------- |, where p = -------------------
|
|
|
|
* | log(1-p) | opt_lg_prof_sample
|
|
|
|
* 2
|
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
*/
|
|
|
|
prng64(r, 53, prof_tdata->prng_state,
|
|
|
|
UINT64_C(6364136223846793005), UINT64_C(1442695040888963407));
|
|
|
|
u = (double)r * (1.0/9007199254740992.0L);
|
|
|
|
prof_tdata->bytes_until_sample = (uint64_t)(log(u) /
|
|
|
|
log(1.0 - (1.0 / (double)((uint64_t)1U << opt_lg_prof_sample))))
|
|
|
|
+ (uint64_t)1U;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-18 07:40:52 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
size_t
|
|
|
|
prof_bt_count(void)
|
|
|
|
{
|
|
|
|
size_t bt_count;
|
|
|
|
prof_tdata_t *prof_tdata;
|
|
|
|
|
|
|
|
prof_tdata = prof_tdata_get(false);
|
|
|
|
if ((uintptr_t)prof_tdata <= (uintptr_t)PROF_TDATA_STATE_MAX)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
prof_enter(prof_tdata);
|
|
|
|
bt_count = ckh_count(&bt2ctx);
|
|
|
|
prof_leave(prof_tdata);
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (fd == -1 && propagate_err == false) {
|
|
|
|
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) {
|
2010-03-03 03:57:30 +08:00
|
|
|
if (propagate_err == false) {
|
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-17 03:58:55 +08:00
|
|
|
static prof_thr_cnt_t *
|
|
|
|
ctx_sum_iter(prof_thr_cnt_tree_t *thr_cnts, prof_thr_cnt_t *thr_cnt, void *arg)
|
|
|
|
{
|
|
|
|
prof_ctx_t *ctx = (prof_ctx_t *)arg;
|
|
|
|
volatile unsigned *epoch = &thr_cnt->epoch;
|
|
|
|
prof_cnt_t tcnt;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
unsigned epoch0 = *epoch;
|
|
|
|
|
|
|
|
/* Make sure epoch is even. */
|
|
|
|
if (epoch0 & 1U)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
memcpy(&tcnt, &thr_cnt->cnts, sizeof(prof_cnt_t));
|
|
|
|
|
|
|
|
/* Terminate if epoch didn't change while reading. */
|
|
|
|
if (*epoch == epoch0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->cnt_summed.curobjs += tcnt.curobjs;
|
|
|
|
ctx->cnt_summed.curbytes += tcnt.curbytes;
|
|
|
|
if (opt_prof_accum) {
|
|
|
|
ctx->cnt_summed.accumobjs += tcnt.accumobjs;
|
|
|
|
ctx->cnt_summed.accumbytes += tcnt.accumbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
static void
|
2014-01-17 05:23:56 +08:00
|
|
|
prof_dump_ctx_prep(prof_ctx_t *ctx, prof_cnt_t *cnt_all, size_t *leak_nctx,
|
2014-08-17 03:58:55 +08:00
|
|
|
prof_ctx_tree_t *ctxs)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2012-03-24 09:05:51 +08:00
|
|
|
malloc_mutex_lock(ctx->lock);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-01-17 05:23:56 +08:00
|
|
|
/*
|
|
|
|
* Increment nlimbo so that ctx won't go away before dump.
|
|
|
|
* Additionally, link ctx into the dump list so that it is included in
|
|
|
|
* prof_dump()'s second pass.
|
|
|
|
*/
|
|
|
|
ctx->nlimbo++;
|
2014-08-17 03:58:55 +08:00
|
|
|
ctx_tree_insert(ctxs, ctx);
|
2014-01-17 05:23:56 +08:00
|
|
|
|
2010-10-03 06:18:50 +08:00
|
|
|
memcpy(&ctx->cnt_summed, &ctx->cnt_merged, sizeof(prof_cnt_t));
|
2014-08-17 03:58:55 +08:00
|
|
|
thr_cnt_tree_iter(&ctx->thr_cnts, NULL, ctx_sum_iter, (void *)ctx);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2010-10-03 13:39:59 +08:00
|
|
|
if (ctx->cnt_summed.curobjs != 0)
|
|
|
|
(*leak_nctx)++;
|
|
|
|
|
2010-10-03 06:18:50 +08:00
|
|
|
/* Add to cnt_all. */
|
|
|
|
cnt_all->curobjs += ctx->cnt_summed.curobjs;
|
|
|
|
cnt_all->curbytes += ctx->cnt_summed.curbytes;
|
|
|
|
if (opt_prof_accum) {
|
|
|
|
cnt_all->accumobjs += ctx->cnt_summed.accumobjs;
|
|
|
|
cnt_all->accumbytes += ctx->cnt_summed.accumbytes;
|
|
|
|
}
|
|
|
|
|
2012-03-24 09:05:51 +08:00
|
|
|
malloc_mutex_unlock(ctx->lock);
|
2010-10-03 06:18:50 +08:00
|
|
|
}
|
|
|
|
|
2014-01-17 05:23:56 +08:00
|
|
|
static bool
|
|
|
|
prof_dump_header(bool propagate_err, const prof_cnt_t *cnt_all)
|
2010-10-03 06:18:50 +08:00
|
|
|
{
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2014-01-17 05:23:56 +08:00
|
|
|
if (opt_lg_prof_sample == 0) {
|
|
|
|
if (prof_dump_printf(propagate_err,
|
|
|
|
"heap profile: %"PRId64": %"PRId64
|
|
|
|
" [%"PRIu64": %"PRIu64"] @ heapprofile\n",
|
|
|
|
cnt_all->curobjs, cnt_all->curbytes,
|
|
|
|
cnt_all->accumobjs, cnt_all->accumbytes))
|
|
|
|
return (true);
|
2010-10-03 06:18:50 +08:00
|
|
|
} else {
|
2014-01-17 05:23:56 +08:00
|
|
|
if (prof_dump_printf(propagate_err,
|
|
|
|
"heap profile: %"PRId64": %"PRId64
|
|
|
|
" [%"PRIu64": %"PRIu64"] @ heap_v2/%"PRIu64"\n",
|
|
|
|
cnt_all->curobjs, cnt_all->curbytes,
|
|
|
|
cnt_all->accumobjs, cnt_all->accumbytes,
|
|
|
|
((uint64_t)1U << opt_lg_prof_sample)))
|
|
|
|
return (true);
|
2010-10-03 06:18:50 +08:00
|
|
|
}
|
2014-01-17 05:23:56 +08:00
|
|
|
|
|
|
|
return (false);
|
2010-10-03 06:18:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-08-17 03:58:55 +08:00
|
|
|
prof_dump_ctx_cleanup_locked(prof_ctx_t *ctx, prof_ctx_tree_t *ctxs)
|
2010-10-03 06:18:50 +08:00
|
|
|
{
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-01-17 05:23:56 +08:00
|
|
|
ctx->nlimbo--;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-08-17 03:58:55 +08:00
|
|
|
prof_dump_ctx_cleanup(prof_ctx_t *ctx, prof_ctx_tree_t *ctxs)
|
2014-01-17 05:23:56 +08:00
|
|
|
{
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2012-03-24 09:05:51 +08:00
|
|
|
malloc_mutex_lock(ctx->lock);
|
2014-08-17 03:58:55 +08:00
|
|
|
prof_dump_ctx_cleanup_locked(ctx, ctxs);
|
2012-03-24 09:05:51 +08:00
|
|
|
malloc_mutex_unlock(ctx->lock);
|
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_ctx(bool propagate_err, prof_ctx_t *ctx, const prof_bt_t *bt,
|
2014-08-17 03:58:55 +08:00
|
|
|
prof_ctx_tree_t *ctxs)
|
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);
|
|
|
|
|
2012-04-23 07:00:11 +08:00
|
|
|
/*
|
|
|
|
* Current statistics can sum to 0 as a result of unmerged per thread
|
|
|
|
* statistics. Additionally, interval- and growth-triggered dumps can
|
|
|
|
* occur between the time a ctx is created and when its statistics are
|
|
|
|
* filled in. Avoid dumping any ctx that is an artifact of either
|
|
|
|
* implementation detail.
|
|
|
|
*/
|
2014-01-17 05:23:56 +08:00
|
|
|
malloc_mutex_lock(ctx->lock);
|
2012-04-23 07:00:11 +08:00
|
|
|
if ((opt_prof_accum == false && ctx->cnt_summed.curobjs == 0) ||
|
|
|
|
(opt_prof_accum && ctx->cnt_summed.accumobjs == 0)) {
|
|
|
|
assert(ctx->cnt_summed.curobjs == 0);
|
2010-10-03 06:18:50 +08:00
|
|
|
assert(ctx->cnt_summed.curbytes == 0);
|
|
|
|
assert(ctx->cnt_summed.accumobjs == 0);
|
|
|
|
assert(ctx->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-01-17 05:23:56 +08:00
|
|
|
if (prof_dump_printf(propagate_err, "%"PRId64": %"PRId64
|
2012-03-07 06:57:45 +08:00
|
|
|
" [%"PRIu64": %"PRIu64"] @",
|
|
|
|
ctx->cnt_summed.curobjs, ctx->cnt_summed.curbytes,
|
2014-01-17 05:23:56 +08:00
|
|
|
ctx->cnt_summed.accumobjs, ctx->cnt_summed.accumbytes)) {
|
|
|
|
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-01-17 05:23:56 +08:00
|
|
|
if (prof_dump_write(propagate_err, "\n")) {
|
|
|
|
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:
|
2014-08-17 03:58:55 +08:00
|
|
|
prof_dump_ctx_cleanup_locked(ctx, ctxs);
|
2014-01-17 05:23:56 +08:00
|
|
|
malloc_mutex_unlock(ctx->lock);
|
|
|
|
return (ret);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
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-03-22 09:33:03 +08:00
|
|
|
char filename[PATH_MAX + 1];
|
2010-02-12 01:25:56 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
2014-03-18 15:00:14 +08:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
malloc_snprintf(filename, sizeof(filename), "/proc/curproc/map");
|
|
|
|
#else
|
|
|
|
malloc_snprintf(filename, sizeof(filename), "/proc/%d/maps",
|
2012-03-22 09:33:03 +08:00
|
|
|
(int)getpid());
|
2014-03-18 15:00:14 +08:00
|
|
|
#endif
|
2012-03-22 09:33:03 +08:00
|
|
|
mfd = open(filename, O_RDONLY);
|
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
|
|
|
|
prof_leakcheck(const prof_cnt_t *cnt_all, size_t leak_nctx,
|
|
|
|
const char *filename)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (cnt_all->curbytes != 0) {
|
|
|
|
malloc_printf("<jemalloc>: Leak summary: %"PRId64" byte%s, %"
|
|
|
|
PRId64" object%s, %zu context%s\n",
|
|
|
|
cnt_all->curbytes, (cnt_all->curbytes != 1) ? "s" : "",
|
|
|
|
cnt_all->curobjs, (cnt_all->curobjs != 1) ? "s" : "",
|
|
|
|
leak_nctx, (leak_nctx != 1) ? "s" : "");
|
|
|
|
malloc_printf(
|
|
|
|
"<jemalloc>: Run pprof on \"%s\" for leak detail\n",
|
|
|
|
filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-17 03:58:55 +08:00
|
|
|
static prof_ctx_t *
|
|
|
|
prof_ctx_dump_iter(prof_ctx_tree_t *ctxs, prof_ctx_t *ctx, void *arg)
|
|
|
|
{
|
|
|
|
bool propagate_err = *(bool *)arg;
|
|
|
|
|
|
|
|
if (prof_dump_ctx(propagate_err, ctx, &ctx->bt, ctxs))
|
|
|
|
return (ctx_tree_next(ctxs, ctx));
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static prof_ctx_t *
|
|
|
|
prof_ctx_cleanup_iter(prof_ctx_tree_t *ctxs, prof_ctx_t *ctx, void *arg)
|
|
|
|
{
|
|
|
|
|
|
|
|
prof_dump_ctx_cleanup(ctx, ctxs);
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2010-03-03 03:57:30 +08:00
|
|
|
static bool
|
2012-03-07 06:57:45 +08:00
|
|
|
prof_dump(bool propagate_err, const char *filename, bool leakcheck)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2012-04-23 07:00:11 +08:00
|
|
|
prof_tdata_t *prof_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 {
|
|
|
|
prof_ctx_t *p;
|
|
|
|
void *v;
|
|
|
|
} ctx;
|
2010-02-11 02:37:56 +08:00
|
|
|
size_t leak_nctx;
|
2014-08-17 03:58:55 +08:00
|
|
|
prof_ctx_tree_t ctxs;
|
|
|
|
prof_ctx_t *cleanup_start = NULL;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2013-01-31 07:03:11 +08:00
|
|
|
prof_tdata = prof_tdata_get(false);
|
2012-04-29 14:27:13 +08:00
|
|
|
if ((uintptr_t)prof_tdata <= (uintptr_t)PROF_TDATA_STATE_MAX)
|
2012-04-23 07:00:11 +08:00
|
|
|
return (true);
|
2014-01-17 05:23:56 +08:00
|
|
|
|
|
|
|
malloc_mutex_lock(&prof_dump_mtx);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/* Merge per thread profile stats, and sum them in cnt_all. */
|
|
|
|
memset(&cnt_all, 0, sizeof(prof_cnt_t));
|
|
|
|
leak_nctx = 0;
|
2014-08-17 03:58:55 +08:00
|
|
|
ctx_tree_new(&ctxs);
|
2014-01-17 05:23:56 +08:00
|
|
|
prof_enter(prof_tdata);
|
2010-10-03 13:38:14 +08:00
|
|
|
for (tabind = 0; ckh_iter(&bt2ctx, &tabind, NULL, &ctx.v) == false;)
|
2014-08-17 03:58:55 +08:00
|
|
|
prof_dump_ctx_prep(ctx.p, &cnt_all, &leak_nctx, &ctxs);
|
2014-01-17 05:23:56 +08:00
|
|
|
prof_leave(prof_tdata);
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2013-10-29 03:41:37 +08:00
|
|
|
/* Dump per ctx profile stats. */
|
2014-08-17 03:58:55 +08:00
|
|
|
cleanup_start = ctx_tree_iter(&ctxs, NULL, prof_ctx_dump_iter,
|
|
|
|
(void *)&propagate_err);
|
|
|
|
if (cleanup_start != NULL)
|
|
|
|
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-01-17 05:23:56 +08:00
|
|
|
malloc_mutex_unlock(&prof_dump_mtx);
|
|
|
|
|
|
|
|
if (leakcheck)
|
|
|
|
prof_leakcheck(&cnt_all, leak_nctx, 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-08-17 03:58:55 +08:00
|
|
|
if (cleanup_start != NULL) {
|
|
|
|
ctx_tree_iter(&ctxs, cleanup_start, prof_ctx_cleanup_iter,
|
|
|
|
NULL);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
char filename[DUMP_FILENAME_BUFSIZE];
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
if (prof_booted == false)
|
|
|
|
return;
|
|
|
|
|
2012-04-18 07:39:33 +08:00
|
|
|
if (opt_prof_final && opt_prof_prefix[0] != '\0') {
|
2010-10-24 09:37:06 +08:00
|
|
|
malloc_mutex_lock(&prof_dump_seq_mtx);
|
2014-01-17 05:23:56 +08:00
|
|
|
prof_dump_filename(filename, 'f', VSEQ_INVALID);
|
2010-10-24 09:37:06 +08:00
|
|
|
malloc_mutex_unlock(&prof_dump_seq_mtx);
|
2012-03-07 06:57:45 +08:00
|
|
|
prof_dump(false, filename, opt_prof_leak);
|
2010-10-24 09:37:06 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prof_idump(void)
|
|
|
|
{
|
2012-04-23 07:00:11 +08:00
|
|
|
prof_tdata_t *prof_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);
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
if (prof_booted == false)
|
|
|
|
return;
|
2013-01-31 07:03:11 +08:00
|
|
|
prof_tdata = prof_tdata_get(false);
|
2012-04-29 09:14:24 +08:00
|
|
|
if ((uintptr_t)prof_tdata <= (uintptr_t)PROF_TDATA_STATE_MAX)
|
2012-04-23 07:00:11 +08:00
|
|
|
return;
|
|
|
|
if (prof_tdata->enq) {
|
|
|
|
prof_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);
|
2012-03-07 06:57:45 +08:00
|
|
|
prof_dump(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
|
|
|
{
|
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);
|
|
|
|
|
2010-03-03 03:57:30 +08:00
|
|
|
if (opt_prof == false || prof_booted == false)
|
|
|
|
return (true);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2012-03-07 06:57:45 +08:00
|
|
|
return (prof_dump(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
|
|
|
{
|
2012-04-23 07:00:11 +08:00
|
|
|
prof_tdata_t *prof_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);
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
if (prof_booted == false)
|
|
|
|
return;
|
2013-01-31 07:03:11 +08:00
|
|
|
prof_tdata = prof_tdata_get(false);
|
2012-04-29 09:14:24 +08:00
|
|
|
if ((uintptr_t)prof_tdata <= (uintptr_t)PROF_TDATA_STATE_MAX)
|
2012-04-23 07:00:11 +08:00
|
|
|
return;
|
|
|
|
if (prof_tdata->enq) {
|
|
|
|
prof_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);
|
2012-03-07 06:57:45 +08:00
|
|
|
prof_dump(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);
|
|
|
|
}
|
|
|
|
|
2010-10-21 10:05:59 +08:00
|
|
|
prof_tdata_t *
|
|
|
|
prof_tdata_init(void)
|
|
|
|
{
|
|
|
|
prof_tdata_t *prof_tdata;
|
|
|
|
|
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. */
|
|
|
|
prof_tdata = (prof_tdata_t *)imalloc(sizeof(prof_tdata_t));
|
|
|
|
if (prof_tdata == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (ckh_new(&prof_tdata->bt2cnt, PROF_CKH_MINITEMS,
|
|
|
|
prof_bt_hash, prof_bt_keycomp)) {
|
|
|
|
idalloc(prof_tdata);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2014-04-16 04:47:13 +08:00
|
|
|
prof_tdata->prng_state = (uint64_t)(uintptr_t)prof_tdata;
|
|
|
|
prof_sample_threshold_update(prof_tdata);
|
2010-10-21 10:05:59 +08:00
|
|
|
|
2012-04-23 07:00:11 +08:00
|
|
|
prof_tdata->enq = false;
|
|
|
|
prof_tdata->enq_idump = false;
|
|
|
|
prof_tdata->enq_gdump = false;
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
prof_tdata_tsd_set(&prof_tdata);
|
2010-10-21 10:05:59 +08:00
|
|
|
|
|
|
|
return (prof_tdata);
|
|
|
|
}
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
void
|
2010-10-21 10:05:59 +08:00
|
|
|
prof_tdata_cleanup(void *arg)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2012-03-22 09:33:03 +08:00
|
|
|
prof_tdata_t *prof_tdata = *(prof_tdata_t **)arg;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2012-04-29 09:14:24 +08:00
|
|
|
if (prof_tdata == PROF_TDATA_STATE_REINCARNATED) {
|
|
|
|
/*
|
|
|
|
* Another destructor deallocated memory after this destructor
|
|
|
|
* was called. Reset prof_tdata to PROF_TDATA_STATE_PURGATORY
|
|
|
|
* in order to receive another callback.
|
|
|
|
*/
|
|
|
|
prof_tdata = PROF_TDATA_STATE_PURGATORY;
|
|
|
|
prof_tdata_tsd_set(&prof_tdata);
|
|
|
|
} else if (prof_tdata == PROF_TDATA_STATE_PURGATORY) {
|
|
|
|
/*
|
|
|
|
* The previous time this destructor was called, we set the key
|
|
|
|
* to PROF_TDATA_STATE_PURGATORY so that other destructors
|
|
|
|
* wouldn't cause re-creation of the prof_tdata. This time, do
|
|
|
|
* nothing, so that the destructor will not be called again.
|
|
|
|
*/
|
|
|
|
} else if (prof_tdata != NULL) {
|
2014-08-16 06:01:15 +08:00
|
|
|
union {
|
|
|
|
prof_thr_cnt_t *p;
|
|
|
|
void *v;
|
|
|
|
} cnt;
|
|
|
|
size_t tabind;
|
|
|
|
|
2012-04-29 09:14:24 +08:00
|
|
|
/*
|
|
|
|
* Iteratively merge cnt's into the global stats and delete
|
|
|
|
* them.
|
|
|
|
*/
|
2014-08-16 06:01:15 +08:00
|
|
|
for (tabind = 0; ckh_iter(&prof_tdata->bt2cnt, &tabind, NULL,
|
|
|
|
&cnt.v);) {
|
|
|
|
prof_ctx_merge(cnt.p->ctx, cnt.p);
|
|
|
|
idalloc(cnt.v);
|
2012-04-29 09:14:24 +08:00
|
|
|
}
|
2014-08-16 06:01:15 +08:00
|
|
|
ckh_delete(&prof_tdata->bt2cnt);
|
2012-04-29 09:14:24 +08:00
|
|
|
idalloc(prof_tdata);
|
|
|
|
prof_tdata = PROF_TDATA_STATE_PURGATORY;
|
|
|
|
prof_tdata_tsd_set(&prof_tdata);
|
2011-08-09 08:10:07 +08:00
|
|
|
}
|
2010-10-03 13:38:14 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
if (opt_prof_leak && opt_prof == false) {
|
|
|
|
/*
|
|
|
|
* 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) {
|
2012-03-24 09:05:51 +08:00
|
|
|
unsigned i;
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
if (ckh_new(&bt2ctx, PROF_CKH_MINITEMS, prof_bt_hash,
|
|
|
|
prof_bt_keycomp))
|
|
|
|
return (true);
|
|
|
|
if (malloc_mutex_init(&bt2ctx_mtx))
|
|
|
|
return (true);
|
2012-03-22 09:33:03 +08:00
|
|
|
if (prof_tdata_tsd_boot()) {
|
2010-03-04 09:45:38 +08:00
|
|
|
malloc_write(
|
|
|
|
"<jemalloc>: Error in pthread_key_create()\n");
|
2010-02-11 02:37:56 +08:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (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
|
|
|
|
|
|
|
ctx_locks = (malloc_mutex_t *)base_alloc(PROF_NCTX_LOCKS *
|
|
|
|
sizeof(malloc_mutex_t));
|
|
|
|
if (ctx_locks == NULL)
|
|
|
|
return (true);
|
|
|
|
for (i = 0; i < PROF_NCTX_LOCKS; i++) {
|
|
|
|
if (malloc_mutex_init(&ctx_locks[i]))
|
|
|
|
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;
|
|
|
|
|
2013-10-22 05:59:10 +08:00
|
|
|
malloc_mutex_prefork(&bt2ctx_mtx);
|
|
|
|
malloc_mutex_prefork(&prof_dump_seq_mtx);
|
2012-10-10 05:46:22 +08:00
|
|
|
for (i = 0; i < PROF_NCTX_LOCKS; i++)
|
2013-10-22 05:59:10 +08:00
|
|
|
malloc_mutex_prefork(&ctx_locks[i]);
|
2012-10-10 05:46:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prof_postfork_parent(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (opt_prof) {
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < PROF_NCTX_LOCKS; i++)
|
|
|
|
malloc_mutex_postfork_parent(&ctx_locks[i]);
|
|
|
|
malloc_mutex_postfork_parent(&prof_dump_seq_mtx);
|
|
|
|
malloc_mutex_postfork_parent(&bt2ctx_mtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prof_postfork_child(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (opt_prof) {
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < PROF_NCTX_LOCKS; i++)
|
|
|
|
malloc_mutex_postfork_child(&ctx_locks[i]);
|
|
|
|
malloc_mutex_postfork_child(&prof_dump_seq_mtx);
|
|
|
|
malloc_mutex_postfork_child(&bt2ctx_mtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
/******************************************************************************/
|