MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / stress

Function stress

3rd/mimalloc-2.0.9/test/test-stress.c:125–178  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

123
124
125static void stress(intptr_t tid) {
126 //bench_start_thread();
127 uintptr_t r = ((tid + 1) * 43); // rand();
128 const size_t max_item_shift = 5; // 128
129 const size_t max_item_retained_shift = max_item_shift + 2;
130 size_t allocs = 100 * ((size_t)SCALE) * (tid % 8 + 1); // some threads do more
131 size_t retain = allocs / 2;
132 void** data = NULL;
133 size_t data_size = 0;
134 size_t data_top = 0;
135 void** retained = (void**)custom_calloc(retain,sizeof(void*));
136 size_t retain_top = 0;
137
138 while (allocs > 0 || retain > 0) {
139 if (retain == 0 || (chance(50, &r) && allocs > 0)) {
140 // 50%+ alloc
141 allocs--;
142 if (data_top >= data_size) {
143 data_size += 100000;
144 data = (void**)custom_realloc(data, data_size * sizeof(void*));
145 }
146 data[data_top++] = alloc_items(1ULL << (pick(&r) % max_item_shift), &r);
147 }
148 else {
149 // 25% retain
150 retained[retain_top++] = alloc_items( 1ULL << (pick(&r) % max_item_retained_shift), &r);
151 retain--;
152 }
153 if (chance(66, &r) && data_top > 0) {
154 // 66% free previous alloc
155 size_t idx = pick(&r) % data_top;
156 free_items(data[idx]);
157 data[idx] = NULL;
158 }
159 if (chance(25, &r) && data_top > 0) {
160 // 25% exchange a local pointer with the (shared) transfer buffer.
161 size_t data_idx = pick(&r) % data_top;
162 size_t transfer_idx = pick(&r) % TRANSFERS;
163 void* p = data[data_idx];
164 void* q = atomic_exchange_ptr(&transfer[transfer_idx], p);
165 data[data_idx] = q;
166 }
167 }
168 // free everything that is left
169 for (size_t i = 0; i < retain_top; i++) {
170 free_items(retained[i]);
171 }
172 for (size_t i = 0; i < data_top; i++) {
173 free_items(data[i]);
174 }
175 custom_free(retained);
176 custom_free(data);
177 //bench_end_thread();
178}
179
180static void run_os_threads(size_t nthreads, void (*entry)(intptr_t tid));
181

Callers

nothing calls this directly

Calls 5

chanceFunction · 0.85
alloc_itemsFunction · 0.85
pickFunction · 0.85
free_itemsFunction · 0.85
atomic_exchange_ptrFunction · 0.85

Tested by

no test coverage detected