Commit | Line | Data |
---|---|---|
d24c33a4 JA |
1 | /* |
2 | * simple memory allocator, backed by mmap() so that it hands out memory | |
3 | * that can be shared across processes and threads | |
4 | */ | |
5 | #include <sys/mman.h> | |
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
8 | #include <assert.h> | |
9 | #include <string.h> | |
10 | #include <unistd.h> | |
11 | #include <sys/types.h> | |
12 | #include <limits.h> | |
3a8600b4 | 13 | #include <fcntl.h> |
d24c33a4 | 14 | |
6548f47f | 15 | #include "mutex.h" |
b3268b92 | 16 | #include "arch/arch.h" |
3a8600b4 | 17 | #include "os/os.h" |
d24c33a4 | 18 | |
55f6491d | 19 | #define SMALLOC_REDZONE /* define to detect memory corruption */ |
d24c33a4 | 20 | |
ec996e9c JA |
21 | #define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */ |
22 | #define SMALLOC_BPI (sizeof(unsigned int) * 8) | |
23 | #define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI) | |
24 | ||
5e012980 | 25 | #define INITIAL_SIZE 8192*1024 /* new pool size */ |
68857686 | 26 | #define MAX_POOLS 128 /* maximum number of pools to setup */ |
d24c33a4 | 27 | |
55f6491d JA |
28 | #define SMALLOC_PRE_RED 0xdeadbeefU |
29 | #define SMALLOC_POST_RED 0x5aa55aa5U | |
55f6491d | 30 | |
2b386d25 | 31 | unsigned int smalloc_pool_size = INITIAL_SIZE; |
122426da | 32 | const int int_mask = sizeof(int) - 1; |
2b386d25 | 33 | |
d24c33a4 | 34 | struct pool { |
6548f47f | 35 | struct fio_mutex *lock; /* protects this pool */ |
d24c33a4 | 36 | void *map; /* map of blocks */ |
ec996e9c JA |
37 | unsigned int *bitmap; /* blocks free/busy map */ |
38 | unsigned int free_blocks; /* free blocks */ | |
39 | unsigned int nr_blocks; /* total blocks */ | |
40 | unsigned int next_non_full; | |
d24c33a4 | 41 | int fd; /* memory backing fd */ |
ec996e9c JA |
42 | unsigned int mmap_size; |
43 | }; | |
44 | ||
45 | struct block_hdr { | |
46 | unsigned int size; | |
47 | #ifdef SMALLOC_REDZONE | |
48 | unsigned int prered; | |
49 | #endif | |
d24c33a4 JA |
50 | }; |
51 | ||
52 | static struct pool mp[MAX_POOLS]; | |
53 | static unsigned int nr_pools; | |
54 | static unsigned int last_pool; | |
6548f47f | 55 | static struct fio_mutex *lock; |
d24c33a4 | 56 | |
d24c33a4 JA |
57 | static inline void pool_lock(struct pool *pool) |
58 | { | |
2e3e31e3 | 59 | fio_mutex_down(pool->lock); |
d24c33a4 JA |
60 | } |
61 | ||
62 | static inline void pool_unlock(struct pool *pool) | |
63 | { | |
2e3e31e3 | 64 | fio_mutex_up(pool->lock); |
d24c33a4 JA |
65 | } |
66 | ||
65864cf7 | 67 | static inline void global_read_lock(void) |
d24c33a4 | 68 | { |
2e3e31e3 | 69 | fio_mutex_down_read(lock); |
d24c33a4 JA |
70 | } |
71 | ||
65864cf7 | 72 | static inline void global_read_unlock(void) |
d24c33a4 | 73 | { |
2e3e31e3 | 74 | fio_mutex_up_read(lock); |
65864cf7 JA |
75 | } |
76 | ||
77 | static inline void global_write_lock(void) | |
78 | { | |
2e3e31e3 | 79 | fio_mutex_down_write(lock); |
65864cf7 JA |
80 | } |
81 | ||
82 | static inline void global_write_unlock(void) | |
83 | { | |
2e3e31e3 | 84 | fio_mutex_up_write(lock); |
d24c33a4 JA |
85 | } |
86 | ||
d24c33a4 JA |
87 | static inline int ptr_valid(struct pool *pool, void *ptr) |
88 | { | |
dcb69098 | 89 | unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL; |
ec996e9c JA |
90 | |
91 | return (ptr >= pool->map) && (ptr < pool->map + pool_size); | |
d24c33a4 JA |
92 | } |
93 | ||
808e9ea8 JA |
94 | static inline unsigned int size_to_blocks(unsigned int size) |
95 | { | |
96 | return (size + SMALLOC_BPB - 1) / SMALLOC_BPB; | |
97 | } | |
98 | ||
dcb69098 JA |
99 | static int blocks_iter(struct pool *pool, unsigned int pool_idx, |
100 | unsigned int idx, unsigned int nr_blocks, | |
ec996e9c | 101 | int (*func)(unsigned int *map, unsigned int mask)) |
d24c33a4 | 102 | { |
dcb69098 | 103 | |
ec996e9c JA |
104 | while (nr_blocks) { |
105 | unsigned int this_blocks, mask; | |
dcb69098 JA |
106 | unsigned int *map; |
107 | ||
108 | if (pool_idx >= pool->nr_blocks) | |
109 | return 0; | |
110 | ||
111 | map = &pool->bitmap[pool_idx]; | |
ec996e9c JA |
112 | |
113 | this_blocks = nr_blocks; | |
114 | if (this_blocks + idx > SMALLOC_BPI) { | |
115 | this_blocks = SMALLOC_BPI - idx; | |
116 | idx = SMALLOC_BPI - this_blocks; | |
117 | } | |
118 | ||
119 | if (this_blocks == SMALLOC_BPI) | |
120 | mask = -1U; | |
121 | else | |
122 | mask = ((1U << this_blocks) - 1) << idx; | |
123 | ||
124 | if (!func(map, mask)) | |
125 | return 0; | |
126 | ||
127 | nr_blocks -= this_blocks; | |
128 | idx = 0; | |
dcb69098 | 129 | pool_idx++; |
ec996e9c JA |
130 | } |
131 | ||
132 | return 1; | |
d24c33a4 JA |
133 | } |
134 | ||
ec996e9c | 135 | static int mask_cmp(unsigned int *map, unsigned int mask) |
d24c33a4 | 136 | { |
ec996e9c | 137 | return !(*map & mask); |
d24c33a4 JA |
138 | } |
139 | ||
ec996e9c | 140 | static int mask_clear(unsigned int *map, unsigned int mask) |
d24c33a4 | 141 | { |
dcb69098 | 142 | assert((*map & mask) == mask); |
ec996e9c JA |
143 | *map &= ~mask; |
144 | return 1; | |
d24c33a4 JA |
145 | } |
146 | ||
ec996e9c | 147 | static int mask_set(unsigned int *map, unsigned int mask) |
d24c33a4 | 148 | { |
dcb69098 | 149 | assert(!(*map & mask)); |
ec996e9c JA |
150 | *map |= mask; |
151 | return 1; | |
d24c33a4 JA |
152 | } |
153 | ||
dcb69098 JA |
154 | static int blocks_free(struct pool *pool, unsigned int pool_idx, |
155 | unsigned int idx, unsigned int nr_blocks) | |
d24c33a4 | 156 | { |
dcb69098 | 157 | return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp); |
d24c33a4 JA |
158 | } |
159 | ||
dcb69098 JA |
160 | static void set_blocks(struct pool *pool, unsigned int pool_idx, |
161 | unsigned int idx, unsigned int nr_blocks) | |
d24c33a4 | 162 | { |
dcb69098 | 163 | blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set); |
d24c33a4 JA |
164 | } |
165 | ||
dcb69098 JA |
166 | static void clear_blocks(struct pool *pool, unsigned int pool_idx, |
167 | unsigned int idx, unsigned int nr_blocks) | |
d24c33a4 | 168 | { |
dcb69098 | 169 | blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear); |
d24c33a4 JA |
170 | } |
171 | ||
ec996e9c JA |
172 | static int find_next_zero(int word, int start) |
173 | { | |
174 | assert(word != -1U); | |
175 | word >>= (start + 1); | |
b3268b92 | 176 | return ffz(word) + start + 1; |
d24c33a4 JA |
177 | } |
178 | ||
adf57099 | 179 | static int add_pool(struct pool *pool, unsigned int alloc_size) |
d24c33a4 | 180 | { |
3a8600b4 | 181 | int fd, bitmap_blocks, ret; |
b8a6582e JA |
182 | char file[] = "/tmp/.fio_smalloc.XXXXXX"; |
183 | void *ptr; | |
ec996e9c | 184 | |
b8a6582e | 185 | fd = mkstemp(file); |
d24c33a4 JA |
186 | if (fd < 0) |
187 | goto out_close; | |
188 | ||
55f6491d | 189 | #ifdef SMALLOC_REDZONE |
ec996e9c | 190 | alloc_size += sizeof(unsigned int); |
55f6491d | 191 | #endif |
ec996e9c JA |
192 | alloc_size += sizeof(struct block_hdr); |
193 | if (alloc_size < INITIAL_SIZE) | |
194 | alloc_size = INITIAL_SIZE; | |
195 | ||
196 | /* round up to nearest full number of blocks */ | |
197 | alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1); | |
198 | bitmap_blocks = alloc_size / SMALLOC_BPL; | |
199 | alloc_size += bitmap_blocks * sizeof(unsigned int); | |
200 | pool->mmap_size = alloc_size; | |
0b9d69ec | 201 | |
ec996e9c JA |
202 | pool->nr_blocks = bitmap_blocks; |
203 | pool->free_blocks = bitmap_blocks * SMALLOC_BPB; | |
adf57099 | 204 | |
3a8600b4 GE |
205 | #ifdef FIO_HAVE_FALLOCATE |
206 | ret = posix_fallocate(fd, 0, alloc_size); | |
207 | if (ret > 0) { | |
208 | fprintf(stderr, "posix_fallocate pool file failed: %s\n", strerror(ret)); | |
209 | goto out_unlink; | |
210 | } | |
211 | #endif | |
212 | ||
ec996e9c | 213 | if (ftruncate(fd, alloc_size) < 0) |
d24c33a4 JA |
214 | goto out_unlink; |
215 | ||
ec996e9c | 216 | ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
d24c33a4 JA |
217 | if (ptr == MAP_FAILED) |
218 | goto out_unlink; | |
219 | ||
ec996e9c JA |
220 | memset(ptr, 0, alloc_size); |
221 | pool->map = ptr; | |
222 | pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL); | |
d24c33a4 | 223 | |
6548f47f | 224 | pool->lock = fio_mutex_init(1); |
d24c33a4 JA |
225 | if (!pool->lock) |
226 | goto out_unlink; | |
d24c33a4 | 227 | |
443bb114 JA |
228 | /* |
229 | * Unlink pool file now. It wont get deleted until the fd is closed, | |
230 | * which happens both for cleanup or unexpected quit. This way we | |
231 | * don't leave temp files around in case of a crash. | |
232 | */ | |
b8a6582e | 233 | unlink(file); |
d24c33a4 JA |
234 | pool->fd = fd; |
235 | ||
d24c33a4 JA |
236 | nr_pools++; |
237 | return 0; | |
238 | out_unlink: | |
ec996e9c | 239 | fprintf(stderr, "smalloc: failed adding pool\n"); |
d24c33a4 | 240 | if (pool->map) |
ec996e9c | 241 | munmap(pool->map, pool->mmap_size); |
b8a6582e | 242 | unlink(file); |
d24c33a4 | 243 | out_close: |
b8a6582e | 244 | close(fd); |
d24c33a4 JA |
245 | return 1; |
246 | } | |
247 | ||
248 | void sinit(void) | |
249 | { | |
4d4e80f2 | 250 | int ret; |
d24c33a4 | 251 | |
6548f47f | 252 | lock = fio_mutex_rw_init(); |
adf57099 | 253 | ret = add_pool(&mp[0], INITIAL_SIZE); |
d24c33a4 JA |
254 | assert(!ret); |
255 | } | |
256 | ||
257 | static void cleanup_pool(struct pool *pool) | |
258 | { | |
443bb114 JA |
259 | /* |
260 | * This will also remove the temporary file we used as a backing | |
261 | * store, it was already unlinked | |
262 | */ | |
d24c33a4 | 263 | close(pool->fd); |
ec996e9c | 264 | munmap(pool->map, pool->mmap_size); |
6548f47f JA |
265 | |
266 | if (pool->lock) | |
267 | fio_mutex_remove(pool->lock); | |
d24c33a4 JA |
268 | } |
269 | ||
270 | void scleanup(void) | |
271 | { | |
272 | unsigned int i; | |
273 | ||
274 | for (i = 0; i < nr_pools; i++) | |
275 | cleanup_pool(&mp[i]); | |
276 | ||
6548f47f JA |
277 | if (lock) |
278 | fio_mutex_remove(lock); | |
d24c33a4 JA |
279 | } |
280 | ||
89da54e8 | 281 | #ifdef SMALLOC_REDZONE |
cf98708d JA |
282 | static void *postred_ptr(struct block_hdr *hdr) |
283 | { | |
cf98708d JA |
284 | unsigned long ptr; |
285 | ||
286 | ptr = (unsigned long) hdr + hdr->size - sizeof(unsigned int); | |
287 | ptr = (ptr + int_mask) & ~int_mask; | |
288 | ||
289 | return (void *) ptr; | |
290 | } | |
291 | ||
ec996e9c | 292 | static void fill_redzone(struct block_hdr *hdr) |
55f6491d | 293 | { |
cf98708d | 294 | unsigned int *postred = postred_ptr(hdr); |
55f6491d | 295 | |
ec996e9c JA |
296 | hdr->prered = SMALLOC_PRE_RED; |
297 | *postred = SMALLOC_POST_RED; | |
ec996e9c | 298 | } |
55f6491d | 299 | |
ec996e9c JA |
300 | static void sfree_check_redzone(struct block_hdr *hdr) |
301 | { | |
cf98708d | 302 | unsigned int *postred = postred_ptr(hdr); |
ec996e9c JA |
303 | |
304 | if (hdr->prered != SMALLOC_PRE_RED) { | |
55f6491d JA |
305 | fprintf(stderr, "smalloc pre redzone destroyed!\n"); |
306 | fprintf(stderr, " ptr=%p, prered=%x, expected %x\n", | |
ec996e9c | 307 | hdr, hdr->prered, SMALLOC_PRE_RED); |
55f6491d JA |
308 | assert(0); |
309 | } | |
310 | if (*postred != SMALLOC_POST_RED) { | |
311 | fprintf(stderr, "smalloc post redzone destroyed!\n"); | |
312 | fprintf(stderr, " ptr=%p, postred=%x, expected %x\n", | |
ec996e9c | 313 | hdr, *postred, SMALLOC_POST_RED); |
55f6491d JA |
314 | assert(0); |
315 | } | |
89da54e8 JA |
316 | } |
317 | #else | |
318 | static void fill_redzone(struct block_hdr *hdr) | |
319 | { | |
55f6491d JA |
320 | } |
321 | ||
89da54e8 JA |
322 | static void sfree_check_redzone(struct block_hdr *hdr) |
323 | { | |
324 | } | |
325 | #endif | |
326 | ||
d24c33a4 JA |
327 | static void sfree_pool(struct pool *pool, void *ptr) |
328 | { | |
ec996e9c | 329 | struct block_hdr *hdr; |
179446e0 | 330 | unsigned int i, idx; |
ec996e9c | 331 | unsigned long offset; |
d24c33a4 JA |
332 | |
333 | if (!ptr) | |
334 | return; | |
335 | ||
ec996e9c JA |
336 | ptr -= sizeof(*hdr); |
337 | hdr = ptr; | |
55f6491d | 338 | |
d24c33a4 JA |
339 | assert(ptr_valid(pool, ptr)); |
340 | ||
ec996e9c | 341 | sfree_check_redzone(hdr); |
d24c33a4 | 342 | |
ec996e9c JA |
343 | offset = ptr - pool->map; |
344 | i = offset / SMALLOC_BPL; | |
345 | idx = (offset % SMALLOC_BPL) / SMALLOC_BPB; | |
d24c33a4 | 346 | |
ec996e9c | 347 | pool_lock(pool); |
dcb69098 | 348 | clear_blocks(pool, i, idx, size_to_blocks(hdr->size)); |
ec996e9c JA |
349 | if (i < pool->next_non_full) |
350 | pool->next_non_full = i; | |
179446e0 | 351 | pool->free_blocks += size_to_blocks(hdr->size); |
d24c33a4 JA |
352 | pool_unlock(pool); |
353 | } | |
354 | ||
355 | void sfree(void *ptr) | |
356 | { | |
357 | struct pool *pool = NULL; | |
358 | unsigned int i; | |
359 | ||
8e5732e5 JA |
360 | if (!ptr) |
361 | return; | |
362 | ||
65864cf7 | 363 | global_read_lock(); |
d24c33a4 JA |
364 | |
365 | for (i = 0; i < nr_pools; i++) { | |
366 | if (ptr_valid(&mp[i], ptr)) { | |
367 | pool = &mp[i]; | |
368 | break; | |
369 | } | |
370 | } | |
371 | ||
65864cf7 | 372 | global_read_unlock(); |
d24c33a4 JA |
373 | |
374 | assert(pool); | |
375 | sfree_pool(pool, ptr); | |
376 | } | |
377 | ||
55f6491d | 378 | static void *__smalloc_pool(struct pool *pool, unsigned int size) |
d24c33a4 | 379 | { |
ec996e9c JA |
380 | unsigned int nr_blocks; |
381 | unsigned int i; | |
382 | unsigned int offset; | |
383 | unsigned int last_idx; | |
384 | void *ret = NULL; | |
d24c33a4 | 385 | |
d24c33a4 | 386 | pool_lock(pool); |
179446e0 JA |
387 | |
388 | nr_blocks = size_to_blocks(size); | |
ec996e9c | 389 | if (nr_blocks > pool->free_blocks) |
8e5732e5 | 390 | goto fail; |
5ec10eaa | 391 | |
ec996e9c JA |
392 | i = pool->next_non_full; |
393 | last_idx = 0; | |
394 | offset = -1U; | |
395 | while (i < pool->nr_blocks) { | |
396 | unsigned int idx; | |
d24c33a4 | 397 | |
ec996e9c JA |
398 | if (pool->bitmap[i] == -1U) { |
399 | i++; | |
400 | pool->next_non_full = i; | |
401 | last_idx = 0; | |
402 | continue; | |
403 | } | |
d24c33a4 | 404 | |
ec996e9c | 405 | idx = find_next_zero(pool->bitmap[i], last_idx); |
dcb69098 | 406 | if (!blocks_free(pool, i, idx, nr_blocks)) { |
ec996e9c JA |
407 | idx += nr_blocks; |
408 | if (idx < SMALLOC_BPI) | |
409 | last_idx = idx; | |
410 | else { | |
411 | last_idx = 0; | |
412 | while (idx >= SMALLOC_BPI) { | |
413 | i++; | |
414 | idx -= SMALLOC_BPI; | |
415 | } | |
416 | } | |
417 | continue; | |
d24c33a4 | 418 | } |
dcb69098 | 419 | set_blocks(pool, i, idx, nr_blocks); |
ec996e9c JA |
420 | offset = i * SMALLOC_BPL + idx * SMALLOC_BPB; |
421 | break; | |
422 | } | |
423 | ||
424 | if (i < pool->nr_blocks) { | |
425 | pool->free_blocks -= nr_blocks; | |
426 | ret = pool->map + offset; | |
d24c33a4 | 427 | } |
ec996e9c | 428 | fail: |
d24c33a4 | 429 | pool_unlock(pool); |
ec996e9c | 430 | return ret; |
d24c33a4 JA |
431 | } |
432 | ||
55f6491d JA |
433 | static void *smalloc_pool(struct pool *pool, unsigned int size) |
434 | { | |
89da54e8 | 435 | unsigned int alloc_size = size + sizeof(struct block_hdr); |
55f6491d JA |
436 | void *ptr; |
437 | ||
cf98708d | 438 | /* |
122426da JA |
439 | * Round to int alignment, so that the postred pointer will |
440 | * be naturally aligned as well. | |
cf98708d | 441 | */ |
ec996e9c | 442 | #ifdef SMALLOC_REDZONE |
122426da JA |
443 | alloc_size += sizeof(unsigned int); |
444 | alloc_size = (alloc_size + int_mask) & ~int_mask; | |
ec996e9c JA |
445 | #endif |
446 | ||
447 | ptr = __smalloc_pool(pool, alloc_size); | |
89da54e8 JA |
448 | if (ptr) { |
449 | struct block_hdr *hdr = ptr; | |
55f6491d | 450 | |
89da54e8 JA |
451 | hdr->size = alloc_size; |
452 | fill_redzone(hdr); | |
55f6491d | 453 | |
89da54e8 JA |
454 | ptr += sizeof(*hdr); |
455 | memset(ptr, 0, size); | |
456 | } | |
ec996e9c | 457 | |
55f6491d | 458 | return ptr; |
55f6491d JA |
459 | } |
460 | ||
d24c33a4 JA |
461 | void *smalloc(unsigned int size) |
462 | { | |
463 | unsigned int i; | |
464 | ||
d1271dc1 | 465 | global_write_lock(); |
d24c33a4 JA |
466 | i = last_pool; |
467 | ||
468 | do { | |
469 | for (; i < nr_pools; i++) { | |
470 | void *ptr = smalloc_pool(&mp[i], size); | |
471 | ||
472 | if (ptr) { | |
473 | last_pool = i; | |
d1271dc1 | 474 | global_write_unlock(); |
d24c33a4 JA |
475 | return ptr; |
476 | } | |
477 | } | |
478 | if (last_pool) { | |
479 | last_pool = 0; | |
480 | continue; | |
481 | } | |
482 | ||
ec996e9c | 483 | if (nr_pools + 1 > MAX_POOLS) |
d24c33a4 JA |
484 | break; |
485 | else { | |
486 | i = nr_pools; | |
adf57099 | 487 | if (add_pool(&mp[nr_pools], size)) |
65864cf7 | 488 | goto out; |
d24c33a4 JA |
489 | } |
490 | } while (1); | |
491 | ||
65864cf7 | 492 | out: |
d1271dc1 | 493 | global_write_unlock(); |
d24c33a4 JA |
494 | return NULL; |
495 | } | |
496 | ||
497 | char *smalloc_strdup(const char *str) | |
498 | { | |
499 | char *ptr; | |
500 | ||
501 | ptr = smalloc(strlen(str) + 1); | |
502 | strcpy(ptr, str); | |
503 | return ptr; | |
504 | } |