Skip to content

Commit 58d13f6

Browse files
committed
collect every N generic calls
1 parent 4f1edad commit 58d13f6

6 files changed

Lines changed: 28 additions & 8 deletions

File tree

‎include/mimalloc-stats.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,4 @@ mi_decl_export char* mi_stats_get_json( size_t buf_size, char* buf ) mi_attr_noe
100100
}
101101
#endif
102102

103-
#endif // MIMALLOC_STATS_H
103+
#endif // MIMALLOC_STATS_H

‎include/mimalloc.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ mi_decl_export void mi_heap_guarded_set_sample_rate(mi_heap_t* heap, size_t samp
310310
mi_decl_export void mi_heap_guarded_set_size_bound(mi_heap_t* heap, size_t min, size_t max);
311311

312312
// Experimental: communicate that the thread is part of a threadpool
313-
mi_decl_export void mi_thread_set_in_threadpool(void) mi_attr_noexcept;
313+
mi_decl_export void mi_thread_set_in_threadpool(void) mi_attr_noexcept;
314314

315315
// Experimental: create a new heap with a specified heap tag. Set `allow_destroy` to false to allow the thread
316316
// to reclaim abandoned memory (with a compatible heap_tag and arena_id) but in that case `mi_heap_destroy` will
@@ -385,6 +385,7 @@ typedef enum mi_option_e {
385385
mi_option_guarded_sample_rate, // 1 out of N allocations in the min/max range will be guarded (=1000)
386386
mi_option_guarded_sample_seed, // can be set to allow for a (more) deterministic re-execution when a guard page is triggered (=0)
387387
mi_option_target_segments_per_thread, // experimental (=0)
388+
mi_option_generic_collect, // collect heaps every N (=10000) generic allocation calls
388389
_mi_option_last,
389390
// legacy option names
390391
mi_option_large_os_pages = mi_option_allow_large_os_pages,

‎include/mimalloc/types.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ typedef int32_t mi_ssize_t;
202202
#define MI_LARGE_OBJ_WSIZE_MAX (MI_LARGE_OBJ_SIZE_MAX/MI_INTPTR_SIZE)
203203

204204
// Maximum number of size classes. (spaced exponentially in 12.5% increments)
205-
#define MI_BIN_HUGE (73U)
205+
#if MI_BIN_HUGE != 73U
206+
#error "mimalloc internal: expecting 73 bins"
207+
#endif
206208

207209
#if (MI_LARGE_OBJ_WSIZE_MAX >= 655360)
208210
#error "mimalloc internal: define more bins"
@@ -501,6 +503,8 @@ struct mi_heap_s {
501503
size_t page_count; // total number of pages in the `pages` queues.
502504
size_t page_retired_min; // smallest retired index (retired pages are fully free, but still in the page queues)
503505
size_t page_retired_max; // largest retired index into the `pages` array.
506+
long generic_count; // how often is `_mi_malloc_generic` called?
507+
long generic_collect_count; // how often is `_mi_malloc_generic` called without collecting?
504508
mi_heap_t* next; // list of heaps per thread
505509
bool no_reclaim; // `true` if this heap should not reclaim abandoned pages
506510
uint8_t tag; // custom tag, can be used for separating heaps based on the object types

‎src/init.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ mi_decl_hidden mi_decl_cache_align const mi_heap_t _mi_heap_empty = {
105105
{ {0}, {0}, 0, true }, // random
106106
0, // page count
107107
MI_BIN_FULL, 0, // page retired min/max
108+
0, 0, // generic count
108109
NULL, // next
109110
false, // can reclaim
110111
0, // tag
@@ -147,6 +148,7 @@ mi_decl_cache_align mi_heap_t _mi_heap_main = {
147148
{ {0x846ca68b}, {0}, 0, true }, // random
148149
0, // page count
149150
MI_BIN_FULL, 0, // page retired min/max
151+
0, 0, // generic count
150152
NULL, // next heap
151153
false, // can reclaim
152154
0, // tag

‎src/options.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ static mi_option_desc_t options[_mi_option_last] =
162162
UNINIT, MI_OPTION(guarded_sample_rate)}, // 1 out of N allocations in the min/max range will be guarded (=4000)
163163
{ 0, UNINIT, MI_OPTION(guarded_sample_seed)},
164164
{ 0, UNINIT, MI_OPTION(target_segments_per_thread) }, // abandon segments beyond this point, or 0 to disable.
165+
{ 10000, UNINIT, MI_OPTION(generic_collect) }, // collect heaps every N (=10000) generic allocation calls
165166
};
166167

167168
static void mi_option_init(mi_option_desc_t* desc);

‎src/page.c‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -970,11 +970,23 @@ void* _mi_malloc_generic(mi_heap_t* heap, size_t size, bool zero, size_t huge_al
970970
}
971971
mi_assert_internal(mi_heap_is_initialized(heap));
972972

973-
// call potential deferred free routines
974-
_mi_deferred_free(heap, false);
975-
976-
// free delayed frees from other threads (but skip contended ones)
977-
_mi_heap_delayed_free_partial(heap);
973+
// do administrative tasks every N generic mallocs
974+
if mi_unlikely(++heap->generic_count >= 100) {
975+
heap->generic_collect_count += heap->generic_count;
976+
heap->generic_count = 0;
977+
// call potential deferred free routines
978+
_mi_deferred_free(heap, false);
979+
980+
// free delayed frees from other threads (but skip contended ones)
981+
_mi_heap_delayed_free_partial(heap);
982+
983+
// collect every once in a while (10000 by default)
984+
const long generic_collect = mi_option_get_clamp(mi_option_generic_collect, 1, 1000000L);
985+
if (heap->generic_collect_count >= generic_collect) {
986+
heap->generic_collect_count = 0;
987+
mi_heap_collect(heap, false /* force? */);
988+
}
989+
}
978990

979991
// find (or allocate) a page of the right size
980992
mi_page_t* page = mi_find_page(heap, size, huge_alignment);

0 commit comments

Comments
 (0)