|
| 1 | +/* ---------------------------------------------------------------------------- |
| 2 | +Copyright (c) 2026, Microsoft Research, Daan Leijen |
| 3 | +This is free software; you can redistribute it and/or modify it under the |
| 4 | +terms of the MIT license. |
| 5 | +-----------------------------------------------------------------------------*/ |
| 6 | + |
| 7 | +/* Release heaps on many threads at once while other threads free into them. |
| 8 | +
|
| 9 | + - NCHURN threads loop: `mi_heap_new`, allocate mixed sizes, hand a third of the blocks to a shared |
| 10 | + ring, free a third, leak a third, then `mi_heap_delete` (or `mi_heap_destroy` when nothing was |
| 11 | + handed out). Some heaps are also published for a while so that short-lived threads allocate from |
| 12 | + them and exit with their blocks live (the abandon path, and the per-bin abandoned maps). |
| 13 | + - NFREE threads pop blocks from the ring and `mi_free` them: those frees land before, during and |
| 14 | + after the delete of the owning heap, so pages are freed and re-abandoned by a foreign thread while |
| 15 | + `mi_heap_delete` claims them (`arena.c:mi_heap_visit_page_claim` against `mi_arenas_page_free_prim`). |
| 16 | + - NSPAWN threads keep creating batches of threads that allocate from a published heap (or the main |
| 17 | + heap) and exit at once. |
| 18 | +
|
| 19 | + Meant to be run under TSAN and ASAN as well as with MI_DEBUG_FULL: it must finish without a report. |
| 20 | +
|
| 21 | + > mimalloc-test-heap-release-mt [SECONDS] |
| 22 | +*/ |
| 23 | +#if defined(_WIN32) |
| 24 | +#include <stdio.h> |
| 25 | +int main(void) { printf("test-heap-release-mt: skipped on Windows (uses pthreads)\n"); return 0; } |
| 26 | +#else |
| 27 | + |
| 28 | +#include <mimalloc.h> |
| 29 | +#include <pthread.h> |
| 30 | +#include <stdatomic.h> |
| 31 | +#include <stdio.h> |
| 32 | +#include <stdlib.h> |
| 33 | +#include <string.h> |
| 34 | +#include <unistd.h> |
| 35 | + |
| 36 | +#define NCHURN 8 |
| 37 | +#define NFREE 4 |
| 38 | +#define NSPAWN 4 |
| 39 | +#define RING (1<<16) |
| 40 | +static _Atomic(void*) ring[RING]; |
| 41 | +static _Atomic unsigned long ring_head, ring_tail; |
| 42 | +static _Atomic int stop; |
| 43 | +static _Atomic unsigned long heaps_done, frees_done, exits_done; |
| 44 | + |
| 45 | +static const size_t sizes[] = {8,16,48,96,200,512,1024,2048,5000,8192,20000,40000,70000,200000}; |
| 46 | +#define NS (sizeof(sizes)/sizeof(sizes[0])) |
| 47 | + |
| 48 | +static void push(void* p) { |
| 49 | + for (;;) { |
| 50 | + unsigned long h = atomic_fetch_add(&ring_head, 1); |
| 51 | + void* expect = NULL; |
| 52 | + if (atomic_compare_exchange_strong(&ring[h % RING], &expect, p)) return; |
| 53 | + // slot busy: free it ourselves instead of spinning |
| 54 | + mi_free(p); return; |
| 55 | + } |
| 56 | +} |
| 57 | +static void* pop(void) { |
| 58 | + unsigned long t = atomic_fetch_add(&ring_tail, 1); |
| 59 | + return atomic_exchange(&ring[t % RING], NULL); |
| 60 | +} |
| 61 | + |
| 62 | +static unsigned rnd(unsigned* s){ *s = *s*1103515245u+12345u; return *s>>8; } |
| 63 | + |
| 64 | +// shared live heaps for the exiting threads |
| 65 | +#define NSHARED 4 |
| 66 | +static _Atomic(mi_heap_t*) shared[NSHARED]; |
| 67 | +static _Atomic int users[NSHARED]; |
| 68 | + |
| 69 | +static void* churn(void* arg) { |
| 70 | + unsigned seed = (unsigned)(uintptr_t)arg * 7919u + 1; |
| 71 | + while (!atomic_load(&stop)) { |
| 72 | + mi_heap_t* h = mi_heap_new(); |
| 73 | + int slot = -1; |
| 74 | + // sometimes publish as a shared heap for exiting threads to allocate from |
| 75 | + if ((rnd(&seed) % 4) == 0) { |
| 76 | + slot = rnd(&seed) % NSHARED; |
| 77 | + mi_heap_t* expect = NULL; |
| 78 | + if (!atomic_compare_exchange_strong(&shared[slot], &expect, h)) slot = -1; |
| 79 | + } |
| 80 | + int handed = 0; |
| 81 | + int n = 50 + rnd(&seed) % 400; |
| 82 | + for (int i = 0; i < n; i++) { |
| 83 | + size_t sz = sizes[rnd(&seed) % NS]; |
| 84 | + void* p = mi_heap_malloc(h, sz); |
| 85 | + if (!p) { fprintf(stderr,"oom\n"); abort(); } |
| 86 | + memset(p, 0xAB, sz < 64 ? sz : 64); |
| 87 | + unsigned r = rnd(&seed) % 3; |
| 88 | + if (r == 0) { push(p); handed++; } |
| 89 | + else if (r == 1) mi_free(p); |
| 90 | + // else leak into the heap; delete moves it to main / destroy frees it |
| 91 | + } |
| 92 | + if (slot >= 0) { |
| 93 | + // let exiting threads use it for a bit, then unpublish |
| 94 | + usleep(200); |
| 95 | + atomic_store(&shared[slot], NULL); |
| 96 | + while (atomic_load(&users[slot]) != 0) usleep(10); // in-flight allocators finish (contract: no alloc during delete) |
| 97 | + handed = 1; // exiting threads may hold blocks: must delete, not destroy |
| 98 | + } |
| 99 | + if (handed == 0 && (rnd(&seed) & 1)) mi_heap_destroy(h); |
| 100 | + else mi_heap_delete(h); |
| 101 | + atomic_fetch_add(&heaps_done, 1); |
| 102 | + } |
| 103 | + return NULL; |
| 104 | +} |
| 105 | + |
| 106 | +static void* freer(void* arg) { |
| 107 | + (void)arg; |
| 108 | + while (!atomic_load(&stop) || atomic_load(&ring_tail) < atomic_load(&ring_head)) { |
| 109 | + void* p = pop(); |
| 110 | + if (p) { mi_free(p); atomic_fetch_add(&frees_done,1); } |
| 111 | + else if (atomic_load(&stop)) break; |
| 112 | + } |
| 113 | + return NULL; |
| 114 | +} |
| 115 | + |
| 116 | +static void* exiter(void* arg) { |
| 117 | + unsigned seed = (unsigned)(uintptr_t)arg; |
| 118 | + int slot = rnd(&seed) % NSHARED; |
| 119 | + atomic_fetch_add(&users[slot], 1); |
| 120 | + mi_heap_t* h = atomic_load(&shared[slot]); |
| 121 | + for (int i = 0; i < 40; i++) { |
| 122 | + size_t sz = sizes[rnd(&seed) % 8]; |
| 123 | + void* p = h ? mi_heap_malloc(h, sz) : mi_malloc(sz); |
| 124 | + if (p) { memset(p, 0xCD, sz < 64 ? sz : 64); push(p); } |
| 125 | + } |
| 126 | + atomic_fetch_sub(&users[slot], 1); |
| 127 | + atomic_fetch_add(&exits_done,1); |
| 128 | + return NULL; // exit with blocks live -> pages abandoned (mapped, lazy bitmap alloc) |
| 129 | +} |
| 130 | + |
| 131 | +static void* spawner(void* arg) { |
| 132 | + unsigned k = (unsigned)(uintptr_t)arg * 100000u; |
| 133 | + while (!atomic_load(&stop)) { |
| 134 | + pthread_t t[8]; |
| 135 | + for (int i = 0; i < 8; i++) pthread_create(&t[i], NULL, exiter, (void*)(uintptr_t)(k++)); |
| 136 | + for (int i = 0; i < 8; i++) pthread_join(t[i], NULL); |
| 137 | + } |
| 138 | + return NULL; |
| 139 | +} |
| 140 | + |
| 141 | +int main(int argc, char** argv) { |
| 142 | + int secs = (argc > 1 ? atoi(argv[1]) : 0); |
| 143 | + if (secs <= 0) { |
| 144 | + #if defined(MI_TSAN) || !defined(NDEBUG) |
| 145 | + secs = 4; |
| 146 | + #else |
| 147 | + secs = 6; |
| 148 | + #endif |
| 149 | + } |
| 150 | + pthread_t tc[NCHURN], tf[NFREE], ts[NSPAWN]; |
| 151 | + for (long i = 0; i < NFREE; i++) pthread_create(&tf[i], NULL, freer, (void*)i); |
| 152 | + for (long i = 0; i < NCHURN; i++) pthread_create(&tc[i], NULL, churn, (void*)(i+1)); |
| 153 | + for (long i = 0; i < NSPAWN; i++) pthread_create(&ts[i], NULL, spawner, (void*)(i+1)); |
| 154 | + sleep(secs); |
| 155 | + atomic_store(&stop, 1); |
| 156 | + for (int i = 0; i < NCHURN; i++) pthread_join(tc[i], NULL); |
| 157 | + for (int i = 0; i < NSPAWN; i++) pthread_join(ts[i], NULL); |
| 158 | + for (int i = 0; i < NFREE; i++) pthread_join(tf[i], NULL); |
| 159 | + // drain |
| 160 | + void* p; while (atomic_load(&ring_tail) < atomic_load(&ring_head) && (p = pop(), 1)) { if (p) mi_free(p); } |
| 161 | + for (unsigned i = 0; i < RING; i++) { void* q = atomic_exchange(&ring[i], NULL); if (q) mi_free(q); } |
| 162 | + mi_collect(true); |
| 163 | + printf("test-heap-release-mt: ok (heaps=%lu frees=%lu thread-exits=%lu)\n", (unsigned long)heaps_done, (unsigned long)frees_done, (unsigned long)exits_done); |
| 164 | + return 0; |
| 165 | +} |
| 166 | + |
| 167 | +#endif |
0 commit comments