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

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