X-Git-Url: https://git.kernel.dk/?a=blobdiff_plain;f=smalloc.c;h=7ffc112cddad1896b23df3b517afead88e1cda34;hb=c5dda9e387101dba4a654f6304553509d73dc298;hp=b7502dc9616a25b0292947c51be0b4daf2022487;hpb=5ec10eaad3b09875b91e19a20bbdfa06f2117562;p=fio.git diff --git a/smalloc.c b/smalloc.c index b7502dc9..7ffc112c 100644 --- a/smalloc.c +++ b/smalloc.c @@ -13,16 +13,12 @@ #include "mutex.h" -#undef ENABLE_RESIZE /* define to enable pool resizing */ #define MP_SAFE /* define to made allocator thread safe */ -#define INITIAL_SIZE 65536 /* new pool size */ -#define MAX_POOLS 32 /* maximum number of pools to setup */ +#define INITIAL_SIZE 32*1048576 /* new pool size */ +#define MAX_POOLS 4 /* maximum number of pools to setup */ -#ifdef ENABLE_RESIZE -#define MAX_SIZE 8 * INITIAL_SIZE -static unsigned int resize_error; -#endif +unsigned int smalloc_pool_size = INITIAL_SIZE; struct pool { struct fio_mutex *lock; /* protects this pool */ @@ -173,52 +169,7 @@ static int compact_pool(struct pool *pool) return !!compacted; } -static int resize_pool(struct pool *pool) -{ -#ifdef ENABLE_RESIZE - unsigned int new_size = pool->size << 1; - struct mem_hdr *hdr, *last_hdr; - void *ptr; - - if (new_size >= MAX_SIZE || resize_error) - return 1; - - if (ftruncate(pool->fd, new_size) < 0) - goto fail; - - ptr = mremap(pool->map, pool->size, new_size, 0); - if (ptr == MAP_FAILED) - goto fail; - - pool->map = ptr; - hdr = pool; - do { - last_hdr = hdr; - } while ((hdr = hdr_nxt(hdr)) != NULL); - - if (hdr_free(last_hdr)) { - last_hdr->size = hdr_size(last_hdr) + new_size - pool_size; - hdr_mark_free(last_hdr); - } else { - struct mem_hdr *nxt; - - nxt = (void *) last_hdr + hdr_size(last_hdr) + sizeof(*hdr); - nxt->size = new_size - pool_size - sizeof(*hdr); - hdr_mark_free(nxt); - } - - pool_room += new_size - pool_size; - pool_size = new_size; - return 0; -fail: - perror("resize"); - resize_error = 1; -#else - return 1; -#endif -} - -static int add_pool(struct pool *pool) +static int add_pool(struct pool *pool, unsigned int alloc_size) { struct mem_hdr *hdr; void *ptr; @@ -229,7 +180,12 @@ static int add_pool(struct pool *pool) if (fd < 0) goto out_close; - pool->size = INITIAL_SIZE; + alloc_size += sizeof(*hdr); + if (alloc_size > smalloc_pool_size) + pool->size = alloc_size; + else + pool->size = smalloc_pool_size; + if (ftruncate(fd, pool->size) < 0) goto out_unlink; @@ -273,7 +229,7 @@ void sinit(void) #ifdef MP_SAFE lock = fio_mutex_rw_init(); #endif - ret = add_pool(&mp[0]); + ret = add_pool(&mp[0], INITIAL_SIZE); assert(!ret); } @@ -329,6 +285,9 @@ void sfree(void *ptr) struct pool *pool = NULL; unsigned int i; + if (!ptr) + return; + global_read_lock(); for (i = 0; i < nr_pools; i++) { @@ -350,14 +309,14 @@ static void *smalloc_pool(struct pool *pool, unsigned int size) int did_restart = 0; void *ret; - /* - * slight chance of race with sfree() here, but acceptable - */ - if (!size || size > pool->room + sizeof(*hdr) || - ((size > pool->largest_block) && pool->largest_block)) + if (!size) return NULL; pool_lock(pool); + if (size > pool->room + sizeof(*hdr)) + goto fail; + if ((size > pool->largest_block) && pool->largest_block) + goto fail; restart: hdr = pool->last; prv = NULL; @@ -411,20 +370,12 @@ fail: * if we fail to allocate, first compact the entries that we missed. * if that also fails, increase the size of the pool */ - ++did_restart; - if (did_restart <= 1) { + if (++did_restart <= 1) { if (!compact_pool(pool)) { pool->last = pool->map; goto restart; } } - ++did_restart; - if (did_restart <= 2) { - if (!resize_pool(pool)) { - pool->last = pool->map; - goto restart; - } - } pool_unlock(pool); return NULL; } @@ -433,6 +384,8 @@ void *smalloc(unsigned int size) { unsigned int i; + printf("size=%u\n", size); + global_read_lock(); i = last_pool; @@ -456,7 +409,7 @@ void *smalloc(unsigned int size) else { i = nr_pools; global_read_unlock(); - if (add_pool(&mp[nr_pools])) + if (add_pool(&mp[nr_pools], size)) goto out; global_read_lock(); }