1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/*
TEST_HEADER
id = $Id$
summary = MVFF low-memory test; failover of CBS to freelist
language = c
link = testlib.o
OUTPUT_SPEC
assert = true
limit < 160000
END_HEADER
*/
/* Purpose:
*
* This tests that the MVFF can continue to return blocks to the arena
* even if its CBS can no longer allocate control blocks (by failing
* over to use the freelist).
*
* This failed to work in release.epcore.anchovy.1.
*
*
* Strategy:
* - Set low commit limit.
* - Allocate large objects in MVFF until we run out of memory.
* - Free one large object.
* - Allocate small objects in MVFF until we run out of memory.
* - Free every other small object.
* At this point, the CBS should have run out of control blocks.
* - Free every other large object.
* - Allocate in another pool.
*/
#include "testlib.h"
#include "mpscmvff.h"
#include "mpsavm.h"
#define MAXSMALLOBJECTS (100000ul)
#define MAXLARGEOBJECTS (100000ul)
mps_arena_t arena;
static mps_addr_t
largeObjects[MAXLARGEOBJECTS],
smallObjects[MAXSMALLOBJECTS];
static void do_test(size_t extendBy, size_t avgSize, size_t align,
int slotHigh, int arenaHigh, int firstFit)
{
mps_pool_t pool, pool2;
mps_res_t res = MPS_RES_OK; /* suppress warning */
mps_addr_t p;
unsigned int i;
unsigned long nLargeObjects = 0, nSmallObjects = 0;
size_t largeObjectSize, smallObjectSize;
largeObjectSize = extendBy;
smallObjectSize = align;
MPS_ARGS_BEGIN(args) {
MPS_ARGS_ADD(args, MPS_KEY_EXTEND_BY, extendBy);
MPS_ARGS_ADD(args, MPS_KEY_MEAN_SIZE, avgSize);
MPS_ARGS_ADD(args, MPS_KEY_MVFF_ARENA_HIGH, arenaHigh);
MPS_ARGS_ADD(args, MPS_KEY_MVFF_SLOT_HIGH, slotHigh);
MPS_ARGS_ADD(args, MPS_KEY_MVFF_FIRST_FIT, firstFit);
/* Set SPARE to 0 as we want this pool to return memory to the
arena as soon as it is freed so we can allocate it elsewhere. */
MPS_ARGS_ADD(args, MPS_KEY_SPARE, 0.0);
die(mps_pool_create_k(&pool, arena, mps_class_mvff(), args),
"create MVFF pool");
} MPS_ARGS_END(args);
MPS_ARGS_BEGIN(args) {
MPS_ARGS_ADD(args, MPS_KEY_EXTEND_BY, extendBy);
die(mps_pool_create_k(&pool2, arena, mps_class_mvff(), args),
"create second MVFF pool");
} MPS_ARGS_END(args);
/* Allocate one small object in pool2 so that its CBS gets some
initial memory. */
res = mps_alloc(&p, pool2, 8);
asserts(res == MPS_RES_OK,
"Couldn't allocate first object of size %lu in second pool",
(unsigned long)8);
/* First we allocate large objects until we run out of memory. */
for(i = 0; i < MAXLARGEOBJECTS; i++) {
res = mps_alloc(&p, pool, largeObjectSize);
if (res != MPS_RES_OK)
break;
largeObjects[nLargeObjects] = p;
++nLargeObjects;
}
asserts(res != MPS_RES_OK,
"Unexpectedly managed to create %lu objects of size %lu",
MAXLARGEOBJECTS, largeObjectSize);
if (nLargeObjects < 2) {
/* Need two large objects for the rest of the test to work */
goto done;
}
/* Then we free one to make sure we can allocate some small objects */
mps_free(pool, largeObjects[nLargeObjects - 1], largeObjectSize);
--nLargeObjects;
comment("Allocated %lu objects of size %lu",
nLargeObjects, largeObjectSize);
/* Then we allocate lots of small objects. */
for(i = 0; i < MAXSMALLOBJECTS; i++) {
res = mps_alloc(&p, pool, smallObjectSize);
if (res != MPS_RES_OK)
break;
smallObjects[nSmallObjects] = p;
++nSmallObjects;
}
asserts(res != MPS_RES_OK,
"Unexpectedly managed to create %lu objects of size %lu",
MAXSMALLOBJECTS, smallObjectSize);
comment("Allocated %lu objects of size %lu",
nSmallObjects, smallObjectSize);
/* Then we free every other small object */
for(i = 0; i < nSmallObjects; i += 2) {
mps_free(pool, smallObjects[i], smallObjectSize);
smallObjects[i] = (mps_addr_t)0;
}
/* MVFF should be failing over from the CBS to the freelist now. */
res = mps_alloc(&p, pool2, largeObjectSize);
asserts(res != MPS_RES_OK, "unexpectedly have some memory left");
/* Then we free every other large object */
for(i = 0; i < nLargeObjects; i += 2) {
mps_free(pool, largeObjects[i], largeObjectSize);
largeObjects[i] = (mps_addr_t)0;
}
/* Then we allocate in another pool. */
res = mps_alloc(&p, pool2, largeObjectSize);
asserts(res == MPS_RES_OK,
"Couldn't allocate second object of size %lu in second pool",
(unsigned long)largeObjectSize);
done:
mps_pool_destroy(pool);
mps_pool_destroy(pool2);
}
static void test(void *stack_pointer)
{
mps_thr_t thread;
int symm;
size_t grainSize = 4096;
size_t comlimit;
mps_bool_t slotHigh, arenaHigh, firstFit;
MPS_ARGS_BEGIN(args) {
MPS_ARGS_ADD(args, MPS_KEY_ARENA_SIZE, 1024*1024*50);
MPS_ARGS_ADD(args, MPS_KEY_ARENA_GRAIN_SIZE, grainSize);
cdie(mps_arena_create_k(&arena, mps_arena_class_vm(), args),
"create arena");
} MPS_ARGS_END(args);
cdie(mps_thread_reg(&thread, arena), "register thread");
for (comlimit = 128 * grainSize; comlimit > 0; comlimit -= grainSize) {
mps_arena_commit_limit_set(arena, comlimit);
report("limit", "%d", comlimit);
symm = ranint(8);
slotHigh = (symm >> 2) & 1;
arenaHigh = (symm >> 1) & 1;
firstFit = (symm & 1);
do_test(grainSize, 8, 8, slotHigh, arenaHigh, firstFit);
}
mps_thread_dereg(thread);
mps_arena_destroy(arena);
}
int main(void)
{
run_test(test);
pass();
return 0;
}
|