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