Skip to content

Commit b20b60d

Browse files
Merge pull request #32 from robobun/robobun/9dfcc910/lazy-abandoned-bitmaps
arena: allocate the abandoned-page bitmaps on first abandon, and not at all for a heap that is being released
2 parents 2c98a55 + 1515c3c commit b20b60d

10 files changed

Lines changed: 738 additions & 38 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,8 @@ if(MI_USE_CXX)
660660
set_source_files_properties(src/static.c test/test-api.c test/test-api-fill.c test/test-stress.c test/test-stress-heaps.c test/test-stress-subprocs.c PROPERTIES LANGUAGE CXX )
661661
# the white-box tests call internal functions, whose names are mangled now; the heap tests use C11 atomics, which MSVC lacks in C mode
662662
set_source_files_properties(test/test-theap-sentinel.c test/test-purge-holes.c test/test-commit-fail.c
663-
test/test-heap-aba.c test/test-heap-delete-race.c test/test-heap-teardown.c PROPERTIES LANGUAGE CXX )
663+
test/test-heap-aba.c test/test-heap-delete-race.c test/test-heap-teardown.c
664+
test/test-abandoned-lazy.c PROPERTIES LANGUAGE CXX )
664665
endif()
665666

666667

@@ -877,7 +878,7 @@ if (MI_BUILD_TESTS)
877878
enable_testing()
878879

879880
# static link tests
880-
set(mi_static_tests api api-fill stress-heaps stress-subprocs stress heap-mt heap-teardown heap-delete-race heap-churn heap-aba heap-burst-destroy fork-user-heap snapshot prof prof-adversarial purge-zero park-handoff free-before-init)
881+
set(mi_static_tests api api-fill stress-heaps stress-subprocs stress heap-mt heap-teardown heap-delete-race heap-churn heap-release-mt heap-aba heap-burst-destroy abandoned-lazy fork-user-heap snapshot prof prof-adversarial purge-zero park-handoff free-before-init)
881882
if(NOT (MI_DEBUG_TSAN OR MI_TRACK_ASAN OR MI_DEBUG_UBSAN))
882883
list(APPEND mi_static_tests thp-optout) # counts madvise calls by interposing it, which a sanitizer runtime does first
883884
endif()
@@ -923,6 +924,7 @@ if (MI_BUILD_TESTS)
923924

924925
# with no scavenger to hand off to, `mi_on_thread_idle_start` must do the work inline instead
925926
add_test(NAME test-park-handoff-no-scavenger COMMAND ${CMAKE_COMMAND} -E env MIMALLOC_SCAVENGER=0 $<TARGET_FILE:mimalloc-test-park-handoff>)
927+
add_test(NAME test-heap-release-mt-no-scavenger COMMAND ${CMAKE_COMMAND} -E env MIMALLOC_SCAVENGER=0 $<TARGET_FILE:mimalloc-test-heap-release-mt>)
926928
# and sweep every park, so the reclaim path is hit as hard as possible
927929
add_test(NAME test-park-handoff-eager COMMAND ${CMAKE_COMMAND} -E env MIMALLOC_PURGE_HOLES_MIN_INTERVAL=0 $<TARGET_FILE:mimalloc-test-park-handoff>)
928930

‎include/mimalloc/internal.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ void _mi_arenas_abandoned_page_free(mi_page_t* page, mi_theap_t* curren
277277
void _mi_arenas_page_abandon(mi_page_t* page, mi_theap_t* current_theap);
278278
void _mi_arenas_page_unabandon(mi_page_t* page, mi_theap_t* current_theapx /* can be NULL */);
279279
bool _mi_arenas_page_try_reabandon_to_mapped(mi_page_t* page);
280+
void _mi_arena_pages_free(mi_arena_pages_t* arena_pages);
280281
size_t mi_arenas_get_count(mi_subproc_t* subproc);
281282
uint8_t* mi_arena_slice_start(mi_arena_t* arena, size_t slice_index);
282283

@@ -356,6 +357,7 @@ extern "C" {
356357
#endif
357358
extern mi_decl_export _Atomic(uintptr_t) mi_debug_stall_in_thread_theaps_done;
358359
extern mi_decl_export _Atomic(uintptr_t) mi_debug_stall_in_heap_delete_claim;
360+
extern mi_decl_export _Atomic(uintptr_t) mi_debug_abandoned_maps_allocated;
359361
extern mi_decl_export volatile long mi_debug_fail_os_commit_after;
360362
#ifdef __cplusplus
361363
}

‎include/mimalloc/prim-tls.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,23 @@ static inline void* mi_prim_tls_slot(size_t slot) {
149149
#else
150150
return mi_prim_thread_pointer()[slot];
151151
#endif
152+
#elif defined(__GNUC__) || defined(__clang__)
153+
// The slot lives in the thread's control block, which the OS reuses for a later thread, so the
154+
// exiting thread's last store (`_mi_thread_done`) and a new thread's first load touch the same
155+
// address from different threads. A relaxed atomic is the same single load/store instruction and
156+
// states that this is by design (and keeps TSAN quiet about it).
157+
return __atomic_load_n(&mi_prim_thread_pointer()[slot], __ATOMIC_RELAXED);
152158
#else
153159
return mi_prim_thread_pointer()[slot];
154160
#endif
155161
}
156162

157163
static inline void mi_prim_tls_slot_set(size_t slot, void* value) {
164+
#if defined(__GNUC__) || defined(__clang__)
165+
__atomic_store_n(&mi_prim_thread_pointer()[slot], value, __ATOMIC_RELAXED);
166+
#else
158167
mi_prim_thread_pointer()[slot] = value;
168+
#endif
159169
}
160170
#endif
161171

‎include/mimalloc/types.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ typedef struct mi_heap_s {
628628
mi_lock_t theaps_lock; // lock for the theaps list operations
629629

630630
_Atomic(size_t) abandoned_count[MI_BIN_COUNT]; // total count of abandoned pages in this heap
631+
_Atomic(uintptr_t) releasing; // set when `mi_heap_delete`/`mi_heap_destroy` starts: its pages are abandoned unmapped
631632
mi_page_t* os_abandoned_pages; // list of pages that are OS allocated and not in an arena
632633
mi_lock_t os_abandoned_pages_lock; // lock for the os abandoned pages list (this lock protects list operations)
633634

@@ -756,8 +757,13 @@ typedef struct mi_bbitmap_s mi_bbitmap_t; // atomic binned bitmap (defined in
756757

757758
struct mi_arena_pages_s {
758759
mi_bitmap_t* pages; // all registered pages (abandoned and owned)
759-
mi_bitmap_t* pages_abandoned[MI_ARENA_BIN_COUNT]; // abandoned pages per size bin (a set bit means the start of the page)
760-
// followed by the bitmaps (whose siz`es depend on the arena size)
760+
// Abandoned pages per size bin (a set bit means the start of the page). Each bitmap is
761+
// allocated the first time a page of that bin is abandoned (`mi_arena_pages_abandoned_ensure`);
762+
// NULL means no page of that bin was ever abandoned. Eagerly laying out all MI_ARENA_BIN_COUNT
763+
// of them cost a page fault per bitmap (one header write each, on its own OS page) for every
764+
// heap that touched an arena, and most heaps never abandon a page.
765+
_Atomic(mi_bitmap_t*) pages_abandoned[MI_ARENA_BIN_COUNT];
766+
// followed by the `pages` bitmap (whose size depends on the arena size)
761767
};
762768

763769

0 commit comments

Comments
 (0)