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