zbd: Fix job zone size initialization
[fio.git] / smalloc.c
CommitLineData
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>
d24c33a4
JA
6#include <assert.h>
7#include <string.h>
d24c33a4 8
248c9436 9#include "fio.h"
971caeb1 10#include "fio_sem.h"
3a8600b4 11#include "os/os.h"
10aa136b 12#include "smalloc.h"
b0f0326a 13#include "log.h"
d24c33a4 14
55f6491d 15#define SMALLOC_REDZONE /* define to detect memory corruption */
d24c33a4 16
ec996e9c
JA
17#define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
18#define SMALLOC_BPI (sizeof(unsigned int) * 8)
19#define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
20
23bd40f9 21#define INITIAL_SIZE 16*1024*1024 /* new pool size */
5f9454a2
JA
22#define INITIAL_POOLS 8 /* maximum number of pools to setup */
23
24#define MAX_POOLS 16
d24c33a4 25
55f6491d
JA
26#define SMALLOC_PRE_RED 0xdeadbeefU
27#define SMALLOC_POST_RED 0x5aa55aa5U
55f6491d 28
2b386d25 29unsigned int smalloc_pool_size = INITIAL_SIZE;
aa1af5fd 30#ifdef SMALLOC_REDZONE
10aa136b 31static const int int_mask = sizeof(int) - 1;
aa1af5fd 32#endif
2b386d25 33
d24c33a4 34struct pool {
971caeb1 35 struct fio_sem *lock; /* protects this pool */
d24c33a4 36 void *map; /* map of blocks */
ec996e9c 37 unsigned int *bitmap; /* blocks free/busy map */
a3ebe7e0
JA
38 size_t free_blocks; /* free blocks */
39 size_t nr_blocks; /* total blocks */
40 size_t next_non_full;
41 size_t mmap_size;
ec996e9c
JA
42};
43
44struct block_hdr {
a3ebe7e0 45 size_t size;
ec996e9c
JA
46#ifdef SMALLOC_REDZONE
47 unsigned int prered;
48#endif
d24c33a4
JA
49};
50
38b253f3
VF
51/*
52 * This suppresses the voluminous potential bitmap printout when
53 * smalloc encounters an OOM error
54 */
55static const bool enable_smalloc_debug = false;
56
d24c33a4
JA
57static struct pool mp[MAX_POOLS];
58static unsigned int nr_pools;
59static unsigned int last_pool;
d24c33a4 60
d24c33a4
JA
61static inline int ptr_valid(struct pool *pool, void *ptr)
62{
dcb69098 63 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
ec996e9c
JA
64
65 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
d24c33a4
JA
66}
67
a3ebe7e0 68static inline size_t size_to_blocks(size_t size)
808e9ea8
JA
69{
70 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
71}
72
dcb69098 73static int blocks_iter(struct pool *pool, unsigned int pool_idx,
a3ebe7e0 74 unsigned int idx, size_t nr_blocks,
ec996e9c 75 int (*func)(unsigned int *map, unsigned int mask))
d24c33a4 76{
dcb69098 77
ec996e9c
JA
78 while (nr_blocks) {
79 unsigned int this_blocks, mask;
dcb69098
JA
80 unsigned int *map;
81
82 if (pool_idx >= pool->nr_blocks)
83 return 0;
84
85 map = &pool->bitmap[pool_idx];
ec996e9c
JA
86
87 this_blocks = nr_blocks;
88 if (this_blocks + idx > SMALLOC_BPI) {
89 this_blocks = SMALLOC_BPI - idx;
90 idx = SMALLOC_BPI - this_blocks;
91 }
92
93 if (this_blocks == SMALLOC_BPI)
94 mask = -1U;
95 else
96 mask = ((1U << this_blocks) - 1) << idx;
97
98 if (!func(map, mask))
99 return 0;
100
101 nr_blocks -= this_blocks;
102 idx = 0;
dcb69098 103 pool_idx++;
ec996e9c
JA
104 }
105
106 return 1;
d24c33a4
JA
107}
108
ec996e9c 109static int mask_cmp(unsigned int *map, unsigned int mask)
d24c33a4 110{
ec996e9c 111 return !(*map & mask);
d24c33a4
JA
112}
113
ec996e9c 114static int mask_clear(unsigned int *map, unsigned int mask)
d24c33a4 115{
dcb69098 116 assert((*map & mask) == mask);
ec996e9c
JA
117 *map &= ~mask;
118 return 1;
d24c33a4
JA
119}
120
ec996e9c 121static int mask_set(unsigned int *map, unsigned int mask)
d24c33a4 122{
dcb69098 123 assert(!(*map & mask));
ec996e9c
JA
124 *map |= mask;
125 return 1;
d24c33a4
JA
126}
127
dcb69098 128static int blocks_free(struct pool *pool, unsigned int pool_idx,
a3ebe7e0 129 unsigned int idx, size_t nr_blocks)
d24c33a4 130{
dcb69098 131 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
d24c33a4
JA
132}
133
dcb69098 134static void set_blocks(struct pool *pool, unsigned int pool_idx,
a3ebe7e0 135 unsigned int idx, size_t nr_blocks)
d24c33a4 136{
dcb69098 137 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
d24c33a4
JA
138}
139
dcb69098 140static void clear_blocks(struct pool *pool, unsigned int pool_idx,
a3ebe7e0 141 unsigned int idx, size_t nr_blocks)
d24c33a4 142{
dcb69098 143 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
d24c33a4
JA
144}
145
ec996e9c
JA
146static int find_next_zero(int word, int start)
147{
148 assert(word != -1U);
271067a6
JH
149 word >>= start;
150 return ffz(word) + start;
d24c33a4
JA
151}
152
5f9454a2 153static bool add_pool(struct pool *pool, unsigned int alloc_size)
d24c33a4 154{
8d5844e9 155 int bitmap_blocks;
c8931876 156 int mmap_flags;
b8a6582e 157 void *ptr;
ec996e9c 158
5f9454a2
JA
159 if (nr_pools == MAX_POOLS)
160 return false;
161
55f6491d 162#ifdef SMALLOC_REDZONE
ec996e9c 163 alloc_size += sizeof(unsigned int);
55f6491d 164#endif
ec996e9c
JA
165 alloc_size += sizeof(struct block_hdr);
166 if (alloc_size < INITIAL_SIZE)
167 alloc_size = INITIAL_SIZE;
168
169 /* round up to nearest full number of blocks */
170 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
171 bitmap_blocks = alloc_size / SMALLOC_BPL;
172 alloc_size += bitmap_blocks * sizeof(unsigned int);
173 pool->mmap_size = alloc_size;
0b9d69ec 174
ec996e9c
JA
175 pool->nr_blocks = bitmap_blocks;
176 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
adf57099 177
c8931876
JA
178 mmap_flags = OS_MAP_ANON;
179#ifdef CONFIG_ESX
180 mmap_flags |= MAP_PRIVATE;
181#else
182 mmap_flags |= MAP_SHARED;
183#endif
184 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0);
185
d24c33a4 186 if (ptr == MAP_FAILED)
8d5844e9 187 goto out_fail;
d24c33a4 188
ec996e9c 189 pool->map = ptr;
17f7fcd0 190 pool->bitmap = (unsigned int *)((char *) ptr + (pool->nr_blocks * SMALLOC_BPL));
9c3e13e3 191 memset(pool->bitmap, 0, bitmap_blocks * sizeof(unsigned int));
d24c33a4 192
971caeb1 193 pool->lock = fio_sem_init(FIO_SEM_UNLOCKED);
d24c33a4 194 if (!pool->lock)
8d5844e9 195 goto out_fail;
d24c33a4 196
d24c33a4 197 nr_pools++;
5f9454a2 198 return true;
8d5844e9 199out_fail:
b0f0326a 200 log_err("smalloc: failed adding pool\n");
d24c33a4 201 if (pool->map)
ec996e9c 202 munmap(pool->map, pool->mmap_size);
5f9454a2 203 return false;
d24c33a4
JA
204}
205
206void sinit(void)
207{
5f9454a2
JA
208 bool ret;
209 int i;
d24c33a4 210
5f9454a2
JA
211 for (i = 0; i < INITIAL_POOLS; i++) {
212 ret = add_pool(&mp[nr_pools], smalloc_pool_size);
213 if (!ret)
85492cb8
JA
214 break;
215 }
216
217 /*
218 * If we added at least one pool, we should be OK for most
219 * cases.
220 */
221 assert(i);
d24c33a4
JA
222}
223
224static void cleanup_pool(struct pool *pool)
225{
443bb114
JA
226 /*
227 * This will also remove the temporary file we used as a backing
228 * store, it was already unlinked
229 */
ec996e9c 230 munmap(pool->map, pool->mmap_size);
6548f47f
JA
231
232 if (pool->lock)
971caeb1 233 fio_sem_remove(pool->lock);
d24c33a4
JA
234}
235
236void scleanup(void)
237{
238 unsigned int i;
239
240 for (i = 0; i < nr_pools; i++)
241 cleanup_pool(&mp[i]);
d24c33a4
JA
242}
243
89da54e8 244#ifdef SMALLOC_REDZONE
cf98708d
JA
245static void *postred_ptr(struct block_hdr *hdr)
246{
e43606c2 247 uintptr_t ptr;
cf98708d 248
e43606c2 249 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
248c9436 250 ptr = (uintptr_t) PTR_ALIGN(ptr, int_mask);
cf98708d
JA
251
252 return (void *) ptr;
253}
254
ec996e9c 255static void fill_redzone(struct block_hdr *hdr)
55f6491d 256{
cf98708d 257 unsigned int *postred = postred_ptr(hdr);
55f6491d 258
ec996e9c
JA
259 hdr->prered = SMALLOC_PRE_RED;
260 *postred = SMALLOC_POST_RED;
ec996e9c 261}
55f6491d 262
ec996e9c
JA
263static void sfree_check_redzone(struct block_hdr *hdr)
264{
cf98708d 265 unsigned int *postred = postred_ptr(hdr);
ec996e9c
JA
266
267 if (hdr->prered != SMALLOC_PRE_RED) {
b0f0326a
JA
268 log_err("smalloc pre redzone destroyed!\n"
269 " ptr=%p, prered=%x, expected %x\n",
ec996e9c 270 hdr, hdr->prered, SMALLOC_PRE_RED);
55f6491d
JA
271 assert(0);
272 }
273 if (*postred != SMALLOC_POST_RED) {
b0f0326a
JA
274 log_err("smalloc post redzone destroyed!\n"
275 " ptr=%p, postred=%x, expected %x\n",
ec996e9c 276 hdr, *postred, SMALLOC_POST_RED);
55f6491d
JA
277 assert(0);
278 }
89da54e8
JA
279}
280#else
281static void fill_redzone(struct block_hdr *hdr)
282{
55f6491d
JA
283}
284
89da54e8
JA
285static void sfree_check_redzone(struct block_hdr *hdr)
286{
287}
288#endif
289
d24c33a4
JA
290static void sfree_pool(struct pool *pool, void *ptr)
291{
ec996e9c 292 struct block_hdr *hdr;
179446e0 293 unsigned int i, idx;
ec996e9c 294 unsigned long offset;
d24c33a4
JA
295
296 if (!ptr)
297 return;
298
ec996e9c
JA
299 ptr -= sizeof(*hdr);
300 hdr = ptr;
55f6491d 301
d24c33a4
JA
302 assert(ptr_valid(pool, ptr));
303
ec996e9c 304 sfree_check_redzone(hdr);
d24c33a4 305
ec996e9c
JA
306 offset = ptr - pool->map;
307 i = offset / SMALLOC_BPL;
308 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
d24c33a4 309
971caeb1 310 fio_sem_down(pool->lock);
dcb69098 311 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
ec996e9c
JA
312 if (i < pool->next_non_full)
313 pool->next_non_full = i;
179446e0 314 pool->free_blocks += size_to_blocks(hdr->size);
971caeb1 315 fio_sem_up(pool->lock);
d24c33a4
JA
316}
317
318void sfree(void *ptr)
319{
320 struct pool *pool = NULL;
321 unsigned int i;
322
8e5732e5
JA
323 if (!ptr)
324 return;
325
d24c33a4
JA
326 for (i = 0; i < nr_pools; i++) {
327 if (ptr_valid(&mp[i], ptr)) {
328 pool = &mp[i];
329 break;
330 }
331 }
332
45a65144
JA
333 if (pool) {
334 sfree_pool(pool, ptr);
335 return;
336 }
337
338 log_err("smalloc: ptr %p not from smalloc pool\n", ptr);
d24c33a4
JA
339}
340
66b182f9 341static unsigned int find_best_index(struct pool *pool)
554461db 342{
66b182f9
JA
343 unsigned int i;
344
345 assert(pool->free_blocks);
554461db 346
66b182f9
JA
347 for (i = pool->next_non_full; pool->bitmap[i] == -1U; i++) {
348 if (i == pool->nr_blocks - 1) {
349 unsigned int j;
554461db 350
66b182f9
JA
351 for (j = 0; j < pool->nr_blocks; j++)
352 if (pool->bitmap[j] != -1U)
353 return j;
354 }
355 }
82a90566 356
66b182f9 357 return i;
554461db
VF
358}
359
a3ebe7e0 360static void *__smalloc_pool(struct pool *pool, size_t size)
d24c33a4 361{
a3ebe7e0 362 size_t nr_blocks;
ec996e9c
JA
363 unsigned int i;
364 unsigned int offset;
365 unsigned int last_idx;
366 void *ret = NULL;
d24c33a4 367
971caeb1 368 fio_sem_down(pool->lock);
179446e0
JA
369
370 nr_blocks = size_to_blocks(size);
ec996e9c 371 if (nr_blocks > pool->free_blocks)
8e5732e5 372 goto fail;
5ec10eaa 373
66b182f9 374 pool->next_non_full = find_best_index(pool);
554461db 375
ec996e9c
JA
376 last_idx = 0;
377 offset = -1U;
66b182f9 378 i = pool->next_non_full;
ec996e9c
JA
379 while (i < pool->nr_blocks) {
380 unsigned int idx;
d24c33a4 381
ec996e9c
JA
382 if (pool->bitmap[i] == -1U) {
383 i++;
ec996e9c
JA
384 last_idx = 0;
385 continue;
386 }
d24c33a4 387
ec996e9c 388 idx = find_next_zero(pool->bitmap[i], last_idx);
dcb69098 389 if (!blocks_free(pool, i, idx, nr_blocks)) {
ec996e9c
JA
390 idx += nr_blocks;
391 if (idx < SMALLOC_BPI)
392 last_idx = idx;
393 else {
394 last_idx = 0;
395 while (idx >= SMALLOC_BPI) {
396 i++;
397 idx -= SMALLOC_BPI;
398 }
399 }
400 continue;
d24c33a4 401 }
dcb69098 402 set_blocks(pool, i, idx, nr_blocks);
ec996e9c
JA
403 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
404 break;
405 }
406
407 if (i < pool->nr_blocks) {
408 pool->free_blocks -= nr_blocks;
409 ret = pool->map + offset;
d24c33a4 410 }
ec996e9c 411fail:
971caeb1 412 fio_sem_up(pool->lock);
ec996e9c 413 return ret;
d24c33a4
JA
414}
415
38b253f3 416static size_t size_to_alloc_size(size_t size)
55f6491d 417{
a3ebe7e0 418 size_t alloc_size = size + sizeof(struct block_hdr);
55f6491d 419
cf98708d 420 /*
122426da
JA
421 * Round to int alignment, so that the postred pointer will
422 * be naturally aligned as well.
cf98708d 423 */
ec996e9c 424#ifdef SMALLOC_REDZONE
122426da
JA
425 alloc_size += sizeof(unsigned int);
426 alloc_size = (alloc_size + int_mask) & ~int_mask;
ec996e9c
JA
427#endif
428
38b253f3
VF
429 return alloc_size;
430}
431
432static void *smalloc_pool(struct pool *pool, size_t size)
433{
434 size_t alloc_size = size_to_alloc_size(size);
435 void *ptr;
436
ec996e9c 437 ptr = __smalloc_pool(pool, alloc_size);
89da54e8
JA
438 if (ptr) {
439 struct block_hdr *hdr = ptr;
55f6491d 440
89da54e8
JA
441 hdr->size = alloc_size;
442 fill_redzone(hdr);
55f6491d 443
89da54e8
JA
444 ptr += sizeof(*hdr);
445 memset(ptr, 0, size);
446 }
ec996e9c 447
55f6491d 448 return ptr;
55f6491d
JA
449}
450
38b253f3
VF
451static void smalloc_print_bitmap(struct pool *pool)
452{
453 size_t nr_blocks = pool->nr_blocks;
454 unsigned int *bitmap = pool->bitmap;
455 unsigned int i, j;
456 char *buffer;
457
458 if (!enable_smalloc_debug)
459 return;
460
461 buffer = malloc(SMALLOC_BPI + 1);
462 if (!buffer)
463 return;
464 buffer[SMALLOC_BPI] = '\0';
465
466 for (i = 0; i < nr_blocks; i++) {
467 unsigned int line = bitmap[i];
468
469 /* skip completely full lines */
470 if (line == -1U)
471 continue;
472
473 for (j = 0; j < SMALLOC_BPI; j++)
474 if ((1 << j) & line)
475 buffer[SMALLOC_BPI-1-j] = '1';
476 else
477 buffer[SMALLOC_BPI-1-j] = '0';
478
479 log_err("smalloc: bitmap %5u, %s\n", i, buffer);
480 }
481
482 free(buffer);
483}
484
485void smalloc_debug(size_t size)
486{
487 unsigned int i;
488 size_t alloc_size = size_to_alloc_size(size);
489 size_t alloc_blocks;
490
491 alloc_blocks = size_to_blocks(alloc_size);
492
493 if (size)
494 log_err("smalloc: size = %lu, alloc_size = %lu, blocks = %lu\n",
495 (unsigned long) size, (unsigned long) alloc_size,
496 (unsigned long) alloc_blocks);
497 for (i = 0; i < nr_pools; i++) {
498 log_err("smalloc: pool %u, free/total blocks %u/%u\n", i,
499 (unsigned int) (mp[i].free_blocks),
500 (unsigned int) (mp[i].nr_blocks*sizeof(unsigned int)*8));
501 if (size && mp[i].free_blocks >= alloc_blocks) {
502 void *ptr = smalloc_pool(&mp[i], size);
503 if (ptr) {
504 sfree(ptr);
505 last_pool = i;
506 log_err("smalloc: smalloc_pool %u succeeded\n", i);
507 } else {
508 log_err("smalloc: smalloc_pool %u failed\n", i);
509 log_err("smalloc: next_non_full=%u, nr_blocks=%u\n",
510 (unsigned int) mp[i].next_non_full, (unsigned int) mp[i].nr_blocks);
511 smalloc_print_bitmap(&mp[i]);
512 }
513 }
514 }
515}
516
5457259f 517void *smalloc(size_t size)
d24c33a4 518{
85492cb8 519 unsigned int i, end_pool;
d24c33a4 520
7982aa7d
JA
521 if (size != (unsigned int) size)
522 return NULL;
523
d24c33a4 524 i = last_pool;
85492cb8 525 end_pool = nr_pools;
d24c33a4
JA
526
527 do {
85492cb8 528 for (; i < end_pool; i++) {
d24c33a4
JA
529 void *ptr = smalloc_pool(&mp[i], size);
530
531 if (ptr) {
532 last_pool = i;
d24c33a4
JA
533 return ptr;
534 }
535 }
536 if (last_pool) {
85492cb8
JA
537 end_pool = last_pool;
538 last_pool = i = 0;
d24c33a4
JA
539 continue;
540 }
541
85492cb8 542 break;
d24c33a4
JA
543 } while (1);
544
81b3c86f
JA
545 log_err("smalloc: OOM. Consider using --alloc-size to increase the "
546 "shared memory available.\n");
38b253f3 547 smalloc_debug(size);
d24c33a4
JA
548 return NULL;
549}
550
544992f7
JA
551void *scalloc(size_t nmemb, size_t size)
552{
5457259f 553 return smalloc(nmemb * size);
544992f7
JA
554}
555
d24c33a4
JA
556char *smalloc_strdup(const char *str)
557{
2894a2d4 558 char *ptr = NULL;
d24c33a4
JA
559
560 ptr = smalloc(strlen(str) + 1);
2894a2d4
CE
561 if (ptr)
562 strcpy(ptr, str);
d24c33a4
JA
563 return ptr;
564}