use main stats for thread count

This commit is contained in:
daan 2020-09-05 18:00:36 -07:00
parent 1ce2e4cb05
commit f09549c98f
3 changed files with 9 additions and 20 deletions

View File

@ -201,7 +201,7 @@ static bool _mi_heap_init(void) {
tld->segments.stats = &tld->stats; tld->segments.stats = &tld->stats;
tld->segments.os = &tld->os; tld->segments.os = &tld->os;
tld->os.stats = &tld->stats; tld->os.stats = &tld->stats;
_mi_heap_set_default_direct(heap); _mi_heap_set_default_direct(heap);
} }
return false; return false;
} }
@ -235,9 +235,8 @@ static bool _mi_heap_done(mi_heap_t* heap) {
_mi_heap_collect_abandon(heap); _mi_heap_collect_abandon(heap);
} }
// merge stats // merge stats
_mi_stats_done(&heap->tld->stats); _mi_stats_done(&heap->tld->stats);
// free if not the main thread // free if not the main thread
if (heap != &_mi_heap_main) { if (heap != &_mi_heap_main) {
@ -337,18 +336,13 @@ void mi_thread_init(void) mi_attr_noexcept
{ {
// ensure our process has started already // ensure our process has started already
mi_process_init(); mi_process_init();
// initialize the thread local default heap // initialize the thread local default heap
// (this will call `_mi_heap_set_default_direct` and thus set the // (this will call `_mi_heap_set_default_direct` and thus set the
// fiber/pthread key to a non-zero value, ensuring `_mi_thread_done` is called) // fiber/pthread key to a non-zero value, ensuring `_mi_thread_done` is called)
if (_mi_heap_init()) return; // returns true if already initialized if (_mi_heap_init()) return; // returns true if already initialized
// don't further initialize for the main thread _mi_stat_increase(&_mi_stats_main.threads, 1);
if (_mi_is_main_thread()) return;
mi_heap_t* const heap = mi_get_default_heap();
if (mi_heap_is_initialized(heap)) { _mi_stat_increase(&heap->tld->stats.threads, 1); }
//_mi_verbose_message("thread init: 0x%zx\n", _mi_thread_id()); //_mi_verbose_message("thread init: 0x%zx\n", _mi_thread_id());
} }
@ -357,14 +351,11 @@ void mi_thread_done(void) mi_attr_noexcept {
} }
static void _mi_thread_done(mi_heap_t* heap) { static void _mi_thread_done(mi_heap_t* heap) {
_mi_stat_decrease(&_mi_stats_main.threads, 1);
// check thread-id as on Windows shutdown with FLS the main (exit) thread may call this on thread-local heaps... // check thread-id as on Windows shutdown with FLS the main (exit) thread may call this on thread-local heaps...
if (heap->thread_id != _mi_thread_id()) return; if (heap->thread_id != _mi_thread_id()) return;
// stats
if (!_mi_is_main_thread() && mi_heap_is_initialized(heap)) {
_mi_stat_decrease(&heap->tld->stats.threads, 1);
}
// abandon the thread local heap // abandon the thread local heap
if (_mi_heap_done(heap)) return; // returns true if already ran if (_mi_heap_done(heap)) return; // returns true if already ran
} }

View File

@ -100,14 +100,12 @@ static bool use_large_os_page(size_t size, size_t alignment) {
// round to a good OS allocation size (bounded by max 12.5% waste) // round to a good OS allocation size (bounded by max 12.5% waste)
size_t _mi_os_good_alloc_size(size_t size) { size_t _mi_os_good_alloc_size(size_t size) {
size_t align_size = _mi_os_page_size(); size_t align_size;
/*
if (size < 512*KiB) align_size = _mi_os_page_size(); if (size < 512*KiB) align_size = _mi_os_page_size();
else if (size < 2*MiB) align_size = 64*KiB; else if (size < 2*MiB) align_size = 64*KiB;
else if (size < 8*MiB) align_size = 256*KiB; else if (size < 8*MiB) align_size = 256*KiB;
else if (size < 32*MiB) align_size = 1*MiB; else if (size < 32*MiB) align_size = 1*MiB;
else align_size = 4*MiB; else align_size = 4*MiB;
*/
if (size >= (SIZE_MAX - align_size)) return size; // possible overflow? if (size >= (SIZE_MAX - align_size)) return size; // possible overflow?
return _mi_align_up(size, align_size); return _mi_align_up(size, align_size);
} }

View File

@ -272,7 +272,7 @@ static void run_os_threads(size_t nthreads, void (*fun)(intptr_t)) {
DWORD* tids = (DWORD*)custom_calloc(nthreads,sizeof(DWORD)); DWORD* tids = (DWORD*)custom_calloc(nthreads,sizeof(DWORD));
HANDLE* thandles = (HANDLE*)custom_calloc(nthreads,sizeof(HANDLE)); HANDLE* thandles = (HANDLE*)custom_calloc(nthreads,sizeof(HANDLE));
for (uintptr_t i = 0; i < nthreads; i++) { for (uintptr_t i = 0; i < nthreads; i++) {
thandles[i] = CreateThread(0, 4096, &thread_entry, (void*)(i), 0, &tids[i]); thandles[i] = CreateThread(0, 8*1024, &thread_entry, (void*)(i), 0, &tids[i]);
} }
for (size_t i = 0; i < nthreads; i++) { for (size_t i = 0; i < nthreads; i++) {
WaitForSingleObject(thandles[i], INFINITE); WaitForSingleObject(thandles[i], INFINITE);