| 1 |
2 |
drasko |
#include <time.h>
|
| 2 |
|
|
#include <stdlib.h>
|
| 3 |
|
|
#include <stdio.h>
|
| 4 |
|
|
#include <string.h>
|
| 5 |
|
|
#include <lib/list.h>
|
| 6 |
|
|
#include <lib/printk.h>
|
| 7 |
|
|
#include <generic/memcache.h>
|
| 8 |
|
|
#include "test_memcache.h"
|
| 9 |
|
|
#include "test_alloc_generic.h"
|
| 10 |
|
|
#include "debug.h"
|
| 11 |
|
|
#include "tests.h"
|
| 12 |
|
|
|
| 13 |
|
|
#include <macros.h>
|
| 14 |
|
|
#include <config.h>
|
| 15 |
|
|
#include INC_GLUE(memory.h)
|
| 16 |
|
|
|
| 17 |
|
|
unsigned int MEM_CACHE_SIZE;
|
| 18 |
|
|
|
| 19 |
|
|
struct mem_cache *this;
|
| 20 |
|
|
|
| 21 |
|
|
void *buffer;
|
| 22 |
|
|
|
| 23 |
|
|
void *mem_cache_alloc_wrapped(int size)
|
| 24 |
|
|
{
|
| 25 |
|
|
return mem_cache_alloc(this);
|
| 26 |
|
|
}
|
| 27 |
|
|
|
| 28 |
|
|
int mem_cache_free_wrapped(void *addr)
|
| 29 |
|
|
{
|
| 30 |
|
|
return mem_cache_free(this, addr);
|
| 31 |
|
|
}
|
| 32 |
|
|
|
| 33 |
|
|
void print_memcache_state(void)
|
| 34 |
|
|
{
|
| 35 |
|
|
printf("%-15s%d\n","Total:", this->total);
|
| 36 |
|
|
printf("%-15s%d\n","Free:", this->free);
|
| 37 |
|
|
printf("Bitmap has %d words:\n", BITWISE_GETWORD(this->total) + 1);
|
| 38 |
|
|
for (int i = 0; i <= BITWISE_GETWORD(this->total); i++)
|
| 39 |
|
|
printf("0x%x\n", this->bitmap[i]);
|
| 40 |
|
|
}
|
| 41 |
|
|
|
| 42 |
|
|
int test_memcache_init_aligned(int *items_max, int item_size)
|
| 43 |
|
|
{
|
| 44 |
|
|
if (item_size * 10 > MEM_CACHE_SIZE)
|
| 45 |
|
|
MEM_CACHE_SIZE = item_size * 10;
|
| 46 |
|
|
if (!(buffer = calloc(1, MEM_CACHE_SIZE))) {
|
| 47 |
|
|
printf("System out of memory.\n");
|
| 48 |
|
|
BUG();
|
| 49 |
|
|
}
|
| 50 |
|
|
if ((this = mem_cache_init((unsigned int)buffer,
|
| 51 |
|
|
MEM_CACHE_SIZE,
|
| 52 |
|
|
item_size, 1)) == 0) {
|
| 53 |
|
|
printf("Unable to initialise cache.\n");
|
| 54 |
|
|
return -1;
|
| 55 |
|
|
}
|
| 56 |
|
|
*items_max = mem_cache_total_free(this);
|
| 57 |
|
|
printf("\nMEMCACHE TEST: ALIGNED ELEMENTS\n==========================\n");
|
| 58 |
|
|
printf("%-30s%d\n", "Item size:", item_size);
|
| 59 |
|
|
printf("%-30s0x%x\n", "Cache occupied space:", MEM_CACHE_SIZE);
|
| 60 |
|
|
printf("%-30s%d\n","Total items in cache:", *items_max);
|
| 61 |
|
|
printf("%-30s0x%x\n","Total items space:", (*items_max * item_size));
|
| 62 |
|
|
return 0;
|
| 63 |
|
|
}
|
| 64 |
|
|
|
| 65 |
|
|
|
| 66 |
|
|
int test_memcache_init(int *items_max, int item_size)
|
| 67 |
|
|
{
|
| 68 |
|
|
if (item_size * 10 > MEM_CACHE_SIZE)
|
| 69 |
|
|
MEM_CACHE_SIZE = item_size * 10;
|
| 70 |
|
|
printf("%s: Allocating cache memory.\n",__FUNCTION__);
|
| 71 |
|
|
if (!(buffer = calloc(1, MEM_CACHE_SIZE))) {
|
| 72 |
|
|
printf("System out of memory.\n");
|
| 73 |
|
|
BUG();
|
| 74 |
|
|
}
|
| 75 |
|
|
if ((this = mem_cache_init((unsigned int)buffer,
|
| 76 |
|
|
MEM_CACHE_SIZE,
|
| 77 |
|
|
item_size, 0)) == 0) {
|
| 78 |
|
|
printf("Unable to initialise cache.\n");
|
| 79 |
|
|
return -1;
|
| 80 |
|
|
}
|
| 81 |
|
|
*items_max = mem_cache_total_free(this);
|
| 82 |
|
|
printf("\nMEMCACHE TEST:\n========================\n");
|
| 83 |
|
|
printf("%-30s%d\n", "Item size:", item_size);
|
| 84 |
|
|
printf("%-30s0x%x\n", "Cache occupied space:", MEM_CACHE_SIZE);
|
| 85 |
|
|
printf("%-30s%d\n","Total items in cache:", *items_max);
|
| 86 |
|
|
printf("%-30s0x%x\n","Total items space:", (*items_max * item_size));
|
| 87 |
|
|
return 0;
|
| 88 |
|
|
}
|
| 89 |
|
|
|
| 90 |
|
|
int test_memcache(int items_max, int item_size, FILE *init_state, FILE *exit_state, int aligned)
|
| 91 |
|
|
{
|
| 92 |
|
|
const unsigned int TEST_CACHE_ITEM_SIZE = 5;
|
| 93 |
|
|
MEM_CACHE_SIZE = PAGE_SIZE * 5;
|
| 94 |
|
|
if (!item_size)
|
| 95 |
|
|
item_size = TEST_CACHE_ITEM_SIZE;
|
| 96 |
|
|
/* items_max value is ignored and overwritten because caches have fixed size. */
|
| 97 |
|
|
test_memcache_init(&items_max, item_size);
|
| 98 |
|
|
test_alloc_free_random_order(items_max, /* unused */ 2, mem_cache_alloc_wrapped,
|
| 99 |
|
|
mem_cache_free_wrapped, print_memcache_state,
|
| 100 |
|
|
init_state, exit_state);
|
| 101 |
|
|
free(buffer);
|
| 102 |
|
|
if (aligned) {
|
| 103 |
|
|
test_memcache_init_aligned(&items_max, item_size);
|
| 104 |
|
|
test_alloc_free_random_order(items_max, /* unused */ 2, mem_cache_alloc_wrapped,
|
| 105 |
|
|
mem_cache_free_wrapped, print_memcache_state,
|
| 106 |
|
|
init_state, exit_state);
|
| 107 |
|
|
}
|
| 108 |
|
|
free(buffer);
|
| 109 |
|
|
return 0;
|
| 110 |
|
|
}
|
| 111 |
|
|
|
| 112 |
|
|
|