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