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