Blame jemalloc/include/jemalloc/internal/prof_inlines_b.h

Packit 345191
#ifndef JEMALLOC_INTERNAL_PROF_INLINES_B_H
Packit 345191
#define JEMALLOC_INTERNAL_PROF_INLINES_B_H
Packit 345191
Packit 345191
#include "jemalloc/internal/safety_check.h"
Packit 345191
#include "jemalloc/internal/sz.h"
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE bool
Packit 345191
prof_gdump_get_unlocked(void) {
Packit 345191
	/*
Packit 345191
	 * No locking is used when reading prof_gdump_val in the fast path, so
Packit 345191
	 * there are no guarantees regarding how long it will take for all
Packit 345191
	 * threads to notice state changes.
Packit 345191
	 */
Packit 345191
	return prof_gdump_val;
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE prof_tdata_t *
Packit 345191
prof_tdata_get(tsd_t *tsd, bool create) {
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	tdata = tsd_prof_tdata_get(tsd);
Packit 345191
	if (create) {
Packit 345191
		if (unlikely(tdata == NULL)) {
Packit 345191
			if (tsd_nominal(tsd)) {
Packit 345191
				tdata = prof_tdata_init(tsd);
Packit 345191
				tsd_prof_tdata_set(tsd, tdata);
Packit 345191
			}
Packit 345191
		} else if (unlikely(tdata->expired)) {
Packit 345191
			tdata = prof_tdata_reinit(tsd, tdata);
Packit 345191
			tsd_prof_tdata_set(tsd, tdata);
Packit 345191
		}
Packit 345191
		assert(tdata == NULL || tdata->attached);
Packit 345191
	}
Packit 345191
Packit 345191
	return tdata;
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE prof_tctx_t *
Packit 345191
prof_tctx_get(tsdn_t *tsdn, const void *ptr, alloc_ctx_t *alloc_ctx) {
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(ptr != NULL);
Packit 345191
Packit 345191
	return arena_prof_tctx_get(tsdn, ptr, alloc_ctx);
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE void
Packit 345191
prof_tctx_set(tsdn_t *tsdn, const void *ptr, size_t usize,
Packit 345191
    alloc_ctx_t *alloc_ctx, prof_tctx_t *tctx) {
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(ptr != NULL);
Packit 345191
Packit 345191
	arena_prof_tctx_set(tsdn, ptr, usize, alloc_ctx, tctx);
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE void
Packit 345191
prof_tctx_reset(tsdn_t *tsdn, const void *ptr, prof_tctx_t *tctx) {
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(ptr != NULL);
Packit 345191
Packit 345191
	arena_prof_tctx_reset(tsdn, ptr, tctx);
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE nstime_t
Packit 345191
prof_alloc_time_get(tsdn_t *tsdn, const void *ptr, alloc_ctx_t *alloc_ctx) {
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(ptr != NULL);
Packit 345191
Packit 345191
	return arena_prof_alloc_time_get(tsdn, ptr, alloc_ctx);
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE void
Packit 345191
prof_alloc_time_set(tsdn_t *tsdn, const void *ptr, alloc_ctx_t *alloc_ctx,
Packit 345191
    nstime_t t) {
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(ptr != NULL);
Packit 345191
Packit 345191
	arena_prof_alloc_time_set(tsdn, ptr, alloc_ctx, t);
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE bool
Packit 345191
prof_sample_check(tsd_t *tsd, size_t usize, bool update) {
Packit 345191
	ssize_t check = update ? 0 : usize;
Packit 345191
Packit 345191
	int64_t bytes_until_sample = tsd_bytes_until_sample_get(tsd);
Packit 345191
	if (update) {
Packit 345191
		bytes_until_sample -= usize;
Packit 345191
		if (tsd_nominal(tsd)) {
Packit 345191
			tsd_bytes_until_sample_set(tsd, bytes_until_sample);
Packit 345191
		}
Packit 345191
	}
Packit 345191
	if (likely(bytes_until_sample >= check)) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	return false;
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE bool
Packit 345191
prof_sample_accum_update(tsd_t *tsd, size_t usize, bool update,
Packit 345191
			 prof_tdata_t **tdata_out) {
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
Packit 345191
	/* Fastpath: no need to load tdata */
Packit 345191
	if (likely(prof_sample_check(tsd, usize, update))) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	bool booted = tsd_prof_tdata_get(tsd);
Packit 345191
	tdata = prof_tdata_get(tsd, true);
Packit 345191
	if (unlikely((uintptr_t)tdata <= (uintptr_t)PROF_TDATA_STATE_MAX)) {
Packit 345191
		tdata = NULL;
Packit 345191
	}
Packit 345191
Packit 345191
	if (tdata_out != NULL) {
Packit 345191
		*tdata_out = tdata;
Packit 345191
	}
Packit 345191
Packit 345191
	if (unlikely(tdata == NULL)) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	/*
Packit 345191
	 * If this was the first creation of tdata, then
Packit 345191
	 * prof_tdata_get() reset bytes_until_sample, so decrement and
Packit 345191
	 * check it again
Packit 345191
	 */
Packit 345191
	if (!booted && prof_sample_check(tsd, usize, update)) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
Packit 345191
	if (tsd_reentrancy_level_get(tsd) > 0) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
	/* Compute new sample threshold. */
Packit 345191
	if (update) {
Packit 345191
		prof_sample_threshold_update(tdata);
Packit 345191
	}
Packit 345191
	return !tdata->active;
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE prof_tctx_t *
Packit 345191
prof_alloc_prep(tsd_t *tsd, size_t usize, bool prof_active, bool update) {
Packit 345191
	prof_tctx_t *ret;
Packit 345191
	prof_tdata_t *tdata;
Packit 345191
	prof_bt_t bt;
Packit 345191
Packit 345191
	assert(usize == sz_s2u(usize));
Packit 345191
Packit 345191
	if (!prof_active || likely(prof_sample_accum_update(tsd, usize, update,
Packit 345191
	    &tdata))) {
Packit 345191
		ret = (prof_tctx_t *)(uintptr_t)1U;
Packit 345191
	} else {
Packit 345191
		bt_init(&bt, tdata->vec);
Packit 345191
		prof_backtrace(&bt;;
Packit 345191
		ret = prof_lookup(tsd, &bt;;
Packit 345191
	}
Packit 345191
Packit 345191
	return ret;
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE void
Packit 345191
prof_malloc(tsdn_t *tsdn, const void *ptr, size_t usize, alloc_ctx_t *alloc_ctx,
Packit 345191
    prof_tctx_t *tctx) {
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(ptr != NULL);
Packit 345191
	assert(usize == isalloc(tsdn, ptr));
Packit 345191
Packit 345191
	if (unlikely((uintptr_t)tctx > (uintptr_t)1U)) {
Packit 345191
		prof_malloc_sample_object(tsdn, ptr, usize, tctx);
Packit 345191
	} else {
Packit 345191
		prof_tctx_set(tsdn, ptr, usize, alloc_ctx,
Packit 345191
		    (prof_tctx_t *)(uintptr_t)1U);
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE void
Packit 345191
prof_realloc(tsd_t *tsd, const void *ptr, size_t usize, prof_tctx_t *tctx,
Packit 345191
    bool prof_active, bool updated, const void *old_ptr, size_t old_usize,
Packit 345191
    prof_tctx_t *old_tctx) {
Packit 345191
	bool sampled, old_sampled, moved;
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(ptr != NULL || (uintptr_t)tctx <= (uintptr_t)1U);
Packit 345191
Packit 345191
	if (prof_active && !updated && ptr != NULL) {
Packit 345191
		assert(usize == isalloc(tsd_tsdn(tsd), ptr));
Packit 345191
		if (prof_sample_accum_update(tsd, usize, true, NULL)) {
Packit 345191
			/*
Packit 345191
			 * Don't sample.  The usize passed to prof_alloc_prep()
Packit 345191
			 * was larger than what actually got allocated, so a
Packit 345191
			 * backtrace was captured for this allocation, even
Packit 345191
			 * though its actual usize was insufficient to cross the
Packit 345191
			 * sample threshold.
Packit 345191
			 */
Packit 345191
			prof_alloc_rollback(tsd, tctx, true);
Packit 345191
			tctx = (prof_tctx_t *)(uintptr_t)1U;
Packit 345191
		}
Packit 345191
	}
Packit 345191
Packit 345191
	sampled = ((uintptr_t)tctx > (uintptr_t)1U);
Packit 345191
	old_sampled = ((uintptr_t)old_tctx > (uintptr_t)1U);
Packit 345191
	moved = (ptr != old_ptr);
Packit 345191
Packit 345191
	if (unlikely(sampled)) {
Packit 345191
		prof_malloc_sample_object(tsd_tsdn(tsd), ptr, usize, tctx);
Packit 345191
	} else if (moved) {
Packit 345191
		prof_tctx_set(tsd_tsdn(tsd), ptr, usize, NULL,
Packit 345191
		    (prof_tctx_t *)(uintptr_t)1U);
Packit 345191
	} else if (unlikely(old_sampled)) {
Packit 345191
		/*
Packit 345191
		 * prof_tctx_set() would work for the !moved case as well, but
Packit 345191
		 * prof_tctx_reset() is slightly cheaper, and the proper thing
Packit 345191
		 * to do here in the presence of explicit knowledge re: moved
Packit 345191
		 * state.
Packit 345191
		 */
Packit 345191
		prof_tctx_reset(tsd_tsdn(tsd), ptr, tctx);
Packit 345191
	} else {
Packit 345191
		assert((uintptr_t)prof_tctx_get(tsd_tsdn(tsd), ptr, NULL) ==
Packit 345191
		    (uintptr_t)1U);
Packit 345191
	}
Packit 345191
Packit 345191
	/*
Packit 345191
	 * The prof_free_sampled_object() call must come after the
Packit 345191
	 * prof_malloc_sample_object() call, because tctx and old_tctx may be
Packit 345191
	 * the same, in which case reversing the call order could cause the tctx
Packit 345191
	 * to be prematurely destroyed as a side effect of momentarily zeroed
Packit 345191
	 * counters.
Packit 345191
	 */
Packit 345191
	if (unlikely(old_sampled)) {
Packit 345191
		prof_free_sampled_object(tsd, ptr, old_usize, old_tctx);
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE void
Packit 345191
prof_free(tsd_t *tsd, const void *ptr, size_t usize, alloc_ctx_t *alloc_ctx) {
Packit 345191
	prof_tctx_t *tctx = prof_tctx_get(tsd_tsdn(tsd), ptr, alloc_ctx);
Packit 345191
Packit 345191
	cassert(config_prof);
Packit 345191
	assert(usize == isalloc(tsd_tsdn(tsd), ptr));
Packit 345191
Packit 345191
	if (unlikely((uintptr_t)tctx > (uintptr_t)1U)) {
Packit 345191
		prof_free_sampled_object(tsd, ptr, usize, tctx);
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
#endif /* JEMALLOC_INTERNAL_PROF_INLINES_B_H */