Add FIO_HAVE_FS_STAT/get_fs_free_size() support for OpenBSD
[fio.git] / smalloc.c
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 <inttypes.h>
12 #include <sys/types.h>
13 #include <limits.h>
14 #include <fcntl.h>
15
16 #include "mutex.h"
17 #include "arch/arch.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
36 unsigned int smalloc_pool_size = INITIAL_SIZE;
37 #ifdef SMALLOC_REDZONE
38 static const int int_mask = sizeof(int) - 1;
39 #endif
40
41 struct pool {
42         struct fio_mutex *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 struct block_hdr {
52         size_t size;
53 #ifdef SMALLOC_REDZONE
54         unsigned int prered;
55 #endif
56 };
57
58 static struct pool mp[MAX_POOLS];
59 static unsigned int nr_pools;
60 static unsigned int last_pool;
61
62 static inline int ptr_valid(struct pool *pool, void *ptr)
63 {
64         unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
65
66         return (ptr >= pool->map) && (ptr < pool->map + pool_size);
67 }
68
69 static inline size_t size_to_blocks(size_t size)
70 {
71         return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
72 }
73
74 static int blocks_iter(struct pool *pool, unsigned int pool_idx,
75                        unsigned int idx, size_t nr_blocks,
76                        int (*func)(unsigned int *map, unsigned int mask))
77 {
78
79         while (nr_blocks) {
80                 unsigned int this_blocks, mask;
81                 unsigned int *map;
82
83                 if (pool_idx >= pool->nr_blocks)
84                         return 0;
85
86                 map = &pool->bitmap[pool_idx];
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;
104                 pool_idx++;
105         }
106
107         return 1;
108 }
109
110 static int mask_cmp(unsigned int *map, unsigned int mask)
111 {
112         return !(*map & mask);
113 }
114
115 static int mask_clear(unsigned int *map, unsigned int mask)
116 {
117         assert((*map & mask) == mask);
118         *map &= ~mask;
119         return 1;
120 }
121
122 static int mask_set(unsigned int *map, unsigned int mask)
123 {
124         assert(!(*map & mask));
125         *map |= mask;
126         return 1;
127 }
128
129 static int blocks_free(struct pool *pool, unsigned int pool_idx,
130                        unsigned int idx, size_t nr_blocks)
131 {
132         return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
133 }
134
135 static void set_blocks(struct pool *pool, unsigned int pool_idx,
136                        unsigned int idx, size_t nr_blocks)
137 {
138         blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
139 }
140
141 static void clear_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_clear);
145 }
146
147 static int find_next_zero(int word, int start)
148 {
149         assert(word != -1U);
150         word >>= start;
151         return ffz(word) + start;
152 }
153
154 static bool add_pool(struct pool *pool, unsigned int alloc_size)
155 {
156         int bitmap_blocks;
157         int mmap_flags;
158         void *ptr;
159
160         if (nr_pools == MAX_POOLS)
161                 return false;
162
163 #ifdef SMALLOC_REDZONE
164         alloc_size += sizeof(unsigned int);
165 #endif
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;
175
176         pool->nr_blocks = bitmap_blocks;
177         pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
178
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
187         if (ptr == MAP_FAILED)
188                 goto out_fail;
189
190         pool->map = ptr;
191         pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
192         memset(pool->bitmap, 0, bitmap_blocks * sizeof(unsigned int));
193
194         pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
195         if (!pool->lock)
196                 goto out_fail;
197
198         nr_pools++;
199         return true;
200 out_fail:
201         log_err("smalloc: failed adding pool\n");
202         if (pool->map)
203                 munmap(pool->map, pool->mmap_size);
204         return false;
205 }
206
207 void sinit(void)
208 {
209         bool ret;
210         int i;
211
212         for (i = 0; i < INITIAL_POOLS; i++) {
213                 ret = add_pool(&mp[nr_pools], smalloc_pool_size);
214                 if (!ret)
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);
223 }
224
225 static void cleanup_pool(struct pool *pool)
226 {
227         /*
228          * This will also remove the temporary file we used as a backing
229          * store, it was already unlinked
230          */
231         munmap(pool->map, pool->mmap_size);
232
233         if (pool->lock)
234                 fio_mutex_remove(pool->lock);
235 }
236
237 void scleanup(void)
238 {
239         unsigned int i;
240
241         for (i = 0; i < nr_pools; i++)
242                 cleanup_pool(&mp[i]);
243 }
244
245 #ifdef SMALLOC_REDZONE
246 static void *postred_ptr(struct block_hdr *hdr)
247 {
248         uintptr_t ptr;
249
250         ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
251         ptr = (ptr + int_mask) & ~int_mask;
252
253         return (void *) ptr;
254 }
255
256 static void fill_redzone(struct block_hdr *hdr)
257 {
258         unsigned int *postred = postred_ptr(hdr);
259
260         hdr->prered = SMALLOC_PRE_RED;
261         *postred = SMALLOC_POST_RED;
262 }
263
264 static void sfree_check_redzone(struct block_hdr *hdr)
265 {
266         unsigned int *postred = postred_ptr(hdr);
267
268         if (hdr->prered != SMALLOC_PRE_RED) {
269                 log_err("smalloc pre redzone destroyed!\n"
270                         " ptr=%p, prered=%x, expected %x\n",
271                                 hdr, hdr->prered, SMALLOC_PRE_RED);
272                 assert(0);
273         }
274         if (*postred != SMALLOC_POST_RED) {
275                 log_err("smalloc post redzone destroyed!\n"
276                         "  ptr=%p, postred=%x, expected %x\n",
277                                 hdr, *postred, SMALLOC_POST_RED);
278                 assert(0);
279         }
280 }
281 #else
282 static void fill_redzone(struct block_hdr *hdr)
283 {
284 }
285
286 static void sfree_check_redzone(struct block_hdr *hdr)
287 {
288 }
289 #endif
290
291 static void sfree_pool(struct pool *pool, void *ptr)
292 {
293         struct block_hdr *hdr;
294         unsigned int i, idx;
295         unsigned long offset;
296
297         if (!ptr)
298                 return;
299
300         ptr -= sizeof(*hdr);
301         hdr = ptr;
302
303         assert(ptr_valid(pool, ptr));
304
305         sfree_check_redzone(hdr);
306
307         offset = ptr - pool->map;
308         i = offset / SMALLOC_BPL;
309         idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
310
311         fio_mutex_down(pool->lock);
312         clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
313         if (i < pool->next_non_full)
314                 pool->next_non_full = i;
315         pool->free_blocks += size_to_blocks(hdr->size);
316         fio_mutex_up(pool->lock);
317 }
318
319 void sfree(void *ptr)
320 {
321         struct pool *pool = NULL;
322         unsigned int i;
323
324         if (!ptr)
325                 return;
326
327         for (i = 0; i < nr_pools; i++) {
328                 if (ptr_valid(&mp[i], ptr)) {
329                         pool = &mp[i];
330                         break;
331                 }
332         }
333
334         if (pool) {
335                 sfree_pool(pool, ptr);
336                 return;
337         }
338
339         log_err("smalloc: ptr %p not from smalloc pool\n", ptr);
340 }
341
342 static void *__smalloc_pool(struct pool *pool, size_t size)
343 {
344         size_t nr_blocks;
345         unsigned int i;
346         unsigned int offset;
347         unsigned int last_idx;
348         void *ret = NULL;
349
350         fio_mutex_down(pool->lock);
351
352         nr_blocks = size_to_blocks(size);
353         if (nr_blocks > pool->free_blocks)
354                 goto fail;
355
356         i = pool->next_non_full;
357         last_idx = 0;
358         offset = -1U;
359         while (i < pool->nr_blocks) {
360                 unsigned int idx;
361
362                 if (pool->bitmap[i] == -1U) {
363                         i++;
364                         pool->next_non_full = i;
365                         last_idx = 0;
366                         continue;
367                 }
368
369                 idx = find_next_zero(pool->bitmap[i], last_idx);
370                 if (!blocks_free(pool, i, idx, nr_blocks)) {
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;
382                 }
383                 set_blocks(pool, i, idx, nr_blocks);
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;
391         }
392 fail:
393         fio_mutex_up(pool->lock);
394         return ret;
395 }
396
397 static void *smalloc_pool(struct pool *pool, size_t size)
398 {
399         size_t alloc_size = size + sizeof(struct block_hdr);
400         void *ptr;
401
402         /*
403          * Round to int alignment, so that the postred pointer will
404          * be naturally aligned as well.
405          */
406 #ifdef SMALLOC_REDZONE
407         alloc_size += sizeof(unsigned int);
408         alloc_size = (alloc_size + int_mask) & ~int_mask;
409 #endif
410
411         ptr = __smalloc_pool(pool, alloc_size);
412         if (ptr) {
413                 struct block_hdr *hdr = ptr;
414
415                 hdr->size = alloc_size;
416                 fill_redzone(hdr);
417
418                 ptr += sizeof(*hdr);
419                 memset(ptr, 0, size);
420         }
421
422         return ptr;
423 }
424
425 void *smalloc(size_t size)
426 {
427         unsigned int i, end_pool;
428
429         if (size != (unsigned int) size)
430                 return NULL;
431
432         i = last_pool;
433         end_pool = nr_pools;
434
435         do {
436                 for (; i < end_pool; i++) {
437                         void *ptr = smalloc_pool(&mp[i], size);
438
439                         if (ptr) {
440                                 last_pool = i;
441                                 return ptr;
442                         }
443                 }
444                 if (last_pool) {
445                         end_pool = last_pool;
446                         last_pool = i = 0;
447                         continue;
448                 }
449
450                 break;
451         } while (1);
452
453         log_err("smalloc: OOM. Consider using --alloc-size to increase the "
454                 "shared memory available.\n");
455         return NULL;
456 }
457
458 void *scalloc(size_t nmemb, size_t size)
459 {
460         return smalloc(nmemb * size);
461 }
462
463 char *smalloc_strdup(const char *str)
464 {
465         char *ptr = NULL;
466
467         ptr = smalloc(strlen(str) + 1);
468         if (ptr)
469                 strcpy(ptr, str);
470         return ptr;
471 }