Update test script for new JSON steadystate['criterion'] reporting
[fio.git] / smalloc.c
... / ...
CommitLineData
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 MAX_POOLS 8 /* maximum number of pools to setup */
30
31#define SMALLOC_PRE_RED 0xdeadbeefU
32#define SMALLOC_POST_RED 0x5aa55aa5U
33
34unsigned int smalloc_pool_size = INITIAL_SIZE;
35#ifdef SMALLOC_REDZONE
36static const int int_mask = sizeof(int) - 1;
37#endif
38
39struct pool {
40 struct fio_mutex *lock; /* protects this pool */
41 void *map; /* map of blocks */
42 unsigned int *bitmap; /* blocks free/busy map */
43 size_t free_blocks; /* free blocks */
44 size_t nr_blocks; /* total blocks */
45 size_t next_non_full;
46 size_t mmap_size;
47};
48
49struct block_hdr {
50 size_t size;
51#ifdef SMALLOC_REDZONE
52 unsigned int prered;
53#endif
54};
55
56static struct pool mp[MAX_POOLS];
57static unsigned int nr_pools;
58static unsigned int last_pool;
59
60static inline int ptr_valid(struct pool *pool, void *ptr)
61{
62 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
63
64 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
65}
66
67static inline size_t size_to_blocks(size_t size)
68{
69 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
70}
71
72static int blocks_iter(struct pool *pool, unsigned int pool_idx,
73 unsigned int idx, size_t nr_blocks,
74 int (*func)(unsigned int *map, unsigned int mask))
75{
76
77 while (nr_blocks) {
78 unsigned int this_blocks, mask;
79 unsigned int *map;
80
81 if (pool_idx >= pool->nr_blocks)
82 return 0;
83
84 map = &pool->bitmap[pool_idx];
85
86 this_blocks = nr_blocks;
87 if (this_blocks + idx > SMALLOC_BPI) {
88 this_blocks = SMALLOC_BPI - idx;
89 idx = SMALLOC_BPI - this_blocks;
90 }
91
92 if (this_blocks == SMALLOC_BPI)
93 mask = -1U;
94 else
95 mask = ((1U << this_blocks) - 1) << idx;
96
97 if (!func(map, mask))
98 return 0;
99
100 nr_blocks -= this_blocks;
101 idx = 0;
102 pool_idx++;
103 }
104
105 return 1;
106}
107
108static int mask_cmp(unsigned int *map, unsigned int mask)
109{
110 return !(*map & mask);
111}
112
113static int mask_clear(unsigned int *map, unsigned int mask)
114{
115 assert((*map & mask) == mask);
116 *map &= ~mask;
117 return 1;
118}
119
120static int mask_set(unsigned int *map, unsigned int mask)
121{
122 assert(!(*map & mask));
123 *map |= mask;
124 return 1;
125}
126
127static int blocks_free(struct pool *pool, unsigned int pool_idx,
128 unsigned int idx, size_t nr_blocks)
129{
130 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
131}
132
133static void set_blocks(struct pool *pool, unsigned int pool_idx,
134 unsigned int idx, size_t nr_blocks)
135{
136 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
137}
138
139static void clear_blocks(struct pool *pool, unsigned int pool_idx,
140 unsigned int idx, size_t nr_blocks)
141{
142 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
143}
144
145static int find_next_zero(int word, int start)
146{
147 assert(word != -1U);
148 word >>= start;
149 return ffz(word) + start;
150}
151
152static int add_pool(struct pool *pool, unsigned int alloc_size)
153{
154 int bitmap_blocks;
155 int mmap_flags;
156 void *ptr;
157
158#ifdef SMALLOC_REDZONE
159 alloc_size += sizeof(unsigned int);
160#endif
161 alloc_size += sizeof(struct block_hdr);
162 if (alloc_size < INITIAL_SIZE)
163 alloc_size = INITIAL_SIZE;
164
165 /* round up to nearest full number of blocks */
166 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
167 bitmap_blocks = alloc_size / SMALLOC_BPL;
168 alloc_size += bitmap_blocks * sizeof(unsigned int);
169 pool->mmap_size = alloc_size;
170
171 pool->nr_blocks = bitmap_blocks;
172 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
173
174 mmap_flags = OS_MAP_ANON;
175#ifdef CONFIG_ESX
176 mmap_flags |= MAP_PRIVATE;
177#else
178 mmap_flags |= MAP_SHARED;
179#endif
180 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0);
181
182 if (ptr == MAP_FAILED)
183 goto out_fail;
184
185 pool->map = ptr;
186 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
187 memset(pool->bitmap, 0, bitmap_blocks * sizeof(unsigned int));
188
189 pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
190 if (!pool->lock)
191 goto out_fail;
192
193 nr_pools++;
194 return 0;
195out_fail:
196 log_err("smalloc: failed adding pool\n");
197 if (pool->map)
198 munmap(pool->map, pool->mmap_size);
199 return 1;
200}
201
202void sinit(void)
203{
204 int i, ret;
205
206 for (i = 0; i < MAX_POOLS; i++) {
207 ret = add_pool(&mp[i], smalloc_pool_size);
208 if (ret)
209 break;
210 }
211
212 /*
213 * If we added at least one pool, we should be OK for most
214 * cases.
215 */
216 assert(i);
217}
218
219static void cleanup_pool(struct pool *pool)
220{
221 /*
222 * This will also remove the temporary file we used as a backing
223 * store, it was already unlinked
224 */
225 munmap(pool->map, pool->mmap_size);
226
227 if (pool->lock)
228 fio_mutex_remove(pool->lock);
229}
230
231void scleanup(void)
232{
233 unsigned int i;
234
235 for (i = 0; i < nr_pools; i++)
236 cleanup_pool(&mp[i]);
237}
238
239#ifdef SMALLOC_REDZONE
240static void *postred_ptr(struct block_hdr *hdr)
241{
242 uintptr_t ptr;
243
244 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
245 ptr = (ptr + int_mask) & ~int_mask;
246
247 return (void *) ptr;
248}
249
250static void fill_redzone(struct block_hdr *hdr)
251{
252 unsigned int *postred = postred_ptr(hdr);
253
254 hdr->prered = SMALLOC_PRE_RED;
255 *postred = SMALLOC_POST_RED;
256}
257
258static void sfree_check_redzone(struct block_hdr *hdr)
259{
260 unsigned int *postred = postred_ptr(hdr);
261
262 if (hdr->prered != SMALLOC_PRE_RED) {
263 log_err("smalloc pre redzone destroyed!\n"
264 " ptr=%p, prered=%x, expected %x\n",
265 hdr, hdr->prered, SMALLOC_PRE_RED);
266 assert(0);
267 }
268 if (*postred != SMALLOC_POST_RED) {
269 log_err("smalloc post redzone destroyed!\n"
270 " ptr=%p, postred=%x, expected %x\n",
271 hdr, *postred, SMALLOC_POST_RED);
272 assert(0);
273 }
274}
275#else
276static void fill_redzone(struct block_hdr *hdr)
277{
278}
279
280static void sfree_check_redzone(struct block_hdr *hdr)
281{
282}
283#endif
284
285static void sfree_pool(struct pool *pool, void *ptr)
286{
287 struct block_hdr *hdr;
288 unsigned int i, idx;
289 unsigned long offset;
290
291 if (!ptr)
292 return;
293
294 ptr -= sizeof(*hdr);
295 hdr = ptr;
296
297 assert(ptr_valid(pool, ptr));
298
299 sfree_check_redzone(hdr);
300
301 offset = ptr - pool->map;
302 i = offset / SMALLOC_BPL;
303 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
304
305 fio_mutex_down(pool->lock);
306 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
307 if (i < pool->next_non_full)
308 pool->next_non_full = i;
309 pool->free_blocks += size_to_blocks(hdr->size);
310 fio_mutex_up(pool->lock);
311}
312
313void sfree(void *ptr)
314{
315 struct pool *pool = NULL;
316 unsigned int i;
317
318 if (!ptr)
319 return;
320
321 for (i = 0; i < nr_pools; i++) {
322 if (ptr_valid(&mp[i], ptr)) {
323 pool = &mp[i];
324 break;
325 }
326 }
327
328 if (pool) {
329 sfree_pool(pool, ptr);
330 return;
331 }
332
333 log_err("smalloc: ptr %p not from smalloc pool\n", ptr);
334}
335
336static void *__smalloc_pool(struct pool *pool, size_t size)
337{
338 size_t nr_blocks;
339 unsigned int i;
340 unsigned int offset;
341 unsigned int last_idx;
342 void *ret = NULL;
343
344 fio_mutex_down(pool->lock);
345
346 nr_blocks = size_to_blocks(size);
347 if (nr_blocks > pool->free_blocks)
348 goto fail;
349
350 i = pool->next_non_full;
351 last_idx = 0;
352 offset = -1U;
353 while (i < pool->nr_blocks) {
354 unsigned int idx;
355
356 if (pool->bitmap[i] == -1U) {
357 i++;
358 pool->next_non_full = i;
359 last_idx = 0;
360 continue;
361 }
362
363 idx = find_next_zero(pool->bitmap[i], last_idx);
364 if (!blocks_free(pool, i, idx, nr_blocks)) {
365 idx += nr_blocks;
366 if (idx < SMALLOC_BPI)
367 last_idx = idx;
368 else {
369 last_idx = 0;
370 while (idx >= SMALLOC_BPI) {
371 i++;
372 idx -= SMALLOC_BPI;
373 }
374 }
375 continue;
376 }
377 set_blocks(pool, i, idx, nr_blocks);
378 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
379 break;
380 }
381
382 if (i < pool->nr_blocks) {
383 pool->free_blocks -= nr_blocks;
384 ret = pool->map + offset;
385 }
386fail:
387 fio_mutex_up(pool->lock);
388 return ret;
389}
390
391static void *smalloc_pool(struct pool *pool, size_t size)
392{
393 size_t alloc_size = size + sizeof(struct block_hdr);
394 void *ptr;
395
396 /*
397 * Round to int alignment, so that the postred pointer will
398 * be naturally aligned as well.
399 */
400#ifdef SMALLOC_REDZONE
401 alloc_size += sizeof(unsigned int);
402 alloc_size = (alloc_size + int_mask) & ~int_mask;
403#endif
404
405 ptr = __smalloc_pool(pool, alloc_size);
406 if (ptr) {
407 struct block_hdr *hdr = ptr;
408
409 hdr->size = alloc_size;
410 fill_redzone(hdr);
411
412 ptr += sizeof(*hdr);
413 memset(ptr, 0, size);
414 }
415
416 return ptr;
417}
418
419void *smalloc(size_t size)
420{
421 unsigned int i, end_pool;
422
423 if (size != (unsigned int) size)
424 return NULL;
425
426 i = last_pool;
427 end_pool = nr_pools;
428
429 do {
430 for (; i < end_pool; i++) {
431 void *ptr = smalloc_pool(&mp[i], size);
432
433 if (ptr) {
434 last_pool = i;
435 return ptr;
436 }
437 }
438 if (last_pool) {
439 end_pool = last_pool;
440 last_pool = i = 0;
441 continue;
442 }
443
444 break;
445 } while (1);
446
447 return NULL;
448}
449
450void *scalloc(size_t nmemb, size_t size)
451{
452 return smalloc(nmemb * size);
453}
454
455char *smalloc_strdup(const char *str)
456{
457 char *ptr = NULL;
458
459 ptr = smalloc(strlen(str) + 1);
460 if (ptr)
461 strcpy(ptr, str);
462 return ptr;
463}