Blame jemalloc/src/prof.c

Packit 345191
#define JEMALLOC_PROF_C_
Packit 345191
#include "jemalloc/internal/jemalloc_preamble.h"
Packit 345191
#include "jemalloc/internal/jemalloc_internal_includes.h"
Packit 345191
Packit 345191
#include "jemalloc/internal/assert.h"
Packit 345191
#include "jemalloc/internal/ckh.h"
Packit 345191
#include "jemalloc/internal/hash.h"
Packit 345191
#include "jemalloc/internal/malloc_io.h"
Packit 345191
#include "jemalloc/internal/mutex.h"
Packit 345191
#include "jemalloc/internal/emitter.h"
Packit 345191
Packit 345191
/******************************************************************************/
Packit 345191
Packit 345191
#ifdef JEMALLOC_PROF_LIBUNWIND
Packit 345191
#define UNW_LOCAL_ONLY
Packit 345191
#include <libunwind.h>
Packit 345191
#endif
Packit 345191
Packit 345191
#ifdef JEMALLOC_PROF_LIBGCC
Packit 345191
/*
Packit 345191
 * We have a circular dependency -- jemalloc_internal.h tells us if we should
Packit 345191
 * use libgcc's unwinding functionality, but after we've included that, we've
Packit 345191
 * already hooked _Unwind_Backtrace.  We'll temporarily disable hooking.
Packit 345191
 */
Packit 345191
#undef _Unwind_Backtrace
Packit 345191
#include <unwind.h>
Packit 345191
#define _Unwind_Backtrace JEMALLOC_HOOK(_Unwind_Backtrace, test_hooks_libc_hook)
Packit 345191
#endif
Packit 345191
Packit 345191
/******************************************************************************/
Packit 345191
/* Data. */
Packit 345191
Packit 345191
bool		opt_prof = false;
Packit 345191
bool		opt_prof_active = true;
Packit 345191
bool		opt_prof_thread_active_init = true;
Packit 345191
size_t		opt_lg_prof_sample = LG_PROF_SAMPLE_DEFAULT;
Packit 345191
ssize_t		opt_lg_prof_interval = LG_PROF_INTERVAL_DEFAULT;
Packit 345191
bool		opt_prof_gdump = false;
Packit 345191
bool		opt_prof_final = false;
Packit 345191
bool		opt_prof_leak = false;
Packit 345191
bool		opt_prof_accum = false;
Packit 345191
bool		opt_prof_log = false;
Packit 345191
char		opt_prof_prefix[
Packit 345191
    /* Minimize memory bloat for non-prof builds. */
Packit 345191
#ifdef JEMALLOC_PROF
Packit 345191
    PATH_MAX +
Packit 345191
#endif
Packit 345191
    1];
Packit 345191
Packit 345191
/*
Packit 345191
 * Initialized as opt_prof_active, and accessed via
Packit 345191
 * prof_active_[gs]et{_unlocked,}().
Packit 345191
 */
Packit 345191
bool			prof_active;
Packit 345191
static malloc_mutex_t	prof_active_mtx;
Packit 345191
Packit 345191
/*
Packit 345191
 * Initialized as opt_prof_thread_active_init, and accessed via
Packit 345191
 * prof_thread_active_init_[gs]et().
Packit 345191
 */
Packit 345191
static bool		prof_thread_active_init;
Packit 345191
static malloc_mutex_t	prof_thread_active_init_mtx;
Packit 345191
Packit 345191
/*
Packit 345191
 * Initialized as opt_prof_gdump, and accessed via
Packit 345191
 * prof_gdump_[gs]et{_unlocked,}().
Packit 345191
 */
Packit 345191
bool			prof_gdump_val;
Packit 345191
static malloc_mutex_t	prof_gdump_mtx;
Packit 345191
Packit 345191
uint64_t	prof_interval = 0;
Packit 345191
Packit 345191
size_t		lg_prof_sample;
Packit 345191
Packit 345191
typedef enum prof_logging_state_e prof_logging_state_t;
Packit 345191
enum prof_logging_state_e {
Packit 345191
	prof_logging_state_stopped,
Packit 345191
	prof_logging_state_started,
Packit 345191
	prof_logging_state_dumping
Packit 345191
};
Packit 345191
Packit 345191
/*
Packit 345191
 * - stopped: log_start never called, or previous log_stop has completed.
Packit 345191
 * - started: log_start called, log_stop not called yet. Allocations are logged.
Packit 345191
 * - dumping: log_stop called but not finished; samples are not logged anymore.
Packit 345191
 */
Packit 345191
prof_logging_state_t prof_logging_state = prof_logging_state_stopped;
Packit 345191
Packit 345191
#ifdef JEMALLOC_JET
Packit 345191
static bool prof_log_dummy = false;
Packit 345191
#endif
Packit 345191
Packit 345191
/* Incremented for every log file that is output. */
Packit 345191
static uint64_t log_seq = 0;
Packit 345191
static char log_filename[
Packit 345191
    /* Minimize memory bloat for non-prof builds. */
Packit 345191
#ifdef JEMALLOC_PROF
Packit 345191
    PATH_MAX +
Packit 345191
#endif
Packit 345191
    1];
Packit 345191
Packit 345191
/* Timestamp for most recent call to log_start(). */
Packit 345191
static nstime_t log_start_timestamp = NSTIME_ZERO_INITIALIZER;
Packit 345191
Packit 345191
/* Increment these when adding to the log_bt and log_thr linked lists. */
Packit 345191
static size_t log_bt_index = 0;
Packit 345191
static size_t log_thr_index = 0;
Packit 345191
Packit 345191
/* Linked list node definitions. These are only used in prof.c. */
Packit 345191
typedef struct prof_bt_node_s prof_bt_node_t;
Packit 345191
Packit 345191
struct prof_bt_node_s {
Packit 345191
	prof_bt_node_t *next;
Packit 345191
	size_t index;
Packit 345191
	prof_bt_t bt;
Packit 345191
	/* Variable size backtrace vector pointed to by bt. */
Packit 345191
	void *vec[1];
Packit 345191
};
Packit 345191
Packit 345191
typedef struct prof_thr_node_s prof_thr_node_t;
Packit 345191
Packit 345191
struct prof_thr_node_s {
Packit 345191
	prof_thr_node_t *next;
Packit 345191
	size_t index;
Packit 345191
	uint64_t thr_uid;
Packit 345191
	/* Variable size based on thr_name_sz. */
Packit 345191
	char name[1];
Packit 345191
};
Packit 345191
Packit 345191
typedef struct prof_alloc_node_s prof_alloc_node_t;
Packit 345191
Packit 345191
/* This is output when logging sampled allocations. */
Packit 345191
struct prof_alloc_node_s {
Packit 345191
	prof_alloc_node_t *next;
Packit 345191
	/* Indices into an array of thread data. */
Packit 345191
	size_t alloc_thr_ind;
Packit 345191
	size_t free_thr_ind;
Packit 345191
Packit 345191
	/* Indices into an array of backtraces. */
Packit 345191
	size_t alloc_bt_ind;
Packit 345191
	size_t free_bt_ind;
Packit 345191
Packit 345191
	uint64_t alloc_time_ns;
Packit 345191
	uint64_t free_time_ns;
Packit 345191
Packit 345191
	size_t usize;
Packit 345191
};
Packit 345191
Packit 345191
/*
Packit 345191
 * Created on the first call to prof_log_start and deleted on prof_log_stop.
Packit 345191
 * These are the backtraces and threads that have already been logged by an
Packit 345191
 * allocation.
Packit 345191
 */
Packit 345191
static bool log_tables_initialized = false;
Packit 345191
static ckh_t log_bt_node_set;
Packit 345191
static ckh_t log_thr_node_set;
Packit 345191
Packit 345191
/* Store linked lists for logged data. */
Packit 345191
static prof_bt_node_t *log_bt_first = NULL;
Packit 345191
static prof_bt_node_t *log_bt_last = NULL;
Packit 345191
static prof_thr_node_t *log_thr_first = NULL;
Packit 345191
static prof_thr_node_t *log_thr_last = NULL;
Packit 345191
static prof_alloc_node_t *log_alloc_first = NULL;
Packit 345191
static prof_alloc_node_t *log_alloc_last = NULL;
Packit 345191
Packit 345191
/* Protects the prof_logging_state and any log_{...} variable. */
Packit 345191
static malloc_mutex_t log_mtx;
Packit 345191
Packit 345191
/*
Packit 345191
 * Table of mutexes that are shared among gctx's.  These are leaf locks, so
Packit 345191
 * there is no problem with using them for more than one gctx at the same time.
Packit 345191
 * The primary motivation for this sharing though is that gctx's are ephemeral,
Packit 345191
 * and destroying mutexes causes complications for systems that allocate when
Packit 345191
 * creating/destroying mutexes.
Packit 345191
 */
Packit 345191
static malloc_mutex_t	*gctx_locks;
Packit 345191
static atomic_u_t	cum_gctxs; /* Atomic counter. */
Packit 345191
Packit 345191
/*
Packit 345191
 * Table of mutexes that are shared among tdata's.  No operations require
Packit 345191
 * holding multiple tdata locks, so there is no problem with using them for more
Packit 345191
 * than one tdata at the same time, even though a gctx lock may be acquired
Packit 345191
 * while holding a tdata lock.
Packit 345191
 */
Packit 345191
static malloc_mutex_t	*tdata_locks;
Packit 345191
Packit 345191
/*
Packit 345191
 * Global hash of (prof_bt_t *)-->(prof_gctx_t *).  This is the master data
Packit 345191
 * structure that knows about all backtraces currently captured.
Packit 345191
 */
Packit 345191
static ckh_t		bt2gctx;
Packit 345191
/* Non static to enable profiling. */
Packit 345191
malloc_mutex_t		bt2gctx_mtx;
Packit 345191
Packit 345191
/*
Packit 345191
 * Tree of all extant prof_tdata_t structures, regardless of state,
Packit 345191
 * {attached,detached,expired}.
Packit 345191
 */
Packit 345191
static prof_tdata_tree_t	tdatas;
Packit 345191
static malloc_mutex_t	tdatas_mtx;
Packit 345191
Packit 345191
static uint64_t		next_thr_uid;
Packit 345191
static malloc_mutex_t	next_thr_uid_mtx;
Packit 345191
Packit 345191
static malloc_mutex_t	prof_dump_seq_mtx;
Packit 345191
static uint64_t		prof_dump_seq;
Packit 345191
static uint64_t		prof_dump_iseq;
Packit 345191
static uint64_t		prof_dump_mseq;
Packit 345191
static uint64_t		prof_dump_useq;
Packit 345191
Packit 345191
/*
Packit 345191
 * This buffer is rather large for stack allocation, so use a single buffer for
Packit 345191
 * all profile dumps.
Packit 345191
 */
Packit 345191
static malloc_mutex_t	prof_dump_mtx;
Packit 345191
static char		prof_dump_buf[
Packit 345191
    /* Minimize memory bloat for non-prof builds. */
Packit 345191
#ifdef JEMALLOC_PROF
Packit 345191
    PROF_DUMP_BUFSIZE
Packit 345191
#else
Packit 345191
    1
Packit 345191
#endif
Packit 345191
];
Packit 345191
static size_t		prof_dump_buf_end;
Packit 345191
static int		prof_dump_fd;
Packit 345191
Packit 345191
/* Do not dump any profiles until bootstrapping is complete. */
Packit 345191
static bool		prof_booted = false;
Packit 345191
Packit 345191
/******************************************************************************/
Packit 345191
/*
Packit 345191
 * Function prototypes for static functions that are referenced prior to
Packit 345191
 * definition.
Packit 345191
 */
Packit 345191
Packit 345191
static bool	prof_tctx_should_destroy(tsdn_t *tsdn, prof_tctx_t *tctx);
Packit 345191
static void	prof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx);
Packit 345191
static bool	prof_tdata_should_destroy(tsdn_t *tsdn, prof_tdata_t *tdata,
Packit 345191
    bool even_if_attached);
Packit 345191
static void	prof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata,
Packit 345191
    bool even_if_attached);
Packit 345191
static char	*prof_thread_name_alloc(tsdn_t *tsdn, const char *thread_name);
Packit 345191
Packit 345191
/* Hashtable functions for log_bt_node_set and log_thr_node_set. */
Packit 345191
static void prof_thr_node_hash(const void *key, size_t r_hash[2]);
Packit 345191
static bool prof_thr_node_keycomp(const void *k1, const void *k2);
Packit 345191
static void prof_bt_node_hash(const void *key, size_t r_hash[2]);
Packit 345191
static bool prof_bt_node_keycomp(const void *k1, const void *k2);
Packit 345191
Packit 345191
/******************************************************************************/
Packit 345191
/* Red-black trees. */
Packit 345191
Packit 345191
static int
Packit 345191
prof_tctx_comp(const prof_tctx_t *a, const prof_tctx_t *b) {
Packit 345191
	uint64_t a_thr_uid = a->thr_uid;
Packit 345191
	uint64_t b_thr_uid = b->thr_uid;
Packit 345191
	int ret = (a_thr_uid > b_thr_uid) - (a_thr_uid < b_thr_uid);
Packit 345191
	if (ret == 0) {
Packit 345191
		uint64_t a_thr_discrim = a->thr_discrim;
Packit 345191
		uint64_t b_thr_discrim = b->thr_discrim;
Packit 345191
		ret = (a_thr_discrim > b_thr_discrim) - (a_thr_discrim <
Packit 345191
		    b_thr_discrim);
Packit 345191
		if (ret == 0) {
Packit 345191
			uint64_t a_tctx_uid = a->tctx_uid;
Packit 345191
			uint64_t b_tctx_uid = b->tctx_uid;
Packit 345191
			ret = (a_tctx_uid > b_tctx_uid) - (a_tctx_uid <
Packit 345191
			    b_tctx_uid);
Packit 345191
		}
Packit 345191
	}
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
Packit 345191
rb_gen(static UNUSED, tctx_tree_, prof_tctx_tree_t, prof_tctx_t,
Packit 345191
    tctx_link, prof_tctx_comp)
Packit 345191
Packit 345191
static int
Packit 345191
prof_gctx_comp(const prof_gctx_t *a, const prof_gctx_t *b) {
Packit 345191
	unsigned a_len = a->bt.len;
Packit 345191
	unsigned b_len = b->bt.len;
Packit 345191
	unsigned comp_len = (a_len < b_len) ? a_len : b_len;
Packit 345191
	int ret = memcmp(a->bt.vec, b->bt.vec, comp_len * sizeof(void *));
Packit 345191
	if (ret == 0) {
Packit 345191
		ret = (a_len > b_len) - (a_len < b_len);
Packit 345191
	}
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
Packit 345191
rb_gen(static UNUSED, gctx_tree_, prof_gctx_tree_t, prof_gctx_t, dump_link,
Packit 345191
    prof_gctx_comp)
Packit 345191
Packit 345191
static int
Packit 345191
prof_tdata_comp(const prof_tdata_t *a, const prof_tdata_t *b) {
Packit 345191
	int ret;
Packit 345191
	uint64_t a_uid = a->thr_uid;
Packit 345191
	uint64_t b_uid = b->thr_uid;
Packit 345191
Packit 345191
	ret = ((a_uid > b_uid) - (a_uid < b_uid));
Packit 345191
	if (ret == 0) {
Packit 345191
		uint64_t a_discrim = a->thr_discrim;
Packit 345191
		uint64_t b_discrim = b->thr_discrim;
Packit 345191
Packit 345191
		ret = ((a_discrim > b_discrim) - (a_discrim < b_discrim));
Packit 345191
	}
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
Packit 345191
rb_gen(static UNUSED, tdata_tree_, prof_tdata_tree_t, prof_tdata_t, tdata_link,
Packit 345191
    prof_tdata_comp)
Packit 345191
Packit 345191
/******************************************************************************/
Packit 345191
Packit 345191
void
Packit 345191
prof_alloc_rollback(tsd_t *tsd, prof_tctx_t *tctx, bool updated) {
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	if (updated) {
Packit 345191
		/*
Packit 345191
		 * Compute a new sample threshold.  This isn't very important in
Packit 345191
		 * practice, because this function is rarely executed, so the
Packit 345191
		 * potential for sample bias is minimal except in contrived
Packit 345191
		 * programs.
Packit 345191
		 */
Packit 345191
		tdata = prof_tdata_get(tsd, true);
Packit 345191
		if (tdata != NULL) {
Packit 345191
			prof_sample_threshold_update(tdata);
Packit 345191
		}
Packit 345191
	}
Packit 345191
Packit 345191
	if ((uintptr_t)tctx > (uintptr_t)1U) {
Packit 345191
		malloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock);
Packit 345191
		tctx->prepared = false;
Packit 345191
		if (prof_tctx_should_destroy(tsd_tsdn(tsd), tctx)) {
Packit 345191
			prof_tctx_destroy(tsd, tctx);
Packit 345191
		} else {
Packit 345191
			malloc_mutex_unlock(tsd_tsdn(tsd), tctx->tdata->lock);
Packit 345191
		}
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_malloc_sample_object(tsdn_t *tsdn, const void *ptr, size_t usize,
Packit 345191
    prof_tctx_t *tctx) {
Packit 345191
	prof_tctx_set(tsdn, ptr, usize, NULL, tctx);
Packit 345191
Packit 345191
	/* Get the current time and set this in the extent_t. We'll read this
Packit 345191
	 * when free() is called. */
Packit 345191
	nstime_t t = NSTIME_ZERO_INITIALIZER;
Packit 345191
	nstime_update(&t);
Packit 345191
	prof_alloc_time_set(tsdn, ptr, NULL, t);
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, tctx->tdata->lock);
Packit 345191
	tctx->cnts.curobjs++;
Packit 345191
	tctx->cnts.curbytes += usize;
Packit 345191
	if (opt_prof_accum) {
Packit 345191
		tctx->cnts.accumobjs++;
Packit 345191
		tctx->cnts.accumbytes += usize;
Packit 345191
	}
Packit 345191
	tctx->prepared = false;
Packit 345191
	malloc_mutex_unlock(tsdn, tctx->tdata->lock);
Packit 345191
}
Packit 345191
Packit 345191
static size_t
Packit 345191
prof_log_bt_index(tsd_t *tsd, prof_bt_t *bt) {
Packit 345191
	assert(prof_logging_state == prof_logging_state_started);
Packit 345191
	malloc_mutex_assert_owner(tsd_tsdn(tsd), &log_mtx);
Packit 345191
Packit 345191
	prof_bt_node_t dummy_node;
Packit 345191
	dummy_node.bt = *bt;
Packit 345191
	prof_bt_node_t *node;
Packit 345191
Packit 345191
	/* See if this backtrace is already cached in the table. */
Packit 345191
	if (ckh_search(&log_bt_node_set, (void *)(&dummy_node),
Packit 345191
	    (void **)(&node), NULL)) {
Packit 345191
		size_t sz = offsetof(prof_bt_node_t, vec) +
Packit 345191
			        (bt->len * sizeof(void *));
Packit 345191
		prof_bt_node_t *new_node = (prof_bt_node_t *)
Packit 345191
		    iallocztm(tsd_tsdn(tsd), sz, sz_size2index(sz), false, NULL,
Packit 345191
		    true, arena_get(TSDN_NULL, 0, true), true);
Packit 345191
		if (log_bt_first == NULL) {
Packit 345191
			log_bt_first = new_node;
Packit 345191
			log_bt_last = new_node;
Packit 345191
		} else {
Packit 345191
			log_bt_last->next = new_node;
Packit 345191
			log_bt_last = new_node;
Packit 345191
		}
Packit 345191
Packit 345191
		new_node->next = NULL;
Packit 345191
		new_node->index = log_bt_index;
Packit 345191
		/*
Packit 345191
		 * Copy the backtrace: bt is inside a tdata or gctx, which
Packit 345191
		 * might die before prof_log_stop is called.
Packit 345191
		 */
Packit 345191
		new_node->bt.len = bt->len;
Packit 345191
		memcpy(new_node->vec, bt->vec, bt->len * sizeof(void *));
Packit 345191
		new_node->bt.vec = new_node->vec;
Packit 345191
Packit 345191
		log_bt_index++;
Packit 345191
		ckh_insert(tsd, &log_bt_node_set, (void *)new_node, NULL);
Packit 345191
		return new_node->index;
Packit 345191
	} else {
Packit 345191
		return node->index;
Packit 345191
	}
Packit 345191
}
Packit 345191
static size_t
Packit 345191
prof_log_thr_index(tsd_t *tsd, uint64_t thr_uid, const char *name) {
Packit 345191
	assert(prof_logging_state == prof_logging_state_started);
Packit 345191
	malloc_mutex_assert_owner(tsd_tsdn(tsd), &log_mtx);
Packit 345191
Packit 345191
	prof_thr_node_t dummy_node;
Packit 345191
	dummy_node.thr_uid = thr_uid;
Packit 345191
	prof_thr_node_t *node;
Packit 345191
Packit 345191
	/* See if this thread is already cached in the table. */
Packit 345191
	if (ckh_search(&log_thr_node_set, (void *)(&dummy_node),
Packit 345191
	    (void **)(&node), NULL)) {
Packit 345191
		size_t sz = offsetof(prof_thr_node_t, name) + strlen(name) + 1;
Packit 345191
		prof_thr_node_t *new_node = (prof_thr_node_t *)
Packit 345191
		    iallocztm(tsd_tsdn(tsd), sz, sz_size2index(sz), false, NULL,
Packit 345191
		    true, arena_get(TSDN_NULL, 0, true), true);
Packit 345191
		if (log_thr_first == NULL) {
Packit 345191
			log_thr_first = new_node;
Packit 345191
			log_thr_last = new_node;
Packit 345191
		} else {
Packit 345191
			log_thr_last->next = new_node;
Packit 345191
			log_thr_last = new_node;
Packit 345191
		}
Packit 345191
Packit 345191
		new_node->next = NULL;
Packit 345191
		new_node->index = log_thr_index;
Packit 345191
		new_node->thr_uid = thr_uid;
Packit 345191
		strcpy(new_node->name, name);
Packit 345191
Packit 345191
		log_thr_index++;
Packit 345191
		ckh_insert(tsd, &log_thr_node_set, (void *)new_node, NULL);
Packit 345191
		return new_node->index;
Packit 345191
	} else {
Packit 345191
		return node->index;
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_try_log(tsd_t *tsd, const void *ptr, size_t usize, prof_tctx_t *tctx) {
Packit 345191
	malloc_mutex_assert_owner(tsd_tsdn(tsd), tctx->tdata->lock);
Packit 345191
Packit 345191
	prof_tdata_t *cons_tdata = prof_tdata_get(tsd, false);
Packit 345191
	if (cons_tdata == NULL) {
Packit 345191
		/*
Packit 345191
		 * We decide not to log these allocations. cons_tdata will be
Packit 345191
		 * NULL only when the current thread is in a weird state (e.g.
Packit 345191
		 * it's being destroyed).
Packit 345191
		 */
Packit 345191
		return;
Packit 345191
	}
Packit 345191
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), &log_mtx);
Packit 345191
Packit 345191
	if (prof_logging_state != prof_logging_state_started) {
Packit 345191
		goto label_done;
Packit 345191
	}
Packit 345191
Packit 345191
	if (!log_tables_initialized) {
Packit 345191
		bool err1 = ckh_new(tsd, &log_bt_node_set, PROF_CKH_MINITEMS,
Packit 345191
				prof_bt_node_hash, prof_bt_node_keycomp);
Packit 345191
		bool err2 = ckh_new(tsd, &log_thr_node_set, PROF_CKH_MINITEMS,
Packit 345191
				prof_thr_node_hash, prof_thr_node_keycomp);
Packit 345191
		if (err1 || err2) {
Packit 345191
			goto label_done;
Packit 345191
		}
Packit 345191
		log_tables_initialized = true;
Packit 345191
	}
Packit 345191
Packit 345191
	nstime_t alloc_time = prof_alloc_time_get(tsd_tsdn(tsd), ptr,
Packit 345191
			          (alloc_ctx_t *)NULL);
Packit 345191
	nstime_t free_time = NSTIME_ZERO_INITIALIZER;
Packit 345191
	nstime_update(&free_time);
Packit 345191
Packit 345191
	size_t sz = sizeof(prof_alloc_node_t);
Packit 345191
	prof_alloc_node_t *new_node = (prof_alloc_node_t *)
Packit 345191
	    iallocztm(tsd_tsdn(tsd), sz, sz_size2index(sz), false, NULL, true,
Packit 345191
	    arena_get(TSDN_NULL, 0, true), true);
Packit 345191
Packit 345191
	const char *prod_thr_name = (tctx->tdata->thread_name == NULL)?
Packit 345191
				        "" : tctx->tdata->thread_name;
Packit 345191
	const char *cons_thr_name = prof_thread_name_get(tsd);
Packit 345191
Packit 345191
	prof_bt_t bt;
Packit 345191
	/* Initialize the backtrace, using the buffer in tdata to store it. */
Packit 345191
	bt_init(&bt, cons_tdata->vec);
Packit 345191
	prof_backtrace(&bt;;
Packit 345191
	prof_bt_t *cons_bt = &bt;
Packit 345191
Packit 345191
	/* We haven't destroyed tctx yet, so gctx should be good to read. */
Packit 345191
	prof_bt_t *prod_bt = &tctx->gctx->bt;
Packit 345191
Packit 345191
	new_node->next = NULL;
Packit 345191
	new_node->alloc_thr_ind = prof_log_thr_index(tsd, tctx->tdata->thr_uid,
Packit 345191
				      prod_thr_name);
Packit 345191
	new_node->free_thr_ind = prof_log_thr_index(tsd, cons_tdata->thr_uid,
Packit 345191
				     cons_thr_name);
Packit 345191
	new_node->alloc_bt_ind = prof_log_bt_index(tsd, prod_bt);
Packit 345191
	new_node->free_bt_ind = prof_log_bt_index(tsd, cons_bt);
Packit 345191
	new_node->alloc_time_ns = nstime_ns(&alloc_time);
Packit 345191
	new_node->free_time_ns = nstime_ns(&free_time);
Packit 345191
	new_node->usize = usize;
Packit 345191
Packit 345191
	if (log_alloc_first == NULL) {
Packit 345191
		log_alloc_first = new_node;
Packit 345191
		log_alloc_last = new_node;
Packit 345191
	} else {
Packit 345191
		log_alloc_last->next = new_node;
Packit 345191
		log_alloc_last = new_node;
Packit 345191
	}
Packit 345191
Packit 345191
label_done:
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), &log_mtx);
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_free_sampled_object(tsd_t *tsd, const void *ptr, size_t usize,
Packit 345191
    prof_tctx_t *tctx) {
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock);
Packit 345191
Packit 345191
	assert(tctx->cnts.curobjs > 0);
Packit 345191
	assert(tctx->cnts.curbytes >= usize);
Packit 345191
	tctx->cnts.curobjs--;
Packit 345191
	tctx->cnts.curbytes -= usize;
Packit 345191
Packit 345191
	prof_try_log(tsd, ptr, usize, tctx);
Packit 345191
Packit 345191
	if (prof_tctx_should_destroy(tsd_tsdn(tsd), tctx)) {
Packit 345191
		prof_tctx_destroy(tsd, tctx);
Packit 345191
	} else {
Packit 345191
		malloc_mutex_unlock(tsd_tsdn(tsd), tctx->tdata->lock);
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
bt_init(prof_bt_t *bt, void **vec) {
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	bt->vec = vec;
Packit 345191
	bt->len = 0;
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_enter(tsd_t *tsd, prof_tdata_t *tdata) {
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(tdata == prof_tdata_get(tsd, false));
Packit 345191
Packit 345191
	if (tdata != NULL) {
Packit 345191
		assert(!tdata->enq);
Packit 345191
		tdata->enq = true;
Packit 345191
	}
Packit 345191
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), &bt2gctx_mtx);
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_leave(tsd_t *tsd, prof_tdata_t *tdata) {
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(tdata == prof_tdata_get(tsd, false));
Packit 345191
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), &bt2gctx_mtx);
Packit 345191
Packit 345191
	if (tdata != NULL) {
Packit 345191
		bool idump, gdump;
Packit 345191
Packit 345191
		assert(tdata->enq);
Packit 345191
		tdata->enq = false;
Packit 345191
		idump = tdata->enq_idump;
Packit 345191
		tdata->enq_idump = false;
Packit 345191
		gdump = tdata->enq_gdump;
Packit 345191
		tdata->enq_gdump = false;
Packit 345191
Packit 345191
		if (idump) {
Packit 345191
			prof_idump(tsd_tsdn(tsd));
Packit 345191
		}
Packit 345191
		if (gdump) {
Packit 345191
			prof_gdump(tsd_tsdn(tsd));
Packit 345191
		}
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
#ifdef JEMALLOC_PROF_LIBUNWIND
Packit 345191
void
Packit 345191
prof_backtrace(prof_bt_t *bt) {
Packit 345191
	int nframes;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(bt->len == 0);
Packit 345191
	assert(bt->vec != NULL);
Packit 345191
Packit 345191
	nframes = unw_backtrace(bt->vec, PROF_BT_MAX);
Packit 345191
	if (nframes <= 0) {
Packit 345191
		return;
Packit 345191
	}
Packit 345191
	bt->len = nframes;
Packit 345191
}
Packit 345191
#elif (defined(JEMALLOC_PROF_LIBGCC))
Packit 345191
static _Unwind_Reason_Code
Packit 345191
prof_unwind_init_callback(struct _Unwind_Context *context, void *arg) {
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	return _URC_NO_REASON;
Packit 345191
}
Packit 345191
Packit 345191
static _Unwind_Reason_Code
Packit 345191
prof_unwind_callback(struct _Unwind_Context *context, void *arg) {
Packit 345191
	prof_unwind_data_t *data = (prof_unwind_data_t *)arg;
Packit 345191
	void *ip;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	ip = (void *)_Unwind_GetIP(context);
Packit 345191
	if (ip == NULL) {
Packit 345191
		return _URC_END_OF_STACK;
Packit 345191
	}
Packit 345191
	data->bt->vec[data->bt->len] = ip;
Packit 345191
	data->bt->len++;
Packit 345191
	if (data->bt->len == data->max) {
Packit 345191
		return _URC_END_OF_STACK;
Packit 345191
	}
Packit 345191
Packit 345191
	return _URC_NO_REASON;
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_backtrace(prof_bt_t *bt) {
Packit 345191
	prof_unwind_data_t data = {bt, PROF_BT_MAX};
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	_Unwind_Backtrace(prof_unwind_callback, &data);
Packit 345191
}
Packit 345191
#elif (defined(JEMALLOC_PROF_GCC))
Packit 345191
void
Packit 345191
prof_backtrace(prof_bt_t *bt) {
Packit 345191
#define BT_FRAME(i)							\
Packit 345191
	if ((i) < PROF_BT_MAX) {					\
Packit 345191
		void *p;						\
Packit 345191
		if (__builtin_frame_address(i) == 0) {			\
Packit 345191
			return;						\
Packit 345191
		}							\
Packit 345191
		p = __builtin_return_address(i);			\
Packit 345191
		if (p == NULL) {					\
Packit 345191
			return;						\
Packit 345191
		}							\
Packit 345191
		bt->vec[(i)] = p;					\
Packit 345191
		bt->len = (i) + 1;					\
Packit 345191
	} else {							\
Packit 345191
		return;							\
Packit 345191
	}
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	BT_FRAME(0)
Packit 345191
	BT_FRAME(1)
Packit 345191
	BT_FRAME(2)
Packit 345191
	BT_FRAME(3)
Packit 345191
	BT_FRAME(4)
Packit 345191
	BT_FRAME(5)
Packit 345191
	BT_FRAME(6)
Packit 345191
	BT_FRAME(7)
Packit 345191
	BT_FRAME(8)
Packit 345191
	BT_FRAME(9)
Packit 345191
Packit 345191
	BT_FRAME(10)
Packit 345191
	BT_FRAME(11)
Packit 345191
	BT_FRAME(12)
Packit 345191
	BT_FRAME(13)
Packit 345191
	BT_FRAME(14)
Packit 345191
	BT_FRAME(15)
Packit 345191
	BT_FRAME(16)
Packit 345191
	BT_FRAME(17)
Packit 345191
	BT_FRAME(18)
Packit 345191
	BT_FRAME(19)
Packit 345191
Packit 345191
	BT_FRAME(20)
Packit 345191
	BT_FRAME(21)
Packit 345191
	BT_FRAME(22)
Packit 345191
	BT_FRAME(23)
Packit 345191
	BT_FRAME(24)
Packit 345191
	BT_FRAME(25)
Packit 345191
	BT_FRAME(26)
Packit 345191
	BT_FRAME(27)
Packit 345191
	BT_FRAME(28)
Packit 345191
	BT_FRAME(29)
Packit 345191
Packit 345191
	BT_FRAME(30)
Packit 345191
	BT_FRAME(31)
Packit 345191
	BT_FRAME(32)
Packit 345191
	BT_FRAME(33)
Packit 345191
	BT_FRAME(34)
Packit 345191
	BT_FRAME(35)
Packit 345191
	BT_FRAME(36)
Packit 345191
	BT_FRAME(37)
Packit 345191
	BT_FRAME(38)
Packit 345191
	BT_FRAME(39)
Packit 345191
Packit 345191
	BT_FRAME(40)
Packit 345191
	BT_FRAME(41)
Packit 345191
	BT_FRAME(42)
Packit 345191
	BT_FRAME(43)
Packit 345191
	BT_FRAME(44)
Packit 345191
	BT_FRAME(45)
Packit 345191
	BT_FRAME(46)
Packit 345191
	BT_FRAME(47)
Packit 345191
	BT_FRAME(48)
Packit 345191
	BT_FRAME(49)
Packit 345191
Packit 345191
	BT_FRAME(50)
Packit 345191
	BT_FRAME(51)
Packit 345191
	BT_FRAME(52)
Packit 345191
	BT_FRAME(53)
Packit 345191
	BT_FRAME(54)
Packit 345191
	BT_FRAME(55)
Packit 345191
	BT_FRAME(56)
Packit 345191
	BT_FRAME(57)
Packit 345191
	BT_FRAME(58)
Packit 345191
	BT_FRAME(59)
Packit 345191
Packit 345191
	BT_FRAME(60)
Packit 345191
	BT_FRAME(61)
Packit 345191
	BT_FRAME(62)
Packit 345191
	BT_FRAME(63)
Packit 345191
	BT_FRAME(64)
Packit 345191
	BT_FRAME(65)
Packit 345191
	BT_FRAME(66)
Packit 345191
	BT_FRAME(67)
Packit 345191
	BT_FRAME(68)
Packit 345191
	BT_FRAME(69)
Packit 345191
Packit 345191
	BT_FRAME(70)
Packit 345191
	BT_FRAME(71)
Packit 345191
	BT_FRAME(72)
Packit 345191
	BT_FRAME(73)
Packit 345191
	BT_FRAME(74)
Packit 345191
	BT_FRAME(75)
Packit 345191
	BT_FRAME(76)
Packit 345191
	BT_FRAME(77)
Packit 345191
	BT_FRAME(78)
Packit 345191
	BT_FRAME(79)
Packit 345191
Packit 345191
	BT_FRAME(80)
Packit 345191
	BT_FRAME(81)
Packit 345191
	BT_FRAME(82)
Packit 345191
	BT_FRAME(83)
Packit 345191
	BT_FRAME(84)
Packit 345191
	BT_FRAME(85)
Packit 345191
	BT_FRAME(86)
Packit 345191
	BT_FRAME(87)
Packit 345191
	BT_FRAME(88)
Packit 345191
	BT_FRAME(89)
Packit 345191
Packit 345191
	BT_FRAME(90)
Packit 345191
	BT_FRAME(91)
Packit 345191
	BT_FRAME(92)
Packit 345191
	BT_FRAME(93)
Packit 345191
	BT_FRAME(94)
Packit 345191
	BT_FRAME(95)
Packit 345191
	BT_FRAME(96)
Packit 345191
	BT_FRAME(97)
Packit 345191
	BT_FRAME(98)
Packit 345191
	BT_FRAME(99)
Packit 345191
Packit 345191
	BT_FRAME(100)
Packit 345191
	BT_FRAME(101)
Packit 345191
	BT_FRAME(102)
Packit 345191
	BT_FRAME(103)
Packit 345191
	BT_FRAME(104)
Packit 345191
	BT_FRAME(105)
Packit 345191
	BT_FRAME(106)
Packit 345191
	BT_FRAME(107)
Packit 345191
	BT_FRAME(108)
Packit 345191
	BT_FRAME(109)
Packit 345191
Packit 345191
	BT_FRAME(110)
Packit 345191
	BT_FRAME(111)
Packit 345191
	BT_FRAME(112)
Packit 345191
	BT_FRAME(113)
Packit 345191
	BT_FRAME(114)
Packit 345191
	BT_FRAME(115)
Packit 345191
	BT_FRAME(116)
Packit 345191
	BT_FRAME(117)
Packit 345191
	BT_FRAME(118)
Packit 345191
	BT_FRAME(119)
Packit 345191
Packit 345191
	BT_FRAME(120)
Packit 345191
	BT_FRAME(121)
Packit 345191
	BT_FRAME(122)
Packit 345191
	BT_FRAME(123)
Packit 345191
	BT_FRAME(124)
Packit 345191
	BT_FRAME(125)
Packit 345191
	BT_FRAME(126)
Packit 345191
	BT_FRAME(127)
Packit 345191
#undef BT_FRAME
Packit 345191
}
Packit 345191
#else
Packit 345191
void
Packit 345191
prof_backtrace(prof_bt_t *bt) {
Packit 345191
	cassert(config_prof);
Packit 345191
	not_reached();
Packit 345191
}
Packit 345191
#endif
Packit 345191
Packit 345191
static malloc_mutex_t *
Packit 345191
prof_gctx_mutex_choose(void) {
Packit 345191
	unsigned ngctxs = atomic_fetch_add_u(&cum_gctxs, 1, ATOMIC_RELAXED);
Packit 345191
Packit 345191
	return &gctx_locks[(ngctxs - 1) % PROF_NCTX_LOCKS];
Packit 345191
}
Packit 345191
Packit 345191
static malloc_mutex_t *
Packit 345191
prof_tdata_mutex_choose(uint64_t thr_uid) {
Packit 345191
	return &tdata_locks[thr_uid % PROF_NTDATA_LOCKS];
Packit 345191
}
Packit 345191
Packit 345191
static prof_gctx_t *
Packit 345191
prof_gctx_create(tsdn_t *tsdn, prof_bt_t *bt) {
Packit 345191
	/*
Packit 345191
	 * Create a single allocation that has space for vec of length bt->len.
Packit 345191
	 */
Packit 345191
	size_t size = offsetof(prof_gctx_t, vec) + (bt->len * sizeof(void *));
Packit 345191
	prof_gctx_t *gctx = (prof_gctx_t *)iallocztm(tsdn, size,
Packit 345191
	    sz_size2index(size), false, NULL, true, arena_get(TSDN_NULL, 0, true),
Packit 345191
	    true);
Packit 345191
	if (gctx == NULL) {
Packit 345191
		return NULL;
Packit 345191
	}
Packit 345191
	gctx->lock = prof_gctx_mutex_choose();
Packit 345191
	/*
Packit 345191
	 * Set nlimbo to 1, in order to avoid a race condition with
Packit 345191
	 * prof_tctx_destroy()/prof_gctx_try_destroy().
Packit 345191
	 */
Packit 345191
	gctx->nlimbo = 1;
Packit 345191
	tctx_tree_new(&gctx->tctxs);
Packit 345191
	/* Duplicate bt. */
Packit 345191
	memcpy(gctx->vec, bt->vec, bt->len * sizeof(void *));
Packit 345191
	gctx->bt.vec = gctx->vec;
Packit 345191
	gctx->bt.len = bt->len;
Packit 345191
	return gctx;
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_gctx_try_destroy(tsd_t *tsd, prof_tdata_t *tdata_self, prof_gctx_t *gctx,
Packit 345191
    prof_tdata_t *tdata) {
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	/*
Packit 345191
	 * Check that gctx is still unused by any thread cache before destroying
Packit 345191
	 * it.  prof_lookup() increments gctx->nlimbo in order to avoid a race
Packit 345191
	 * condition with this function, as does prof_tctx_destroy() in order to
Packit 345191
	 * avoid a race between the main body of prof_tctx_destroy() and entry
Packit 345191
	 * into this function.
Packit 345191
	 */
Packit 345191
	prof_enter(tsd, tdata_self);
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), gctx->lock);
Packit 345191
	assert(gctx->nlimbo != 0);
Packit 345191
	if (tctx_tree_empty(&gctx->tctxs) && gctx->nlimbo == 1) {
Packit 345191
		/* Remove gctx from bt2gctx. */
Packit 345191
		if (ckh_remove(tsd, &bt2gctx, &gctx->bt, NULL, NULL)) {
Packit 345191
			not_reached();
Packit 345191
		}
Packit 345191
		prof_leave(tsd, tdata_self);
Packit 345191
		/* Destroy gctx. */
Packit 345191
		malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);
Packit 345191
		idalloctm(tsd_tsdn(tsd), gctx, NULL, NULL, true, true);
Packit 345191
	} else {
Packit 345191
		/*
Packit 345191
		 * Compensate for increment in prof_tctx_destroy() or
Packit 345191
		 * prof_lookup().
Packit 345191
		 */
Packit 345191
		gctx->nlimbo--;
Packit 345191
		malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);
Packit 345191
		prof_leave(tsd, tdata_self);
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_tctx_should_destroy(tsdn_t *tsdn, prof_tctx_t *tctx) {
Packit 345191
	malloc_mutex_assert_owner(tsdn, tctx->tdata->lock);
Packit 345191
Packit 345191
	if (opt_prof_accum) {
Packit 345191
		return false;
Packit 345191
	}
Packit 345191
	if (tctx->cnts.curobjs != 0) {
Packit 345191
		return false;
Packit 345191
	}
Packit 345191
	if (tctx->prepared) {
Packit 345191
		return false;
Packit 345191
	}
Packit 345191
	return true;
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_gctx_should_destroy(prof_gctx_t *gctx) {
Packit 345191
	if (opt_prof_accum) {
Packit 345191
		return false;
Packit 345191
	}
Packit 345191
	if (!tctx_tree_empty(&gctx->tctxs)) {
Packit 345191
		return false;
Packit 345191
	}
Packit 345191
	if (gctx->nlimbo != 0) {
Packit 345191
		return false;
Packit 345191
	}
Packit 345191
	return true;
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx) {
Packit 345191
	prof_tdata_t *tdata = tctx->tdata;
Packit 345191
	prof_gctx_t *gctx = tctx->gctx;
Packit 345191
	bool destroy_tdata, destroy_tctx, destroy_gctx;
Packit 345191
Packit 345191
	malloc_mutex_assert_owner(tsd_tsdn(tsd), tctx->tdata->lock);
Packit 345191
Packit 345191
	assert(tctx->cnts.curobjs == 0);
Packit 345191
	assert(tctx->cnts.curbytes == 0);
Packit 345191
	assert(!opt_prof_accum);
Packit 345191
	assert(tctx->cnts.accumobjs == 0);
Packit 345191
	assert(tctx->cnts.accumbytes == 0);
Packit 345191
Packit 345191
	ckh_remove(tsd, &tdata->bt2tctx, &gctx->bt, NULL, NULL);
Packit 345191
	destroy_tdata = prof_tdata_should_destroy(tsd_tsdn(tsd), tdata, false);
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);
Packit 345191
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), gctx->lock);
Packit 345191
	switch (tctx->state) {
Packit 345191
	case prof_tctx_state_nominal:
Packit 345191
		tctx_tree_remove(&gctx->tctxs, tctx);
Packit 345191
		destroy_tctx = true;
Packit 345191
		if (prof_gctx_should_destroy(gctx)) {
Packit 345191
			/*
Packit 345191
			 * Increment gctx->nlimbo in order to keep another
Packit 345191
			 * thread from winning the race to destroy gctx while
Packit 345191
			 * this one has gctx->lock dropped.  Without this, it
Packit 345191
			 * would be possible for another thread to:
Packit 345191
			 *
Packit 345191
			 * 1) Sample an allocation associated with gctx.
Packit 345191
			 * 2) Deallocate the sampled object.
Packit 345191
			 * 3) Successfully prof_gctx_try_destroy(gctx).
Packit 345191
			 *
Packit 345191
			 * The result would be that gctx no longer exists by the
Packit 345191
			 * time this thread accesses it in
Packit 345191
			 * prof_gctx_try_destroy().
Packit 345191
			 */
Packit 345191
			gctx->nlimbo++;
Packit 345191
			destroy_gctx = true;
Packit 345191
		} else {
Packit 345191
			destroy_gctx = false;
Packit 345191
		}
Packit 345191
		break;
Packit 345191
	case prof_tctx_state_dumping:
Packit 345191
		/*
Packit 345191
		 * A dumping thread needs tctx to remain valid until dumping
Packit 345191
		 * has finished.  Change state such that the dumping thread will
Packit 345191
		 * complete destruction during a late dump iteration phase.
Packit 345191
		 */
Packit 345191
		tctx->state = prof_tctx_state_purgatory;
Packit 345191
		destroy_tctx = false;
Packit 345191
		destroy_gctx = false;
Packit 345191
		break;
Packit 345191
	default:
Packit 345191
		not_reached();
Packit 345191
		destroy_tctx = false;
Packit 345191
		destroy_gctx = false;
Packit 345191
	}
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);
Packit 345191
	if (destroy_gctx) {
Packit 345191
		prof_gctx_try_destroy(tsd, prof_tdata_get(tsd, false), gctx,
Packit 345191
		    tdata);
Packit 345191
	}
Packit 345191
Packit 345191
	malloc_mutex_assert_not_owner(tsd_tsdn(tsd), tctx->tdata->lock);
Packit 345191
Packit 345191
	if (destroy_tdata) {
Packit 345191
		prof_tdata_destroy(tsd, tdata, false);
Packit 345191
	}
Packit 345191
Packit 345191
	if (destroy_tctx) {
Packit 345191
		idalloctm(tsd_tsdn(tsd), tctx, NULL, NULL, true, true);
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_lookup_global(tsd_t *tsd, prof_bt_t *bt, prof_tdata_t *tdata,
Packit 345191
    void **p_btkey, prof_gctx_t **p_gctx, bool *p_new_gctx) {
Packit 345191
	union {
Packit 345191
		prof_gctx_t	*p;
Packit 345191
		void		*v;
Packit 345191
	} gctx, tgctx;
Packit 345191
	union {
Packit 345191
		prof_bt_t	*p;
Packit 345191
		void		*v;
Packit 345191
	} btkey;
Packit 345191
	bool new_gctx;
Packit 345191
Packit 345191
	prof_enter(tsd, tdata);
Packit 345191
	if (ckh_search(&bt2gctx, bt, &btkey.v, &gctx.v)) {
Packit 345191
		/* bt has never been seen before.  Insert it. */
Packit 345191
		prof_leave(tsd, tdata);
Packit 345191
		tgctx.p = prof_gctx_create(tsd_tsdn(tsd), bt);
Packit 345191
		if (tgctx.v == NULL) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
		prof_enter(tsd, tdata);
Packit 345191
		if (ckh_search(&bt2gctx, bt, &btkey.v, &gctx.v)) {
Packit 345191
			gctx.p = tgctx.p;
Packit 345191
			btkey.p = &gctx.p->bt;
Packit 345191
			if (ckh_insert(tsd, &bt2gctx, btkey.v, gctx.v)) {
Packit 345191
				/* OOM. */
Packit 345191
				prof_leave(tsd, tdata);
Packit 345191
				idalloctm(tsd_tsdn(tsd), gctx.v, NULL, NULL,
Packit 345191
				    true, true);
Packit 345191
				return true;
Packit 345191
			}
Packit 345191
			new_gctx = true;
Packit 345191
		} else {
Packit 345191
			new_gctx = false;
Packit 345191
		}
Packit 345191
	} else {
Packit 345191
		tgctx.v = NULL;
Packit 345191
		new_gctx = false;
Packit 345191
	}
Packit 345191
Packit 345191
	if (!new_gctx) {
Packit 345191
		/*
Packit 345191
		 * Increment nlimbo, in order to avoid a race condition with
Packit 345191
		 * prof_tctx_destroy()/prof_gctx_try_destroy().
Packit 345191
		 */
Packit 345191
		malloc_mutex_lock(tsd_tsdn(tsd), gctx.p->lock);
Packit 345191
		gctx.p->nlimbo++;
Packit 345191
		malloc_mutex_unlock(tsd_tsdn(tsd), gctx.p->lock);
Packit 345191
		new_gctx = false;
Packit 345191
Packit 345191
		if (tgctx.v != NULL) {
Packit 345191
			/* Lost race to insert. */
Packit 345191
			idalloctm(tsd_tsdn(tsd), tgctx.v, NULL, NULL, true,
Packit 345191
			    true);
Packit 345191
		}
Packit 345191
	}
Packit 345191
	prof_leave(tsd, tdata);
Packit 345191
Packit 345191
	*p_btkey = btkey.v;
Packit 345191
	*p_gctx = gctx.p;
Packit 345191
	*p_new_gctx = new_gctx;
Packit 345191
	return false;
Packit 345191
}
Packit 345191
Packit 345191
prof_tctx_t *
Packit 345191
prof_lookup(tsd_t *tsd, prof_bt_t *bt) {
Packit 345191
	union {
Packit 345191
		prof_tctx_t	*p;
Packit 345191
		void		*v;
Packit 345191
	} ret;
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
	bool not_found;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	tdata = prof_tdata_get(tsd, false);
Packit 345191
	if (tdata == NULL) {
Packit 345191
		return NULL;
Packit 345191
	}
Packit 345191
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), tdata->lock);
Packit 345191
	not_found = ckh_search(&tdata->bt2tctx, bt, NULL, &ret.v);
Packit 345191
	if (!not_found) { /* Note double negative! */
Packit 345191
		ret.p->prepared = true;
Packit 345191
	}
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);
Packit 345191
	if (not_found) {
Packit 345191
		void *btkey;
Packit 345191
		prof_gctx_t *gctx;
Packit 345191
		bool new_gctx, error;
Packit 345191
Packit 345191
		/*
Packit 345191
		 * This thread's cache lacks bt.  Look for it in the global
Packit 345191
		 * cache.
Packit 345191
		 */
Packit 345191
		if (prof_lookup_global(tsd, bt, tdata, &btkey, &gctx,
Packit 345191
		    &new_gctx)) {
Packit 345191
			return NULL;
Packit 345191
		}
Packit 345191
Packit 345191
		/* Link a prof_tctx_t into gctx for this thread. */
Packit 345191
		ret.v = iallocztm(tsd_tsdn(tsd), sizeof(prof_tctx_t),
Packit 345191
		    sz_size2index(sizeof(prof_tctx_t)), false, NULL, true,
Packit 345191
		    arena_ichoose(tsd, NULL), true);
Packit 345191
		if (ret.p == NULL) {
Packit 345191
			if (new_gctx) {
Packit 345191
				prof_gctx_try_destroy(tsd, tdata, gctx, tdata);
Packit 345191
			}
Packit 345191
			return NULL;
Packit 345191
		}
Packit 345191
		ret.p->tdata = tdata;
Packit 345191
		ret.p->thr_uid = tdata->thr_uid;
Packit 345191
		ret.p->thr_discrim = tdata->thr_discrim;
Packit 345191
		memset(&ret.p->cnts, 0, sizeof(prof_cnt_t));
Packit 345191
		ret.p->gctx = gctx;
Packit 345191
		ret.p->tctx_uid = tdata->tctx_uid_next++;
Packit 345191
		ret.p->prepared = true;
Packit 345191
		ret.p->state = prof_tctx_state_initializing;
Packit 345191
		malloc_mutex_lock(tsd_tsdn(tsd), tdata->lock);
Packit 345191
		error = ckh_insert(tsd, &tdata->bt2tctx, btkey, ret.v);
Packit 345191
		malloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);
Packit 345191
		if (error) {
Packit 345191
			if (new_gctx) {
Packit 345191
				prof_gctx_try_destroy(tsd, tdata, gctx, tdata);
Packit 345191
			}
Packit 345191
			idalloctm(tsd_tsdn(tsd), ret.v, NULL, NULL, true, true);
Packit 345191
			return NULL;
Packit 345191
		}
Packit 345191
		malloc_mutex_lock(tsd_tsdn(tsd), gctx->lock);
Packit 345191
		ret.p->state = prof_tctx_state_nominal;
Packit 345191
		tctx_tree_insert(&gctx->tctxs, ret.p);
Packit 345191
		gctx->nlimbo--;
Packit 345191
		malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);
Packit 345191
	}
Packit 345191
Packit 345191
	return ret.p;
Packit 345191
}
Packit 345191
Packit 345191
/*
Packit 345191
 * The bodies of this function and prof_leakcheck() are compiled out unless heap
Packit 345191
 * profiling is enabled, so that it is possible to compile jemalloc with
Packit 345191
 * floating point support completely disabled.  Avoiding floating point code is
Packit 345191
 * important on memory-constrained systems, but it also enables a workaround for
Packit 345191
 * versions of glibc that don't properly save/restore floating point registers
Packit 345191
 * during dynamic lazy symbol loading (which internally calls into whatever
Packit 345191
 * malloc implementation happens to be integrated into the application).  Note
Packit 345191
 * that some compilers (e.g.  gcc 4.8) may use floating point registers for fast
Packit 345191
 * memory moves, so jemalloc must be compiled with such optimizations disabled
Packit 345191
 * (e.g.
Packit 345191
 * -mno-sse) in order for the workaround to be complete.
Packit 345191
 */
Packit 345191
void
Packit 345191
prof_sample_threshold_update(prof_tdata_t *tdata) {
Packit 345191
#ifdef JEMALLOC_PROF
Packit 345191
	if (!config_prof) {
Packit 345191
		return;
Packit 345191
	}
Packit 345191
Packit 345191
	if (lg_prof_sample == 0) {
Packit 345191
		tsd_bytes_until_sample_set(tsd_fetch(), 0);
Packit 345191
		return;
Packit 345191
	}
Packit 345191
Packit 345191
	/*
Packit 345191
	 * Compute sample interval as a geometrically distributed random
Packit 345191
	 * variable with mean (2^lg_prof_sample).
Packit 345191
	 *
Packit 345191
	 *                             __        __
Packit 345191
	 *                             |  log(u)  |                     1
Packit 345191
	 * tdata->bytes_until_sample = | -------- |, where p = ---------------
Packit 345191
	 *                             | log(1-p) |             lg_prof_sample
Packit 345191
	 *                                                     2
Packit 345191
	 *
Packit 345191
	 * For more information on the math, see:
Packit 345191
	 *
Packit 345191
	 *   Non-Uniform Random Variate Generation
Packit 345191
	 *   Luc Devroye
Packit 345191
	 *   Springer-Verlag, New York, 1986
Packit 345191
	 *   pp 500
Packit 345191
	 *   (http://luc.devroye.org/rnbookindex.html)
Packit 345191
	 */
Packit 345191
	uint64_t r = prng_lg_range_u64(&tdata->prng_state, 53);
Packit 345191
	double u = (double)r * (1.0/9007199254740992.0L);
Packit 345191
	uint64_t bytes_until_sample = (uint64_t)(log(u) /
Packit 345191
	    log(1.0 - (1.0 / (double)((uint64_t)1U << lg_prof_sample))))
Packit 345191
	    + (uint64_t)1U;
Packit 345191
	if (bytes_until_sample > SSIZE_MAX) {
Packit 345191
		bytes_until_sample = SSIZE_MAX;
Packit 345191
	}
Packit 345191
	tsd_bytes_until_sample_set(tsd_fetch(), bytes_until_sample);
Packit 345191
Packit 345191
#endif
Packit 345191
}
Packit 345191
Packit 345191
#ifdef JEMALLOC_JET
Packit 345191
static prof_tdata_t *
Packit 345191
prof_tdata_count_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,
Packit 345191
    void *arg) {
Packit 345191
	size_t *tdata_count = (size_t *)arg;
Packit 345191
Packit 345191
	(*tdata_count)++;
Packit 345191
Packit 345191
	return NULL;
Packit 345191
}
Packit 345191
Packit 345191
size_t
Packit 345191
prof_tdata_count(void) {
Packit 345191
	size_t tdata_count = 0;
Packit 345191
	tsdn_t *tsdn;
Packit 345191
Packit 345191
	tsdn = tsdn_fetch();
Packit 345191
	malloc_mutex_lock(tsdn, &tdatas_mtx);
Packit 345191
	tdata_tree_iter(&tdatas, NULL, prof_tdata_count_iter,
Packit 345191
	    (void *)&tdata_count);
Packit 345191
	malloc_mutex_unlock(tsdn, &tdatas_mtx);
Packit 345191
Packit 345191
	return tdata_count;
Packit 345191
}
Packit 345191
Packit 345191
size_t
Packit 345191
prof_bt_count(void) {
Packit 345191
	size_t bt_count;
Packit 345191
	tsd_t *tsd;
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
Packit 345191
	tsd = tsd_fetch();
Packit 345191
	tdata = prof_tdata_get(tsd, false);
Packit 345191
	if (tdata == NULL) {
Packit 345191
		return 0;
Packit 345191
	}
Packit 345191
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), &bt2gctx_mtx);
Packit 345191
	bt_count = ckh_count(&bt2gctx);
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), &bt2gctx_mtx);
Packit 345191
Packit 345191
	return bt_count;
Packit 345191
}
Packit 345191
#endif
Packit 345191
Packit 345191
static int
Packit 345191
prof_dump_open_impl(bool propagate_err, const char *filename) {
Packit 345191
	int fd;
Packit 345191
Packit 345191
	fd = creat(filename, 0644);
Packit 345191
	if (fd == -1 && !propagate_err) {
Packit 345191
		malloc_printf("<jemalloc>: creat(\"%s\"), 0644) failed\n",
Packit 345191
		    filename);
Packit 345191
		if (opt_abort) {
Packit 345191
			abort();
Packit 345191
		}
Packit 345191
	}
Packit 345191
Packit 345191
	return fd;
Packit 345191
}
Packit 345191
prof_dump_open_t *JET_MUTABLE prof_dump_open = prof_dump_open_impl;
Packit 345191
Packit 345191
static bool
Packit 345191
prof_dump_flush(bool propagate_err) {
Packit 345191
	bool ret = false;
Packit 345191
	ssize_t err;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	err = malloc_write_fd(prof_dump_fd, prof_dump_buf, prof_dump_buf_end);
Packit 345191
	if (err == -1) {
Packit 345191
		if (!propagate_err) {
Packit 345191
			malloc_write("<jemalloc>: write() failed during heap "
Packit 345191
			    "profile flush\n");
Packit 345191
			if (opt_abort) {
Packit 345191
				abort();
Packit 345191
			}
Packit 345191
		}
Packit 345191
		ret = true;
Packit 345191
	}
Packit 345191
	prof_dump_buf_end = 0;
Packit 345191
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_dump_close(bool propagate_err) {
Packit 345191
	bool ret;
Packit 345191
Packit 345191
	assert(prof_dump_fd != -1);
Packit 345191
	ret = prof_dump_flush(propagate_err);
Packit 345191
	close(prof_dump_fd);
Packit 345191
	prof_dump_fd = -1;
Packit 345191
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_dump_write(bool propagate_err, const char *s) {
Packit 345191
	size_t i, slen, n;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	i = 0;
Packit 345191
	slen = strlen(s);
Packit 345191
	while (i < slen) {
Packit 345191
		/* Flush the buffer if it is full. */
Packit 345191
		if (prof_dump_buf_end == PROF_DUMP_BUFSIZE) {
Packit 345191
			if (prof_dump_flush(propagate_err) && propagate_err) {
Packit 345191
				return true;
Packit 345191
			}
Packit 345191
		}
Packit 345191
Packit 345191
		if (prof_dump_buf_end + slen - i <= PROF_DUMP_BUFSIZE) {
Packit 345191
			/* Finish writing. */
Packit 345191
			n = slen - i;
Packit 345191
		} else {
Packit 345191
			/* Write as much of s as will fit. */
Packit 345191
			n = PROF_DUMP_BUFSIZE - prof_dump_buf_end;
Packit 345191
		}
Packit 345191
		memcpy(&prof_dump_buf[prof_dump_buf_end], &s[i], n);
Packit 345191
		prof_dump_buf_end += n;
Packit 345191
		i += n;
Packit 345191
	}
Packit 345191
	assert(i == slen);
Packit 345191
Packit 345191
	return false;
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_FORMAT_PRINTF(2, 3)
Packit 345191
static bool
Packit 345191
prof_dump_printf(bool propagate_err, const char *format, ...) {
Packit 345191
	bool ret;
Packit 345191
	va_list ap;
Packit 345191
	char buf[PROF_PRINTF_BUFSIZE];
Packit 345191
Packit 345191
	va_start(ap, format);
Packit 345191
	malloc_vsnprintf(buf, sizeof(buf), format, ap);
Packit 345191
	va_end(ap);
Packit 345191
	ret = prof_dump_write(propagate_err, buf);
Packit 345191
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_tctx_merge_tdata(tsdn_t *tsdn, prof_tctx_t *tctx, prof_tdata_t *tdata) {
Packit 345191
	malloc_mutex_assert_owner(tsdn, tctx->tdata->lock);
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, tctx->gctx->lock);
Packit 345191
Packit 345191
	switch (tctx->state) {
Packit 345191
	case prof_tctx_state_initializing:
Packit 345191
		malloc_mutex_unlock(tsdn, tctx->gctx->lock);
Packit 345191
		return;
Packit 345191
	case prof_tctx_state_nominal:
Packit 345191
		tctx->state = prof_tctx_state_dumping;
Packit 345191
		malloc_mutex_unlock(tsdn, tctx->gctx->lock);
Packit 345191
Packit 345191
		memcpy(&tctx->dump_cnts, &tctx->cnts, sizeof(prof_cnt_t));
Packit 345191
Packit 345191
		tdata->cnt_summed.curobjs += tctx->dump_cnts.curobjs;
Packit 345191
		tdata->cnt_summed.curbytes += tctx->dump_cnts.curbytes;
Packit 345191
		if (opt_prof_accum) {
Packit 345191
			tdata->cnt_summed.accumobjs +=
Packit 345191
			    tctx->dump_cnts.accumobjs;
Packit 345191
			tdata->cnt_summed.accumbytes +=
Packit 345191
			    tctx->dump_cnts.accumbytes;
Packit 345191
		}
Packit 345191
		break;
Packit 345191
	case prof_tctx_state_dumping:
Packit 345191
	case prof_tctx_state_purgatory:
Packit 345191
		not_reached();
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_tctx_merge_gctx(tsdn_t *tsdn, prof_tctx_t *tctx, prof_gctx_t *gctx) {
Packit 345191
	malloc_mutex_assert_owner(tsdn, gctx->lock);
Packit 345191
Packit 345191
	gctx->cnt_summed.curobjs += tctx->dump_cnts.curobjs;
Packit 345191
	gctx->cnt_summed.curbytes += tctx->dump_cnts.curbytes;
Packit 345191
	if (opt_prof_accum) {
Packit 345191
		gctx->cnt_summed.accumobjs += tctx->dump_cnts.accumobjs;
Packit 345191
		gctx->cnt_summed.accumbytes += tctx->dump_cnts.accumbytes;
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
static prof_tctx_t *
Packit 345191
prof_tctx_merge_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg) {
Packit 345191
	tsdn_t *tsdn = (tsdn_t *)arg;
Packit 345191
Packit 345191
	malloc_mutex_assert_owner(tsdn, tctx->gctx->lock);
Packit 345191
Packit 345191
	switch (tctx->state) {
Packit 345191
	case prof_tctx_state_nominal:
Packit 345191
		/* New since dumping started; ignore. */
Packit 345191
		break;
Packit 345191
	case prof_tctx_state_dumping:
Packit 345191
	case prof_tctx_state_purgatory:
Packit 345191
		prof_tctx_merge_gctx(tsdn, tctx, tctx->gctx);
Packit 345191
		break;
Packit 345191
	default:
Packit 345191
		not_reached();
Packit 345191
	}
Packit 345191
Packit 345191
	return NULL;
Packit 345191
}
Packit 345191
Packit 345191
struct prof_tctx_dump_iter_arg_s {
Packit 345191
	tsdn_t	*tsdn;
Packit 345191
	bool	propagate_err;
Packit 345191
};
Packit 345191
Packit 345191
static prof_tctx_t *
Packit 345191
prof_tctx_dump_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *opaque) {
Packit 345191
	struct prof_tctx_dump_iter_arg_s *arg =
Packit 345191
	    (struct prof_tctx_dump_iter_arg_s *)opaque;
Packit 345191
Packit 345191
	malloc_mutex_assert_owner(arg->tsdn, tctx->gctx->lock);
Packit 345191
Packit 345191
	switch (tctx->state) {
Packit 345191
	case prof_tctx_state_initializing:
Packit 345191
	case prof_tctx_state_nominal:
Packit 345191
		/* Not captured by this dump. */
Packit 345191
		break;
Packit 345191
	case prof_tctx_state_dumping:
Packit 345191
	case prof_tctx_state_purgatory:
Packit 345191
		if (prof_dump_printf(arg->propagate_err,
Packit 345191
		    "  t%"FMTu64": %"FMTu64": %"FMTu64" [%"FMTu64": "
Packit 345191
		    "%"FMTu64"]\n", tctx->thr_uid, tctx->dump_cnts.curobjs,
Packit 345191
		    tctx->dump_cnts.curbytes, tctx->dump_cnts.accumobjs,
Packit 345191
		    tctx->dump_cnts.accumbytes)) {
Packit 345191
			return tctx;
Packit 345191
		}
Packit 345191
		break;
Packit 345191
	default:
Packit 345191
		not_reached();
Packit 345191
	}
Packit 345191
	return NULL;
Packit 345191
}
Packit 345191
Packit 345191
static prof_tctx_t *
Packit 345191
prof_tctx_finish_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg) {
Packit 345191
	tsdn_t *tsdn = (tsdn_t *)arg;
Packit 345191
	prof_tctx_t *ret;
Packit 345191
Packit 345191
	malloc_mutex_assert_owner(tsdn, tctx->gctx->lock);
Packit 345191
Packit 345191
	switch (tctx->state) {
Packit 345191
	case prof_tctx_state_nominal:
Packit 345191
		/* New since dumping started; ignore. */
Packit 345191
		break;
Packit 345191
	case prof_tctx_state_dumping:
Packit 345191
		tctx->state = prof_tctx_state_nominal;
Packit 345191
		break;
Packit 345191
	case prof_tctx_state_purgatory:
Packit 345191
		ret = tctx;
Packit 345191
		goto label_return;
Packit 345191
	default:
Packit 345191
		not_reached();
Packit 345191
	}
Packit 345191
Packit 345191
	ret = NULL;
Packit 345191
label_return:
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_dump_gctx_prep(tsdn_t *tsdn, prof_gctx_t *gctx, prof_gctx_tree_t *gctxs) {
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, gctx->lock);
Packit 345191
Packit 345191
	/*
Packit 345191
	 * Increment nlimbo so that gctx won't go away before dump.
Packit 345191
	 * Additionally, link gctx into the dump list so that it is included in
Packit 345191
	 * prof_dump()'s second pass.
Packit 345191
	 */
Packit 345191
	gctx->nlimbo++;
Packit 345191
	gctx_tree_insert(gctxs, gctx);
Packit 345191
Packit 345191
	memset(&gctx->cnt_summed, 0, sizeof(prof_cnt_t));
Packit 345191
Packit 345191
	malloc_mutex_unlock(tsdn, gctx->lock);
Packit 345191
}
Packit 345191
Packit 345191
struct prof_gctx_merge_iter_arg_s {
Packit 345191
	tsdn_t	*tsdn;
Packit 345191
	size_t	leak_ngctx;
Packit 345191
};
Packit 345191
Packit 345191
static prof_gctx_t *
Packit 345191
prof_gctx_merge_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *opaque) {
Packit 345191
	struct prof_gctx_merge_iter_arg_s *arg =
Packit 345191
	    (struct prof_gctx_merge_iter_arg_s *)opaque;
Packit 345191
Packit 345191
	malloc_mutex_lock(arg->tsdn, gctx->lock);
Packit 345191
	tctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_merge_iter,
Packit 345191
	    (void *)arg->tsdn);
Packit 345191
	if (gctx->cnt_summed.curobjs != 0) {
Packit 345191
		arg->leak_ngctx++;
Packit 345191
	}
Packit 345191
	malloc_mutex_unlock(arg->tsdn, gctx->lock);
Packit 345191
Packit 345191
	return NULL;
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_gctx_finish(tsd_t *tsd, prof_gctx_tree_t *gctxs) {
Packit 345191
	prof_tdata_t *tdata = prof_tdata_get(tsd, false);
Packit 345191
	prof_gctx_t *gctx;
Packit 345191
Packit 345191
	/*
Packit 345191
	 * Standard tree iteration won't work here, because as soon as we
Packit 345191
	 * decrement gctx->nlimbo and unlock gctx, another thread can
Packit 345191
	 * concurrently destroy it, which will corrupt the tree.  Therefore,
Packit 345191
	 * tear down the tree one node at a time during iteration.
Packit 345191
	 */
Packit 345191
	while ((gctx = gctx_tree_first(gctxs)) != NULL) {
Packit 345191
		gctx_tree_remove(gctxs, gctx);
Packit 345191
		malloc_mutex_lock(tsd_tsdn(tsd), gctx->lock);
Packit 345191
		{
Packit 345191
			prof_tctx_t *next;
Packit 345191
Packit 345191
			next = NULL;
Packit 345191
			do {
Packit 345191
				prof_tctx_t *to_destroy =
Packit 345191
				    tctx_tree_iter(&gctx->tctxs, next,
Packit 345191
				    prof_tctx_finish_iter,
Packit 345191
				    (void *)tsd_tsdn(tsd));
Packit 345191
				if (to_destroy != NULL) {
Packit 345191
					next = tctx_tree_next(&gctx->tctxs,
Packit 345191
					    to_destroy);
Packit 345191
					tctx_tree_remove(&gctx->tctxs,
Packit 345191
					    to_destroy);
Packit 345191
					idalloctm(tsd_tsdn(tsd), to_destroy,
Packit 345191
					    NULL, NULL, true, true);
Packit 345191
				} else {
Packit 345191
					next = NULL;
Packit 345191
				}
Packit 345191
			} while (next != NULL);
Packit 345191
		}
Packit 345191
		gctx->nlimbo--;
Packit 345191
		if (prof_gctx_should_destroy(gctx)) {
Packit 345191
			gctx->nlimbo++;
Packit 345191
			malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);
Packit 345191
			prof_gctx_try_destroy(tsd, tdata, gctx, tdata);
Packit 345191
		} else {
Packit 345191
			malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);
Packit 345191
		}
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
struct prof_tdata_merge_iter_arg_s {
Packit 345191
	tsdn_t		*tsdn;
Packit 345191
	prof_cnt_t	cnt_all;
Packit 345191
};
Packit 345191
Packit 345191
static prof_tdata_t *
Packit 345191
prof_tdata_merge_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,
Packit 345191
    void *opaque) {
Packit 345191
	struct prof_tdata_merge_iter_arg_s *arg =
Packit 345191
	    (struct prof_tdata_merge_iter_arg_s *)opaque;
Packit 345191
Packit 345191
	malloc_mutex_lock(arg->tsdn, tdata->lock);
Packit 345191
	if (!tdata->expired) {
Packit 345191
		size_t tabind;
Packit 345191
		union {
Packit 345191
			prof_tctx_t	*p;
Packit 345191
			void		*v;
Packit 345191
		} tctx;
Packit 345191
Packit 345191
		tdata->dumping = true;
Packit 345191
		memset(&tdata->cnt_summed, 0, sizeof(prof_cnt_t));
Packit 345191
		for (tabind = 0; !ckh_iter(&tdata->bt2tctx, &tabind, NULL,
Packit 345191
		    &tctx.v);) {
Packit 345191
			prof_tctx_merge_tdata(arg->tsdn, tctx.p, tdata);
Packit 345191
		}
Packit 345191
Packit 345191
		arg->cnt_all.curobjs += tdata->cnt_summed.curobjs;
Packit 345191
		arg->cnt_all.curbytes += tdata->cnt_summed.curbytes;
Packit 345191
		if (opt_prof_accum) {
Packit 345191
			arg->cnt_all.accumobjs += tdata->cnt_summed.accumobjs;
Packit 345191
			arg->cnt_all.accumbytes += tdata->cnt_summed.accumbytes;
Packit 345191
		}
Packit 345191
	} else {
Packit 345191
		tdata->dumping = false;
Packit 345191
	}
Packit 345191
	malloc_mutex_unlock(arg->tsdn, tdata->lock);
Packit 345191
Packit 345191
	return NULL;
Packit 345191
}
Packit 345191
Packit 345191
static prof_tdata_t *
Packit 345191
prof_tdata_dump_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,
Packit 345191
    void *arg) {
Packit 345191
	bool propagate_err = *(bool *)arg;
Packit 345191
Packit 345191
	if (!tdata->dumping) {
Packit 345191
		return NULL;
Packit 345191
	}
Packit 345191
Packit 345191
	if (prof_dump_printf(propagate_err,
Packit 345191
	    "  t%"FMTu64": %"FMTu64": %"FMTu64" [%"FMTu64": %"FMTu64"]%s%s\n",
Packit 345191
	    tdata->thr_uid, tdata->cnt_summed.curobjs,
Packit 345191
	    tdata->cnt_summed.curbytes, tdata->cnt_summed.accumobjs,
Packit 345191
	    tdata->cnt_summed.accumbytes,
Packit 345191
	    (tdata->thread_name != NULL) ? " " : "",
Packit 345191
	    (tdata->thread_name != NULL) ? tdata->thread_name : "")) {
Packit 345191
		return tdata;
Packit 345191
	}
Packit 345191
	return NULL;
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_dump_header_impl(tsdn_t *tsdn, bool propagate_err,
Packit 345191
    const prof_cnt_t *cnt_all) {
Packit 345191
	bool ret;
Packit 345191
Packit 345191
	if (prof_dump_printf(propagate_err,
Packit 345191
	    "heap_v2/%"FMTu64"\n"
Packit 345191
	    "  t*: %"FMTu64": %"FMTu64" [%"FMTu64": %"FMTu64"]\n",
Packit 345191
	    ((uint64_t)1U << lg_prof_sample), cnt_all->curobjs,
Packit 345191
	    cnt_all->curbytes, cnt_all->accumobjs, cnt_all->accumbytes)) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, &tdatas_mtx);
Packit 345191
	ret = (tdata_tree_iter(&tdatas, NULL, prof_tdata_dump_iter,
Packit 345191
	    (void *)&propagate_err) != NULL);
Packit 345191
	malloc_mutex_unlock(tsdn, &tdatas_mtx);
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
prof_dump_header_t *JET_MUTABLE prof_dump_header = prof_dump_header_impl;
Packit 345191
Packit 345191
static bool
Packit 345191
prof_dump_gctx(tsdn_t *tsdn, bool propagate_err, prof_gctx_t *gctx,
Packit 345191
    const prof_bt_t *bt, prof_gctx_tree_t *gctxs) {
Packit 345191
	bool ret;
Packit 345191
	unsigned i;
Packit 345191
	struct prof_tctx_dump_iter_arg_s prof_tctx_dump_iter_arg;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
	malloc_mutex_assert_owner(tsdn, gctx->lock);
Packit 345191
Packit 345191
	/* Avoid dumping such gctx's that have no useful data. */
Packit 345191
	if ((!opt_prof_accum && gctx->cnt_summed.curobjs == 0) ||
Packit 345191
	    (opt_prof_accum && gctx->cnt_summed.accumobjs == 0)) {
Packit 345191
		assert(gctx->cnt_summed.curobjs == 0);
Packit 345191
		assert(gctx->cnt_summed.curbytes == 0);
Packit 345191
		assert(gctx->cnt_summed.accumobjs == 0);
Packit 345191
		assert(gctx->cnt_summed.accumbytes == 0);
Packit 345191
		ret = false;
Packit 345191
		goto label_return;
Packit 345191
	}
Packit 345191
Packit 345191
	if (prof_dump_printf(propagate_err, "@")) {
Packit 345191
		ret = true;
Packit 345191
		goto label_return;
Packit 345191
	}
Packit 345191
	for (i = 0; i < bt->len; i++) {
Packit 345191
		if (prof_dump_printf(propagate_err, " %#"FMTxPTR,
Packit 345191
		    (uintptr_t)bt->vec[i])) {
Packit 345191
			ret = true;
Packit 345191
			goto label_return;
Packit 345191
		}
Packit 345191
	}
Packit 345191
Packit 345191
	if (prof_dump_printf(propagate_err,
Packit 345191
	    "\n"
Packit 345191
	    "  t*: %"FMTu64": %"FMTu64" [%"FMTu64": %"FMTu64"]\n",
Packit 345191
	    gctx->cnt_summed.curobjs, gctx->cnt_summed.curbytes,
Packit 345191
	    gctx->cnt_summed.accumobjs, gctx->cnt_summed.accumbytes)) {
Packit 345191
		ret = true;
Packit 345191
		goto label_return;
Packit 345191
	}
Packit 345191
Packit 345191
	prof_tctx_dump_iter_arg.tsdn = tsdn;
Packit 345191
	prof_tctx_dump_iter_arg.propagate_err = propagate_err;
Packit 345191
	if (tctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_dump_iter,
Packit 345191
	    (void *)&prof_tctx_dump_iter_arg) != NULL) {
Packit 345191
		ret = true;
Packit 345191
		goto label_return;
Packit 345191
	}
Packit 345191
Packit 345191
	ret = false;
Packit 345191
label_return:
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
Packit 345191
#ifndef _WIN32
Packit 345191
JEMALLOC_FORMAT_PRINTF(1, 2)
Packit 345191
static int
Packit 345191
prof_open_maps(const char *format, ...) {
Packit 345191
	int mfd;
Packit 345191
	va_list ap;
Packit 345191
	char filename[PATH_MAX + 1];
Packit 345191
Packit 345191
	va_start(ap, format);
Packit 345191
	malloc_vsnprintf(filename, sizeof(filename), format, ap);
Packit 345191
	va_end(ap);
Packit 345191
Packit 345191
#if defined(O_CLOEXEC)
Packit 345191
	mfd = open(filename, O_RDONLY | O_CLOEXEC);
Packit 345191
#else
Packit 345191
	mfd = open(filename, O_RDONLY);
Packit 345191
	if (mfd != -1) {
Packit 345191
		fcntl(mfd, F_SETFD, fcntl(mfd, F_GETFD) | FD_CLOEXEC);
Packit 345191
	}
Packit 345191
#endif
Packit 345191
Packit 345191
	return mfd;
Packit 345191
}
Packit 345191
#endif
Packit 345191
Packit 345191
static int
Packit 345191
prof_getpid(void) {
Packit 345191
#ifdef _WIN32
Packit 345191
	return GetCurrentProcessId();
Packit 345191
#else
Packit 345191
	return getpid();
Packit 345191
#endif
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_dump_maps(bool propagate_err) {
Packit 345191
	bool ret;
Packit 345191
	int mfd;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
#ifdef __FreeBSD__
Packit 345191
	mfd = prof_open_maps("/proc/curproc/map");
Packit 345191
#elif defined(_WIN32)
Packit 345191
	mfd = -1; // Not implemented
Packit 345191
#else
Packit 345191
	{
Packit 345191
		int pid = prof_getpid();
Packit 345191
Packit 345191
		mfd = prof_open_maps("/proc/%d/task/%d/maps", pid, pid);
Packit 345191
		if (mfd == -1) {
Packit 345191
			mfd = prof_open_maps("/proc/%d/maps", pid);
Packit 345191
		}
Packit 345191
	}
Packit 345191
#endif
Packit 345191
	if (mfd != -1) {
Packit 345191
		ssize_t nread;
Packit 345191
Packit 345191
		if (prof_dump_write(propagate_err, "\nMAPPED_LIBRARIES:\n") &&
Packit 345191
		    propagate_err) {
Packit 345191
			ret = true;
Packit 345191
			goto label_return;
Packit 345191
		}
Packit 345191
		nread = 0;
Packit 345191
		do {
Packit 345191
			prof_dump_buf_end += nread;
Packit 345191
			if (prof_dump_buf_end == PROF_DUMP_BUFSIZE) {
Packit 345191
				/* Make space in prof_dump_buf before read(). */
Packit 345191
				if (prof_dump_flush(propagate_err) &&
Packit 345191
				    propagate_err) {
Packit 345191
					ret = true;
Packit 345191
					goto label_return;
Packit 345191
				}
Packit 345191
			}
Packit 345191
			nread = malloc_read_fd(mfd,
Packit 345191
			    &prof_dump_buf[prof_dump_buf_end], PROF_DUMP_BUFSIZE
Packit 345191
			    - prof_dump_buf_end);
Packit 345191
		} while (nread > 0);
Packit 345191
	} else {
Packit 345191
		ret = true;
Packit 345191
		goto label_return;
Packit 345191
	}
Packit 345191
Packit 345191
	ret = false;
Packit 345191
label_return:
Packit 345191
	if (mfd != -1) {
Packit 345191
		close(mfd);
Packit 345191
	}
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
Packit 345191
/*
Packit 345191
 * See prof_sample_threshold_update() comment for why the body of this function
Packit 345191
 * is conditionally compiled.
Packit 345191
 */
Packit 345191
static void
Packit 345191
prof_leakcheck(const prof_cnt_t *cnt_all, size_t leak_ngctx,
Packit 345191
    const char *filename) {
Packit 345191
#ifdef JEMALLOC_PROF
Packit 345191
	/*
Packit 345191
	 * Scaling is equivalent AdjustSamples() in jeprof, but the result may
Packit 345191
	 * differ slightly from what jeprof reports, because here we scale the
Packit 345191
	 * summary values, whereas jeprof scales each context individually and
Packit 345191
	 * reports the sums of the scaled values.
Packit 345191
	 */
Packit 345191
	if (cnt_all->curbytes != 0) {
Packit 345191
		double sample_period = (double)((uint64_t)1 << lg_prof_sample);
Packit 345191
		double ratio = (((double)cnt_all->curbytes) /
Packit 345191
		    (double)cnt_all->curobjs) / sample_period;
Packit 345191
		double scale_factor = 1.0 / (1.0 - exp(-ratio));
Packit 345191
		uint64_t curbytes = (uint64_t)round(((double)cnt_all->curbytes)
Packit 345191
		    * scale_factor);
Packit 345191
		uint64_t curobjs = (uint64_t)round(((double)cnt_all->curobjs) *
Packit 345191
		    scale_factor);
Packit 345191
Packit 345191
		malloc_printf("<jemalloc>: Leak approximation summary: ~%"FMTu64
Packit 345191
		    " byte%s, ~%"FMTu64" object%s, >= %zu context%s\n",
Packit 345191
		    curbytes, (curbytes != 1) ? "s" : "", curobjs, (curobjs !=
Packit 345191
		    1) ? "s" : "", leak_ngctx, (leak_ngctx != 1) ? "s" : "");
Packit 345191
		malloc_printf(
Packit 345191
		    "<jemalloc>: Run jeprof on \"%s\" for leak detail\n",
Packit 345191
		    filename);
Packit 345191
	}
Packit 345191
#endif
Packit 345191
}
Packit 345191
Packit 345191
struct prof_gctx_dump_iter_arg_s {
Packit 345191
	tsdn_t	*tsdn;
Packit 345191
	bool	propagate_err;
Packit 345191
};
Packit 345191
Packit 345191
static prof_gctx_t *
Packit 345191
prof_gctx_dump_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *opaque) {
Packit 345191
	prof_gctx_t *ret;
Packit 345191
	struct prof_gctx_dump_iter_arg_s *arg =
Packit 345191
	    (struct prof_gctx_dump_iter_arg_s *)opaque;
Packit 345191
Packit 345191
	malloc_mutex_lock(arg->tsdn, gctx->lock);
Packit 345191
Packit 345191
	if (prof_dump_gctx(arg->tsdn, arg->propagate_err, gctx, &gctx->bt,
Packit 345191
	    gctxs)) {
Packit 345191
		ret = gctx;
Packit 345191
		goto label_return;
Packit 345191
	}
Packit 345191
Packit 345191
	ret = NULL;
Packit 345191
label_return:
Packit 345191
	malloc_mutex_unlock(arg->tsdn, gctx->lock);
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_dump_prep(tsd_t *tsd, prof_tdata_t *tdata,
Packit 345191
    struct prof_tdata_merge_iter_arg_s *prof_tdata_merge_iter_arg,
Packit 345191
    struct prof_gctx_merge_iter_arg_s *prof_gctx_merge_iter_arg,
Packit 345191
    prof_gctx_tree_t *gctxs) {
Packit 345191
	size_t tabind;
Packit 345191
	union {
Packit 345191
		prof_gctx_t	*p;
Packit 345191
		void		*v;
Packit 345191
	} gctx;
Packit 345191
Packit 345191
	prof_enter(tsd, tdata);
Packit 345191
Packit 345191
	/*
Packit 345191
	 * Put gctx's in limbo and clear their counters in preparation for
Packit 345191
	 * summing.
Packit 345191
	 */
Packit 345191
	gctx_tree_new(gctxs);
Packit 345191
	for (tabind = 0; !ckh_iter(&bt2gctx, &tabind, NULL, &gctx.v);) {
Packit 345191
		prof_dump_gctx_prep(tsd_tsdn(tsd), gctx.p, gctxs);
Packit 345191
	}
Packit 345191
Packit 345191
	/*
Packit 345191
	 * Iterate over tdatas, and for the non-expired ones snapshot their tctx
Packit 345191
	 * stats and merge them into the associated gctx's.
Packit 345191
	 */
Packit 345191
	prof_tdata_merge_iter_arg->tsdn = tsd_tsdn(tsd);
Packit 345191
	memset(&prof_tdata_merge_iter_arg->cnt_all, 0, sizeof(prof_cnt_t));
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);
Packit 345191
	tdata_tree_iter(&tdatas, NULL, prof_tdata_merge_iter,
Packit 345191
	    (void *)prof_tdata_merge_iter_arg);
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);
Packit 345191
Packit 345191
	/* Merge tctx stats into gctx's. */
Packit 345191
	prof_gctx_merge_iter_arg->tsdn = tsd_tsdn(tsd);
Packit 345191
	prof_gctx_merge_iter_arg->leak_ngctx = 0;
Packit 345191
	gctx_tree_iter(gctxs, NULL, prof_gctx_merge_iter,
Packit 345191
	    (void *)prof_gctx_merge_iter_arg);
Packit 345191
Packit 345191
	prof_leave(tsd, tdata);
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_dump_file(tsd_t *tsd, bool propagate_err, const char *filename,
Packit 345191
    bool leakcheck, prof_tdata_t *tdata,
Packit 345191
    struct prof_tdata_merge_iter_arg_s *prof_tdata_merge_iter_arg,
Packit 345191
    struct prof_gctx_merge_iter_arg_s *prof_gctx_merge_iter_arg,
Packit 345191
    struct prof_gctx_dump_iter_arg_s *prof_gctx_dump_iter_arg,
Packit 345191
    prof_gctx_tree_t *gctxs) {
Packit 345191
	/* Create dump file. */
Packit 345191
	if ((prof_dump_fd = prof_dump_open(propagate_err, filename)) == -1) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	/* Dump profile header. */
Packit 345191
	if (prof_dump_header(tsd_tsdn(tsd), propagate_err,
Packit 345191
	    &prof_tdata_merge_iter_arg->cnt_all)) {
Packit 345191
		goto label_write_error;
Packit 345191
	}
Packit 345191
Packit 345191
	/* Dump per gctx profile stats. */
Packit 345191
	prof_gctx_dump_iter_arg->tsdn = tsd_tsdn(tsd);
Packit 345191
	prof_gctx_dump_iter_arg->propagate_err = propagate_err;
Packit 345191
	if (gctx_tree_iter(gctxs, NULL, prof_gctx_dump_iter,
Packit 345191
	    (void *)prof_gctx_dump_iter_arg) != NULL) {
Packit 345191
		goto label_write_error;
Packit 345191
	}
Packit 345191
Packit 345191
	/* Dump /proc/<pid>/maps if possible. */
Packit 345191
	if (prof_dump_maps(propagate_err)) {
Packit 345191
		goto label_write_error;
Packit 345191
	}
Packit 345191
Packit 345191
	if (prof_dump_close(propagate_err)) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	return false;
Packit 345191
label_write_error:
Packit 345191
	prof_dump_close(propagate_err);
Packit 345191
	return true;
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_dump(tsd_t *tsd, bool propagate_err, const char *filename,
Packit 345191
    bool leakcheck) {
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(tsd_reentrancy_level_get(tsd) == 0);
Packit 345191
Packit 345191
	prof_tdata_t * tdata = prof_tdata_get(tsd, true);
Packit 345191
	if (tdata == NULL) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	pre_reentrancy(tsd, NULL);
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_mtx);
Packit 345191
Packit 345191
	prof_gctx_tree_t gctxs;
Packit 345191
	struct prof_tdata_merge_iter_arg_s prof_tdata_merge_iter_arg;
Packit 345191
	struct prof_gctx_merge_iter_arg_s prof_gctx_merge_iter_arg;
Packit 345191
	struct prof_gctx_dump_iter_arg_s prof_gctx_dump_iter_arg;
Packit 345191
	prof_dump_prep(tsd, tdata, &prof_tdata_merge_iter_arg,
Packit 345191
	    &prof_gctx_merge_iter_arg, &gctxs);
Packit 345191
	bool err = prof_dump_file(tsd, propagate_err, filename, leakcheck, tdata,
Packit 345191
	    &prof_tdata_merge_iter_arg, &prof_gctx_merge_iter_arg,
Packit 345191
	    &prof_gctx_dump_iter_arg, &gctxs);
Packit 345191
	prof_gctx_finish(tsd, &gctxs);
Packit 345191
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_mtx);
Packit 345191
	post_reentrancy(tsd);
Packit 345191
Packit 345191
	if (err) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	if (leakcheck) {
Packit 345191
		prof_leakcheck(&prof_tdata_merge_iter_arg.cnt_all,
Packit 345191
		    prof_gctx_merge_iter_arg.leak_ngctx, filename);
Packit 345191
	}
Packit 345191
	return false;
Packit 345191
}
Packit 345191
Packit 345191
#ifdef JEMALLOC_JET
Packit 345191
void
Packit 345191
prof_cnt_all(uint64_t *curobjs, uint64_t *curbytes, uint64_t *accumobjs,
Packit 345191
    uint64_t *accumbytes) {
Packit 345191
	tsd_t *tsd;
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
	struct prof_tdata_merge_iter_arg_s prof_tdata_merge_iter_arg;
Packit 345191
	struct prof_gctx_merge_iter_arg_s prof_gctx_merge_iter_arg;
Packit 345191
	prof_gctx_tree_t gctxs;
Packit 345191
Packit 345191
	tsd = tsd_fetch();
Packit 345191
	tdata = prof_tdata_get(tsd, false);
Packit 345191
	if (tdata == NULL) {
Packit 345191
		if (curobjs != NULL) {
Packit 345191
			*curobjs = 0;
Packit 345191
		}
Packit 345191
		if (curbytes != NULL) {
Packit 345191
			*curbytes = 0;
Packit 345191
		}
Packit 345191
		if (accumobjs != NULL) {
Packit 345191
			*accumobjs = 0;
Packit 345191
		}
Packit 345191
		if (accumbytes != NULL) {
Packit 345191
			*accumbytes = 0;
Packit 345191
		}
Packit 345191
		return;
Packit 345191
	}
Packit 345191
Packit 345191
	prof_dump_prep(tsd, tdata, &prof_tdata_merge_iter_arg,
Packit 345191
	    &prof_gctx_merge_iter_arg, &gctxs);
Packit 345191
	prof_gctx_finish(tsd, &gctxs);
Packit 345191
Packit 345191
	if (curobjs != NULL) {
Packit 345191
		*curobjs = prof_tdata_merge_iter_arg.cnt_all.curobjs;
Packit 345191
	}
Packit 345191
	if (curbytes != NULL) {
Packit 345191
		*curbytes = prof_tdata_merge_iter_arg.cnt_all.curbytes;
Packit 345191
	}
Packit 345191
	if (accumobjs != NULL) {
Packit 345191
		*accumobjs = prof_tdata_merge_iter_arg.cnt_all.accumobjs;
Packit 345191
	}
Packit 345191
	if (accumbytes != NULL) {
Packit 345191
		*accumbytes = prof_tdata_merge_iter_arg.cnt_all.accumbytes;
Packit 345191
	}
Packit 345191
}
Packit 345191
#endif
Packit 345191
Packit 345191
#define DUMP_FILENAME_BUFSIZE	(PATH_MAX + 1)
Packit 345191
#define VSEQ_INVALID		UINT64_C(0xffffffffffffffff)
Packit 345191
static void
Packit 345191
prof_dump_filename(char *filename, char v, uint64_t vseq) {
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	if (vseq != VSEQ_INVALID) {
Packit 345191
	        /* "<prefix>.<pid>.<seq>.v<vseq>.heap" */
Packit 345191
		malloc_snprintf(filename, DUMP_FILENAME_BUFSIZE,
Packit 345191
		    "%s.%d.%"FMTu64".%c%"FMTu64".heap",
Packit 345191
		    opt_prof_prefix, prof_getpid(), prof_dump_seq, v, vseq);
Packit 345191
	} else {
Packit 345191
	        /* "<prefix>.<pid>.<seq>.<v>.heap" */
Packit 345191
		malloc_snprintf(filename, DUMP_FILENAME_BUFSIZE,
Packit 345191
		    "%s.%d.%"FMTu64".%c.heap",
Packit 345191
		    opt_prof_prefix, prof_getpid(), prof_dump_seq, v);
Packit 345191
	}
Packit 345191
	prof_dump_seq++;
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_fdump(void) {
Packit 345191
	tsd_t *tsd;
Packit 345191
	char filename[DUMP_FILENAME_BUFSIZE];
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(opt_prof_final);
Packit 345191
	assert(opt_prof_prefix[0] != '\0');
Packit 345191
Packit 345191
	if (!prof_booted) {
Packit 345191
		return;
Packit 345191
	}
Packit 345191
	tsd = tsd_fetch();
Packit 345191
	assert(tsd_reentrancy_level_get(tsd) == 0);
Packit 345191
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx);
Packit 345191
	prof_dump_filename(filename, 'f', VSEQ_INVALID);
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx);
Packit 345191
	prof_dump(tsd, false, filename, opt_prof_leak);
Packit 345191
}
Packit 345191
Packit 345191
bool
Packit 345191
prof_accum_init(tsdn_t *tsdn, prof_accum_t *prof_accum) {
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
#ifndef JEMALLOC_ATOMIC_U64
Packit 345191
	if (malloc_mutex_init(&prof_accum->mtx, "prof_accum",
Packit 345191
	    WITNESS_RANK_PROF_ACCUM, malloc_mutex_rank_exclusive)) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
	prof_accum->accumbytes = 0;
Packit 345191
#else
Packit 345191
	atomic_store_u64(&prof_accum->accumbytes, 0, ATOMIC_RELAXED);
Packit 345191
#endif
Packit 345191
	return false;
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_idump(tsdn_t *tsdn) {
Packit 345191
	tsd_t *tsd;
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	if (!prof_booted || tsdn_null(tsdn) || !prof_active_get_unlocked()) {
Packit 345191
		return;
Packit 345191
	}
Packit 345191
	tsd = tsdn_tsd(tsdn);
Packit 345191
	if (tsd_reentrancy_level_get(tsd) > 0) {
Packit 345191
		return;
Packit 345191
	}
Packit 345191
Packit 345191
	tdata = prof_tdata_get(tsd, false);
Packit 345191
	if (tdata == NULL) {
Packit 345191
		return;
Packit 345191
	}
Packit 345191
	if (tdata->enq) {
Packit 345191
		tdata->enq_idump = true;
Packit 345191
		return;
Packit 345191
	}
Packit 345191
Packit 345191
	if (opt_prof_prefix[0] != '\0') {
Packit 345191
		char filename[PATH_MAX + 1];
Packit 345191
		malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx);
Packit 345191
		prof_dump_filename(filename, 'i', prof_dump_iseq);
Packit 345191
		prof_dump_iseq++;
Packit 345191
		malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx);
Packit 345191
		prof_dump(tsd, false, filename, false);
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
bool
Packit 345191
prof_mdump(tsd_t *tsd, const char *filename) {
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(tsd_reentrancy_level_get(tsd) == 0);
Packit 345191
Packit 345191
	if (!opt_prof || !prof_booted) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
	char filename_buf[DUMP_FILENAME_BUFSIZE];
Packit 345191
	if (filename == NULL) {
Packit 345191
		/* No filename specified, so automatically generate one. */
Packit 345191
		if (opt_prof_prefix[0] == '\0') {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
		malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx);
Packit 345191
		prof_dump_filename(filename_buf, 'm', prof_dump_mseq);
Packit 345191
		prof_dump_mseq++;
Packit 345191
		malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx);
Packit 345191
		filename = filename_buf;
Packit 345191
	}
Packit 345191
	return prof_dump(tsd, true, filename, false);
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_gdump(tsdn_t *tsdn) {
Packit 345191
	tsd_t *tsd;
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	if (!prof_booted || tsdn_null(tsdn) || !prof_active_get_unlocked()) {
Packit 345191
		return;
Packit 345191
	}
Packit 345191
	tsd = tsdn_tsd(tsdn);
Packit 345191
	if (tsd_reentrancy_level_get(tsd) > 0) {
Packit 345191
		return;
Packit 345191
	}
Packit 345191
Packit 345191
	tdata = prof_tdata_get(tsd, false);
Packit 345191
	if (tdata == NULL) {
Packit 345191
		return;
Packit 345191
	}
Packit 345191
	if (tdata->enq) {
Packit 345191
		tdata->enq_gdump = true;
Packit 345191
		return;
Packit 345191
	}
Packit 345191
Packit 345191
	if (opt_prof_prefix[0] != '\0') {
Packit 345191
		char filename[DUMP_FILENAME_BUFSIZE];
Packit 345191
		malloc_mutex_lock(tsdn, &prof_dump_seq_mtx);
Packit 345191
		prof_dump_filename(filename, 'u', prof_dump_useq);
Packit 345191
		prof_dump_useq++;
Packit 345191
		malloc_mutex_unlock(tsdn, &prof_dump_seq_mtx);
Packit 345191
		prof_dump(tsd, false, filename, false);
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_bt_hash(const void *key, size_t r_hash[2]) {
Packit 345191
	prof_bt_t *bt = (prof_bt_t *)key;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	hash(bt->vec, bt->len * sizeof(void *), 0x94122f33U, r_hash);
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_bt_keycomp(const void *k1, const void *k2) {
Packit 345191
	const prof_bt_t *bt1 = (prof_bt_t *)k1;
Packit 345191
	const prof_bt_t *bt2 = (prof_bt_t *)k2;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	if (bt1->len != bt2->len) {
Packit 345191
		return false;
Packit 345191
	}
Packit 345191
	return (memcmp(bt1->vec, bt2->vec, bt1->len * sizeof(void *)) == 0);
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_bt_node_hash(const void *key, size_t r_hash[2]) {
Packit 345191
	const prof_bt_node_t *bt_node = (prof_bt_node_t *)key;
Packit 345191
	prof_bt_hash((void *)(&bt_node->bt), r_hash);
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_bt_node_keycomp(const void *k1, const void *k2) {
Packit 345191
	const prof_bt_node_t *bt_node1 = (prof_bt_node_t *)k1;
Packit 345191
	const prof_bt_node_t *bt_node2 = (prof_bt_node_t *)k2;
Packit 345191
	return prof_bt_keycomp((void *)(&bt_node1->bt),
Packit 345191
	    (void *)(&bt_node2->bt));
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_thr_node_hash(const void *key, size_t r_hash[2]) {
Packit 345191
	const prof_thr_node_t *thr_node = (prof_thr_node_t *)key;
Packit 345191
	hash(&thr_node->thr_uid, sizeof(uint64_t), 0x94122f35U, r_hash);
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_thr_node_keycomp(const void *k1, const void *k2) {
Packit 345191
	const prof_thr_node_t *thr_node1 = (prof_thr_node_t *)k1;
Packit 345191
	const prof_thr_node_t *thr_node2 = (prof_thr_node_t *)k2;
Packit 345191
	return thr_node1->thr_uid == thr_node2->thr_uid;
Packit 345191
}
Packit 345191
Packit 345191
static uint64_t
Packit 345191
prof_thr_uid_alloc(tsdn_t *tsdn) {
Packit 345191
	uint64_t thr_uid;
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, &next_thr_uid_mtx);
Packit 345191
	thr_uid = next_thr_uid;
Packit 345191
	next_thr_uid++;
Packit 345191
	malloc_mutex_unlock(tsdn, &next_thr_uid_mtx);
Packit 345191
Packit 345191
	return thr_uid;
Packit 345191
}
Packit 345191
Packit 345191
static prof_tdata_t *
Packit 345191
prof_tdata_init_impl(tsd_t *tsd, uint64_t thr_uid, uint64_t thr_discrim,
Packit 345191
    char *thread_name, bool active) {
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	/* Initialize an empty cache for this thread. */
Packit 345191
	tdata = (prof_tdata_t *)iallocztm(tsd_tsdn(tsd), sizeof(prof_tdata_t),
Packit 345191
	    sz_size2index(sizeof(prof_tdata_t)), false, NULL, true,
Packit 345191
	    arena_get(TSDN_NULL, 0, true), true);
Packit 345191
	if (tdata == NULL) {
Packit 345191
		return NULL;
Packit 345191
	}
Packit 345191
Packit 345191
	tdata->lock = prof_tdata_mutex_choose(thr_uid);
Packit 345191
	tdata->thr_uid = thr_uid;
Packit 345191
	tdata->thr_discrim = thr_discrim;
Packit 345191
	tdata->thread_name = thread_name;
Packit 345191
	tdata->attached = true;
Packit 345191
	tdata->expired = false;
Packit 345191
	tdata->tctx_uid_next = 0;
Packit 345191
Packit 345191
	if (ckh_new(tsd, &tdata->bt2tctx, PROF_CKH_MINITEMS, prof_bt_hash,
Packit 345191
	    prof_bt_keycomp)) {
Packit 345191
		idalloctm(tsd_tsdn(tsd), tdata, NULL, NULL, true, true);
Packit 345191
		return NULL;
Packit 345191
	}
Packit 345191
Packit 345191
	tdata->prng_state = (uint64_t)(uintptr_t)tdata;
Packit 345191
	prof_sample_threshold_update(tdata);
Packit 345191
Packit 345191
	tdata->enq = false;
Packit 345191
	tdata->enq_idump = false;
Packit 345191
	tdata->enq_gdump = false;
Packit 345191
Packit 345191
	tdata->dumping = false;
Packit 345191
	tdata->active = active;
Packit 345191
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);
Packit 345191
	tdata_tree_insert(&tdatas, tdata);
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);
Packit 345191
Packit 345191
	return tdata;
Packit 345191
}
Packit 345191
Packit 345191
prof_tdata_t *
Packit 345191
prof_tdata_init(tsd_t *tsd) {
Packit 345191
	return prof_tdata_init_impl(tsd, prof_thr_uid_alloc(tsd_tsdn(tsd)), 0,
Packit 345191
	    NULL, prof_thread_active_init_get(tsd_tsdn(tsd)));
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_tdata_should_destroy_unlocked(prof_tdata_t *tdata, bool even_if_attached) {
Packit 345191
	if (tdata->attached && !even_if_attached) {
Packit 345191
		return false;
Packit 345191
	}
Packit 345191
	if (ckh_count(&tdata->bt2tctx) != 0) {
Packit 345191
		return false;
Packit 345191
	}
Packit 345191
	return true;
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_tdata_should_destroy(tsdn_t *tsdn, prof_tdata_t *tdata,
Packit 345191
    bool even_if_attached) {
Packit 345191
	malloc_mutex_assert_owner(tsdn, tdata->lock);
Packit 345191
Packit 345191
	return prof_tdata_should_destroy_unlocked(tdata, even_if_attached);
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_tdata_destroy_locked(tsd_t *tsd, prof_tdata_t *tdata,
Packit 345191
    bool even_if_attached) {
Packit 345191
	malloc_mutex_assert_owner(tsd_tsdn(tsd), &tdatas_mtx);
Packit 345191
Packit 345191
	tdata_tree_remove(&tdatas, tdata);
Packit 345191
Packit 345191
	assert(prof_tdata_should_destroy_unlocked(tdata, even_if_attached));
Packit 345191
Packit 345191
	if (tdata->thread_name != NULL) {
Packit 345191
		idalloctm(tsd_tsdn(tsd), tdata->thread_name, NULL, NULL, true,
Packit 345191
		    true);
Packit 345191
	}
Packit 345191
	ckh_delete(tsd, &tdata->bt2tctx);
Packit 345191
	idalloctm(tsd_tsdn(tsd), tdata, NULL, NULL, true, true);
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata, bool even_if_attached) {
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);
Packit 345191
	prof_tdata_destroy_locked(tsd, tdata, even_if_attached);
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_tdata_detach(tsd_t *tsd, prof_tdata_t *tdata) {
Packit 345191
	bool destroy_tdata;
Packit 345191
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), tdata->lock);
Packit 345191
	if (tdata->attached) {
Packit 345191
		destroy_tdata = prof_tdata_should_destroy(tsd_tsdn(tsd), tdata,
Packit 345191
		    true);
Packit 345191
		/*
Packit 345191
		 * Only detach if !destroy_tdata, because detaching would allow
Packit 345191
		 * another thread to win the race to destroy tdata.
Packit 345191
		 */
Packit 345191
		if (!destroy_tdata) {
Packit 345191
			tdata->attached = false;
Packit 345191
		}
Packit 345191
		tsd_prof_tdata_set(tsd, NULL);
Packit 345191
	} else {
Packit 345191
		destroy_tdata = false;
Packit 345191
	}
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);
Packit 345191
	if (destroy_tdata) {
Packit 345191
		prof_tdata_destroy(tsd, tdata, true);
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
prof_tdata_t *
Packit 345191
prof_tdata_reinit(tsd_t *tsd, prof_tdata_t *tdata) {
Packit 345191
	uint64_t thr_uid = tdata->thr_uid;
Packit 345191
	uint64_t thr_discrim = tdata->thr_discrim + 1;
Packit 345191
	char *thread_name = (tdata->thread_name != NULL) ?
Packit 345191
	    prof_thread_name_alloc(tsd_tsdn(tsd), tdata->thread_name) : NULL;
Packit 345191
	bool active = tdata->active;
Packit 345191
Packit 345191
	prof_tdata_detach(tsd, tdata);
Packit 345191
	return prof_tdata_init_impl(tsd, thr_uid, thr_discrim, thread_name,
Packit 345191
	    active);
Packit 345191
}
Packit 345191
Packit 345191
static bool
Packit 345191
prof_tdata_expire(tsdn_t *tsdn, prof_tdata_t *tdata) {
Packit 345191
	bool destroy_tdata;
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, tdata->lock);
Packit 345191
	if (!tdata->expired) {
Packit 345191
		tdata->expired = true;
Packit 345191
		destroy_tdata = tdata->attached ? false :
Packit 345191
		    prof_tdata_should_destroy(tsdn, tdata, false);
Packit 345191
	} else {
Packit 345191
		destroy_tdata = false;
Packit 345191
	}
Packit 345191
	malloc_mutex_unlock(tsdn, tdata->lock);
Packit 345191
Packit 345191
	return destroy_tdata;
Packit 345191
}
Packit 345191
Packit 345191
static prof_tdata_t *
Packit 345191
prof_tdata_reset_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,
Packit 345191
    void *arg) {
Packit 345191
	tsdn_t *tsdn = (tsdn_t *)arg;
Packit 345191
Packit 345191
	return (prof_tdata_expire(tsdn, tdata) ? tdata : NULL);
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_reset(tsd_t *tsd, size_t lg_sample) {
Packit 345191
	prof_tdata_t *next;
Packit 345191
Packit 345191
	assert(lg_sample < (sizeof(uint64_t) << 3));
Packit 345191
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_mtx);
Packit 345191
	malloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);
Packit 345191
Packit 345191
	lg_prof_sample = lg_sample;
Packit 345191
Packit 345191
	next = NULL;
Packit 345191
	do {
Packit 345191
		prof_tdata_t *to_destroy = tdata_tree_iter(&tdatas, next,
Packit 345191
		    prof_tdata_reset_iter, (void *)tsd);
Packit 345191
		if (to_destroy != NULL) {
Packit 345191
			next = tdata_tree_next(&tdatas, to_destroy);
Packit 345191
			prof_tdata_destroy_locked(tsd, to_destroy, false);
Packit 345191
		} else {
Packit 345191
			next = NULL;
Packit 345191
		}
Packit 345191
	} while (next != NULL);
Packit 345191
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);
Packit 345191
	malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_mtx);
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_tdata_cleanup(tsd_t *tsd) {
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
Packit 345191
	if (!config_prof) {
Packit 345191
		return;
Packit 345191
	}
Packit 345191
Packit 345191
	tdata = tsd_prof_tdata_get(tsd);
Packit 345191
	if (tdata != NULL) {
Packit 345191
		prof_tdata_detach(tsd, tdata);
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
bool
Packit 345191
prof_active_get(tsdn_t *tsdn) {
Packit 345191
	bool prof_active_current;
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, &prof_active_mtx);
Packit 345191
	prof_active_current = prof_active;
Packit 345191
	malloc_mutex_unlock(tsdn, &prof_active_mtx);
Packit 345191
	return prof_active_current;
Packit 345191
}
Packit 345191
Packit 345191
bool
Packit 345191
prof_active_set(tsdn_t *tsdn, bool active) {
Packit 345191
	bool prof_active_old;
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, &prof_active_mtx);
Packit 345191
	prof_active_old = prof_active;
Packit 345191
	prof_active = active;
Packit 345191
	malloc_mutex_unlock(tsdn, &prof_active_mtx);
Packit 345191
	return prof_active_old;
Packit 345191
}
Packit 345191
Packit 345191
#ifdef JEMALLOC_JET
Packit 345191
size_t
Packit 345191
prof_log_bt_count(void) {
Packit 345191
	size_t cnt = 0;
Packit 345191
	prof_bt_node_t *node = log_bt_first;
Packit 345191
	while (node != NULL) {
Packit 345191
		cnt++;
Packit 345191
		node = node->next;
Packit 345191
	}
Packit 345191
	return cnt;
Packit 345191
}
Packit 345191
Packit 345191
size_t
Packit 345191
prof_log_alloc_count(void) {
Packit 345191
	size_t cnt = 0;
Packit 345191
	prof_alloc_node_t *node = log_alloc_first;
Packit 345191
	while (node != NULL) {
Packit 345191
		cnt++;
Packit 345191
		node = node->next;
Packit 345191
	}
Packit 345191
	return cnt;
Packit 345191
}
Packit 345191
Packit 345191
size_t
Packit 345191
prof_log_thr_count(void) {
Packit 345191
	size_t cnt = 0;
Packit 345191
	prof_thr_node_t *node = log_thr_first;
Packit 345191
	while (node != NULL) {
Packit 345191
		cnt++;
Packit 345191
		node = node->next;
Packit 345191
	}
Packit 345191
	return cnt;
Packit 345191
}
Packit 345191
Packit 345191
bool
Packit 345191
prof_log_is_logging(void) {
Packit 345191
	return prof_logging_state == prof_logging_state_started;
Packit 345191
}
Packit 345191
Packit 345191
bool
Packit 345191
prof_log_rep_check(void) {
Packit 345191
	if (prof_logging_state == prof_logging_state_stopped
Packit 345191
	    && log_tables_initialized) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	if (log_bt_last != NULL && log_bt_last->next != NULL) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
	if (log_thr_last != NULL && log_thr_last->next != NULL) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
	if (log_alloc_last != NULL && log_alloc_last->next != NULL) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	size_t bt_count = prof_log_bt_count();
Packit 345191
	size_t thr_count = prof_log_thr_count();
Packit 345191
	size_t alloc_count = prof_log_alloc_count();
Packit 345191
Packit 345191
Packit 345191
	if (prof_logging_state == prof_logging_state_stopped) {
Packit 345191
		if (bt_count != 0 || thr_count != 0 || alloc_count || 0) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
	}
Packit 345191
Packit 345191
	prof_alloc_node_t *node = log_alloc_first;
Packit 345191
	while (node != NULL) {
Packit 345191
		if (node->alloc_bt_ind >= bt_count) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
		if (node->free_bt_ind >= bt_count) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
		if (node->alloc_thr_ind >= thr_count) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
		if (node->free_thr_ind >= thr_count) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
		if (node->alloc_time_ns > node->free_time_ns) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
		node = node->next;
Packit 345191
	}
Packit 345191
Packit 345191
	return false;
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_log_dummy_set(bool new_value) {
Packit 345191
	prof_log_dummy = new_value;
Packit 345191
}
Packit 345191
#endif
Packit 345191
Packit 345191
bool
Packit 345191
prof_log_start(tsdn_t *tsdn, const char *filename) {
Packit 345191
	if (!opt_prof || !prof_booted) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	bool ret = false;
Packit 345191
	size_t buf_size = PATH_MAX + 1;
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, &log_mtx);
Packit 345191
Packit 345191
	if (prof_logging_state != prof_logging_state_stopped) {
Packit 345191
		ret = true;
Packit 345191
	} else if (filename == NULL) {
Packit 345191
		/* Make default name. */
Packit 345191
		malloc_snprintf(log_filename, buf_size, "%s.%d.%"FMTu64".json",
Packit 345191
		    opt_prof_prefix, prof_getpid(), log_seq);
Packit 345191
		log_seq++;
Packit 345191
		prof_logging_state = prof_logging_state_started;
Packit 345191
	} else if (strlen(filename) >= buf_size) {
Packit 345191
		ret = true;
Packit 345191
	} else {
Packit 345191
		strcpy(log_filename, filename);
Packit 345191
		prof_logging_state = prof_logging_state_started;
Packit 345191
	}
Packit 345191
Packit 345191
	if (!ret) {
Packit 345191
		nstime_update(&log_start_timestamp);
Packit 345191
	}
Packit 345191
Packit 345191
	malloc_mutex_unlock(tsdn, &log_mtx);
Packit 345191
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
Packit 345191
/* Used as an atexit function to stop logging on exit. */
Packit 345191
static void
Packit 345191
prof_log_stop_final(void) {
Packit 345191
	tsd_t *tsd = tsd_fetch();
Packit 345191
	prof_log_stop(tsd_tsdn(tsd));
Packit 345191
}
Packit 345191
Packit 345191
struct prof_emitter_cb_arg_s {
Packit 345191
	int fd;
Packit 345191
	ssize_t ret;
Packit 345191
};
Packit 345191
Packit 345191
static void
Packit 345191
prof_emitter_write_cb(void *opaque, const char *to_write) {
Packit 345191
	struct prof_emitter_cb_arg_s *arg =
Packit 345191
	    (struct prof_emitter_cb_arg_s *)opaque;
Packit 345191
	size_t bytes = strlen(to_write);
Packit 345191
#ifdef JEMALLOC_JET
Packit 345191
	if (prof_log_dummy) {
Packit 345191
		return;
Packit 345191
	}
Packit 345191
#endif
Packit 345191
	arg->ret = write(arg->fd, (void *)to_write, bytes);
Packit 345191
}
Packit 345191
Packit 345191
/*
Packit 345191
 * prof_log_emit_{...} goes through the appropriate linked list, emitting each
Packit 345191
 * node to the json and deallocating it.
Packit 345191
 */
Packit 345191
static void
Packit 345191
prof_log_emit_threads(tsd_t *tsd, emitter_t *emitter) {
Packit 345191
	emitter_json_array_kv_begin(emitter, "threads");
Packit 345191
	prof_thr_node_t *thr_node = log_thr_first;
Packit 345191
	prof_thr_node_t *thr_old_node;
Packit 345191
	while (thr_node != NULL) {
Packit 345191
		emitter_json_object_begin(emitter);
Packit 345191
Packit 345191
		emitter_json_kv(emitter, "thr_uid", emitter_type_uint64,
Packit 345191
		    &thr_node->thr_uid);
Packit 345191
Packit 345191
		char *thr_name = thr_node->name;
Packit 345191
Packit 345191
		emitter_json_kv(emitter, "thr_name", emitter_type_string,
Packit 345191
		    &thr_name);
Packit 345191
Packit 345191
		emitter_json_object_end(emitter);
Packit 345191
		thr_old_node = thr_node;
Packit 345191
		thr_node = thr_node->next;
Packit 345191
		idalloc(tsd, thr_old_node);
Packit 345191
	}
Packit 345191
	emitter_json_array_end(emitter);
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_log_emit_traces(tsd_t *tsd, emitter_t *emitter) {
Packit 345191
	emitter_json_array_kv_begin(emitter, "stack_traces");
Packit 345191
	prof_bt_node_t *bt_node = log_bt_first;
Packit 345191
	prof_bt_node_t *bt_old_node;
Packit 345191
	/*
Packit 345191
	 * Calculate how many hex digits we need: twice number of bytes, two for
Packit 345191
	 * "0x", and then one more for terminating '\0'.
Packit 345191
	 */
Packit 345191
	char buf[2 * sizeof(intptr_t) + 3];
Packit 345191
	size_t buf_sz = sizeof(buf);
Packit 345191
	while (bt_node != NULL) {
Packit 345191
		emitter_json_array_begin(emitter);
Packit 345191
		size_t i;
Packit 345191
		for (i = 0; i < bt_node->bt.len; i++) {
Packit 345191
			malloc_snprintf(buf, buf_sz, "%p", bt_node->bt.vec[i]);
Packit 345191
			char *trace_str = buf;
Packit 345191
			emitter_json_value(emitter, emitter_type_string,
Packit 345191
			    &trace_str);
Packit 345191
		}
Packit 345191
		emitter_json_array_end(emitter);
Packit 345191
Packit 345191
		bt_old_node = bt_node;
Packit 345191
		bt_node = bt_node->next;
Packit 345191
		idalloc(tsd, bt_old_node);
Packit 345191
	}
Packit 345191
	emitter_json_array_end(emitter);
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_log_emit_allocs(tsd_t *tsd, emitter_t *emitter) {
Packit 345191
	emitter_json_array_kv_begin(emitter, "allocations");
Packit 345191
	prof_alloc_node_t *alloc_node = log_alloc_first;
Packit 345191
	prof_alloc_node_t *alloc_old_node;
Packit 345191
	while (alloc_node != NULL) {
Packit 345191
		emitter_json_object_begin(emitter);
Packit 345191
Packit 345191
		emitter_json_kv(emitter, "alloc_thread", emitter_type_size,
Packit 345191
		    &alloc_node->alloc_thr_ind);
Packit 345191
Packit 345191
		emitter_json_kv(emitter, "free_thread", emitter_type_size,
Packit 345191
		    &alloc_node->free_thr_ind);
Packit 345191
Packit 345191
		emitter_json_kv(emitter, "alloc_trace", emitter_type_size,
Packit 345191
		    &alloc_node->alloc_bt_ind);
Packit 345191
Packit 345191
		emitter_json_kv(emitter, "free_trace", emitter_type_size,
Packit 345191
		    &alloc_node->free_bt_ind);
Packit 345191
Packit 345191
		emitter_json_kv(emitter, "alloc_timestamp",
Packit 345191
		    emitter_type_uint64, &alloc_node->alloc_time_ns);
Packit 345191
Packit 345191
		emitter_json_kv(emitter, "free_timestamp", emitter_type_uint64,
Packit 345191
		    &alloc_node->free_time_ns);
Packit 345191
Packit 345191
		emitter_json_kv(emitter, "usize", emitter_type_uint64,
Packit 345191
		    &alloc_node->usize);
Packit 345191
Packit 345191
		emitter_json_object_end(emitter);
Packit 345191
Packit 345191
		alloc_old_node = alloc_node;
Packit 345191
		alloc_node = alloc_node->next;
Packit 345191
		idalloc(tsd, alloc_old_node);
Packit 345191
	}
Packit 345191
	emitter_json_array_end(emitter);
Packit 345191
}
Packit 345191
Packit 345191
static void
Packit 345191
prof_log_emit_metadata(emitter_t *emitter) {
Packit 345191
	emitter_json_object_kv_begin(emitter, "info");
Packit 345191
Packit 345191
	nstime_t now = NSTIME_ZERO_INITIALIZER;
Packit 345191
Packit 345191
	nstime_update(&now;;
Packit 345191
	uint64_t ns = nstime_ns(&now) - nstime_ns(&log_start_timestamp);
Packit 345191
	emitter_json_kv(emitter, "duration", emitter_type_uint64, &ns);
Packit 345191
Packit 345191
	char *vers = JEMALLOC_VERSION;
Packit 345191
	emitter_json_kv(emitter, "version",
Packit 345191
	    emitter_type_string, &vers;;
Packit 345191
Packit 345191
	emitter_json_kv(emitter, "lg_sample_rate",
Packit 345191
	    emitter_type_int, &lg_prof_sample);
Packit 345191
Packit 345191
	int pid = prof_getpid();
Packit 345191
	emitter_json_kv(emitter, "pid", emitter_type_int, &pid;;
Packit 345191
Packit 345191
	emitter_json_object_end(emitter);
Packit 345191
}
Packit 345191
Packit 345191
Packit 345191
bool
Packit 345191
prof_log_stop(tsdn_t *tsdn) {
Packit 345191
	if (!opt_prof || !prof_booted) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	tsd_t *tsd = tsdn_tsd(tsdn);
Packit 345191
	malloc_mutex_lock(tsdn, &log_mtx);
Packit 345191
Packit 345191
	if (prof_logging_state != prof_logging_state_started) {
Packit 345191
		malloc_mutex_unlock(tsdn, &log_mtx);
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	/*
Packit 345191
	 * Set the state to dumping. We'll set it to stopped when we're done.
Packit 345191
	 * Since other threads won't be able to start/stop/log when the state is
Packit 345191
	 * dumping, we don't have to hold the lock during the whole method.
Packit 345191
	 */
Packit 345191
	prof_logging_state = prof_logging_state_dumping;
Packit 345191
	malloc_mutex_unlock(tsdn, &log_mtx);
Packit 345191
Packit 345191
Packit 345191
	emitter_t emitter;
Packit 345191
Packit 345191
	/* Create a file. */
Packit 345191
Packit 345191
	int fd;
Packit 345191
#ifdef JEMALLOC_JET
Packit 345191
	if (prof_log_dummy) {
Packit 345191
		fd = 0;
Packit 345191
	} else {
Packit 345191
		fd = creat(log_filename, 0644);
Packit 345191
	}
Packit 345191
#else
Packit 345191
	fd = creat(log_filename, 0644);
Packit 345191
#endif
Packit 345191
Packit 345191
	if (fd == -1) {
Packit 345191
		malloc_printf("<jemalloc>: creat() for log file \"%s\" "
Packit 345191
			      " failed with %d\n", log_filename, errno);
Packit 345191
		if (opt_abort) {
Packit 345191
			abort();
Packit 345191
		}
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	/* Emit to json. */
Packit 345191
	struct prof_emitter_cb_arg_s arg;
Packit 345191
	arg.fd = fd;
Packit 345191
	emitter_init(&emitter, emitter_output_json, &prof_emitter_write_cb,
Packit 345191
	    (void *)(&arg));
Packit 345191
Packit 345191
	emitter_begin(&emitter);
Packit 345191
	prof_log_emit_metadata(&emitter);
Packit 345191
	prof_log_emit_threads(tsd, &emitter);
Packit 345191
	prof_log_emit_traces(tsd, &emitter);
Packit 345191
	prof_log_emit_allocs(tsd, &emitter);
Packit 345191
	emitter_end(&emitter);
Packit 345191
Packit 345191
	/* Reset global state. */
Packit 345191
	if (log_tables_initialized) {
Packit 345191
		ckh_delete(tsd, &log_bt_node_set);
Packit 345191
		ckh_delete(tsd, &log_thr_node_set);
Packit 345191
	}
Packit 345191
	log_tables_initialized = false;
Packit 345191
	log_bt_index = 0;
Packit 345191
	log_thr_index = 0;
Packit 345191
	log_bt_first = NULL;
Packit 345191
	log_bt_last = NULL;
Packit 345191
	log_thr_first = NULL;
Packit 345191
	log_thr_last = NULL;
Packit 345191
	log_alloc_first = NULL;
Packit 345191
	log_alloc_last = NULL;
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, &log_mtx);
Packit 345191
	prof_logging_state = prof_logging_state_stopped;
Packit 345191
	malloc_mutex_unlock(tsdn, &log_mtx);
Packit 345191
Packit 345191
#ifdef JEMALLOC_JET
Packit 345191
	if (prof_log_dummy) {
Packit 345191
		return false;
Packit 345191
	}
Packit 345191
#endif
Packit 345191
	return close(fd);
Packit 345191
}
Packit 345191
Packit 345191
const char *
Packit 345191
prof_thread_name_get(tsd_t *tsd) {
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
Packit 345191
	tdata = prof_tdata_get(tsd, true);
Packit 345191
	if (tdata == NULL) {
Packit 345191
		return "";
Packit 345191
	}
Packit 345191
	return (tdata->thread_name != NULL ? tdata->thread_name : "");
Packit 345191
}
Packit 345191
Packit 345191
static char *
Packit 345191
prof_thread_name_alloc(tsdn_t *tsdn, const char *thread_name) {
Packit 345191
	char *ret;
Packit 345191
	size_t size;
Packit 345191
Packit 345191
	if (thread_name == NULL) {
Packit 345191
		return NULL;
Packit 345191
	}
Packit 345191
Packit 345191
	size = strlen(thread_name) + 1;
Packit 345191
	if (size == 1) {
Packit 345191
		return "";
Packit 345191
	}
Packit 345191
Packit 345191
	ret = iallocztm(tsdn, size, sz_size2index(size), false, NULL, true,
Packit 345191
	    arena_get(TSDN_NULL, 0, true), true);
Packit 345191
	if (ret == NULL) {
Packit 345191
		return NULL;
Packit 345191
	}
Packit 345191
	memcpy(ret, thread_name, size);
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
Packit 345191
int
Packit 345191
prof_thread_name_set(tsd_t *tsd, const char *thread_name) {
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
	unsigned i;
Packit 345191
	char *s;
Packit 345191
Packit 345191
	tdata = prof_tdata_get(tsd, true);
Packit 345191
	if (tdata == NULL) {
Packit 345191
		return EAGAIN;
Packit 345191
	}
Packit 345191
Packit 345191
	/* Validate input. */
Packit 345191
	if (thread_name == NULL) {
Packit 345191
		return EFAULT;
Packit 345191
	}
Packit 345191
	for (i = 0; thread_name[i] != '\0'; i++) {
Packit 345191
		char c = thread_name[i];
Packit 345191
		if (!isgraph(c) && !isblank(c)) {
Packit 345191
			return EFAULT;
Packit 345191
		}
Packit 345191
	}
Packit 345191
Packit 345191
	s = prof_thread_name_alloc(tsd_tsdn(tsd), thread_name);
Packit 345191
	if (s == NULL) {
Packit 345191
		return EAGAIN;
Packit 345191
	}
Packit 345191
Packit 345191
	if (tdata->thread_name != NULL) {
Packit 345191
		idalloctm(tsd_tsdn(tsd), tdata->thread_name, NULL, NULL, true,
Packit 345191
		    true);
Packit 345191
		tdata->thread_name = NULL;
Packit 345191
	}
Packit 345191
	if (strlen(s) > 0) {
Packit 345191
		tdata->thread_name = s;
Packit 345191
	}
Packit 345191
	return 0;
Packit 345191
}
Packit 345191
Packit 345191
bool
Packit 345191
prof_thread_active_get(tsd_t *tsd) {
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
Packit 345191
	tdata = prof_tdata_get(tsd, true);
Packit 345191
	if (tdata == NULL) {
Packit 345191
		return false;
Packit 345191
	}
Packit 345191
	return tdata->active;
Packit 345191
}
Packit 345191
Packit 345191
bool
Packit 345191
prof_thread_active_set(tsd_t *tsd, bool active) {
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
Packit 345191
	tdata = prof_tdata_get(tsd, true);
Packit 345191
	if (tdata == NULL) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
	tdata->active = active;
Packit 345191
	return false;
Packit 345191
}
Packit 345191
Packit 345191
bool
Packit 345191
prof_thread_active_init_get(tsdn_t *tsdn) {
Packit 345191
	bool active_init;
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, &prof_thread_active_init_mtx);
Packit 345191
	active_init = prof_thread_active_init;
Packit 345191
	malloc_mutex_unlock(tsdn, &prof_thread_active_init_mtx);
Packit 345191
	return active_init;
Packit 345191
}
Packit 345191
Packit 345191
bool
Packit 345191
prof_thread_active_init_set(tsdn_t *tsdn, bool active_init) {
Packit 345191
	bool active_init_old;
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, &prof_thread_active_init_mtx);
Packit 345191
	active_init_old = prof_thread_active_init;
Packit 345191
	prof_thread_active_init = active_init;
Packit 345191
	malloc_mutex_unlock(tsdn, &prof_thread_active_init_mtx);
Packit 345191
	return active_init_old;
Packit 345191
}
Packit 345191
Packit 345191
bool
Packit 345191
prof_gdump_get(tsdn_t *tsdn) {
Packit 345191
	bool prof_gdump_current;
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, &prof_gdump_mtx);
Packit 345191
	prof_gdump_current = prof_gdump_val;
Packit 345191
	malloc_mutex_unlock(tsdn, &prof_gdump_mtx);
Packit 345191
	return prof_gdump_current;
Packit 345191
}
Packit 345191
Packit 345191
bool
Packit 345191
prof_gdump_set(tsdn_t *tsdn, bool gdump) {
Packit 345191
	bool prof_gdump_old;
Packit 345191
Packit 345191
	malloc_mutex_lock(tsdn, &prof_gdump_mtx);
Packit 345191
	prof_gdump_old = prof_gdump_val;
Packit 345191
	prof_gdump_val = gdump;
Packit 345191
	malloc_mutex_unlock(tsdn, &prof_gdump_mtx);
Packit 345191
	return prof_gdump_old;
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_boot0(void) {
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	memcpy(opt_prof_prefix, PROF_PREFIX_DEFAULT,
Packit 345191
	    sizeof(PROF_PREFIX_DEFAULT));
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_boot1(void) {
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	/*
Packit 345191
	 * opt_prof must be in its final state before any arenas are
Packit 345191
	 * initialized, so this function must be executed early.
Packit 345191
	 */
Packit 345191
Packit 345191
	if (opt_prof_leak && !opt_prof) {
Packit 345191
		/*
Packit 345191
		 * Enable opt_prof, but in such a way that profiles are never
Packit 345191
		 * automatically dumped.
Packit 345191
		 */
Packit 345191
		opt_prof = true;
Packit 345191
		opt_prof_gdump = false;
Packit 345191
	} else if (opt_prof) {
Packit 345191
		if (opt_lg_prof_interval >= 0) {
Packit 345191
			prof_interval = (((uint64_t)1U) <<
Packit 345191
			    opt_lg_prof_interval);
Packit 345191
		}
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
bool
Packit 345191
prof_boot2(tsd_t *tsd) {
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	if (opt_prof) {
Packit 345191
		unsigned i;
Packit 345191
Packit 345191
		lg_prof_sample = opt_lg_prof_sample;
Packit 345191
Packit 345191
		prof_active = opt_prof_active;
Packit 345191
		if (malloc_mutex_init(&prof_active_mtx, "prof_active",
Packit 345191
		    WITNESS_RANK_PROF_ACTIVE, malloc_mutex_rank_exclusive)) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
Packit 345191
		prof_gdump_val = opt_prof_gdump;
Packit 345191
		if (malloc_mutex_init(&prof_gdump_mtx, "prof_gdump",
Packit 345191
		    WITNESS_RANK_PROF_GDUMP, malloc_mutex_rank_exclusive)) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
Packit 345191
		prof_thread_active_init = opt_prof_thread_active_init;
Packit 345191
		if (malloc_mutex_init(&prof_thread_active_init_mtx,
Packit 345191
		    "prof_thread_active_init",
Packit 345191
		    WITNESS_RANK_PROF_THREAD_ACTIVE_INIT,
Packit 345191
		    malloc_mutex_rank_exclusive)) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
Packit 345191
		if (ckh_new(tsd, &bt2gctx, PROF_CKH_MINITEMS, prof_bt_hash,
Packit 345191
		    prof_bt_keycomp)) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
		if (malloc_mutex_init(&bt2gctx_mtx, "prof_bt2gctx",
Packit 345191
		    WITNESS_RANK_PROF_BT2GCTX, malloc_mutex_rank_exclusive)) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
Packit 345191
		tdata_tree_new(&tdatas);
Packit 345191
		if (malloc_mutex_init(&tdatas_mtx, "prof_tdatas",
Packit 345191
		    WITNESS_RANK_PROF_TDATAS, malloc_mutex_rank_exclusive)) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
Packit 345191
		next_thr_uid = 0;
Packit 345191
		if (malloc_mutex_init(&next_thr_uid_mtx, "prof_next_thr_uid",
Packit 345191
		    WITNESS_RANK_PROF_NEXT_THR_UID, malloc_mutex_rank_exclusive)) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
Packit 345191
		if (malloc_mutex_init(&prof_dump_seq_mtx, "prof_dump_seq",
Packit 345191
		    WITNESS_RANK_PROF_DUMP_SEQ, malloc_mutex_rank_exclusive)) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
		if (malloc_mutex_init(&prof_dump_mtx, "prof_dump",
Packit 345191
		    WITNESS_RANK_PROF_DUMP, malloc_mutex_rank_exclusive)) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
Packit 345191
		if (opt_prof_final && opt_prof_prefix[0] != '\0' &&
Packit 345191
		    atexit(prof_fdump) != 0) {
Packit 345191
			malloc_write("<jemalloc>: Error in atexit()\n");
Packit 345191
			if (opt_abort) {
Packit 345191
				abort();
Packit 345191
			}
Packit 345191
		}
Packit 345191
Packit 345191
		if (opt_prof_log) {
Packit 345191
			prof_log_start(tsd_tsdn(tsd), NULL);
Packit 345191
		}
Packit 345191
Packit 345191
		if (atexit(prof_log_stop_final) != 0) {
Packit 345191
			malloc_write("<jemalloc>: Error in atexit() "
Packit 345191
				     "for logging\n");
Packit 345191
			if (opt_abort) {
Packit 345191
				abort();
Packit 345191
			}
Packit 345191
		}
Packit 345191
Packit 345191
		if (malloc_mutex_init(&log_mtx, "prof_log",
Packit 345191
		    WITNESS_RANK_PROF_LOG, malloc_mutex_rank_exclusive)) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
Packit 345191
		if (ckh_new(tsd, &log_bt_node_set, PROF_CKH_MINITEMS,
Packit 345191
		    prof_bt_node_hash, prof_bt_node_keycomp)) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
Packit 345191
		if (ckh_new(tsd, &log_thr_node_set, PROF_CKH_MINITEMS,
Packit 345191
		    prof_thr_node_hash, prof_thr_node_keycomp)) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
Packit 345191
		log_tables_initialized = true;
Packit 345191
Packit 345191
		gctx_locks = (malloc_mutex_t *)base_alloc(tsd_tsdn(tsd),
Packit 345191
		    b0get(), PROF_NCTX_LOCKS * sizeof(malloc_mutex_t),
Packit 345191
		    CACHELINE);
Packit 345191
		if (gctx_locks == NULL) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
		for (i = 0; i < PROF_NCTX_LOCKS; i++) {
Packit 345191
			if (malloc_mutex_init(&gctx_locks[i], "prof_gctx",
Packit 345191
			    WITNESS_RANK_PROF_GCTX,
Packit 345191
			    malloc_mutex_rank_exclusive)) {
Packit 345191
				return true;
Packit 345191
			}
Packit 345191
		}
Packit 345191
Packit 345191
		tdata_locks = (malloc_mutex_t *)base_alloc(tsd_tsdn(tsd),
Packit 345191
		    b0get(), PROF_NTDATA_LOCKS * sizeof(malloc_mutex_t),
Packit 345191
		    CACHELINE);
Packit 345191
		if (tdata_locks == NULL) {
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
		for (i = 0; i < PROF_NTDATA_LOCKS; i++) {
Packit 345191
			if (malloc_mutex_init(&tdata_locks[i], "prof_tdata",
Packit 345191
			    WITNESS_RANK_PROF_TDATA,
Packit 345191
			    malloc_mutex_rank_exclusive)) {
Packit 345191
				return true;
Packit 345191
			}
Packit 345191
		}
Packit 345191
#ifdef JEMALLOC_PROF_LIBGCC
Packit 345191
		/*
Packit 345191
		 * Cause the backtracing machinery to allocate its internal
Packit 345191
		 * state before enabling profiling.
Packit 345191
		 */
Packit 345191
		_Unwind_Backtrace(prof_unwind_init_callback, NULL);
Packit 345191
#endif
Packit 345191
	}
Packit 345191
	prof_booted = true;
Packit 345191
Packit 345191
	return false;
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_prefork0(tsdn_t *tsdn) {
Packit 345191
	if (config_prof && opt_prof) {
Packit 345191
		unsigned i;
Packit 345191
Packit 345191
		malloc_mutex_prefork(tsdn, &prof_dump_mtx);
Packit 345191
		malloc_mutex_prefork(tsdn, &bt2gctx_mtx);
Packit 345191
		malloc_mutex_prefork(tsdn, &tdatas_mtx);
Packit 345191
		for (i = 0; i < PROF_NTDATA_LOCKS; i++) {
Packit 345191
			malloc_mutex_prefork(tsdn, &tdata_locks[i]);
Packit 345191
		}
Packit 345191
		for (i = 0; i < PROF_NCTX_LOCKS; i++) {
Packit 345191
			malloc_mutex_prefork(tsdn, &gctx_locks[i]);
Packit 345191
		}
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_prefork1(tsdn_t *tsdn) {
Packit 345191
	if (config_prof && opt_prof) {
Packit 345191
		malloc_mutex_prefork(tsdn, &prof_active_mtx);
Packit 345191
		malloc_mutex_prefork(tsdn, &prof_dump_seq_mtx);
Packit 345191
		malloc_mutex_prefork(tsdn, &prof_gdump_mtx);
Packit 345191
		malloc_mutex_prefork(tsdn, &next_thr_uid_mtx);
Packit 345191
		malloc_mutex_prefork(tsdn, &prof_thread_active_init_mtx);
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_postfork_parent(tsdn_t *tsdn) {
Packit 345191
	if (config_prof && opt_prof) {
Packit 345191
		unsigned i;
Packit 345191
Packit 345191
		malloc_mutex_postfork_parent(tsdn,
Packit 345191
		    &prof_thread_active_init_mtx);
Packit 345191
		malloc_mutex_postfork_parent(tsdn, &next_thr_uid_mtx);
Packit 345191
		malloc_mutex_postfork_parent(tsdn, &prof_gdump_mtx);
Packit 345191
		malloc_mutex_postfork_parent(tsdn, &prof_dump_seq_mtx);
Packit 345191
		malloc_mutex_postfork_parent(tsdn, &prof_active_mtx);
Packit 345191
		for (i = 0; i < PROF_NCTX_LOCKS; i++) {
Packit 345191
			malloc_mutex_postfork_parent(tsdn, &gctx_locks[i]);
Packit 345191
		}
Packit 345191
		for (i = 0; i < PROF_NTDATA_LOCKS; i++) {
Packit 345191
			malloc_mutex_postfork_parent(tsdn, &tdata_locks[i]);
Packit 345191
		}
Packit 345191
		malloc_mutex_postfork_parent(tsdn, &tdatas_mtx);
Packit 345191
		malloc_mutex_postfork_parent(tsdn, &bt2gctx_mtx);
Packit 345191
		malloc_mutex_postfork_parent(tsdn, &prof_dump_mtx);
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
void
Packit 345191
prof_postfork_child(tsdn_t *tsdn) {
Packit 345191
	if (config_prof && opt_prof) {
Packit 345191
		unsigned i;
Packit 345191
Packit 345191
		malloc_mutex_postfork_child(tsdn, &prof_thread_active_init_mtx);
Packit 345191
		malloc_mutex_postfork_child(tsdn, &next_thr_uid_mtx);
Packit 345191
		malloc_mutex_postfork_child(tsdn, &prof_gdump_mtx);
Packit 345191
		malloc_mutex_postfork_child(tsdn, &prof_dump_seq_mtx);
Packit 345191
		malloc_mutex_postfork_child(tsdn, &prof_active_mtx);
Packit 345191
		for (i = 0; i < PROF_NCTX_LOCKS; i++) {
Packit 345191
			malloc_mutex_postfork_child(tsdn, &gctx_locks[i]);
Packit 345191
		}
Packit 345191
		for (i = 0; i < PROF_NTDATA_LOCKS; i++) {
Packit 345191
			malloc_mutex_postfork_child(tsdn, &tdata_locks[i]);
Packit 345191
		}
Packit 345191
		malloc_mutex_postfork_child(tsdn, &tdatas_mtx);
Packit 345191
		malloc_mutex_postfork_child(tsdn, &bt2gctx_mtx);
Packit 345191
		malloc_mutex_postfork_child(tsdn, &prof_dump_mtx);
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
/******************************************************************************/