diff --git a/test_programs/slab-test.c b/test_programs/slab-test.c index 980151f..fbc4c3a 100644 --- a/test_programs/slab-test.c +++ b/test_programs/slab-test.c @@ -13,14 +13,78 @@ static int print_json(void * ignore,const char * buffer,size_t len) int main(int argc,char ** argv) { + int num_allocations = 1000; + int max_allocation_size = 8192; + int random_free_percentage = 33; + char ** allocation_table; + char * allocation; int i; + allocation_table = malloc(sizeof(*allocation_table) * num_allocations); + if(allocation_table == NULL) + exit(EXIT_FAILURE); + srand(1); - for(i = 0 ; i < 1000 ; i++) - malloc(1 + (rand() % 8192)); + printf("/* Allocating %d random length fragments of memory (maximum size = %ld bytes). */\n", num_allocations, max_allocation_size); + + for(i = 0 ; i < num_allocations ; i++) + { + allocation = malloc(1 + (rand() % max_allocation_size)); + if(allocation == NULL) + exit(EXIT_FAILURE); + + allocation_table[i] = allocation; + } __get_slab_stats(NULL, print_json); - return(0); + printf("\n/* Changing all allocations to different random lengths. */\n"); + + for(i = 0 ; i < num_allocations ; i++) + { + allocation = realloc(allocation_table[i], 1 + (rand() % max_allocation_size)); + if(allocation == NULL) + exit(EXIT_FAILURE); + + allocation_table[i] = allocation; + } + + __get_slab_stats(NULL, print_json); + + printf("\n/* Freeing %d%% of all allocations. */\n", random_free_percentage); + + for(i = 0 ; i < num_allocations ; i++) + { + if((rand() % 100) < 33) + { + free(allocation_table[i]); + allocation_table[i] = NULL; + } + } + + __get_slab_stats(NULL, print_json); + + printf("\n/* Marking unused slabs for reuse; reallocating memory/changing allocation lengths. */\n"); + + __decay_unused_slabs(); + + for(i = 0 ; i < num_allocations ; i++) + { + allocation = realloc(allocation_table[i], 1 + (rand() % max_allocation_size)); + if(allocation == NULL) + exit(EXIT_FAILURE); + + allocation_table[i] = allocation; + } + + __get_slab_stats(NULL, print_json); + + printf("\n/* Freeing all unused slabs. */\n"); + + __free_unused_slabs(); + + __get_slab_stats(NULL, print_json); + + return(EXIT_SUCCESS); }