2017-01-20 13:41:41 +08:00
|
|
|
#define JEMALLOC_PROF_C_
|
2017-04-11 09:17:55 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_preamble.h"
|
|
|
|
#include "jemalloc/internal/jemalloc_internal_includes.h"
|
|
|
|
|
2017-04-12 05:43:12 +08:00
|
|
|
#include "jemalloc/internal/assert.h"
|
2017-04-20 05:56:42 +08:00
|
|
|
#include "jemalloc/internal/ckh.h"
|
2017-04-25 08:16:36 +08:00
|
|
|
#include "jemalloc/internal/hash.h"
|
2017-04-12 04:06:31 +08:00
|
|
|
#include "jemalloc/internal/malloc_io.h"
|
2017-05-24 03:28:19 +08:00
|
|
|
#include "jemalloc/internal/mutex.h"
|
2018-07-06 01:56:33 +08:00
|
|
|
#include "jemalloc/internal/emitter.h"
|
2017-04-12 04:06:31 +08:00
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_PROF_LIBUNWIND
|
2017-01-20 13:41:41 +08:00
|
|
|
#define UNW_LOCAL_ONLY
|
2010-02-11 02:37:56 +08:00
|
|
|
#include <libunwind.h>
|
|
|
|
#endif
|
|
|
|
|
2011-03-16 13:23:12 +08:00
|
|
|
#ifdef JEMALLOC_PROF_LIBGCC
|
2017-03-29 08:30:54 +08:00
|
|
|
/*
|
|
|
|
* We have a circular dependency -- jemalloc_internal.h tells us if we should
|
|
|
|
* use libgcc's unwinding functionality, but after we've included that, we've
|
|
|
|
* already hooked _Unwind_Backtrace. We'll temporarily disable hooking.
|
|
|
|
*/
|
|
|
|
#undef _Unwind_Backtrace
|
2011-03-16 13:23:12 +08:00
|
|
|
#include <unwind.h>
|
2018-04-10 09:09:34 +08:00
|
|
|
#define _Unwind_Backtrace JEMALLOC_HOOK(_Unwind_Backtrace, test_hooks_libc_hook)
|
2011-03-16 13:23:12 +08:00
|
|
|
#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;
|
2018-07-06 01:56:33 +08:00
|
|
|
bool opt_prof_log = 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;
|
|
|
|
|
2018-07-06 01:56:33 +08:00
|
|
|
typedef enum prof_logging_state_e prof_logging_state_t;
|
|
|
|
enum prof_logging_state_e {
|
|
|
|
prof_logging_state_stopped,
|
|
|
|
prof_logging_state_started,
|
|
|
|
prof_logging_state_dumping
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* - stopped: log_start never called, or previous log_stop has completed.
|
|
|
|
* - started: log_start called, log_stop not called yet. Allocations are logged.
|
|
|
|
* - dumping: log_stop called but not finished; samples are not logged anymore.
|
|
|
|
*/
|
|
|
|
prof_logging_state_t prof_logging_state = prof_logging_state_stopped;
|
|
|
|
|
2018-07-04 02:10:09 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
static bool prof_log_dummy = false;
|
|
|
|
#endif
|
|
|
|
|
2018-07-06 01:56:33 +08:00
|
|
|
/* Incremented for every log file that is output. */
|
|
|
|
static uint64_t log_seq = 0;
|
|
|
|
static char log_filename[
|
|
|
|
/* Minimize memory bloat for non-prof builds. */
|
|
|
|
#ifdef JEMALLOC_PROF
|
|
|
|
PATH_MAX +
|
|
|
|
#endif
|
|
|
|
1];
|
|
|
|
|
|
|
|
/* Timestamp for most recent call to log_start(). */
|
|
|
|
static nstime_t log_start_timestamp = NSTIME_ZERO_INITIALIZER;
|
|
|
|
|
|
|
|
/* Increment these when adding to the log_bt and log_thr linked lists. */
|
|
|
|
static size_t log_bt_index = 0;
|
|
|
|
static size_t log_thr_index = 0;
|
|
|
|
|
|
|
|
/* Linked list node definitions. These are only used in prof.c. */
|
|
|
|
typedef struct prof_bt_node_s prof_bt_node_t;
|
|
|
|
|
|
|
|
struct prof_bt_node_s {
|
|
|
|
prof_bt_node_t *next;
|
|
|
|
size_t index;
|
|
|
|
prof_bt_t bt;
|
|
|
|
/* Variable size backtrace vector pointed to by bt. */
|
|
|
|
void *vec[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct prof_thr_node_s prof_thr_node_t;
|
|
|
|
|
|
|
|
struct prof_thr_node_s {
|
|
|
|
prof_thr_node_t *next;
|
|
|
|
size_t index;
|
|
|
|
uint64_t thr_uid;
|
|
|
|
/* Variable size based on thr_name_sz. */
|
|
|
|
char name[1];
|
2019-03-23 03:53:11 +08:00
|
|
|
};
|
2018-07-06 01:56:33 +08:00
|
|
|
|
|
|
|
typedef struct prof_alloc_node_s prof_alloc_node_t;
|
|
|
|
|
|
|
|
/* This is output when logging sampled allocations. */
|
|
|
|
struct prof_alloc_node_s {
|
|
|
|
prof_alloc_node_t *next;
|
|
|
|
/* Indices into an array of thread data. */
|
|
|
|
size_t alloc_thr_ind;
|
|
|
|
size_t free_thr_ind;
|
|
|
|
|
|
|
|
/* Indices into an array of backtraces. */
|
|
|
|
size_t alloc_bt_ind;
|
|
|
|
size_t free_bt_ind;
|
|
|
|
|
|
|
|
uint64_t alloc_time_ns;
|
|
|
|
uint64_t free_time_ns;
|
|
|
|
|
|
|
|
size_t usize;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Created on the first call to prof_log_start and deleted on prof_log_stop.
|
|
|
|
* These are the backtraces and threads that have already been logged by an
|
|
|
|
* allocation.
|
|
|
|
*/
|
|
|
|
static bool log_tables_initialized = false;
|
|
|
|
static ckh_t log_bt_node_set;
|
|
|
|
static ckh_t log_thr_node_set;
|
|
|
|
|
|
|
|
/* Store linked lists for logged data. */
|
|
|
|
static prof_bt_node_t *log_bt_first = NULL;
|
|
|
|
static prof_bt_node_t *log_bt_last = NULL;
|
|
|
|
static prof_thr_node_t *log_thr_first = NULL;
|
|
|
|
static prof_thr_node_t *log_thr_last = NULL;
|
|
|
|
static prof_alloc_node_t *log_alloc_first = NULL;
|
|
|
|
static prof_alloc_node_t *log_alloc_last = NULL;
|
|
|
|
|
|
|
|
/* Protects the prof_logging_state and any log_{...} variable. */
|
|
|
|
static malloc_mutex_t log_mtx;
|
|
|
|
|
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;
|
2017-04-05 09:36:45 +08:00
|
|
|
static atomic_u_t 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;
|
2017-03-12 12:28:31 +08:00
|
|
|
/* Non static to enable profiling. */
|
|
|
|
malloc_mutex_t bt2gctx_mtx;
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
];
|
2016-02-26 12:51:00 +08:00
|
|
|
static size_t prof_dump_buf_end;
|
2010-02-11 02:37:56 +08:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
static bool prof_tctx_should_destroy(tsdn_t *tsdn, prof_tctx_t *tctx);
|
2014-09-23 12:09:23 +08:00
|
|
|
static void prof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx);
|
2016-05-11 13:21:10 +08:00
|
|
|
static bool prof_tdata_should_destroy(tsdn_t *tsdn, prof_tdata_t *tdata,
|
2014-10-05 06:03:49 +08:00
|
|
|
bool even_if_attached);
|
2016-10-21 14:59:12 +08:00
|
|
|
static void prof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata,
|
2014-10-05 06:03:49 +08:00
|
|
|
bool even_if_attached);
|
2016-05-11 13:21:10 +08:00
|
|
|
static char *prof_thread_name_alloc(tsdn_t *tsdn, const char *thread_name);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2018-07-06 01:56:33 +08:00
|
|
|
/* Hashtable functions for log_bt_node_set and log_thr_node_set. */
|
|
|
|
static void prof_thr_node_hash(const void *key, size_t r_hash[2]);
|
|
|
|
static bool prof_thr_node_keycomp(const void *k1, const void *k2);
|
|
|
|
static void prof_bt_node_hash(const void *key, size_t r_hash[2]);
|
|
|
|
static bool prof_bt_node_keycomp(const void *k1, const void *k2);
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Red-black trees. */
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static int
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tctx_comp(const prof_tctx_t *a, const prof_tctx_t *b) {
|
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-09-10 14:16:10 +08:00
|
|
|
uint64_t a_thr_discrim = a->thr_discrim;
|
|
|
|
uint64_t b_thr_discrim = b->thr_discrim;
|
|
|
|
ret = (a_thr_discrim > b_thr_discrim) - (a_thr_discrim <
|
|
|
|
b_thr_discrim);
|
|
|
|
if (ret == 0) {
|
|
|
|
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
|
|
|
}
|
2017-01-20 10:15:45 +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
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static int
|
2017-01-16 08:56:30 +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 *));
|
2017-01-16 08:56:30 +08:00
|
|
|
if (ret == 0) {
|
2014-08-17 03:58:55 +08:00
|
|
|
ret = (a_len > b_len) - (a_len < b_len);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2014-08-17 03:58:55 +08:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static int
|
2017-01-16 08:56:30 +08:00
|
|
|
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));
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
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
|
2017-01-16 08:56:30 +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);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tdata != NULL) {
|
2015-09-18 05:47:39 +08:00
|
|
|
prof_sample_threshold_update(tdata);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-09-10 10:37:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((uintptr_t)tctx > (uintptr_t)1U) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock);
|
2014-09-10 10:37:26 +08:00
|
|
|
tctx->prepared = false;
|
2017-01-16 08:56:30 +08:00
|
|
|
if (prof_tctx_should_destroy(tsd_tsdn(tsd), tctx)) {
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_tctx_destroy(tsd, tctx);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), tctx->tdata->lock);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-09-10 10:37:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
void
|
2017-03-21 02:00:07 +08:00
|
|
|
prof_malloc_sample_object(tsdn_t *tsdn, const void *ptr, size_t usize,
|
|
|
|
prof_tctx_t *tctx) {
|
2017-04-12 09:13:10 +08:00
|
|
|
prof_tctx_set(tsdn, ptr, usize, NULL, tctx);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2018-07-06 01:56:33 +08:00
|
|
|
/* Get the current time and set this in the extent_t. We'll read this
|
|
|
|
* when free() is called. */
|
|
|
|
nstime_t t = NSTIME_ZERO_INITIALIZER;
|
|
|
|
nstime_update(&t);
|
|
|
|
prof_alloc_time_set(tsdn, ptr, NULL, t);
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, tctx->tdata->lock);
|
2014-08-19 07:22:13 +08:00
|
|
|
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;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, tctx->tdata->lock);
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
2018-07-06 01:56:33 +08:00
|
|
|
static size_t
|
|
|
|
prof_log_bt_index(tsd_t *tsd, prof_bt_t *bt) {
|
|
|
|
assert(prof_logging_state == prof_logging_state_started);
|
|
|
|
malloc_mutex_assert_owner(tsd_tsdn(tsd), &log_mtx);
|
|
|
|
|
|
|
|
prof_bt_node_t dummy_node;
|
|
|
|
dummy_node.bt = *bt;
|
|
|
|
prof_bt_node_t *node;
|
|
|
|
|
|
|
|
/* See if this backtrace is already cached in the table. */
|
|
|
|
if (ckh_search(&log_bt_node_set, (void *)(&dummy_node),
|
|
|
|
(void **)(&node), NULL)) {
|
|
|
|
size_t sz = offsetof(prof_bt_node_t, vec) +
|
|
|
|
(bt->len * sizeof(void *));
|
|
|
|
prof_bt_node_t *new_node = (prof_bt_node_t *)
|
2019-04-03 04:34:50 +08:00
|
|
|
iallocztm(tsd_tsdn(tsd), sz, sz_size2index(sz), false, NULL,
|
|
|
|
true, arena_get(TSDN_NULL, 0, true), true);
|
2018-07-06 01:56:33 +08:00
|
|
|
if (log_bt_first == NULL) {
|
|
|
|
log_bt_first = new_node;
|
|
|
|
log_bt_last = new_node;
|
|
|
|
} else {
|
|
|
|
log_bt_last->next = new_node;
|
|
|
|
log_bt_last = new_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_node->next = NULL;
|
|
|
|
new_node->index = log_bt_index;
|
2019-03-23 03:53:11 +08:00
|
|
|
/*
|
2018-07-06 01:56:33 +08:00
|
|
|
* Copy the backtrace: bt is inside a tdata or gctx, which
|
|
|
|
* might die before prof_log_stop is called.
|
|
|
|
*/
|
|
|
|
new_node->bt.len = bt->len;
|
|
|
|
memcpy(new_node->vec, bt->vec, bt->len * sizeof(void *));
|
|
|
|
new_node->bt.vec = new_node->vec;
|
|
|
|
|
|
|
|
log_bt_index++;
|
|
|
|
ckh_insert(tsd, &log_bt_node_set, (void *)new_node, NULL);
|
|
|
|
return new_node->index;
|
|
|
|
} else {
|
|
|
|
return node->index;
|
|
|
|
}
|
2019-03-23 03:53:11 +08:00
|
|
|
}
|
2018-07-06 01:56:33 +08:00
|
|
|
static size_t
|
|
|
|
prof_log_thr_index(tsd_t *tsd, uint64_t thr_uid, const char *name) {
|
|
|
|
assert(prof_logging_state == prof_logging_state_started);
|
|
|
|
malloc_mutex_assert_owner(tsd_tsdn(tsd), &log_mtx);
|
|
|
|
|
|
|
|
prof_thr_node_t dummy_node;
|
|
|
|
dummy_node.thr_uid = thr_uid;
|
|
|
|
prof_thr_node_t *node;
|
|
|
|
|
|
|
|
/* See if this thread is already cached in the table. */
|
|
|
|
if (ckh_search(&log_thr_node_set, (void *)(&dummy_node),
|
|
|
|
(void **)(&node), NULL)) {
|
|
|
|
size_t sz = offsetof(prof_thr_node_t, name) + strlen(name) + 1;
|
|
|
|
prof_thr_node_t *new_node = (prof_thr_node_t *)
|
2019-04-03 04:34:50 +08:00
|
|
|
iallocztm(tsd_tsdn(tsd), sz, sz_size2index(sz), false, NULL,
|
|
|
|
true, arena_get(TSDN_NULL, 0, true), true);
|
2018-07-06 01:56:33 +08:00
|
|
|
if (log_thr_first == NULL) {
|
|
|
|
log_thr_first = new_node;
|
|
|
|
log_thr_last = new_node;
|
|
|
|
} else {
|
|
|
|
log_thr_last->next = new_node;
|
|
|
|
log_thr_last = new_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_node->next = NULL;
|
|
|
|
new_node->index = log_thr_index;
|
|
|
|
new_node->thr_uid = thr_uid;
|
|
|
|
strcpy(new_node->name, name);
|
|
|
|
|
|
|
|
log_thr_index++;
|
|
|
|
ckh_insert(tsd, &log_thr_node_set, (void *)new_node, NULL);
|
|
|
|
return new_node->index;
|
|
|
|
} else {
|
|
|
|
return node->index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prof_try_log(tsd_t *tsd, const void *ptr, size_t usize, prof_tctx_t *tctx) {
|
|
|
|
malloc_mutex_assert_owner(tsd_tsdn(tsd), tctx->tdata->lock);
|
|
|
|
|
|
|
|
prof_tdata_t *cons_tdata = prof_tdata_get(tsd, false);
|
|
|
|
if (cons_tdata == NULL) {
|
|
|
|
/*
|
|
|
|
* We decide not to log these allocations. cons_tdata will be
|
|
|
|
* NULL only when the current thread is in a weird state (e.g.
|
|
|
|
* it's being destroyed).
|
|
|
|
*/
|
|
|
|
return;
|
2019-03-23 03:53:11 +08:00
|
|
|
}
|
2018-07-06 01:56:33 +08:00
|
|
|
|
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &log_mtx);
|
|
|
|
|
|
|
|
if (prof_logging_state != prof_logging_state_started) {
|
|
|
|
goto label_done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!log_tables_initialized) {
|
|
|
|
bool err1 = ckh_new(tsd, &log_bt_node_set, PROF_CKH_MINITEMS,
|
|
|
|
prof_bt_node_hash, prof_bt_node_keycomp);
|
|
|
|
bool err2 = ckh_new(tsd, &log_thr_node_set, PROF_CKH_MINITEMS,
|
|
|
|
prof_thr_node_hash, prof_thr_node_keycomp);
|
|
|
|
if (err1 || err2) {
|
|
|
|
goto label_done;
|
|
|
|
}
|
|
|
|
log_tables_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nstime_t alloc_time = prof_alloc_time_get(tsd_tsdn(tsd), ptr,
|
|
|
|
(alloc_ctx_t *)NULL);
|
|
|
|
nstime_t free_time = NSTIME_ZERO_INITIALIZER;
|
|
|
|
nstime_update(&free_time);
|
|
|
|
|
2019-04-03 04:34:50 +08:00
|
|
|
size_t sz = sizeof(prof_alloc_node_t);
|
2018-07-06 01:56:33 +08:00
|
|
|
prof_alloc_node_t *new_node = (prof_alloc_node_t *)
|
2019-04-03 04:34:50 +08:00
|
|
|
iallocztm(tsd_tsdn(tsd), sz, sz_size2index(sz), false, NULL, true,
|
|
|
|
arena_get(TSDN_NULL, 0, true), true);
|
|
|
|
|
2018-07-06 01:56:33 +08:00
|
|
|
const char *prod_thr_name = (tctx->tdata->thread_name == NULL)?
|
|
|
|
"" : tctx->tdata->thread_name;
|
|
|
|
const char *cons_thr_name = prof_thread_name_get(tsd);
|
|
|
|
|
|
|
|
prof_bt_t bt;
|
|
|
|
/* Initialize the backtrace, using the buffer in tdata to store it. */
|
|
|
|
bt_init(&bt, cons_tdata->vec);
|
|
|
|
prof_backtrace(&bt);
|
|
|
|
prof_bt_t *cons_bt = &bt;
|
|
|
|
|
|
|
|
/* We haven't destroyed tctx yet, so gctx should be good to read. */
|
|
|
|
prof_bt_t *prod_bt = &tctx->gctx->bt;
|
|
|
|
|
|
|
|
new_node->next = NULL;
|
|
|
|
new_node->alloc_thr_ind = prof_log_thr_index(tsd, tctx->tdata->thr_uid,
|
|
|
|
prod_thr_name);
|
|
|
|
new_node->free_thr_ind = prof_log_thr_index(tsd, cons_tdata->thr_uid,
|
|
|
|
cons_thr_name);
|
|
|
|
new_node->alloc_bt_ind = prof_log_bt_index(tsd, prod_bt);
|
|
|
|
new_node->free_bt_ind = prof_log_bt_index(tsd, cons_bt);
|
|
|
|
new_node->alloc_time_ns = nstime_ns(&alloc_time);
|
|
|
|
new_node->free_time_ns = nstime_ns(&free_time);
|
|
|
|
new_node->usize = usize;
|
|
|
|
|
|
|
|
if (log_alloc_first == NULL) {
|
|
|
|
log_alloc_first = new_node;
|
|
|
|
log_alloc_last = new_node;
|
|
|
|
} else {
|
|
|
|
log_alloc_last->next = new_node;
|
|
|
|
log_alloc_last = new_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
label_done:
|
2019-03-23 03:53:11 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &log_mtx);
|
2018-07-06 01:56:33 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
void
|
2019-03-23 03:53:11 +08:00
|
|
|
prof_free_sampled_object(tsd_t *tsd, const void *ptr, size_t usize,
|
2018-07-06 01:56:33 +08:00
|
|
|
prof_tctx_t *tctx) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock);
|
2018-07-06 01:56:33 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
assert(tctx->cnts.curobjs > 0);
|
|
|
|
assert(tctx->cnts.curbytes >= usize);
|
|
|
|
tctx->cnts.curobjs--;
|
|
|
|
tctx->cnts.curbytes -= usize;
|
|
|
|
|
2018-07-06 01:56:33 +08:00
|
|
|
prof_try_log(tsd, ptr, usize, tctx);
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (prof_tctx_should_destroy(tsd_tsdn(tsd), tctx)) {
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_tctx_destroy(tsd, tctx);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), tctx->tdata->lock);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
2014-08-17 03:58:55 +08:00
|
|
|
|
2010-10-21 10:05:59 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +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;
|
|
|
|
}
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_enter(tsd_t *tsd, prof_tdata_t *tdata) {
|
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
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &bt2gctx_mtx);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_leave(tsd_t *tsd, prof_tdata_t *tdata) {
|
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
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &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;
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (idump) {
|
2016-05-11 13:21:10 +08:00
|
|
|
prof_idump(tsd_tsdn(tsd));
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (gdump) {
|
2016-05-11 13:21:10 +08:00
|
|
|
prof_gdump(tsd_tsdn(tsd));
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-11-01 15:20:28 +08:00
|
|
|
}
|
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
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_backtrace(prof_bt_t *bt) {
|
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);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (nframes <= 0) {
|
2014-04-22 11:52:35 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_unwind_init_callback(struct _Unwind_Context *context, void *arg) {
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return _URC_NO_REASON;
|
2011-03-16 13:23:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static _Unwind_Reason_Code
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_unwind_callback(struct _Unwind_Context *context, void *arg) {
|
2011-03-16 13:23:12 +08:00
|
|
|
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);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (ip == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return _URC_END_OF_STACK;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-04-23 09:41:15 +08:00
|
|
|
data->bt->vec[data->bt->len] = ip;
|
|
|
|
data->bt->len++;
|
2017-01-16 08:56:30 +08:00
|
|
|
if (data->bt->len == data->max) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return _URC_END_OF_STACK;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2011-03-16 13:23:12 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return _URC_NO_REASON;
|
2011-03-16 13:23:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_backtrace(prof_bt_t *bt) {
|
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
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_backtrace(prof_bt_t *bt) {
|
2017-01-20 13:41:41 +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; \
|
2017-01-16 08:56:30 +08:00
|
|
|
if (__builtin_frame_address(i) == 0) { \
|
2010-02-11 10:15:53 +08:00
|
|
|
return; \
|
2017-01-16 08:56:30 +08:00
|
|
|
} \
|
2010-02-11 02:37:56 +08:00
|
|
|
p = __builtin_return_address(i); \
|
2017-01-16 08:56:30 +08:00
|
|
|
if (p == NULL) { \
|
2010-02-11 10:15:53 +08:00
|
|
|
return; \
|
2017-01-16 08:56:30 +08:00
|
|
|
} \
|
2014-04-23 09:41:15 +08:00
|
|
|
bt->vec[(i)] = p; \
|
|
|
|
bt->len = (i) + 1; \
|
2017-01-16 08:56:30 +08:00
|
|
|
} else { \
|
|
|
|
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
|
2017-01-16 08:56:30 +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 *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_gctx_mutex_choose(void) {
|
2017-04-05 09:36:45 +08:00
|
|
|
unsigned ngctxs = atomic_fetch_add_u(&cum_gctxs, 1, ATOMIC_RELAXED);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return &gctx_locks[(ngctxs - 1) % PROF_NCTX_LOCKS];
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static malloc_mutex_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tdata_mutex_choose(uint64_t thr_uid) {
|
2017-01-20 10:15:45 +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 *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_gctx_create(tsdn_t *tsdn, prof_bt_t *bt) {
|
2014-08-16 06:05:12 +08:00
|
|
|
/*
|
|
|
|
* Create a single allocation that has space for vec of length bt->len.
|
|
|
|
*/
|
2015-10-28 06:12:10 +08:00
|
|
|
size_t size = offsetof(prof_gctx_t, vec) + (bt->len * sizeof(void *));
|
2016-05-11 13:21:10 +08:00
|
|
|
prof_gctx_t *gctx = (prof_gctx_t *)iallocztm(tsdn, size,
|
2017-05-31 01:45:37 +08:00
|
|
|
sz_size2index(size), false, NULL, true, arena_get(TSDN_NULL, 0, true),
|
2016-04-23 05:34:14 +08:00
|
|
|
true);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (gctx == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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;
|
2017-01-20 10:15:45 +08:00
|
|
|
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,
|
2017-01-16 08:56:30 +08:00
|
|
|
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);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), 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. */
|
2017-01-16 08:56:30 +08:00
|
|
|
if (ckh_remove(tsd, &bt2gctx, &gctx->bt, NULL, NULL)) {
|
2014-01-17 05:23:56 +08:00
|
|
|
not_reached();
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_leave(tsd, tdata_self);
|
2014-08-19 07:22:13 +08:00
|
|
|
/* Destroy gctx. */
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), gctx, NULL, NULL, true, 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--;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), 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
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tctx_should_destroy(tsdn_t *tsdn, prof_tctx_t *tctx) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_assert_owner(tsdn, tctx->tdata->lock);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (opt_prof_accum) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (tctx->cnts.curobjs != 0) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (tctx->prepared) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_gctx_should_destroy(prof_gctx_t *gctx) {
|
|
|
|
if (opt_prof_accum) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (!tctx_tree_empty(&gctx->tctxs)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (gctx->nlimbo != 0) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx) {
|
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
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_assert_owner(tsd_tsdn(tsd), tctx->tdata->lock);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
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);
|
|
|
|
|
2016-10-21 14:59:12 +08:00
|
|
|
ckh_remove(tsd, &tdata->bt2tctx, &gctx->bt, NULL, NULL);
|
2016-05-11 13:21:10 +08:00
|
|
|
destroy_tdata = prof_tdata_should_destroy(tsd_tsdn(tsd), tdata, false);
|
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), 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;
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2014-10-07 07:35:11 +08:00
|
|
|
destroy_gctx = false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), 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
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_assert_not_owner(tsd_tsdn(tsd), tctx->tdata->lock);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (destroy_tdata) {
|
2016-10-21 14:59:12 +08:00
|
|
|
prof_tdata_destroy(tsd, tdata, false);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (destroy_tctx) {
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), tctx, NULL, NULL, true, true);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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,
|
2017-01-16 08:56:30 +08:00
|
|
|
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;
|
2017-01-30 13:51:30 +08:00
|
|
|
} gctx, tgctx;
|
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. */
|
2017-01-30 13:51:30 +08:00
|
|
|
prof_leave(tsd, tdata);
|
|
|
|
tgctx.p = prof_gctx_create(tsd_tsdn(tsd), bt);
|
|
|
|
if (tgctx.v == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2014-01-15 09:04:34 +08:00
|
|
|
}
|
2017-01-30 13:51:30 +08:00
|
|
|
prof_enter(tsd, tdata);
|
|
|
|
if (ckh_search(&bt2gctx, bt, &btkey.v, &gctx.v)) {
|
|
|
|
gctx.p = tgctx.p;
|
|
|
|
btkey.p = &gctx.p->bt;
|
|
|
|
if (ckh_insert(tsd, &bt2gctx, btkey.v, gctx.v)) {
|
|
|
|
/* OOM. */
|
|
|
|
prof_leave(tsd, tdata);
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), gctx.v, NULL, NULL,
|
|
|
|
true, true);
|
2017-01-30 13:51:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
new_gctx = true;
|
|
|
|
} else {
|
|
|
|
new_gctx = false;
|
2014-01-15 09:04:34 +08:00
|
|
|
}
|
|
|
|
} else {
|
2017-01-30 13:51:30 +08:00
|
|
|
tgctx.v = NULL;
|
|
|
|
new_gctx = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!new_gctx) {
|
2014-01-15 09:04:34 +08:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
*/
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), gctx.p->lock);
|
2014-08-19 07:22:13 +08:00
|
|
|
gctx.p->nlimbo++;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), gctx.p->lock);
|
2014-08-19 07:22:13 +08:00
|
|
|
new_gctx = false;
|
2017-01-30 13:51:30 +08:00
|
|
|
|
|
|
|
if (tgctx.v != NULL) {
|
|
|
|
/* Lost race to insert. */
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), tgctx.v, NULL, NULL, true,
|
|
|
|
true);
|
2017-01-30 13:51:30 +08:00
|
|
|
}
|
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;
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2014-01-15 09:04:34 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tctx_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_lookup(tsd_t *tsd, prof_bt_t *bt) {
|
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);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tdata == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), tdata->lock);
|
2014-08-19 07:22:13 +08:00
|
|
|
not_found = ckh_search(&tdata->bt2tctx, bt, NULL, &ret.v);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!not_found) { /* Note double negative! */
|
2014-09-10 10:37:26 +08:00
|
|
|
ret.p->prepared = true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);
|
2014-08-19 07:22:13 +08:00
|
|
|
if (not_found) {
|
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,
|
2017-01-16 08:56:30 +08:00
|
|
|
&new_gctx)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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. */
|
2016-05-11 13:21:10 +08:00
|
|
|
ret.v = iallocztm(tsd_tsdn(tsd), sizeof(prof_tctx_t),
|
2017-05-31 01:45:37 +08:00
|
|
|
sz_size2index(sizeof(prof_tctx_t)), false, NULL, true,
|
2016-10-21 14:59:12 +08:00
|
|
|
arena_ichoose(tsd, NULL), true);
|
2014-08-16 06:01:15 +08:00
|
|
|
if (ret.p == NULL) {
|
2017-01-16 08:56:30 +08:00
|
|
|
if (new_gctx) {
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_gctx_try_destroy(tsd, tdata, gctx, tdata);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +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;
|
2015-09-10 14:16:10 +08:00
|
|
|
ret.p->thr_discrim = tdata->thr_discrim;
|
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;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), tdata->lock);
|
2016-10-21 14:59:12 +08:00
|
|
|
error = ckh_insert(tsd, &tdata->bt2tctx, btkey, ret.v);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);
|
2014-08-19 07:22:13 +08:00
|
|
|
if (error) {
|
2017-01-16 08:56:30 +08:00
|
|
|
if (new_gctx) {
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_gctx_try_destroy(tsd, tdata, gctx, tdata);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), ret.v, NULL, NULL, true, true);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), 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--;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret.p;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2016-05-05 03:14:36 +08:00
|
|
|
/*
|
|
|
|
* The bodies of this function and prof_leakcheck() are 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.
|
|
|
|
*/
|
2014-04-16 04:47:13 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_sample_threshold_update(prof_tdata_t *tdata) {
|
2014-04-16 04:47:13 +08:00
|
|
|
#ifdef JEMALLOC_PROF
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!config_prof) {
|
2014-04-16 04:47:13 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-04-16 04:47:13 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
if (lg_prof_sample == 0) {
|
2018-10-10 01:59:02 +08:00
|
|
|
tsd_bytes_until_sample_set(tsd_fetch(), 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)
|
|
|
|
*/
|
2018-10-10 01:59:02 +08:00
|
|
|
uint64_t r = prng_lg_range_u64(&tdata->prng_state, 53);
|
|
|
|
double u = (double)r * (1.0/9007199254740992.0L);
|
|
|
|
uint64_t bytes_until_sample = (uint64_t)(log(u) /
|
2014-08-19 07:22:13 +08:00
|
|
|
log(1.0 - (1.0 / (double)((uint64_t)1U << lg_prof_sample))))
|
2014-04-16 04:47:13 +08:00
|
|
|
+ (uint64_t)1U;
|
2018-10-10 02:25:36 +08:00
|
|
|
if (bytes_until_sample > SSIZE_MAX) {
|
|
|
|
bytes_until_sample = SSIZE_MAX;
|
|
|
|
}
|
2018-10-10 01:59:02 +08:00
|
|
|
tsd_bytes_until_sample_set(tsd_fetch(), bytes_until_sample);
|
|
|
|
|
2014-04-16 04:47:13 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
static prof_tdata_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tdata_count_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,
|
|
|
|
void *arg) {
|
2014-10-03 14:01:10 +08:00
|
|
|
size_t *tdata_count = (size_t *)arg;
|
|
|
|
|
|
|
|
(*tdata_count)++;
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2014-10-03 14:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tdata_count(void) {
|
2014-10-03 14:01:10 +08:00
|
|
|
size_t tdata_count = 0;
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn;
|
2014-10-03 14:01:10 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn = tsdn_fetch();
|
|
|
|
malloc_mutex_lock(tsdn, &tdatas_mtx);
|
2014-10-03 14:01:10 +08:00
|
|
|
tdata_tree_iter(&tdatas, NULL, prof_tdata_count_iter,
|
|
|
|
(void *)&tdata_count);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &tdatas_mtx);
|
2014-10-03 14:01:10 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return tdata_count;
|
2014-10-03 14:01:10 +08:00
|
|
|
}
|
|
|
|
|
2014-01-18 07:40:52 +08:00
|
|
|
size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_bt_count(void) {
|
2014-01-18 07:40:52 +08:00
|
|
|
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);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tdata == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return 0;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-01-18 07:40:52 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &bt2gctx_mtx);
|
2014-08-19 07:22:13 +08:00
|
|
|
bt_count = ckh_count(&bt2gctx);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &bt2gctx_mtx);
|
2014-01-18 07:40:52 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return bt_count;
|
2014-01-18 07:40:52 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
2017-05-02 14:10:42 +08:00
|
|
|
prof_dump_open_impl(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);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (opt_abort) {
|
2014-01-18 07:40:52 +08:00
|
|
|
abort();
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-01-17 05:23:56 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return fd;
|
2014-01-17 05:23:56 +08:00
|
|
|
}
|
2017-05-02 14:10:42 +08:00
|
|
|
prof_dump_open_t *JET_MUTABLE prof_dump_open = prof_dump_open_impl;
|
2014-01-17 05:23:56 +08:00
|
|
|
|
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_dump_flush(bool propagate_err) {
|
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);
|
|
|
|
|
2018-04-07 02:40:44 +08:00
|
|
|
err = malloc_write_fd(prof_dump_fd, prof_dump_buf, prof_dump_buf_end);
|
2010-02-11 02:37:56 +08:00
|
|
|
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");
|
2017-01-16 08:56:30 +08:00
|
|
|
if (opt_abort) {
|
2010-03-03 03:57:30 +08:00
|
|
|
abort();
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-03-03 03:57:30 +08:00
|
|
|
}
|
|
|
|
ret = true;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
prof_dump_buf_end = 0;
|
2010-03-03 03:57:30 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2010-03-03 03:57:30 +08:00
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_dump_close(bool propagate_err) {
|
2014-01-17 05:23:56 +08:00
|
|
|
bool ret;
|
|
|
|
|
|
|
|
assert(prof_dump_fd != -1);
|
|
|
|
ret = prof_dump_flush(propagate_err);
|
|
|
|
close(prof_dump_fd);
|
|
|
|
prof_dump_fd = -1;
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2014-01-17 05:23:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_dump_write(bool propagate_err, const char *s) {
|
2016-02-25 05:16:51 +08:00
|
|
|
size_t i, slen, n;
|
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
|
|
|
i = 0;
|
|
|
|
slen = strlen(s);
|
|
|
|
while (i < slen) {
|
|
|
|
/* Flush the buffer if it is full. */
|
2017-01-16 08:56:30 +08:00
|
|
|
if (prof_dump_buf_end == PROF_DUMP_BUFSIZE) {
|
|
|
|
if (prof_dump_flush(propagate_err) && propagate_err) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2015-07-23 06:44:47 +08:00
|
|
|
JEMALLOC_FORMAT_PRINTF(2, 3)
|
2012-03-07 06:57:45 +08:00
|
|
|
static bool
|
2017-01-16 08:56:30 +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
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2012-03-07 06:57:45 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tctx_merge_tdata(tsdn_t *tsdn, prof_tctx_t *tctx, prof_tdata_t *tdata) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_assert_owner(tsdn, tctx->tdata->lock);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, tctx->gctx->lock);
|
2015-03-15 05:01:35 +08:00
|
|
|
|
|
|
|
switch (tctx->state) {
|
|
|
|
case prof_tctx_state_initializing:
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, tctx->gctx->lock);
|
2014-09-25 13:14:21 +08:00
|
|
|
return;
|
2015-03-15 05:01:35 +08:00
|
|
|
case prof_tctx_state_nominal:
|
|
|
|
tctx->state = prof_tctx_state_dumping;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, 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
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tctx_merge_gctx(tsdn_t *tsdn, prof_tctx_t *tctx, prof_gctx_t *gctx) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_assert_owner(tsdn, gctx->lock);
|
2016-04-14 14:36:15 +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
|
|
|
static prof_tctx_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tctx_merge_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg) {
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn = (tsdn_t *)arg;
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_assert_owner(tsdn, tctx->gctx->lock);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
switch (tctx->state) {
|
|
|
|
case prof_tctx_state_nominal:
|
|
|
|
/* New since dumping started; ignore. */
|
|
|
|
break;
|
|
|
|
case prof_tctx_state_dumping:
|
|
|
|
case prof_tctx_state_purgatory:
|
2016-05-11 13:21:10 +08:00
|
|
|
prof_tctx_merge_gctx(tsdn, tctx, tctx->gctx);
|
2014-08-19 07:22:13 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
not_reached();
|
2014-08-17 03:58:55 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2014-08-17 03:58:55 +08:00
|
|
|
}
|
|
|
|
|
2016-04-14 14:36:15 +08:00
|
|
|
struct prof_tctx_dump_iter_arg_s {
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn;
|
2016-04-14 14:36:15 +08:00
|
|
|
bool propagate_err;
|
|
|
|
};
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
static prof_tctx_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tctx_dump_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *opaque) {
|
2016-04-14 14:36:15 +08:00
|
|
|
struct prof_tctx_dump_iter_arg_s *arg =
|
|
|
|
(struct prof_tctx_dump_iter_arg_s *)opaque;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_assert_owner(arg->tsdn, tctx->gctx->lock);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2015-09-22 09:37:18 +08:00
|
|
|
switch (tctx->state) {
|
|
|
|
case prof_tctx_state_initializing:
|
|
|
|
case prof_tctx_state_nominal:
|
|
|
|
/* Not captured by this dump. */
|
|
|
|
break;
|
|
|
|
case prof_tctx_state_dumping:
|
|
|
|
case prof_tctx_state_purgatory:
|
2016-04-14 14:36:15 +08:00
|
|
|
if (prof_dump_printf(arg->propagate_err,
|
2015-09-22 09:37:18 +08:00
|
|
|
" t%"FMTu64": %"FMTu64": %"FMTu64" [%"FMTu64": "
|
|
|
|
"%"FMTu64"]\n", tctx->thr_uid, tctx->dump_cnts.curobjs,
|
|
|
|
tctx->dump_cnts.curbytes, tctx->dump_cnts.accumobjs,
|
2017-01-16 08:56:30 +08:00
|
|
|
tctx->dump_cnts.accumbytes)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return tctx;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2015-09-22 09:37:18 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
not_reached();
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static prof_tctx_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tctx_finish_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg) {
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn = (tsdn_t *)arg;
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tctx_t *ret;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_assert_owner(tsdn, tctx->gctx->lock);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
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:
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_dump_gctx_prep(tsdn_t *tsdn, prof_gctx_t *gctx, prof_gctx_tree_t *gctxs) {
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, 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
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, gctx->lock);
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
2010-10-03 13:39:59 +08:00
|
|
|
|
2016-04-14 14:36:15 +08:00
|
|
|
struct prof_gctx_merge_iter_arg_s {
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn;
|
2016-04-14 14:36:15 +08:00
|
|
|
size_t leak_ngctx;
|
|
|
|
};
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
static prof_gctx_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_gctx_merge_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *opaque) {
|
2016-04-14 14:36:15 +08:00
|
|
|
struct prof_gctx_merge_iter_arg_s *arg =
|
|
|
|
(struct prof_gctx_merge_iter_arg_s *)opaque;
|
2010-10-03 06:18:50 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(arg->tsdn, gctx->lock);
|
2016-04-14 14:36:15 +08:00
|
|
|
tctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_merge_iter,
|
2016-05-11 13:21:10 +08:00
|
|
|
(void *)arg->tsdn);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (gctx->cnt_summed.curobjs != 0) {
|
2016-04-14 14:36:15 +08:00
|
|
|
arg->leak_ngctx++;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(arg->tsdn, gctx->lock);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2010-10-03 06:18:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-03 14:01:10 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_gctx_finish(tsd_t *tsd, prof_gctx_tree_t *gctxs) {
|
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);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), gctx->lock);
|
2014-10-03 14:01:10 +08:00
|
|
|
{
|
|
|
|
prof_tctx_t *next;
|
|
|
|
|
|
|
|
next = NULL;
|
|
|
|
do {
|
|
|
|
prof_tctx_t *to_destroy =
|
|
|
|
tctx_tree_iter(&gctx->tctxs, next,
|
2016-05-11 13:21:10 +08:00
|
|
|
prof_tctx_finish_iter,
|
|
|
|
(void *)tsd_tsdn(tsd));
|
2014-10-03 14:01:10 +08:00
|
|
|
if (to_destroy != NULL) {
|
|
|
|
next = tctx_tree_next(&gctx->tctxs,
|
|
|
|
to_destroy);
|
|
|
|
tctx_tree_remove(&gctx->tctxs,
|
|
|
|
to_destroy);
|
2017-03-17 17:45:12 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), to_destroy,
|
2017-04-08 05:12:30 +08:00
|
|
|
NULL, NULL, true, true);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2014-10-03 14:01:10 +08:00
|
|
|
next = NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-03 14:01:10 +08:00
|
|
|
} while (next != NULL);
|
|
|
|
}
|
|
|
|
gctx->nlimbo--;
|
|
|
|
if (prof_gctx_should_destroy(gctx)) {
|
|
|
|
gctx->nlimbo++;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_gctx_try_destroy(tsd, tdata, gctx, tdata);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-03 14:01:10 +08:00
|
|
|
}
|
2010-10-03 06:18:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-14 14:36:15 +08:00
|
|
|
struct prof_tdata_merge_iter_arg_s {
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn;
|
2016-04-14 14:36:15 +08:00
|
|
|
prof_cnt_t cnt_all;
|
|
|
|
};
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
static prof_tdata_t *
|
2016-04-14 14:36:15 +08:00
|
|
|
prof_tdata_merge_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,
|
2017-01-16 08:56:30 +08:00
|
|
|
void *opaque) {
|
2016-04-14 14:36:15 +08:00
|
|
|
struct prof_tdata_merge_iter_arg_s *arg =
|
|
|
|
(struct prof_tdata_merge_iter_arg_s *)opaque;
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(arg->tsdn, 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,
|
2017-01-16 08:56:30 +08:00
|
|
|
&tctx.v);) {
|
2016-05-11 13:21:10 +08:00
|
|
|
prof_tctx_merge_tdata(arg->tsdn, tctx.p, tdata);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2016-04-14 14:36:15 +08:00
|
|
|
arg->cnt_all.curobjs += tdata->cnt_summed.curobjs;
|
|
|
|
arg->cnt_all.curbytes += tdata->cnt_summed.curbytes;
|
2014-08-19 07:22:13 +08:00
|
|
|
if (opt_prof_accum) {
|
2016-04-14 14:36:15 +08:00
|
|
|
arg->cnt_all.accumobjs += tdata->cnt_summed.accumobjs;
|
|
|
|
arg->cnt_all.accumbytes += tdata->cnt_summed.accumbytes;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2014-08-19 07:22:13 +08:00
|
|
|
tdata->dumping = false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(arg->tsdn, tdata->lock);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2014-01-17 05:23:56 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
static prof_tdata_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tdata_dump_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,
|
|
|
|
void *arg) {
|
2014-08-19 07:22:13 +08:00
|
|
|
bool propagate_err = *(bool *)arg;
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!tdata->dumping) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
if (prof_dump_printf(propagate_err,
|
2015-07-24 04:56:25 +08:00
|
|
|
" t%"FMTu64": %"FMTu64": %"FMTu64" [%"FMTu64": %"FMTu64"]%s%s\n",
|
2014-08-19 07:22:13 +08:00
|
|
|
tdata->thr_uid, tdata->cnt_summed.curobjs,
|
|
|
|
tdata->cnt_summed.curbytes, tdata->cnt_summed.accumobjs,
|
|
|
|
tdata->cnt_summed.accumbytes,
|
|
|
|
(tdata->thread_name != NULL) ? " " : "",
|
2017-01-16 08:56:30 +08:00
|
|
|
(tdata->thread_name != NULL) ? tdata->thread_name : "")) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return tdata;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-05-02 14:10:42 +08:00
|
|
|
prof_dump_header_impl(tsdn_t *tsdn, bool propagate_err,
|
|
|
|
const prof_cnt_t *cnt_all) {
|
2014-08-19 07:22:13 +08:00
|
|
|
bool ret;
|
|
|
|
|
|
|
|
if (prof_dump_printf(propagate_err,
|
2015-07-24 04:56:25 +08:00
|
|
|
"heap_v2/%"FMTu64"\n"
|
|
|
|
" t*: %"FMTu64": %"FMTu64" [%"FMTu64": %"FMTu64"]\n",
|
2014-08-19 07:22:13 +08:00
|
|
|
((uint64_t)1U << lg_prof_sample), cnt_all->curobjs,
|
2017-01-16 08:56:30 +08:00
|
|
|
cnt_all->curbytes, cnt_all->accumobjs, cnt_all->accumbytes)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &tdatas_mtx);
|
2014-08-19 07:22:13 +08:00
|
|
|
ret = (tdata_tree_iter(&tdatas, NULL, prof_tdata_dump_iter,
|
|
|
|
(void *)&propagate_err) != NULL);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &tdatas_mtx);
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
2017-05-02 14:10:42 +08:00
|
|
|
prof_dump_header_t *JET_MUTABLE prof_dump_header = prof_dump_header_impl;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2010-03-03 03:57:30 +08:00
|
|
|
static bool
|
2016-05-11 13:21:10 +08:00
|
|
|
prof_dump_gctx(tsdn_t *tsdn, bool propagate_err, prof_gctx_t *gctx,
|
2017-01-16 08:56:30 +08:00
|
|
|
const prof_bt_t *bt, prof_gctx_tree_t *gctxs) {
|
2014-01-17 05:23:56 +08:00
|
|
|
bool ret;
|
2010-02-11 02:37:56 +08:00
|
|
|
unsigned i;
|
2016-04-14 14:36:15 +08:00
|
|
|
struct prof_tctx_dump_iter_arg_s prof_tctx_dump_iter_arg;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_assert_owner(tsdn, gctx->lock);
|
2012-02-11 12:22:09 +08:00
|
|
|
|
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++) {
|
2015-07-24 04:56:25 +08:00
|
|
|
if (prof_dump_printf(propagate_err, " %#"FMTxPTR,
|
2014-01-17 05:23:56 +08:00
|
|
|
(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"
|
2015-07-24 04:56:25 +08:00
|
|
|
" t*: %"FMTu64": %"FMTu64" [%"FMTu64": %"FMTu64"]\n",
|
2014-08-19 07:22:13 +08:00
|
|
|
gctx->cnt_summed.curobjs, gctx->cnt_summed.curbytes,
|
|
|
|
gctx->cnt_summed.accumobjs, gctx->cnt_summed.accumbytes)) {
|
|
|
|
ret = true;
|
|
|
|
goto label_return;
|
|
|
|
}
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
prof_tctx_dump_iter_arg.tsdn = tsdn;
|
2016-04-14 14:36:15 +08:00
|
|
|
prof_tctx_dump_iter_arg.propagate_err = propagate_err;
|
2014-08-19 07:22:13 +08:00
|
|
|
if (tctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_dump_iter,
|
2016-04-14 14:36:15 +08:00
|
|
|
(void *)&prof_tctx_dump_iter_arg) != 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:
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2016-02-21 15:46:14 +08:00
|
|
|
#ifndef _WIN32
|
2015-07-23 06:44:47 +08:00
|
|
|
JEMALLOC_FORMAT_PRINTF(1, 2)
|
2015-05-02 00:03:20 +08:00
|
|
|
static int
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_open_maps(const char *format, ...) {
|
2015-05-02 00:03:20 +08:00
|
|
|
int mfd;
|
|
|
|
va_list ap;
|
|
|
|
char filename[PATH_MAX + 1];
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
malloc_vsnprintf(filename, sizeof(filename), format, ap);
|
|
|
|
va_end(ap);
|
2017-07-20 23:02:23 +08:00
|
|
|
|
|
|
|
#if defined(O_CLOEXEC)
|
2017-05-31 05:36:55 +08:00
|
|
|
mfd = open(filename, O_RDONLY | O_CLOEXEC);
|
2017-07-20 23:02:23 +08:00
|
|
|
#else
|
|
|
|
mfd = open(filename, O_RDONLY);
|
2017-07-21 21:40:29 +08:00
|
|
|
if (mfd != -1) {
|
|
|
|
fcntl(mfd, F_SETFD, fcntl(mfd, F_GETFD) | FD_CLOEXEC);
|
|
|
|
}
|
2017-07-20 23:02:23 +08:00
|
|
|
#endif
|
2015-05-02 00:03:20 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return mfd;
|
2015-05-02 00:03:20 +08:00
|
|
|
}
|
2016-02-21 15:46:14 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_getpid(void) {
|
2016-02-21 15:46:14 +08:00
|
|
|
#ifdef _WIN32
|
2017-01-20 10:15:45 +08:00
|
|
|
return GetCurrentProcessId();
|
2016-02-21 15:46:14 +08:00
|
|
|
#else
|
2017-01-20 10:15:45 +08:00
|
|
|
return getpid();
|
2016-02-21 15:46:14 +08:00
|
|
|
#endif
|
|
|
|
}
|
2015-05-02 00:03:20 +08:00
|
|
|
|
2010-03-03 03:57:30 +08:00
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_dump_maps(bool propagate_err) {
|
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");
|
2016-01-30 21:51:16 +08:00
|
|
|
#elif defined(_WIN32)
|
|
|
|
mfd = -1; // Not implemented
|
2014-03-18 15:00:14 +08:00
|
|
|
#else
|
2015-05-02 00:03:20 +08:00
|
|
|
{
|
2016-02-21 15:46:14 +08:00
|
|
|
int pid = prof_getpid();
|
2015-05-02 00:03:20 +08:00
|
|
|
|
|
|
|
mfd = prof_open_maps("/proc/%d/task/%d/maps", pid, pid);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (mfd == -1) {
|
2015-05-02 00:03:20 +08:00
|
|
|
mfd = prof_open_maps("/proc/%d/maps", pid);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2015-05-02 00:03:20 +08:00
|
|
|
}
|
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
|
|
|
}
|
2018-04-07 02:40:44 +08:00
|
|
|
nread = malloc_read_fd(mfd,
|
|
|
|
&prof_dump_buf[prof_dump_buf_end], 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:
|
2017-01-16 08:56:30 +08:00
|
|
|
if (mfd != -1) {
|
2013-10-22 06:07:40 +08:00
|
|
|
close(mfd);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2010-02-12 01:25:56 +08:00
|
|
|
}
|
|
|
|
|
2016-05-05 03:14:36 +08:00
|
|
|
/*
|
|
|
|
* See prof_sample_threshold_update() comment for why the body of this function
|
|
|
|
* is conditionally compiled.
|
|
|
|
*/
|
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,
|
2017-01-16 08:56:30 +08:00
|
|
|
const char *filename) {
|
2016-05-05 03:14:36 +08:00
|
|
|
#ifdef JEMALLOC_PROF
|
|
|
|
/*
|
|
|
|
* Scaling is equivalent AdjustSamples() in jeprof, but the result may
|
|
|
|
* differ slightly from what jeprof reports, because here we scale the
|
|
|
|
* summary values, whereas jeprof scales each context individually and
|
|
|
|
* reports the sums of the scaled values.
|
|
|
|
*/
|
2014-01-17 05:23:56 +08:00
|
|
|
if (cnt_all->curbytes != 0) {
|
2016-05-05 03:14:36 +08:00
|
|
|
double sample_period = (double)((uint64_t)1 << lg_prof_sample);
|
|
|
|
double ratio = (((double)cnt_all->curbytes) /
|
|
|
|
(double)cnt_all->curobjs) / sample_period;
|
|
|
|
double scale_factor = 1.0 / (1.0 - exp(-ratio));
|
|
|
|
uint64_t curbytes = (uint64_t)round(((double)cnt_all->curbytes)
|
|
|
|
* scale_factor);
|
|
|
|
uint64_t curobjs = (uint64_t)round(((double)cnt_all->curobjs) *
|
|
|
|
scale_factor);
|
|
|
|
|
|
|
|
malloc_printf("<jemalloc>: Leak approximation summary: ~%"FMTu64
|
|
|
|
" byte%s, ~%"FMTu64" object%s, >= %zu context%s\n",
|
|
|
|
curbytes, (curbytes != 1) ? "s" : "", curobjs, (curobjs !=
|
|
|
|
1) ? "s" : "", 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);
|
|
|
|
}
|
2016-05-05 03:14:36 +08:00
|
|
|
#endif
|
2014-01-17 05:23:56 +08:00
|
|
|
}
|
|
|
|
|
2016-04-14 14:36:15 +08:00
|
|
|
struct prof_gctx_dump_iter_arg_s {
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn;
|
2016-04-14 14:36:15 +08:00
|
|
|
bool propagate_err;
|
|
|
|
};
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
static prof_gctx_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_gctx_dump_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *opaque) {
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_gctx_t *ret;
|
2016-04-14 14:36:15 +08:00
|
|
|
struct prof_gctx_dump_iter_arg_s *arg =
|
|
|
|
(struct prof_gctx_dump_iter_arg_s *)opaque;
|
2014-08-17 03:58:55 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(arg->tsdn, gctx->lock);
|
2014-08-17 03:58:55 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
if (prof_dump_gctx(arg->tsdn, arg->propagate_err, gctx, &gctx->bt,
|
2016-04-14 14:36:15 +08:00
|
|
|
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:
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(arg->tsdn, gctx->lock);
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2014-08-17 03:58:55 +08:00
|
|
|
}
|
|
|
|
|
2017-01-17 03:09:24 +08:00
|
|
|
static void
|
|
|
|
prof_dump_prep(tsd_t *tsd, prof_tdata_t *tdata,
|
|
|
|
struct prof_tdata_merge_iter_arg_s *prof_tdata_merge_iter_arg,
|
|
|
|
struct prof_gctx_merge_iter_arg_s *prof_gctx_merge_iter_arg,
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_gctx_tree_t *gctxs) {
|
2010-02-11 02:37:56 +08:00
|
|
|
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;
|
2012-02-11 12:22:09 +08:00
|
|
|
|
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.
|
|
|
|
*/
|
2017-01-17 03:09:24 +08:00
|
|
|
gctx_tree_new(gctxs);
|
|
|
|
for (tabind = 0; !ckh_iter(&bt2gctx, &tabind, NULL, &gctx.v);) {
|
|
|
|
prof_dump_gctx_prep(tsd_tsdn(tsd), gctx.p, gctxs);
|
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate over tdatas, and for the non-expired ones snapshot their tctx
|
|
|
|
* stats and merge them into the associated gctx's.
|
|
|
|
*/
|
2017-01-17 03:09:24 +08:00
|
|
|
prof_tdata_merge_iter_arg->tsdn = tsd_tsdn(tsd);
|
|
|
|
memset(&prof_tdata_merge_iter_arg->cnt_all, 0, sizeof(prof_cnt_t));
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);
|
2016-04-14 14:36:15 +08:00
|
|
|
tdata_tree_iter(&tdatas, NULL, prof_tdata_merge_iter,
|
2017-01-17 03:09:24 +08:00
|
|
|
(void *)prof_tdata_merge_iter_arg);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
/* Merge tctx stats into gctx's. */
|
2017-01-17 03:09:24 +08:00
|
|
|
prof_gctx_merge_iter_arg->tsdn = tsd_tsdn(tsd);
|
|
|
|
prof_gctx_merge_iter_arg->leak_ngctx = 0;
|
|
|
|
gctx_tree_iter(gctxs, NULL, prof_gctx_merge_iter,
|
|
|
|
(void *)prof_gctx_merge_iter_arg);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2014-10-31 07:50:33 +08:00
|
|
|
prof_leave(tsd, tdata);
|
2017-01-17 03:09:24 +08:00
|
|
|
}
|
2014-01-17 05:23:56 +08:00
|
|
|
|
2017-01-17 03:09:24 +08:00
|
|
|
static bool
|
|
|
|
prof_dump_file(tsd_t *tsd, bool propagate_err, const char *filename,
|
|
|
|
bool leakcheck, prof_tdata_t *tdata,
|
|
|
|
struct prof_tdata_merge_iter_arg_s *prof_tdata_merge_iter_arg,
|
|
|
|
struct prof_gctx_merge_iter_arg_s *prof_gctx_merge_iter_arg,
|
|
|
|
struct prof_gctx_dump_iter_arg_s *prof_gctx_dump_iter_arg,
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_gctx_tree_t *gctxs) {
|
2014-01-17 05:23:56 +08:00
|
|
|
/* Create dump file. */
|
2017-01-17 03:09:24 +08:00
|
|
|
if ((prof_dump_fd = prof_dump_open(propagate_err, filename)) == -1) {
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/* Dump profile header. */
|
2016-05-11 13:21:10 +08:00
|
|
|
if (prof_dump_header(tsd_tsdn(tsd), propagate_err,
|
2017-01-17 03:09:24 +08:00
|
|
|
&prof_tdata_merge_iter_arg->cnt_all)) {
|
2014-01-17 05:23:56 +08:00
|
|
|
goto label_write_error;
|
2017-01-17 03:09:24 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
/* Dump per gctx profile stats. */
|
2017-01-17 03:09:24 +08:00
|
|
|
prof_gctx_dump_iter_arg->tsdn = tsd_tsdn(tsd);
|
|
|
|
prof_gctx_dump_iter_arg->propagate_err = propagate_err;
|
|
|
|
if (gctx_tree_iter(gctxs, NULL, prof_gctx_dump_iter,
|
|
|
|
(void *)prof_gctx_dump_iter_arg) != NULL) {
|
2014-08-17 03:58:55 +08:00
|
|
|
goto label_write_error;
|
2017-01-17 03:09:24 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2010-02-12 01:25:56 +08:00
|
|
|
/* Dump /proc/<pid>/maps if possible. */
|
2017-01-17 03:09:24 +08:00
|
|
|
if (prof_dump_maps(propagate_err)) {
|
2014-01-17 05:23:56 +08:00
|
|
|
goto label_write_error;
|
2017-01-17 03:09:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prof_dump_close(propagate_err)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
label_write_error:
|
|
|
|
prof_dump_close(propagate_err);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_dump(tsd_t *tsd, bool propagate_err, const char *filename,
|
|
|
|
bool leakcheck) {
|
2017-01-17 03:09:24 +08:00
|
|
|
cassert(config_prof);
|
2017-04-27 09:37:44 +08:00
|
|
|
assert(tsd_reentrancy_level_get(tsd) == 0);
|
2010-02-12 01:25:56 +08:00
|
|
|
|
2017-04-25 09:14:57 +08:00
|
|
|
prof_tdata_t * tdata = prof_tdata_get(tsd, true);
|
2017-01-17 03:09:24 +08:00
|
|
|
if (tdata == NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:18:30 +08:00
|
|
|
pre_reentrancy(tsd, NULL);
|
2017-01-17 03:09:24 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_mtx);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2017-04-25 09:14:57 +08:00
|
|
|
prof_gctx_tree_t gctxs;
|
|
|
|
struct prof_tdata_merge_iter_arg_s prof_tdata_merge_iter_arg;
|
|
|
|
struct prof_gctx_merge_iter_arg_s prof_gctx_merge_iter_arg;
|
|
|
|
struct prof_gctx_dump_iter_arg_s prof_gctx_dump_iter_arg;
|
2017-01-17 03:09:24 +08:00
|
|
|
prof_dump_prep(tsd, tdata, &prof_tdata_merge_iter_arg,
|
|
|
|
&prof_gctx_merge_iter_arg, &gctxs);
|
2017-04-25 09:14:57 +08:00
|
|
|
bool err = prof_dump_file(tsd, propagate_err, filename, leakcheck, tdata,
|
2017-01-17 03:09:24 +08:00
|
|
|
&prof_tdata_merge_iter_arg, &prof_gctx_merge_iter_arg,
|
|
|
|
&prof_gctx_dump_iter_arg, &gctxs);
|
2014-10-03 14:01:10 +08:00
|
|
|
prof_gctx_finish(tsd, &gctxs);
|
2017-01-17 03:09:24 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_mtx);
|
2017-04-25 09:14:57 +08:00
|
|
|
post_reentrancy(tsd);
|
2010-03-03 03:57:30 +08:00
|
|
|
|
2017-01-17 03:09:24 +08:00
|
|
|
if (err) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-14 14:36:15 +08:00
|
|
|
if (leakcheck) {
|
|
|
|
prof_leakcheck(&prof_tdata_merge_iter_arg.cnt_all,
|
|
|
|
prof_gctx_merge_iter_arg.leak_ngctx, filename);
|
|
|
|
}
|
2017-01-17 03:09:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
void
|
|
|
|
prof_cnt_all(uint64_t *curobjs, uint64_t *curbytes, uint64_t *accumobjs,
|
2017-01-16 08:56:30 +08:00
|
|
|
uint64_t *accumbytes) {
|
2017-01-17 03:09:24 +08:00
|
|
|
tsd_t *tsd;
|
|
|
|
prof_tdata_t *tdata;
|
|
|
|
struct prof_tdata_merge_iter_arg_s prof_tdata_merge_iter_arg;
|
|
|
|
struct prof_gctx_merge_iter_arg_s prof_gctx_merge_iter_arg;
|
|
|
|
prof_gctx_tree_t gctxs;
|
|
|
|
|
|
|
|
tsd = tsd_fetch();
|
|
|
|
tdata = prof_tdata_get(tsd, false);
|
|
|
|
if (tdata == NULL) {
|
|
|
|
if (curobjs != NULL) {
|
|
|
|
*curobjs = 0;
|
|
|
|
}
|
|
|
|
if (curbytes != NULL) {
|
|
|
|
*curbytes = 0;
|
|
|
|
}
|
|
|
|
if (accumobjs != NULL) {
|
|
|
|
*accumobjs = 0;
|
|
|
|
}
|
|
|
|
if (accumbytes != NULL) {
|
|
|
|
*accumbytes = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
prof_dump_prep(tsd, tdata, &prof_tdata_merge_iter_arg,
|
|
|
|
&prof_gctx_merge_iter_arg, &gctxs);
|
2014-10-03 14:01:10 +08:00
|
|
|
prof_gctx_finish(tsd, &gctxs);
|
2017-01-17 03:09:24 +08:00
|
|
|
|
|
|
|
if (curobjs != NULL) {
|
|
|
|
*curobjs = prof_tdata_merge_iter_arg.cnt_all.curobjs;
|
|
|
|
}
|
|
|
|
if (curbytes != NULL) {
|
|
|
|
*curbytes = prof_tdata_merge_iter_arg.cnt_all.curbytes;
|
|
|
|
}
|
|
|
|
if (accumobjs != NULL) {
|
|
|
|
*accumobjs = prof_tdata_merge_iter_arg.cnt_all.accumobjs;
|
|
|
|
}
|
|
|
|
if (accumbytes != NULL) {
|
|
|
|
*accumbytes = prof_tdata_merge_iter_arg.cnt_all.accumbytes;
|
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
2017-01-17 03:09:24 +08:00
|
|
|
#endif
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2017-01-20 13:41:41 +08:00
|
|
|
#define DUMP_FILENAME_BUFSIZE (PATH_MAX + 1)
|
|
|
|
#define VSEQ_INVALID UINT64_C(0xffffffffffffffff)
|
2010-02-11 02:37:56 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_dump_filename(char *filename, char v, uint64_t vseq) {
|
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,
|
2015-07-24 04:56:25 +08:00
|
|
|
"%s.%d.%"FMTu64".%c%"FMTu64".heap",
|
2016-02-21 15:46:14 +08:00
|
|
|
opt_prof_prefix, prof_getpid(), prof_dump_seq, v, vseq);
|
2012-03-07 06:57:45 +08:00
|
|
|
} else {
|
|
|
|
/* "<prefix>.<pid>.<seq>.<v>.heap" */
|
|
|
|
malloc_snprintf(filename, DUMP_FILENAME_BUFSIZE,
|
2015-07-24 04:56:25 +08:00
|
|
|
"%s.%d.%"FMTu64".%c.heap",
|
2016-02-21 15:46:14 +08:00
|
|
|
opt_prof_prefix, prof_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
|
2017-01-16 08:56:30 +08:00
|
|
|
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
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!prof_booted) {
|
2010-02-11 02:37:56 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-05 02:12:53 +08:00
|
|
|
tsd = tsd_fetch();
|
2017-04-27 09:37:44 +08:00
|
|
|
assert(tsd_reentrancy_level_get(tsd) == 0);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx);
|
2014-10-09 08:57:19 +08:00
|
|
|
prof_dump_filename(filename, 'f', VSEQ_INVALID);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx);
|
2014-10-09 08:57:19 +08:00
|
|
|
prof_dump(tsd, false, filename, opt_prof_leak);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2017-02-13 09:03:46 +08:00
|
|
|
bool
|
|
|
|
prof_accum_init(tsdn_t *tsdn, prof_accum_t *prof_accum) {
|
|
|
|
cassert(config_prof);
|
|
|
|
|
|
|
|
#ifndef JEMALLOC_ATOMIC_U64
|
|
|
|
if (malloc_mutex_init(&prof_accum->mtx, "prof_accum",
|
2017-05-16 06:38:15 +08:00
|
|
|
WITNESS_RANK_PROF_ACCUM, malloc_mutex_rank_exclusive)) {
|
2017-02-13 09:03:46 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
prof_accum->accumbytes = 0;
|
2017-04-05 09:08:58 +08:00
|
|
|
#else
|
|
|
|
atomic_store_u64(&prof_accum->accumbytes, 0, ATOMIC_RELAXED);
|
|
|
|
#endif
|
2017-02-13 09:03:46 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_idump(tsdn_t *tsdn) {
|
2016-05-11 13:21:10 +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
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2018-04-07 04:45:37 +08:00
|
|
|
if (!prof_booted || tsdn_null(tsdn) || !prof_active_get_unlocked()) {
|
2010-02-11 02:37:56 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
tsd = tsdn_tsd(tsdn);
|
2017-04-27 09:37:44 +08:00
|
|
|
if (tsd_reentrancy_level_get(tsd) > 0) {
|
2017-04-25 09:14:57 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = prof_tdata_get(tsd, false);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tdata == NULL) {
|
2012-04-23 07:00:11 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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') {
|
2015-09-08 20:09:20 +08:00
|
|
|
char filename[PATH_MAX + 1];
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx);
|
2010-10-24 09:37:06 +08:00
|
|
|
prof_dump_filename(filename, 'i', prof_dump_iseq);
|
|
|
|
prof_dump_iseq++;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &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
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_mdump(tsd_t *tsd, const char *filename) {
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
2017-04-27 09:37:44 +08:00
|
|
|
assert(tsd_reentrancy_level_get(tsd) == 0);
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!opt_prof || !prof_booted) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-04-25 09:14:57 +08:00
|
|
|
char filename_buf[DUMP_FILENAME_BUFSIZE];
|
2010-03-03 03:57:30 +08:00
|
|
|
if (filename == NULL) {
|
|
|
|
/* No filename specified, so automatically generate one. */
|
2017-01-16 08:56:30 +08:00
|
|
|
if (opt_prof_prefix[0] == '\0') {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx);
|
2010-03-03 03:57:30 +08:00
|
|
|
prof_dump_filename(filename_buf, 'm', prof_dump_mseq);
|
|
|
|
prof_dump_mseq++;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx);
|
2010-03-03 03:57:30 +08:00
|
|
|
filename = filename_buf;
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return prof_dump(tsd, true, filename, false);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_gdump(tsdn_t *tsdn) {
|
2016-05-11 13:21:10 +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
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
cassert(config_prof);
|
|
|
|
|
2018-04-07 04:45:37 +08:00
|
|
|
if (!prof_booted || tsdn_null(tsdn) || !prof_active_get_unlocked()) {
|
2010-02-11 02:37:56 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
tsd = tsdn_tsd(tsdn);
|
2017-04-27 09:37:44 +08:00
|
|
|
if (tsd_reentrancy_level_get(tsd) > 0) {
|
2017-04-25 09:14:57 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = prof_tdata_get(tsd, false);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tdata == NULL) {
|
2012-04-23 07:00:11 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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') {
|
2015-09-08 20:09:20 +08:00
|
|
|
char filename[DUMP_FILENAME_BUFSIZE];
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &prof_dump_seq_mtx);
|
2010-10-24 09:37:06 +08:00
|
|
|
prof_dump_filename(filename, 'u', prof_dump_useq);
|
|
|
|
prof_dump_useq++;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &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
|
2017-01-16 08:56:30 +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
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_bt_keycomp(const void *k1, const void *k2) {
|
2010-02-11 02:37:56 +08:00
|
|
|
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);
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (bt1->len != bt2->len) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
return (memcmp(bt1->vec, bt2->vec, bt1->len * sizeof(void *)) == 0);
|
|
|
|
}
|
|
|
|
|
2018-07-06 01:56:33 +08:00
|
|
|
static void
|
|
|
|
prof_bt_node_hash(const void *key, size_t r_hash[2]) {
|
|
|
|
const prof_bt_node_t *bt_node = (prof_bt_node_t *)key;
|
|
|
|
prof_bt_hash((void *)(&bt_node->bt), r_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
prof_bt_node_keycomp(const void *k1, const void *k2) {
|
|
|
|
const prof_bt_node_t *bt_node1 = (prof_bt_node_t *)k1;
|
|
|
|
const prof_bt_node_t *bt_node2 = (prof_bt_node_t *)k2;
|
|
|
|
return prof_bt_keycomp((void *)(&bt_node1->bt),
|
|
|
|
(void *)(&bt_node2->bt));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prof_thr_node_hash(const void *key, size_t r_hash[2]) {
|
|
|
|
const prof_thr_node_t *thr_node = (prof_thr_node_t *)key;
|
|
|
|
hash(&thr_node->thr_uid, sizeof(uint64_t), 0x94122f35U, r_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
prof_thr_node_keycomp(const void *k1, const void *k2) {
|
|
|
|
const prof_thr_node_t *thr_node1 = (prof_thr_node_t *)k1;
|
|
|
|
const prof_thr_node_t *thr_node2 = (prof_thr_node_t *)k2;
|
|
|
|
return thr_node1->thr_uid == thr_node2->thr_uid;
|
|
|
|
}
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static uint64_t
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_thr_uid_alloc(tsdn_t *tsdn) {
|
2014-09-12 09:06:30 +08:00
|
|
|
uint64_t thr_uid;
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &next_thr_uid_mtx);
|
2014-09-12 09:06:30 +08:00
|
|
|
thr_uid = next_thr_uid;
|
|
|
|
next_thr_uid++;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &next_thr_uid_mtx);
|
2014-09-12 09:06:30 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return thr_uid;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static prof_tdata_t *
|
2016-10-21 14:59:12 +08:00
|
|
|
prof_tdata_init_impl(tsd_t *tsd, uint64_t thr_uid, uint64_t thr_discrim,
|
2017-01-16 08:56:30 +08:00
|
|
|
char *thread_name, bool active) {
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tdata_t *tdata;
|
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. */
|
2016-10-21 14:59:12 +08:00
|
|
|
tdata = (prof_tdata_t *)iallocztm(tsd_tsdn(tsd), sizeof(prof_tdata_t),
|
2017-05-31 01:45:37 +08:00
|
|
|
sz_size2index(sizeof(prof_tdata_t)), false, NULL, true,
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_get(TSDN_NULL, 0, true), true);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tdata == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-10-21 10:05:59 +08:00
|
|
|
|
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
|
|
|
|
2016-10-21 14:59:12 +08:00
|
|
|
if (ckh_new(tsd, &tdata->bt2tctx, PROF_CKH_MINITEMS, prof_bt_hash,
|
|
|
|
prof_bt_keycomp)) {
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), tdata, NULL, NULL, true, true);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2010-10-21 10:05:59 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-10-21 14:59:12 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);
|
2014-08-19 07:22:13 +08:00
|
|
|
tdata_tree_insert(&tdatas, tdata);
|
2016-10-21 14:59:12 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return tdata;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
prof_tdata_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tdata_init(tsd_t *tsd) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return prof_tdata_init_impl(tsd, prof_thr_uid_alloc(tsd_tsdn(tsd)), 0,
|
|
|
|
NULL, prof_thread_active_init_get(tsd_tsdn(tsd)));
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tdata_should_destroy_unlocked(prof_tdata_t *tdata, bool even_if_attached) {
|
|
|
|
if (tdata->attached && !even_if_attached) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (ckh_count(&tdata->bt2tctx) != 0) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
2016-04-14 14:36:15 +08:00
|
|
|
static bool
|
2016-05-11 13:21:10 +08:00
|
|
|
prof_tdata_should_destroy(tsdn_t *tsdn, prof_tdata_t *tdata,
|
2017-01-16 08:56:30 +08:00
|
|
|
bool even_if_attached) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_assert_owner(tsdn, tdata->lock);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return prof_tdata_should_destroy_unlocked(tdata, even_if_attached);
|
2016-04-14 14:36:15 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
static void
|
2016-10-21 14:59:12 +08:00
|
|
|
prof_tdata_destroy_locked(tsd_t *tsd, prof_tdata_t *tdata,
|
2017-01-16 08:56:30 +08:00
|
|
|
bool even_if_attached) {
|
2016-10-21 14:59:12 +08:00
|
|
|
malloc_mutex_assert_owner(tsd_tsdn(tsd), &tdatas_mtx);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
tdata_tree_remove(&tdatas, tdata);
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
assert(prof_tdata_should_destroy_unlocked(tdata, even_if_attached));
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-03-24 11:29:33 +08:00
|
|
|
if (tdata->thread_name != NULL) {
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), tdata->thread_name, NULL, NULL, true,
|
|
|
|
true);
|
2016-03-24 11:29:33 +08:00
|
|
|
}
|
2016-10-21 14:59:12 +08:00
|
|
|
ckh_delete(tsd, &tdata->bt2tctx);
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), tdata, NULL, NULL, true, true);
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata, bool even_if_attached) {
|
2016-10-21 14:59:12 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);
|
|
|
|
prof_tdata_destroy_locked(tsd, tdata, even_if_attached);
|
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);
|
2014-10-03 14:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tdata_detach(tsd_t *tsd, prof_tdata_t *tdata) {
|
2014-08-19 07:22:13 +08:00
|
|
|
bool destroy_tdata;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), tdata->lock);
|
2014-10-03 14:01:10 +08:00
|
|
|
if (tdata->attached) {
|
2016-05-11 13:21:10 +08:00
|
|
|
destroy_tdata = prof_tdata_should_destroy(tsd_tsdn(tsd), tdata,
|
|
|
|
true);
|
2014-10-05 06:03:49 +08:00
|
|
|
/*
|
|
|
|
* Only detach if !destroy_tdata, because detaching would allow
|
|
|
|
* another thread to win the race to destroy tdata.
|
|
|
|
*/
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!destroy_tdata) {
|
2014-10-05 06:03:49 +08:00
|
|
|
tdata->attached = false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-05 02:12:53 +08:00
|
|
|
tsd_prof_tdata_set(tsd, NULL);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2014-08-19 07:22:13 +08:00
|
|
|
destroy_tdata = false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (destroy_tdata) {
|
2016-10-21 14:59:12 +08:00
|
|
|
prof_tdata_destroy(tsd, tdata, true);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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 *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tdata_reinit(tsd_t *tsd, prof_tdata_t *tdata) {
|
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) ?
|
2016-05-11 13:21:10 +08:00
|
|
|
prof_thread_name_alloc(tsd_tsdn(tsd), tdata->thread_name) : NULL;
|
2014-10-04 14:25:30 +08:00
|
|
|
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);
|
2017-01-20 10:15:45 +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
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tdata_expire(tsdn_t *tsdn, prof_tdata_t *tdata) {
|
2014-10-03 14:01:10 +08:00
|
|
|
bool destroy_tdata;
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, tdata->lock);
|
2014-10-03 14:01:10 +08:00
|
|
|
if (!tdata->expired) {
|
|
|
|
tdata->expired = true;
|
|
|
|
destroy_tdata = tdata->attached ? false :
|
2016-05-11 13:21:10 +08:00
|
|
|
prof_tdata_should_destroy(tsdn, tdata, false);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2014-10-03 14:01:10 +08:00
|
|
|
destroy_tdata = false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, tdata->lock);
|
2014-10-03 14:01:10 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return destroy_tdata;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static prof_tdata_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tdata_reset_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,
|
|
|
|
void *arg) {
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn = (tsdn_t *)arg;
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
return (prof_tdata_expire(tsdn, tdata) ? tdata : NULL);
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_reset(tsd_t *tsd, size_t lg_sample) {
|
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));
|
|
|
|
|
2016-10-21 14:59:12 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_mtx);
|
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
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,
|
2016-10-21 14:59:12 +08:00
|
|
|
prof_tdata_reset_iter, (void *)tsd);
|
2014-10-03 14:01:10 +08:00
|
|
|
if (to_destroy != NULL) {
|
|
|
|
next = tdata_tree_next(&tdatas, to_destroy);
|
2016-10-21 14:59:12 +08:00
|
|
|
prof_tdata_destroy_locked(tsd, to_destroy, false);
|
2017-01-16 08:56:30 +08:00
|
|
|
} else {
|
2014-10-03 14:01:10 +08:00
|
|
|
next = NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-03 14:01:10 +08:00
|
|
|
} while (next != NULL);
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2016-10-21 14:59:12 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);
|
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_mtx);
|
2010-10-21 10:05:59 +08:00
|
|
|
}
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_tdata_cleanup(tsd_t *tsd) {
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_tdata_t *tdata;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!config_prof) {
|
2014-09-23 12:09:23 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = tsd_prof_tdata_get(tsd);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tdata != NULL) {
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_tdata_detach(tsd, tdata);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-10-03 13:38:14 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_active_get(tsdn_t *tsdn) {
|
2014-10-04 14:25:30 +08:00
|
|
|
bool prof_active_current;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &prof_active_mtx);
|
2014-10-04 14:25:30 +08:00
|
|
|
prof_active_current = prof_active;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &prof_active_mtx);
|
2017-01-20 10:15:45 +08:00
|
|
|
return prof_active_current;
|
2014-10-04 14:25:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_active_set(tsdn_t *tsdn, bool active) {
|
2014-10-04 14:25:30 +08:00
|
|
|
bool prof_active_old;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &prof_active_mtx);
|
2014-10-04 14:25:30 +08:00
|
|
|
prof_active_old = prof_active;
|
|
|
|
prof_active = active;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &prof_active_mtx);
|
2017-01-20 10:15:45 +08:00
|
|
|
return prof_active_old;
|
2014-10-04 14:25:30 +08:00
|
|
|
}
|
|
|
|
|
2018-07-04 02:10:09 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
size_t
|
|
|
|
prof_log_bt_count(void) {
|
|
|
|
size_t cnt = 0;
|
|
|
|
prof_bt_node_t *node = log_bt_first;
|
|
|
|
while (node != NULL) {
|
|
|
|
cnt++;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
prof_log_alloc_count(void) {
|
|
|
|
size_t cnt = 0;
|
|
|
|
prof_alloc_node_t *node = log_alloc_first;
|
|
|
|
while (node != NULL) {
|
|
|
|
cnt++;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
prof_log_thr_count(void) {
|
|
|
|
size_t cnt = 0;
|
|
|
|
prof_thr_node_t *node = log_thr_first;
|
|
|
|
while (node != NULL) {
|
|
|
|
cnt++;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
prof_log_is_logging(void) {
|
|
|
|
return prof_logging_state == prof_logging_state_started;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
prof_log_rep_check(void) {
|
|
|
|
if (prof_logging_state == prof_logging_state_stopped
|
|
|
|
&& log_tables_initialized) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (log_bt_last != NULL && log_bt_last->next != NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (log_thr_last != NULL && log_thr_last->next != NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (log_alloc_last != NULL && log_alloc_last->next != NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t bt_count = prof_log_bt_count();
|
|
|
|
size_t thr_count = prof_log_thr_count();
|
|
|
|
size_t alloc_count = prof_log_alloc_count();
|
|
|
|
|
|
|
|
|
|
|
|
if (prof_logging_state == prof_logging_state_stopped) {
|
|
|
|
if (bt_count != 0 || thr_count != 0 || alloc_count || 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prof_alloc_node_t *node = log_alloc_first;
|
|
|
|
while (node != NULL) {
|
|
|
|
if (node->alloc_bt_ind >= bt_count) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (node->free_bt_ind >= bt_count) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (node->alloc_thr_ind >= thr_count) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (node->free_thr_ind >= thr_count) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (node->alloc_time_ns > node->free_time_ns) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prof_log_dummy_set(bool new_value) {
|
|
|
|
prof_log_dummy = new_value;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-06 01:56:33 +08:00
|
|
|
bool
|
|
|
|
prof_log_start(tsdn_t *tsdn, const char *filename) {
|
|
|
|
if (!opt_prof || !prof_booted) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
size_t buf_size = PATH_MAX + 1;
|
|
|
|
|
|
|
|
malloc_mutex_lock(tsdn, &log_mtx);
|
|
|
|
|
|
|
|
if (prof_logging_state != prof_logging_state_stopped) {
|
|
|
|
ret = true;
|
|
|
|
} else if (filename == NULL) {
|
|
|
|
/* Make default name. */
|
|
|
|
malloc_snprintf(log_filename, buf_size, "%s.%d.%"FMTu64".json",
|
|
|
|
opt_prof_prefix, prof_getpid(), log_seq);
|
|
|
|
log_seq++;
|
|
|
|
prof_logging_state = prof_logging_state_started;
|
|
|
|
} else if (strlen(filename) >= buf_size) {
|
|
|
|
ret = true;
|
|
|
|
} else {
|
|
|
|
strcpy(log_filename, filename);
|
|
|
|
prof_logging_state = prof_logging_state_started;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
nstime_update(&log_start_timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
malloc_mutex_unlock(tsdn, &log_mtx);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Used as an atexit function to stop logging on exit. */
|
|
|
|
static void
|
|
|
|
prof_log_stop_final(void) {
|
|
|
|
tsd_t *tsd = tsd_fetch();
|
|
|
|
prof_log_stop(tsd_tsdn(tsd));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct prof_emitter_cb_arg_s {
|
|
|
|
int fd;
|
|
|
|
ssize_t ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
prof_emitter_write_cb(void *opaque, const char *to_write) {
|
|
|
|
struct prof_emitter_cb_arg_s *arg =
|
|
|
|
(struct prof_emitter_cb_arg_s *)opaque;
|
|
|
|
size_t bytes = strlen(to_write);
|
2018-07-04 02:10:09 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
if (prof_log_dummy) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-06 01:56:33 +08:00
|
|
|
arg->ret = write(arg->fd, (void *)to_write, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prof_log_emit_{...} goes through the appropriate linked list, emitting each
|
|
|
|
* node to the json and deallocating it.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
prof_log_emit_threads(tsd_t *tsd, emitter_t *emitter) {
|
|
|
|
emitter_json_array_kv_begin(emitter, "threads");
|
|
|
|
prof_thr_node_t *thr_node = log_thr_first;
|
|
|
|
prof_thr_node_t *thr_old_node;
|
|
|
|
while (thr_node != NULL) {
|
|
|
|
emitter_json_object_begin(emitter);
|
|
|
|
|
|
|
|
emitter_json_kv(emitter, "thr_uid", emitter_type_uint64,
|
|
|
|
&thr_node->thr_uid);
|
|
|
|
|
|
|
|
char *thr_name = thr_node->name;
|
|
|
|
|
|
|
|
emitter_json_kv(emitter, "thr_name", emitter_type_string,
|
|
|
|
&thr_name);
|
|
|
|
|
|
|
|
emitter_json_object_end(emitter);
|
|
|
|
thr_old_node = thr_node;
|
|
|
|
thr_node = thr_node->next;
|
|
|
|
idalloc(tsd, thr_old_node);
|
|
|
|
}
|
|
|
|
emitter_json_array_end(emitter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prof_log_emit_traces(tsd_t *tsd, emitter_t *emitter) {
|
|
|
|
emitter_json_array_kv_begin(emitter, "stack_traces");
|
|
|
|
prof_bt_node_t *bt_node = log_bt_first;
|
2019-03-23 03:53:11 +08:00
|
|
|
prof_bt_node_t *bt_old_node;
|
|
|
|
/*
|
2018-07-06 01:56:33 +08:00
|
|
|
* Calculate how many hex digits we need: twice number of bytes, two for
|
|
|
|
* "0x", and then one more for terminating '\0'.
|
|
|
|
*/
|
|
|
|
char buf[2 * sizeof(intptr_t) + 3];
|
|
|
|
size_t buf_sz = sizeof(buf);
|
|
|
|
while (bt_node != NULL) {
|
|
|
|
emitter_json_array_begin(emitter);
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < bt_node->bt.len; i++) {
|
|
|
|
malloc_snprintf(buf, buf_sz, "%p", bt_node->bt.vec[i]);
|
|
|
|
char *trace_str = buf;
|
|
|
|
emitter_json_value(emitter, emitter_type_string,
|
|
|
|
&trace_str);
|
|
|
|
}
|
|
|
|
emitter_json_array_end(emitter);
|
|
|
|
|
|
|
|
bt_old_node = bt_node;
|
|
|
|
bt_node = bt_node->next;
|
|
|
|
idalloc(tsd, bt_old_node);
|
|
|
|
}
|
|
|
|
emitter_json_array_end(emitter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prof_log_emit_allocs(tsd_t *tsd, emitter_t *emitter) {
|
|
|
|
emitter_json_array_kv_begin(emitter, "allocations");
|
|
|
|
prof_alloc_node_t *alloc_node = log_alloc_first;
|
|
|
|
prof_alloc_node_t *alloc_old_node;
|
|
|
|
while (alloc_node != NULL) {
|
|
|
|
emitter_json_object_begin(emitter);
|
|
|
|
|
|
|
|
emitter_json_kv(emitter, "alloc_thread", emitter_type_size,
|
|
|
|
&alloc_node->alloc_thr_ind);
|
|
|
|
|
|
|
|
emitter_json_kv(emitter, "free_thread", emitter_type_size,
|
|
|
|
&alloc_node->free_thr_ind);
|
|
|
|
|
|
|
|
emitter_json_kv(emitter, "alloc_trace", emitter_type_size,
|
|
|
|
&alloc_node->alloc_bt_ind);
|
|
|
|
|
|
|
|
emitter_json_kv(emitter, "free_trace", emitter_type_size,
|
|
|
|
&alloc_node->free_bt_ind);
|
|
|
|
|
|
|
|
emitter_json_kv(emitter, "alloc_timestamp",
|
|
|
|
emitter_type_uint64, &alloc_node->alloc_time_ns);
|
|
|
|
|
|
|
|
emitter_json_kv(emitter, "free_timestamp", emitter_type_uint64,
|
|
|
|
&alloc_node->free_time_ns);
|
|
|
|
|
|
|
|
emitter_json_kv(emitter, "usize", emitter_type_uint64,
|
|
|
|
&alloc_node->usize);
|
|
|
|
|
|
|
|
emitter_json_object_end(emitter);
|
|
|
|
|
|
|
|
alloc_old_node = alloc_node;
|
|
|
|
alloc_node = alloc_node->next;
|
|
|
|
idalloc(tsd, alloc_old_node);
|
|
|
|
}
|
|
|
|
emitter_json_array_end(emitter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prof_log_emit_metadata(emitter_t *emitter) {
|
|
|
|
emitter_json_object_kv_begin(emitter, "info");
|
|
|
|
|
|
|
|
nstime_t now = NSTIME_ZERO_INITIALIZER;
|
|
|
|
|
|
|
|
nstime_update(&now);
|
|
|
|
uint64_t ns = nstime_ns(&now) - nstime_ns(&log_start_timestamp);
|
|
|
|
emitter_json_kv(emitter, "duration", emitter_type_uint64, &ns);
|
|
|
|
|
|
|
|
char *vers = JEMALLOC_VERSION;
|
|
|
|
emitter_json_kv(emitter, "version",
|
|
|
|
emitter_type_string, &vers);
|
|
|
|
|
|
|
|
emitter_json_kv(emitter, "lg_sample_rate",
|
|
|
|
emitter_type_int, &lg_prof_sample);
|
|
|
|
|
|
|
|
int pid = prof_getpid();
|
|
|
|
emitter_json_kv(emitter, "pid", emitter_type_int, &pid);
|
|
|
|
|
|
|
|
emitter_json_object_end(emitter);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
prof_log_stop(tsdn_t *tsdn) {
|
|
|
|
if (!opt_prof || !prof_booted) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
tsd_t *tsd = tsdn_tsd(tsdn);
|
|
|
|
malloc_mutex_lock(tsdn, &log_mtx);
|
|
|
|
|
|
|
|
if (prof_logging_state != prof_logging_state_started) {
|
|
|
|
malloc_mutex_unlock(tsdn, &log_mtx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the state to dumping. We'll set it to stopped when we're done.
|
|
|
|
* Since other threads won't be able to start/stop/log when the state is
|
|
|
|
* dumping, we don't have to hold the lock during the whole method.
|
|
|
|
*/
|
|
|
|
prof_logging_state = prof_logging_state_dumping;
|
|
|
|
malloc_mutex_unlock(tsdn, &log_mtx);
|
|
|
|
|
|
|
|
|
|
|
|
emitter_t emitter;
|
|
|
|
|
|
|
|
/* Create a file. */
|
2018-07-04 02:10:09 +08:00
|
|
|
|
|
|
|
int fd;
|
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
if (prof_log_dummy) {
|
|
|
|
fd = 0;
|
|
|
|
} else {
|
|
|
|
fd = creat(log_filename, 0644);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
fd = creat(log_filename, 0644);
|
|
|
|
#endif
|
2018-07-06 01:56:33 +08:00
|
|
|
|
|
|
|
if (fd == -1) {
|
|
|
|
malloc_printf("<jemalloc>: creat() for log file \"%s\" "
|
|
|
|
" failed with %d\n", log_filename, errno);
|
|
|
|
if (opt_abort) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Emit to json. */
|
|
|
|
struct prof_emitter_cb_arg_s arg;
|
|
|
|
arg.fd = fd;
|
|
|
|
emitter_init(&emitter, emitter_output_json, &prof_emitter_write_cb,
|
|
|
|
(void *)(&arg));
|
|
|
|
|
|
|
|
emitter_json_object_begin(&emitter);
|
|
|
|
prof_log_emit_metadata(&emitter);
|
|
|
|
prof_log_emit_threads(tsd, &emitter);
|
|
|
|
prof_log_emit_traces(tsd, &emitter);
|
|
|
|
prof_log_emit_allocs(tsd, &emitter);
|
|
|
|
emitter_json_object_end(&emitter);
|
|
|
|
|
|
|
|
/* Reset global state. */
|
|
|
|
if (log_tables_initialized) {
|
|
|
|
ckh_delete(tsd, &log_bt_node_set);
|
|
|
|
ckh_delete(tsd, &log_thr_node_set);
|
|
|
|
}
|
|
|
|
log_tables_initialized = false;
|
|
|
|
log_bt_index = 0;
|
|
|
|
log_thr_index = 0;
|
|
|
|
log_bt_first = NULL;
|
|
|
|
log_bt_last = NULL;
|
|
|
|
log_thr_first = NULL;
|
|
|
|
log_thr_last = NULL;
|
|
|
|
log_alloc_first = NULL;
|
|
|
|
log_alloc_last = NULL;
|
|
|
|
|
|
|
|
malloc_mutex_lock(tsdn, &log_mtx);
|
|
|
|
prof_logging_state = prof_logging_state_stopped;
|
|
|
|
malloc_mutex_unlock(tsdn, &log_mtx);
|
|
|
|
|
2018-07-04 02:10:09 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
if (prof_log_dummy) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-06 01:56:33 +08:00
|
|
|
return close(fd);
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
const char *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_thread_name_get(tsd_t *tsd) {
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_tdata_t *tdata;
|
|
|
|
|
|
|
|
tdata = prof_tdata_get(tsd, true);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tdata == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return "";
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-04 14:25:30 +08:00
|
|
|
return (tdata->thread_name != NULL ? tdata->thread_name : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_thread_name_alloc(tsdn_t *tsdn, const char *thread_name) {
|
2014-10-04 14:25:30 +08:00
|
|
|
char *ret;
|
|
|
|
size_t size;
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (thread_name == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-04 14:25:30 +08:00
|
|
|
|
|
|
|
size = strlen(thread_name) + 1;
|
2017-01-16 08:56:30 +08:00
|
|
|
if (size == 1) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return "";
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-04 14:25:30 +08:00
|
|
|
|
2017-05-31 01:45:37 +08:00
|
|
|
ret = iallocztm(tsdn, size, sz_size2index(size), false, NULL, true,
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_get(TSDN_NULL, 0, true), true);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (ret == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-04 14:25:30 +08:00
|
|
|
memcpy(ret, thread_name, size);
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
int
|
2017-01-16 08:56:30 +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);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tdata == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return EAGAIN;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-04 14:25:30 +08:00
|
|
|
|
|
|
|
/* Validate input. */
|
2017-01-16 08:56:30 +08:00
|
|
|
if (thread_name == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return EFAULT;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-04 14:25:30 +08:00
|
|
|
for (i = 0; thread_name[i] != '\0'; i++) {
|
|
|
|
char c = thread_name[i];
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!isgraph(c) && !isblank(c)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return EFAULT;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-04 14:25:30 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
s = prof_thread_name_alloc(tsd_tsdn(tsd), thread_name);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (s == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return EAGAIN;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
if (tdata->thread_name != NULL) {
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), tdata->thread_name, NULL, NULL, true,
|
|
|
|
true);
|
2014-10-04 14:25:30 +08:00
|
|
|
tdata->thread_name = NULL;
|
|
|
|
}
|
2017-01-16 08:56:30 +08:00
|
|
|
if (strlen(s) > 0) {
|
2014-10-04 14:25:30 +08:00
|
|
|
tdata->thread_name = s;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return 0;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_thread_active_get(tsd_t *tsd) {
|
2014-09-23 12:09:23 +08:00
|
|
|
prof_tdata_t *tdata;
|
|
|
|
|
|
|
|
tdata = prof_tdata_get(tsd, true);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tdata == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return tdata->active;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_thread_active_set(tsd_t *tsd, bool active) {
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tdata_t *tdata;
|
|
|
|
|
2014-09-23 12:09:23 +08:00
|
|
|
tdata = prof_tdata_get(tsd, true);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tdata == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
tdata->active = active;
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_thread_active_init_get(tsdn_t *tsdn) {
|
2014-10-04 14:25:30 +08:00
|
|
|
bool active_init;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &prof_thread_active_init_mtx);
|
2014-10-04 14:25:30 +08:00
|
|
|
active_init = prof_thread_active_init;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &prof_thread_active_init_mtx);
|
2017-01-20 10:15:45 +08:00
|
|
|
return active_init;
|
2014-10-04 14:25:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_thread_active_init_set(tsdn_t *tsdn, bool active_init) {
|
2014-10-04 14:25:30 +08:00
|
|
|
bool active_init_old;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &prof_thread_active_init_mtx);
|
2014-10-04 14:25:30 +08:00
|
|
|
active_init_old = prof_thread_active_init;
|
|
|
|
prof_thread_active_init = active_init;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &prof_thread_active_init_mtx);
|
2017-01-20 10:15:45 +08:00
|
|
|
return active_init_old;
|
2014-10-04 14:25:30 +08:00
|
|
|
}
|
|
|
|
|
2015-01-26 13:16:57 +08:00
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_gdump_get(tsdn_t *tsdn) {
|
2015-01-26 13:16:57 +08:00
|
|
|
bool prof_gdump_current;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &prof_gdump_mtx);
|
2015-01-26 13:16:57 +08:00
|
|
|
prof_gdump_current = prof_gdump_val;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &prof_gdump_mtx);
|
2017-01-20 10:15:45 +08:00
|
|
|
return prof_gdump_current;
|
2015-01-26 13:16:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_gdump_set(tsdn_t *tsdn, bool gdump) {
|
2015-01-26 13:16:57 +08:00
|
|
|
bool prof_gdump_old;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &prof_gdump_mtx);
|
2015-01-26 13:16:57 +08:00
|
|
|
prof_gdump_old = prof_gdump_val;
|
|
|
|
prof_gdump_val = gdump;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &prof_gdump_mtx);
|
2017-01-20 10:15:45 +08:00
|
|
|
return prof_gdump_old;
|
2015-01-26 13:16:57 +08:00
|
|
|
}
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_boot0(void) {
|
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
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_boot1(void) {
|
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
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_boot2(tsd_t *tsd) {
|
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;
|
|
|
|
|
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;
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&prof_active_mtx, "prof_active",
|
2017-05-16 06:38:15 +08:00
|
|
|
WITNESS_RANK_PROF_ACTIVE, malloc_mutex_rank_exclusive)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-04 14:25:30 +08:00
|
|
|
|
2015-01-26 13:16:57 +08:00
|
|
|
prof_gdump_val = opt_prof_gdump;
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&prof_gdump_mtx, "prof_gdump",
|
2017-05-16 06:38:15 +08:00
|
|
|
WITNESS_RANK_PROF_GDUMP, malloc_mutex_rank_exclusive)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2015-01-26 13:16:57 +08:00
|
|
|
|
2014-10-04 14:25:30 +08:00
|
|
|
prof_thread_active_init = opt_prof_thread_active_init;
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&prof_thread_active_init_mtx,
|
|
|
|
"prof_thread_active_init",
|
2017-05-16 06:38:15 +08:00
|
|
|
WITNESS_RANK_PROF_THREAD_ACTIVE_INIT,
|
|
|
|
malloc_mutex_rank_exclusive)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-10-04 14:25:30 +08:00
|
|
|
|
2016-10-21 14:59:12 +08:00
|
|
|
if (ckh_new(tsd, &bt2gctx, PROF_CKH_MINITEMS, prof_bt_hash,
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_bt_keycomp)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&bt2gctx_mtx, "prof_bt2gctx",
|
2017-05-16 06:38:15 +08:00
|
|
|
WITNESS_RANK_PROF_BT2GCTX, malloc_mutex_rank_exclusive)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
tdata_tree_new(&tdatas);
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&tdatas_mtx, "prof_tdatas",
|
2017-05-16 06:38:15 +08:00
|
|
|
WITNESS_RANK_PROF_TDATAS, malloc_mutex_rank_exclusive)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
|
|
|
|
next_thr_uid = 0;
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&next_thr_uid_mtx, "prof_next_thr_uid",
|
2017-05-16 06:38:15 +08:00
|
|
|
WITNESS_RANK_PROF_NEXT_THR_UID, malloc_mutex_rank_exclusive)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&prof_dump_seq_mtx, "prof_dump_seq",
|
2017-05-16 06:38:15 +08:00
|
|
|
WITNESS_RANK_PROF_DUMP_SEQ, malloc_mutex_rank_exclusive)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&prof_dump_mtx, "prof_dump",
|
2017-05-16 06:38:15 +08:00
|
|
|
WITNESS_RANK_PROF_DUMP, malloc_mutex_rank_exclusive)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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");
|
2017-01-16 08:56:30 +08:00
|
|
|
if (opt_abort) {
|
2010-02-11 02:37:56 +08:00
|
|
|
abort();
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
2012-03-24 09:05:51 +08:00
|
|
|
|
2018-07-06 01:56:33 +08:00
|
|
|
if (opt_prof_log) {
|
|
|
|
prof_log_start(tsd_tsdn(tsd), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atexit(prof_log_stop_final) != 0) {
|
|
|
|
malloc_write("<jemalloc>: Error in atexit() "
|
|
|
|
"for logging\n");
|
|
|
|
if (opt_abort) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (malloc_mutex_init(&log_mtx, "prof_log",
|
|
|
|
WITNESS_RANK_PROF_LOG, malloc_mutex_rank_exclusive)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ckh_new(tsd, &log_bt_node_set, PROF_CKH_MINITEMS,
|
|
|
|
prof_bt_node_hash, prof_bt_node_keycomp)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ckh_new(tsd, &log_thr_node_set, PROF_CKH_MINITEMS,
|
|
|
|
prof_thr_node_hash, prof_thr_node_keycomp)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_tables_initialized = true;
|
|
|
|
|
2016-10-21 14:59:12 +08:00
|
|
|
gctx_locks = (malloc_mutex_t *)base_alloc(tsd_tsdn(tsd),
|
2016-12-23 06:39:10 +08:00
|
|
|
b0get(), PROF_NCTX_LOCKS * sizeof(malloc_mutex_t),
|
|
|
|
CACHELINE);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (gctx_locks == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-03-24 09:05:51 +08:00
|
|
|
for (i = 0; i < PROF_NCTX_LOCKS; i++) {
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&gctx_locks[i], "prof_gctx",
|
2017-05-16 06:38:15 +08:00
|
|
|
WITNESS_RANK_PROF_GCTX,
|
|
|
|
malloc_mutex_rank_exclusive)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
}
|
|
|
|
|
2016-10-21 14:59:12 +08:00
|
|
|
tdata_locks = (malloc_mutex_t *)base_alloc(tsd_tsdn(tsd),
|
2016-12-23 06:39:10 +08:00
|
|
|
b0get(), PROF_NTDATA_LOCKS * sizeof(malloc_mutex_t),
|
|
|
|
CACHELINE);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (tdata_locks == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-08-19 07:22:13 +08:00
|
|
|
for (i = 0; i < PROF_NTDATA_LOCKS; i++) {
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&tdata_locks[i], "prof_tdata",
|
2017-05-16 06:38:15 +08:00
|
|
|
WITNESS_RANK_PROF_TDATA,
|
|
|
|
malloc_mutex_rank_exclusive)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2012-03-24 09:05:51 +08:00
|
|
|
}
|
2010-02-11 10:15:53 +08:00
|
|
|
#ifdef JEMALLOC_PROF_LIBGCC
|
2019-02-20 07:58:13 +08:00
|
|
|
/*
|
|
|
|
* Cause the backtracing machinery to allocate its internal
|
|
|
|
* state before enabling profiling.
|
|
|
|
*/
|
|
|
|
_Unwind_Backtrace(prof_unwind_init_callback, NULL);
|
2010-02-11 10:15:53 +08:00
|
|
|
#endif
|
2019-02-20 07:58:13 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
prof_booted = true;
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2012-10-10 05:46:22 +08:00
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_prefork0(tsdn_t *tsdn) {
|
2017-01-30 09:35:57 +08:00
|
|
|
if (config_prof && opt_prof) {
|
2012-10-10 05:46:22 +08:00
|
|
|
unsigned i;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_prefork(tsdn, &prof_dump_mtx);
|
|
|
|
malloc_mutex_prefork(tsdn, &bt2gctx_mtx);
|
|
|
|
malloc_mutex_prefork(tsdn, &tdatas_mtx);
|
2017-01-16 08:56:30 +08:00
|
|
|
for (i = 0; i < PROF_NTDATA_LOCKS; i++) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_prefork(tsdn, &tdata_locks[i]);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
for (i = 0; i < PROF_NCTX_LOCKS; i++) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_prefork(tsdn, &gctx_locks[i]);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-04-26 14:14:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_prefork1(tsdn_t *tsdn) {
|
2017-01-30 09:35:57 +08:00
|
|
|
if (config_prof && opt_prof) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_prefork(tsdn, &prof_active_mtx);
|
|
|
|
malloc_mutex_prefork(tsdn, &prof_dump_seq_mtx);
|
|
|
|
malloc_mutex_prefork(tsdn, &prof_gdump_mtx);
|
|
|
|
malloc_mutex_prefork(tsdn, &next_thr_uid_mtx);
|
|
|
|
malloc_mutex_prefork(tsdn, &prof_thread_active_init_mtx);
|
2012-10-10 05:46:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_postfork_parent(tsdn_t *tsdn) {
|
2017-01-30 09:35:57 +08:00
|
|
|
if (config_prof && opt_prof) {
|
2012-10-10 05:46:22 +08:00
|
|
|
unsigned i;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_parent(tsdn,
|
|
|
|
&prof_thread_active_init_mtx);
|
|
|
|
malloc_mutex_postfork_parent(tsdn, &next_thr_uid_mtx);
|
|
|
|
malloc_mutex_postfork_parent(tsdn, &prof_gdump_mtx);
|
|
|
|
malloc_mutex_postfork_parent(tsdn, &prof_dump_seq_mtx);
|
|
|
|
malloc_mutex_postfork_parent(tsdn, &prof_active_mtx);
|
2017-01-16 08:56:30 +08:00
|
|
|
for (i = 0; i < PROF_NCTX_LOCKS; i++) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_parent(tsdn, &gctx_locks[i]);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
for (i = 0; i < PROF_NTDATA_LOCKS; i++) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_parent(tsdn, &tdata_locks[i]);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_parent(tsdn, &tdatas_mtx);
|
|
|
|
malloc_mutex_postfork_parent(tsdn, &bt2gctx_mtx);
|
|
|
|
malloc_mutex_postfork_parent(tsdn, &prof_dump_mtx);
|
2012-10-10 05:46:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
prof_postfork_child(tsdn_t *tsdn) {
|
2017-01-30 09:35:57 +08:00
|
|
|
if (config_prof && opt_prof) {
|
2012-10-10 05:46:22 +08:00
|
|
|
unsigned i;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_child(tsdn, &prof_thread_active_init_mtx);
|
|
|
|
malloc_mutex_postfork_child(tsdn, &next_thr_uid_mtx);
|
|
|
|
malloc_mutex_postfork_child(tsdn, &prof_gdump_mtx);
|
|
|
|
malloc_mutex_postfork_child(tsdn, &prof_dump_seq_mtx);
|
|
|
|
malloc_mutex_postfork_child(tsdn, &prof_active_mtx);
|
2017-01-16 08:56:30 +08:00
|
|
|
for (i = 0; i < PROF_NCTX_LOCKS; i++) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_child(tsdn, &gctx_locks[i]);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
for (i = 0; i < PROF_NTDATA_LOCKS; i++) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_child(tsdn, &tdata_locks[i]);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_child(tsdn, &tdatas_mtx);
|
|
|
|
malloc_mutex_postfork_child(tsdn, &bt2gctx_mtx);
|
|
|
|
malloc_mutex_postfork_child(tsdn, &prof_dump_mtx);
|
2012-10-10 05:46:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
/******************************************************************************/
|