Commit | Line | Data |
---|---|---|
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> | |
3a8600b4 | 13 | #include <fcntl.h> |
d24c33a4 | 14 | |
6548f47f | 15 | #include "mutex.h" |
b3268b92 | 16 | #include "arch/arch.h" |
3a8600b4 | 17 | #include "os/os.h" |
d24c33a4 | 18 | |
55f6491d | 19 | #define SMALLOC_REDZONE /* define to detect memory corruption */ |
d24c33a4 | 20 | |
ec996e9c JA |
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 | ||
5e012980 | 25 | #define INITIAL_SIZE 8192*1024 /* new pool size */ |
68857686 | 26 | #define MAX_POOLS 128 /* maximum number of pools to setup */ |
d24c33a4 | 27 | |
55f6491d JA |
28 | #define SMALLOC_PRE_RED 0xdeadbeefU |
29 | #define SMALLOC_POST_RED 0x5aa55aa5U | |
55f6491d | 30 | |
2b386d25 | 31 | unsigned int smalloc_pool_size = INITIAL_SIZE; |
122426da | 32 | const int int_mask = sizeof(int) - 1; |
2b386d25 | 33 | |
d24c33a4 | 34 | struct pool { |
6548f47f | 35 | struct fio_mutex *lock; /* protects this pool */ |
d24c33a4 | 36 | void *map; /* map of blocks */ |
ec996e9c JA |
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; | |
d24c33a4 | 41 | int fd; /* memory backing fd */ |
ec996e9c JA |
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 | |
d24c33a4 JA |
50 | }; |
51 | ||
52 | static struct pool mp[MAX_POOLS]; | |
53 | static unsigned int nr_pools; | |
54 | static unsigned int last_pool; | |
6548f47f | 55 | static struct fio_mutex *lock; |
d24c33a4 | 56 | |
d24c33a4 JA |
57 | static inline void pool_lock(struct pool *pool) |
58 | { | |
2e3e31e3 | 59 | fio_mutex_down(pool->lock); |
d24c33a4 JA |
60 | } |
61 | ||
62 | static inline void pool_unlock(struct pool *pool) | |
63 | { | |
2e3e31e3 | 64 | fio_mutex_up(pool->lock); |
d24c33a4 JA |
65 | } |
66 | ||
65864cf7 | 67 | static inline void global_read_lock(void) |
d24c33a4 | 68 | { |
2e3e31e3 | 69 | fio_mutex_down_read(lock); |
d24c33a4 JA |
70 | } |
71 | ||
65864cf7 | 72 | static inline void global_read_unlock(void) |
d24c33a4 | 73 | { |
2e3e31e3 | 74 | fio_mutex_up_read(lock); |
65864cf7 JA |
75 | } |
76 | ||
77 | static inline void global_write_lock(void) | |
78 | { | |
2e3e31e3 | 79 | fio_mutex_down_write(lock); |
65864cf7 JA |
80 | } |
81 | ||
82 | static inline void global_write_unlock(void) | |
83 | { | |
2e3e31e3 | 84 | fio_mutex_up_write(lock); |
d24c33a4 JA |
85 | } |
86 | ||
d24c33a4 JA |
87 | static inline int ptr_valid(struct pool *pool, void *ptr) |
88 | { | |
dcb69098 | 89 | unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL; |
ec996e9c JA |
90 | |
91 | return (ptr >= pool->map) && (ptr < pool->map + pool_size); | |
d24c33a4 JA |
92 | } |
93 | ||
808e9ea8 JA |
94 | static inline unsigned int size_to_blocks(unsigned int size) |
95 | { | |
96 | return (size + SMALLOC_BPB - 1) / SMALLOC_BPB; | |
97 | } | |
98 | ||
dcb69098 JA |
99 | static int blocks_iter(struct pool *pool, unsigned int pool_idx, |
100 | unsigned int idx, unsigned int nr_blocks, | |
ec996e9c | 101 | int (*func)(unsigned int *map, unsigned int mask)) |
d24c33a4 | 102 | { |
dcb69098 | 103 | |
ec996e9c JA |
104 | while (nr_blocks) { |
105 | unsigned int this_blocks, mask; | |
dcb69098 JA |
106 | unsigned int *map; |
107 | ||
108 | if (pool_idx >= pool->nr_blocks) | |
109 | return 0; | |
110 | ||
111 | map = &pool->bitmap[pool_idx]; | |
ec996e9c JA |
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; | |
dcb69098 | 129 | pool_idx++; |
ec996e9c JA |
130 | } |
131 | ||
132 | return 1; | |
d24c33a4 JA |
133 | } |
134 | ||
ec996e9c | 135 | static int mask_cmp(unsigned int *map, unsigned int mask) |
d24c33a4 | 136 | { |
ec996e9c | 137 | return !(*map & mask); |
d24c33a4 JA |
138 | } |
139 | ||
ec996e9c | 140 | static int mask_clear(unsigned int *map, unsigned int mask) |
d24c33a4 | 141 | { |
dcb69098 | 142 | assert((*map & mask) == mask); |
ec996e9c JA |
143 | *map &= ~mask; |
144 | return 1; | |
d24c33a4 JA |
145 | } |
146 | ||
ec996e9c | 147 | static int mask_set(unsigned int *map, unsigned int mask) |
d24c33a4 | 148 | { |
dcb69098 | 149 | assert(!(*map & mask)); |
ec996e9c JA |
150 | *map |= mask; |
151 | return 1; | |
d24c33a4 JA |
152 | } |
153 | ||
dcb69098 JA |
154 | static int blocks_free(struct pool *pool, unsigned int pool_idx, |
155 | unsigned int idx, unsigned int nr_blocks) | |
d24c33a4 | 156 | { |
dcb69098 | 157 | return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp); |
d24c33a4 JA |
158 | } |
159 | ||
dcb69098 JA |
160 | static void set_blocks(struct pool *pool, unsigned int pool_idx, |
161 | unsigned int idx, unsigned int nr_blocks) | |
d24c33a4 | 162 | { |
dcb69098 | 163 | blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set); |
d24c33a4 JA |
164 | } |
165 | ||
dcb69098 JA |
166 | static void clear_blocks(struct pool *pool, unsigned int pool_idx, |
167 | unsigned int idx, unsigned int nr_blocks) | |
d24c33a4 | 168 | { |
dcb69098 | 169 | blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear); |
d24c33a4 JA |
170 | } |
171 | ||
ec996e9c JA |
172 | static int find_next_zero(int word, int start) |
173 | { | |
174 | assert(word != -1U); | |
175 | word >>= (start + 1); | |
b3268b92 | 176 | return ffz(word) + start + 1; |
d24c33a4 JA |
177 | } |
178 | ||
adf57099 | 179 | static int add_pool(struct pool *pool, unsigned int alloc_size) |
d24c33a4 | 180 | { |
c0e75abf | 181 | int fd, bitmap_blocks; |
b8a6582e JA |
182 | char file[] = "/tmp/.fio_smalloc.XXXXXX"; |
183 | void *ptr; | |
ec996e9c | 184 | |
b8a6582e | 185 | fd = mkstemp(file); |
d24c33a4 JA |
186 | if (fd < 0) |
187 | goto out_close; | |
188 | ||
55f6491d | 189 | #ifdef SMALLOC_REDZONE |
ec996e9c | 190 | alloc_size += sizeof(unsigned int); |
55f6491d | 191 | #endif |
ec996e9c JA |
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; | |
0b9d69ec | 201 | |
ec996e9c JA |
202 | pool->nr_blocks = bitmap_blocks; |
203 | pool->free_blocks = bitmap_blocks * SMALLOC_BPB; | |
adf57099 | 204 | |
3a8600b4 | 205 | #ifdef FIO_HAVE_FALLOCATE |
c0e75abf JA |
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 | } | |
3a8600b4 GE |
214 | } |
215 | #endif | |
216 | ||
ec996e9c | 217 | if (ftruncate(fd, alloc_size) < 0) |
d24c33a4 JA |
218 | goto out_unlink; |
219 | ||
ec996e9c | 220 | ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
d24c33a4 JA |
221 | if (ptr == MAP_FAILED) |
222 | goto out_unlink; | |
223 | ||
ec996e9c JA |
224 | memset(ptr, 0, alloc_size); |
225 | pool->map = ptr; | |
226 | pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL); | |
d24c33a4 | 227 | |
6548f47f | 228 | pool->lock = fio_mutex_init(1); |
d24c33a4 JA |
229 | if (!pool->lock) |
230 | goto out_unlink; | |
d24c33a4 | 231 | |
443bb114 JA |
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 | */ | |
b8a6582e | 237 | unlink(file); |
d24c33a4 JA |
238 | pool->fd = fd; |
239 | ||
d24c33a4 JA |
240 | nr_pools++; |
241 | return 0; | |
242 | out_unlink: | |
ec996e9c | 243 | fprintf(stderr, "smalloc: failed adding pool\n"); |
d24c33a4 | 244 | if (pool->map) |
ec996e9c | 245 | munmap(pool->map, pool->mmap_size); |
b8a6582e | 246 | unlink(file); |
d24c33a4 | 247 | out_close: |
b8a6582e | 248 | close(fd); |
d24c33a4 JA |
249 | return 1; |
250 | } | |
251 | ||
252 | void sinit(void) | |
253 | { | |
4d4e80f2 | 254 | int ret; |
d24c33a4 | 255 | |
6548f47f | 256 | lock = fio_mutex_rw_init(); |
adf57099 | 257 | ret = add_pool(&mp[0], INITIAL_SIZE); |
d24c33a4 JA |
258 | assert(!ret); |
259 | } | |
260 | ||
261 | static void cleanup_pool(struct pool *pool) | |
262 | { | |
443bb114 JA |
263 | /* |
264 | * This will also remove the temporary file we used as a backing | |
265 | * store, it was already unlinked | |
266 | */ | |
d24c33a4 | 267 | close(pool->fd); |
ec996e9c | 268 | munmap(pool->map, pool->mmap_size); |
6548f47f JA |
269 | |
270 | if (pool->lock) | |
271 | fio_mutex_remove(pool->lock); | |
d24c33a4 JA |
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 | ||
6548f47f JA |
281 | if (lock) |
282 | fio_mutex_remove(lock); | |
d24c33a4 JA |
283 | } |
284 | ||
89da54e8 | 285 | #ifdef SMALLOC_REDZONE |
cf98708d JA |
286 | static void *postred_ptr(struct block_hdr *hdr) |
287 | { | |
cf98708d JA |
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 | ||
ec996e9c | 296 | static void fill_redzone(struct block_hdr *hdr) |
55f6491d | 297 | { |
cf98708d | 298 | unsigned int *postred = postred_ptr(hdr); |
55f6491d | 299 | |
ec996e9c JA |
300 | hdr->prered = SMALLOC_PRE_RED; |
301 | *postred = SMALLOC_POST_RED; | |
ec996e9c | 302 | } |
55f6491d | 303 | |
ec996e9c JA |
304 | static void sfree_check_redzone(struct block_hdr *hdr) |
305 | { | |
cf98708d | 306 | unsigned int *postred = postred_ptr(hdr); |
ec996e9c JA |
307 | |
308 | if (hdr->prered != SMALLOC_PRE_RED) { | |
55f6491d JA |
309 | fprintf(stderr, "smalloc pre redzone destroyed!\n"); |
310 | fprintf(stderr, " ptr=%p, prered=%x, expected %x\n", | |
ec996e9c | 311 | hdr, hdr->prered, SMALLOC_PRE_RED); |
55f6491d JA |
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", | |
ec996e9c | 317 | hdr, *postred, SMALLOC_POST_RED); |
55f6491d JA |
318 | assert(0); |
319 | } | |
89da54e8 JA |
320 | } |
321 | #else | |
322 | static void fill_redzone(struct block_hdr *hdr) | |
323 | { | |
55f6491d JA |
324 | } |
325 | ||
89da54e8 JA |
326 | static void sfree_check_redzone(struct block_hdr *hdr) |
327 | { | |
328 | } | |
329 | #endif | |
330 | ||
d24c33a4 JA |
331 | static void sfree_pool(struct pool *pool, void *ptr) |
332 | { | |
ec996e9c | 333 | struct block_hdr *hdr; |
179446e0 | 334 | unsigned int i, idx; |
ec996e9c | 335 | unsigned long offset; |
d24c33a4 JA |
336 | |
337 | if (!ptr) | |
338 | return; | |
339 | ||
ec996e9c JA |
340 | ptr -= sizeof(*hdr); |
341 | hdr = ptr; | |
55f6491d | 342 | |
d24c33a4 JA |
343 | assert(ptr_valid(pool, ptr)); |
344 | ||
ec996e9c | 345 | sfree_check_redzone(hdr); |
d24c33a4 | 346 | |
ec996e9c JA |
347 | offset = ptr - pool->map; |
348 | i = offset / SMALLOC_BPL; | |
349 | idx = (offset % SMALLOC_BPL) / SMALLOC_BPB; | |
d24c33a4 | 350 | |
ec996e9c | 351 | pool_lock(pool); |
dcb69098 | 352 | clear_blocks(pool, i, idx, size_to_blocks(hdr->size)); |
ec996e9c JA |
353 | if (i < pool->next_non_full) |
354 | pool->next_non_full = i; | |
179446e0 | 355 | pool->free_blocks += size_to_blocks(hdr->size); |
d24c33a4 JA |
356 | pool_unlock(pool); |
357 | } | |
358 | ||
359 | void sfree(void *ptr) | |
360 | { | |
361 | struct pool *pool = NULL; | |
362 | unsigned int i; | |
363 | ||
8e5732e5 JA |
364 | if (!ptr) |
365 | return; | |
366 | ||
65864cf7 | 367 | global_read_lock(); |
d24c33a4 JA |
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 | ||
65864cf7 | 376 | global_read_unlock(); |
d24c33a4 JA |
377 | |
378 | assert(pool); | |
379 | sfree_pool(pool, ptr); | |
380 | } | |
381 | ||
55f6491d | 382 | static void *__smalloc_pool(struct pool *pool, unsigned int size) |
d24c33a4 | 383 | { |
ec996e9c JA |
384 | unsigned int nr_blocks; |
385 | unsigned int i; | |
386 | unsigned int offset; | |
387 | unsigned int last_idx; | |
388 | void *ret = NULL; | |
d24c33a4 | 389 | |
d24c33a4 | 390 | pool_lock(pool); |
179446e0 JA |
391 | |
392 | nr_blocks = size_to_blocks(size); | |
ec996e9c | 393 | if (nr_blocks > pool->free_blocks) |
8e5732e5 | 394 | goto fail; |
5ec10eaa | 395 | |
ec996e9c JA |
396 | i = pool->next_non_full; |
397 | last_idx = 0; | |
398 | offset = -1U; | |
399 | while (i < pool->nr_blocks) { | |
400 | unsigned int idx; | |
d24c33a4 | 401 | |
ec996e9c JA |
402 | if (pool->bitmap[i] == -1U) { |
403 | i++; | |
404 | pool->next_non_full = i; | |
405 | last_idx = 0; | |
406 | continue; | |
407 | } | |
d24c33a4 | 408 | |
ec996e9c | 409 | idx = find_next_zero(pool->bitmap[i], last_idx); |
dcb69098 | 410 | if (!blocks_free(pool, i, idx, nr_blocks)) { |
ec996e9c JA |
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; | |
d24c33a4 | 422 | } |
dcb69098 | 423 | set_blocks(pool, i, idx, nr_blocks); |
ec996e9c JA |
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; | |
d24c33a4 | 431 | } |
ec996e9c | 432 | fail: |
d24c33a4 | 433 | pool_unlock(pool); |
ec996e9c | 434 | return ret; |
d24c33a4 JA |
435 | } |
436 | ||
55f6491d JA |
437 | static void *smalloc_pool(struct pool *pool, unsigned int size) |
438 | { | |
89da54e8 | 439 | unsigned int alloc_size = size + sizeof(struct block_hdr); |
55f6491d JA |
440 | void *ptr; |
441 | ||
cf98708d | 442 | /* |
122426da JA |
443 | * Round to int alignment, so that the postred pointer will |
444 | * be naturally aligned as well. | |
cf98708d | 445 | */ |
ec996e9c | 446 | #ifdef SMALLOC_REDZONE |
122426da JA |
447 | alloc_size += sizeof(unsigned int); |
448 | alloc_size = (alloc_size + int_mask) & ~int_mask; | |
ec996e9c JA |
449 | #endif |
450 | ||
451 | ptr = __smalloc_pool(pool, alloc_size); | |
89da54e8 JA |
452 | if (ptr) { |
453 | struct block_hdr *hdr = ptr; | |
55f6491d | 454 | |
89da54e8 JA |
455 | hdr->size = alloc_size; |
456 | fill_redzone(hdr); | |
55f6491d | 457 | |
89da54e8 JA |
458 | ptr += sizeof(*hdr); |
459 | memset(ptr, 0, size); | |
460 | } | |
ec996e9c | 461 | |
55f6491d | 462 | return ptr; |
55f6491d JA |
463 | } |
464 | ||
d24c33a4 JA |
465 | void *smalloc(unsigned int size) |
466 | { | |
467 | unsigned int i; | |
468 | ||
d1271dc1 | 469 | global_write_lock(); |
d24c33a4 JA |
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; | |
d1271dc1 | 478 | global_write_unlock(); |
d24c33a4 JA |
479 | return ptr; |
480 | } | |
481 | } | |
482 | if (last_pool) { | |
483 | last_pool = 0; | |
484 | continue; | |
485 | } | |
486 | ||
ec996e9c | 487 | if (nr_pools + 1 > MAX_POOLS) |
d24c33a4 JA |
488 | break; |
489 | else { | |
490 | i = nr_pools; | |
adf57099 | 491 | if (add_pool(&mp[nr_pools], size)) |
65864cf7 | 492 | goto out; |
d24c33a4 JA |
493 | } |
494 | } while (1); | |
495 | ||
65864cf7 | 496 | out: |
d1271dc1 | 497 | global_write_unlock(); |
d24c33a4 JA |
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 | } |