From 4afd709d1f3ae7a727f144a96d8b834157d31e17 Mon Sep 17 00:00:00 2001 From: Yinan Zhang Date: Fri, 13 Dec 2019 16:48:03 -0800 Subject: [PATCH] Restructure setters for profiling info Explicitly define three setters: - `prof_tctx_reset()`: set `prof_tctx` to `1U`, if we don't know in advance whether the allocation is large or not; - `prof_tctx_reset_sampled()`: set `prof_tctx` to `1U`, if we already know in advance that the allocation is large; - `prof_info_set()`: set a real `prof_tctx`, and also set other profiling info e.g. the allocation time. Code structure wise, the prof level is kept as a thin wrapper, the large level only provides low level setter APIs, and the arena level carries out the main logic. --- include/jemalloc/internal/arena_inlines_b.h | 15 +++++------ include/jemalloc/internal/large_externs.h | 3 +-- include/jemalloc/internal/prof_inlines_b.h | 29 ++++++++++----------- src/large.c | 9 ++++--- src/prof.c | 8 +----- 5 files changed, 29 insertions(+), 35 deletions(-) diff --git a/include/jemalloc/internal/arena_inlines_b.h b/include/jemalloc/internal/arena_inlines_b.h index fbb8fa18..23b3455b 100644 --- a/include/jemalloc/internal/arena_inlines_b.h +++ b/include/jemalloc/internal/arena_inlines_b.h @@ -68,8 +68,7 @@ arena_prof_info_get(tsd_t *tsd, const void *ptr, alloc_ctx_t *alloc_ctx, } JEMALLOC_ALWAYS_INLINE void -arena_prof_tctx_set(tsd_t *tsd, const void *ptr, alloc_ctx_t *alloc_ctx, - prof_tctx_t *tctx) { +arena_prof_tctx_reset(tsd_t *tsd, const void *ptr, alloc_ctx_t *alloc_ctx) { cassert(config_prof); assert(ptr != NULL); @@ -77,17 +76,17 @@ arena_prof_tctx_set(tsd_t *tsd, const void *ptr, alloc_ctx_t *alloc_ctx, if (alloc_ctx == NULL) { extent_t *extent = iealloc(tsd_tsdn(tsd), ptr); if (unlikely(!extent_slab_get(extent))) { - large_prof_tctx_set(extent, tctx); + large_prof_tctx_reset(extent); } } else { if (unlikely(!alloc_ctx->slab)) { - large_prof_tctx_set(iealloc(tsd_tsdn(tsd), ptr), tctx); + large_prof_tctx_reset(iealloc(tsd_tsdn(tsd), ptr)); } } } -static inline void -arena_prof_tctx_reset(tsd_t *tsd, const void *ptr, prof_tctx_t *tctx) { +JEMALLOC_ALWAYS_INLINE void +arena_prof_tctx_reset_sampled(tsd_t *tsd, const void *ptr) { cassert(config_prof); assert(ptr != NULL); @@ -98,13 +97,13 @@ arena_prof_tctx_reset(tsd_t *tsd, const void *ptr, prof_tctx_t *tctx) { } JEMALLOC_ALWAYS_INLINE void -arena_prof_alloc_time_set(tsd_t *tsd, const void *ptr, nstime_t *t) { +arena_prof_info_set(tsd_t *tsd, const void *ptr, prof_tctx_t *tctx) { cassert(config_prof); assert(ptr != NULL); extent_t *extent = iealloc(tsd_tsdn(tsd), ptr); assert(!extent_slab_get(extent)); - large_prof_alloc_time_set(extent, t); + large_prof_info_set(extent, tctx); } JEMALLOC_ALWAYS_INLINE void diff --git a/include/jemalloc/internal/large_externs.h b/include/jemalloc/internal/large_externs.h index a0f48b81..2299920d 100644 --- a/include/jemalloc/internal/large_externs.h +++ b/include/jemalloc/internal/large_externs.h @@ -23,8 +23,7 @@ void large_dalloc_finish(tsdn_t *tsdn, extent_t *extent); void large_dalloc(tsdn_t *tsdn, extent_t *extent); size_t large_salloc(tsdn_t *tsdn, const extent_t *extent); void large_prof_info_get(const extent_t *extent, prof_info_t *prof_info); -void large_prof_tctx_set(extent_t *extent, prof_tctx_t *tctx); void large_prof_tctx_reset(extent_t *extent); -void large_prof_alloc_time_set(extent_t *extent, nstime_t *time); +void large_prof_info_set(extent_t *extent, prof_tctx_t *tctx); #endif /* JEMALLOC_INTERNAL_LARGE_EXTERNS_H */ diff --git a/include/jemalloc/internal/prof_inlines_b.h b/include/jemalloc/internal/prof_inlines_b.h index 657e1168..193ede71 100644 --- a/include/jemalloc/internal/prof_inlines_b.h +++ b/include/jemalloc/internal/prof_inlines_b.h @@ -50,28 +50,28 @@ prof_info_get(tsd_t *tsd, const void *ptr, alloc_ctx_t *alloc_ctx, } JEMALLOC_ALWAYS_INLINE void -prof_tctx_set(tsd_t *tsd, const void *ptr, alloc_ctx_t *alloc_ctx, - prof_tctx_t *tctx) { +prof_tctx_reset(tsd_t *tsd, const void *ptr, alloc_ctx_t *alloc_ctx) { cassert(config_prof); assert(ptr != NULL); - arena_prof_tctx_set(tsd, ptr, alloc_ctx, tctx); + arena_prof_tctx_reset(tsd, ptr, alloc_ctx); } JEMALLOC_ALWAYS_INLINE void -prof_tctx_reset(tsd_t *tsd, const void *ptr, prof_tctx_t *tctx) { +prof_tctx_reset_sampled(tsd_t *tsd, const void *ptr) { cassert(config_prof); assert(ptr != NULL); - arena_prof_tctx_reset(tsd, ptr, tctx); + arena_prof_tctx_reset_sampled(tsd, ptr); } JEMALLOC_ALWAYS_INLINE void -prof_alloc_time_set(tsd_t *tsd, const void *ptr, nstime_t *t) { +prof_info_set(tsd_t *tsd, const void *ptr, prof_tctx_t *tctx) { cassert(config_prof); assert(ptr != NULL); + assert((uintptr_t)tctx > (uintptr_t)1U); - arena_prof_alloc_time_set(tsd, ptr, t); + arena_prof_info_set(tsd, ptr, tctx); } JEMALLOC_ALWAYS_INLINE bool @@ -125,8 +125,7 @@ prof_malloc(tsd_t *tsd, const void *ptr, size_t usize, alloc_ctx_t *alloc_ctx, if (unlikely((uintptr_t)tctx > (uintptr_t)1U)) { prof_malloc_sample_object(tsd, ptr, usize, tctx); } else { - prof_tctx_set(tsd, ptr, alloc_ctx, - (prof_tctx_t *)(uintptr_t)1U); + prof_tctx_reset(tsd, ptr, alloc_ctx); } } @@ -161,15 +160,15 @@ prof_realloc(tsd_t *tsd, const void *ptr, size_t usize, prof_tctx_t *tctx, if (unlikely(sampled)) { prof_malloc_sample_object(tsd, ptr, usize, tctx); } else if (moved) { - prof_tctx_set(tsd, ptr, NULL, (prof_tctx_t *)(uintptr_t)1U); + prof_tctx_reset(tsd, ptr, NULL); } else if (unlikely(old_sampled)) { /* - * prof_tctx_set() would work for the !moved case as well, but - * prof_tctx_reset() is slightly cheaper, and the proper thing - * to do here in the presence of explicit knowledge re: moved - * state. + * prof_tctx_reset() would work for the !moved case as well, + * but prof_tctx_reset_sampled() is slightly cheaper, and the + * proper thing to do here in the presence of explicit + * knowledge re: moved state. */ - prof_tctx_reset(tsd, ptr, tctx); + prof_tctx_reset_sampled(tsd, ptr); } else { prof_info_t prof_info; prof_info_get(tsd, ptr, NULL, &prof_info); diff --git a/src/large.c b/src/large.c index f10b0d15..1a1e82b6 100644 --- a/src/large.c +++ b/src/large.c @@ -372,7 +372,7 @@ large_prof_info_get(const extent_t *extent, prof_info_t *prof_info) { extent_prof_info_get(extent, prof_info); } -void +static void large_prof_tctx_set(extent_t *extent, prof_tctx_t *tctx) { extent_prof_tctx_set(extent, tctx); } @@ -383,6 +383,9 @@ large_prof_tctx_reset(extent_t *extent) { } void -large_prof_alloc_time_set(extent_t *extent, nstime_t *t) { - extent_prof_alloc_time_set(extent, t); +large_prof_info_set(extent_t *extent, prof_tctx_t *tctx) { + large_prof_tctx_set(extent, tctx); + nstime_t t; + nstime_init_update(&t); + extent_prof_alloc_time_set(extent, &t); } diff --git a/src/prof.c b/src/prof.c index d1d46e2e..3a72e9c4 100644 --- a/src/prof.c +++ b/src/prof.c @@ -162,13 +162,7 @@ prof_alloc_rollback(tsd_t *tsd, prof_tctx_t *tctx, bool updated) { void prof_malloc_sample_object(tsd_t *tsd, const void *ptr, size_t usize, prof_tctx_t *tctx) { - prof_tctx_set(tsd, ptr, NULL, tctx); - - /* Get the current time and set this in the extent_t. We'll read this - * when free() is called. */ - nstime_t t; - nstime_init_update(&t); - prof_alloc_time_set(tsd, ptr, &t); + prof_info_set(tsd, ptr, tctx); malloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock); tctx->cnts.curobjs++;