Skip to content

Commit

Permalink
a code path of cache doesn't trigger aging
Browse files Browse the repository at this point in the history
in one cache put path, it doesn't trigger aging, and it will cause a
fatal.
  • Loading branch information
liuliu committed Jul 3, 2012
1 parent ea4c10d commit 7e37e52
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/ccv_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ int ccv_cache_put(ccv_cache_t* cache, uint64_t sign, void* x, uint32_t size, uin
cache->age = 1;
cache->origin.terminal.off = (uint64_t)x | 0x1;
cache->origin.terminal.sign = sign;
cache->origin.terminal.type = CCV_SET_TERMINAL_TYPE(type, cache->age, size);
cache->size = size;
cache->rnum = 1;
return 0;
}
Expand Down Expand Up @@ -293,6 +295,7 @@ int ccv_cache_put(ccv_cache_t* cache, uint64_t sign, void* x, uint32_t size, uin
if (total == 63)
branch->branch.set |= 0x2;
}
_ccv_cache_aging(&cache->origin, sign);
cache->rnum++;
cache->size += size;
return 0;
Expand Down
50 changes: 49 additions & 1 deletion test/functional/memory.tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TEST_CASE("random cache put/delete/get")
sigs[i] = uniqid();
mems[i] = ccmalloc(1);
ccv_cache_put(&cache, sigs[i], mems[i], 1, 0);
REQUIRE_EQ(i, cache.size, "at %d should has cache size %d", i, i);
REQUIRE_EQ(i + 1, cache.size, "at %d should has cache size %d", i, i);
}
uint8_t deleted[N];
for (i = 0; i < N; i++)
Expand Down Expand Up @@ -95,4 +95,52 @@ TEST_CASE("garbage collector 95\% hit rate")
ccv_disable_cache();
}

TEST_CASE("multi-type garbage collector 92\% hit rate")
{
int i;
ccv_enable_cache(((sizeof(ccv_dense_matrix_t) + 4) + (sizeof(ccv_array_t) + 4 * 4)) * N * 90 / 100);
for (i = 0; i < N; i++)
{
ccv_dense_matrix_t* dmt = ccv_dense_matrix_new(1, 1, CCV_32S | CCV_C1, 0, 0);
dmt->data.i32[0] = i;
dmt->sig = ccv_cache_generate_signature((const char*)&i, 4, 0);
dmt->type |= CCV_REUSABLE;
ccv_matrix_free(dmt);
ccv_array_t* array = ccv_array_new(4, 4, 0);
int j = i;
ccv_array_push(array, &j);
j = 0;
ccv_array_push(array, &j);
j = ~i;
ccv_array_push(array, &j);
j = -i;
ccv_array_push(array, &j);
array->sig = ccv_cache_generate_signature((const char*)&j, 4, 0);
array->type |= CCV_REUSABLE;
ccv_array_free(array);
}
int percent = 0, total = 0;
for (i = N - 1; i > N * 6 / 100; i--)
{
uint64_t sig = ccv_cache_generate_signature((const char*)&i, 4, 0);
ccv_dense_matrix_t* dmt = ccv_dense_matrix_new(1, 1, CCV_32S | CCV_C1, 0, sig);
if (i == dmt->data.i32[0])
++percent;
++total;
ccv_matrix_free_immediately(dmt);
int j = -i;
sig = ccv_cache_generate_signature((const char*)&j, 4, 0);
ccv_array_t* array = ccv_array_new(4, 4, sig);
if (i == *(int*)ccv_array_get(array, 0) &&
0 == *(int*)ccv_array_get(array, 1) &&
~i == *(int*)ccv_array_get(array, 2) &&
-i == *(int*)ccv_array_get(array, 3))
++percent;
++total;
ccv_array_free_immediately(array);
}
REQUIRE((double)percent / (double)total > 0.92, "the cache hit (%lf) should be greater than 92%%", (double)percent / (double)total);
ccv_disable_cache();
}

#include "case_main.h"

0 comments on commit 7e37e52

Please sign in to comment.