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