Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ jobs:
run: ctest -C ${{env.BUILD_TYPE}} --output-on-failure --parallel

ubuntu:
name: ${{ matrix.name }}
runs-on: ubuntu-latest
strategy:
matrix:
include:
- ttl: "OFF"
name: ubuntu
- ttl: "ON"
name: ubuntu / TTL ON
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
Expand All @@ -39,6 +47,7 @@ jobs:
run: |
cmake -G Ninja -B ${{github.workspace}}/build \
-DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \
-DSUPPORT_TTL=${{ matrix.ttl }} \
-DCMAKE_C_FLAGS="-fsanitize=leak" \
-DCMAKE_CXX_FLAGS="-fsanitize=leak"
- name: Build
Expand Down
2 changes: 1 addition & 1 deletion libCacheSim/cache/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ cache_obj_t *cache_find_base(cache_t *cache, const request_t *req,
cache->remove(cache, cache_obj->obj_id);
}

cache_obj = NULL;
return NULL;
}
#endif

Expand Down
78 changes: 78 additions & 0 deletions test/test_evictionAlgo.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,78 @@ static void test_LRUv0(gconstpointer user_data) {
test_cache_algorithm(user_data, &test_data);
}

#ifdef SUPPORT_TTL
typedef struct {
const char *test_path;
cache_init_func_ptr init;
int warmup_objects;
} expired_object_test_t;

static const expired_object_test_t expired_object_tests[] = {
{"/libCacheSim/cacheAlgo_FIFO_expired_object", FIFO_init, 0},
{"/libCacheSim/cacheAlgo_LRU_expired_object", LRU_init, 0},
{"/libCacheSim/cacheAlgo_Sieve_expired_object", Sieve_init, 0},
{"/libCacheSim/cacheAlgo_S3FIFO_expired_object", S3FIFO_init, 0},
{"/libCacheSim/cacheAlgo_S3FIFO_main_expired_object", S3FIFO_init, 10},
};

// Expired non-updating lookups leave the object resident. Updating lookups
// remove it and report a miss without touching the object they just freed.
static void test_expired_object(gconstpointer user_data) {
const expired_object_test_t *test = user_data;
common_cache_params_t cc_params = {.cache_size = 100, .hashpower = 8};
cache_t *cache = test->init(cc_params, NULL);
request_t *req = new_request();
req->obj_size = 1;

// Filling S3FIFO's ten-byte small queue puts the next object in main.
for (int i = 0; i < test->warmup_objects; i++) {
req->obj_id = i + 1;
g_assert_false(cache->get(cache, req));
}
req->obj_id = test->warmup_objects + 1;
req->ttl = 10;
req->next_access_vtime = 42;
g_assert_false(cache->get(cache, req));
cache_obj_t *obj = cache->find(cache, req, false);
g_assert_nonnull(obj);
const int64_t n_obj = cache->get_n_obj(cache);
const int64_t occupied_byte = cache->get_occupied_byte(cache);
const int32_t freq = obj->freq;

req->clock_time = 20;
req->next_access_vtime = 84;
g_assert_null(cache->find(cache, req, false));
g_assert_cmpint(cache->get_n_obj(cache), ==, n_obj);
g_assert_cmpint(cache->get_occupied_byte(cache), ==, occupied_byte);

// Observe at the expiry boundary without updating: the same object and
// metadata remain, and expiry is strictly earlier than the request time.
req->clock_time = 10;
g_assert_true(cache->find(cache, req, false) == obj);
g_assert_cmpint(obj->freq, ==, freq);
g_assert_cmpint(obj->next_access_vtime, ==, 42);

req->clock_time = 20;
g_assert_null(cache->find(cache, req, true));
g_assert_cmpint(cache->get_n_obj(cache), ==, n_obj - 1);
g_assert_cmpint(cache->get_occupied_byte(cache), ==, occupied_byte - 1);
g_assert_false(cache->get(cache, req));
g_assert_cmpint(cache->get_n_obj(cache), ==, n_obj);
g_assert_cmpint(cache->get_occupied_byte(cache), ==, occupied_byte);
g_assert_true(cache->get(cache, req));

// Exercise expiration through get(), which removes and reinserts the item.
req->clock_time = 40;
g_assert_false(cache->get(cache, req));
g_assert_cmpint(cache->get_n_obj(cache), ==, n_obj);
g_assert_cmpint(cache->get_occupied_byte(cache), ==, occupied_byte);

free_request(req);
cache->cache_free(cache);
}
#endif

static void test_LRU_K(gconstpointer user_data) {
test_cache_algorithm(user_data, &test_data_truth[17]);
}
Expand Down Expand Up @@ -798,6 +870,12 @@ int main(int argc, char *argv[]) {
g_test_add_data_func("/libCacheSim/cacheAlgo_LRU", reader, test_LRU);
g_test_add_data_func("/libCacheSim/cacheAlgo_LRUv0", reader, test_LRUv0);
g_test_add_data_func("/libCacheSim/cacheAlgo_LRU_K", reader, test_LRU_K);
#ifdef SUPPORT_TTL
for (gsize i = 0; i < G_N_ELEMENTS(expired_object_tests); i++) {
g_test_add_data_func(expired_object_tests[i].test_path,
&expired_object_tests[i], test_expired_object);
}
#endif
g_test_add_data_func("/libCacheSim/cacheAlgo_Merlin", reader, test_Merlin);
g_test_add_data_func("/libCacheSim/cacheAlgo_MQ", reader, test_MQ);
g_test_add_data_func("/libCacheSim/cacheAlgo_MRU", reader, test_MRU);
Expand Down
Loading