Skip to content

Commit 9d57105

Browse files
committed
add test_Mithril
1 parent e541afa commit 9d57105

8 files changed

Lines changed: 116 additions & 5 deletions

File tree

libCacheSim/cache/admission/adaptsize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void free_adaptsize_admissioner(admissioner_t *admissioner) {
6262
static_cast<adaptsize_admission_params_t *>(admissioner->params);
6363

6464
free(pa);
65-
if(admissioner->init_params){
65+
if (admissioner->init_params) {
6666
free(admissioner->init_params);
6767
}
6868
free(admissioner);

libCacheSim/cache/admission/bloomfilter.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ typedef struct bloomfilter_admission {
1818
bool bloomfilter_admit(admissioner_t *admissioner, const request_t *req) {
1919
bf_admission_params_t *bf = admissioner->params;
2020
gpointer key = GINT_TO_POINTER(req->obj_id);
21-
gpointer n_times = g_hash_table_lookup(bf->seen_times, GSIZE_TO_POINTER(req->obj_id));
21+
gpointer n_times =
22+
g_hash_table_lookup(bf->seen_times, GSIZE_TO_POINTER(req->obj_id));
2223
if (n_times == NULL) {
2324
g_hash_table_insert(bf->seen_times, key, GINT_TO_POINTER(1));
2425
return false;
@@ -37,7 +38,7 @@ void free_bloomfilter_admissioner(admissioner_t *admissioner) {
3738
struct bloomfilter_admission *bf = admissioner->params;
3839
g_hash_table_destroy(bf->seen_times);
3940
free(bf);
40-
if(admissioner->init_params){
41+
if (admissioner->init_params) {
4142
free(admissioner->init_params);
4243
}
4344
free(admissioner);

libCacheSim/cache/admission/prob.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void free_prob_admissioner(admissioner_t *admissioner) {
7575
prob_admission_params_t *pa = admissioner->params;
7676

7777
free(pa);
78-
if(admissioner->init_params){
78+
if (admissioner->init_params) {
7979
free(admissioner->init_params);
8080
}
8181
free(admissioner);

libCacheSim/cache/cache.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ cache_t *create_cache_with_new_size(const cache_t *old_cache,
9898
if (old_cache->admissioner != NULL) {
9999
cache->admissioner = old_cache->admissioner->clone(old_cache->admissioner);
100100
}
101+
if (old_cache->prefetcher != NULL) {
102+
cache->prefetcher =
103+
old_cache->prefetcher->clone(old_cache->prefetcher, new_size);
104+
}
101105
cache->future_stack_dist = old_cache->future_stack_dist;
102106
cache->future_stack_dist_array_size = old_cache->future_stack_dist_array_size;
103107
return cache;

libCacheSim/cache/prefetch/Mithril.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ static void set_Mithril_params(Mithril_params_t *Mithril_params,
232232
// **** ****
233233
// **** prefetcher interfaces ****
234234
// **** ****
235-
// **** create, free, handle_find, prefetch, handle_evict ****
235+
// **** create, free, clone, handle_find, handle_evict, prefetch ****
236236
// ***********************************************************************
237237
/**
238238
1. record the request in cache_size_map for being aware of prefetching object's
@@ -435,9 +435,17 @@ void free_Mithril_prefetcher(prefetcher_t *prefetcher) {
435435
g_hash_table_destroy(Mithril_params->prefetched_hashtable_sequential);
436436
}
437437
my_free(sizeof(Mithril_params_t), Mithril_params);
438+
if (prefetcher->init_params) {
439+
free(prefetcher->init_params);
440+
}
438441
my_free(sizeof(prefetcher_t), prefetcher);
439442
}
440443

444+
prefetcher_t *clone_Mithril_prefetcher(prefetcher_t *prefetcher,
445+
uint64_t cache_size) {
446+
return create_Mithril_prefetcher(prefetcher->init_params, cache_size);
447+
}
448+
441449
prefetcher_t *create_Mithril_prefetcher(const char *init_params,
442450
uint64_t cache_size) {
443451
Mithril_init_params_t *Mithril_init_params = my_malloc(Mithril_init_params_t);
@@ -463,6 +471,10 @@ prefetcher_t *create_Mithril_prefetcher(const char *init_params,
463471
prefetcher->handle_find = Mithril_handle_find;
464472
prefetcher->handle_evict = Mithril_handle_evict;
465473
prefetcher->free = free_Mithril_prefetcher;
474+
prefetcher->clone = clone_Mithril_prefetcher;
475+
if (init_params) {
476+
prefetcher->init_params = strdup(init_params);
477+
}
466478

467479
my_free(sizeof(Mithril_init_params_t), Mithril_init_params);
468480
return prefetcher;

libCacheSim/include/libCacheSim/prefetchAlgo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef PREFETCHINGALGO_H
22
#define PREFETCHINGALGO_H
3+
34
#include "cache.h"
45
#include "request.h"
56

@@ -13,13 +14,17 @@ typedef void (*prefetcher_prefetch_func_ptr)(cache_t *, const request_t *);
1314
typedef void (*prefetcher_handle_find_func_ptr)(cache_t *, const request_t *);
1415
typedef void (*prefetcher_handle_evict_func_ptr)(cache_t *, const request_t *);
1516
typedef void (*prefetcher_free_func_ptr)(struct prefetcher *);
17+
typedef struct prefetcher *(*prefetcher_clone_func_ptr)(struct prefetcher *,
18+
uint64_t);
1619

1720
typedef struct prefetcher {
1821
void *params;
22+
void *init_params;
1923
prefetcher_prefetch_func_ptr prefetch;
2024
prefetcher_handle_find_func_ptr handle_find;
2125
prefetcher_handle_evict_func_ptr handle_evict;
2226
prefetcher_free_func_ptr free;
27+
prefetcher_clone_func_ptr clone;
2328
} prefetcher_t;
2429

2530
prefetcher_t *create_Mithril_prefetcher(const char *init_paramsm,

test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ target_link_libraries(testSimulator ${coreLib})
1616
add_executable(testEvictionAlgo test_evictionAlgo.c)
1717
target_link_libraries(testEvictionAlgo ${coreLib})
1818

19+
add_executable(testPrefetchAlgo test_prefetchAlgo.c)
20+
target_link_libraries(testPrefetchAlgo ${coreLib})
21+
1922

2023
add_test(NAME testReader COMMAND testReader WORKING_DIRECTORY .)
2124
add_test(NAME testDistUtils COMMAND testDistUtils WORKING_DIRECTORY .)
2225
add_test(NAME testProfilerLRU COMMAND testProfilerLRU WORKING_DIRECTORY .)
2326
add_test(NAME testSimulator COMMAND testSimulator WORKING_DIRECTORY .)
2427
add_test(NAME testEvictionAlgo COMMAND testEvictionAlgo WORKING_DIRECTORY .)
28+
add_test(NAME testPrefetchAlgo COMMAND testPrefetchAlgo WORKING_DIRECTORY .)
2529

2630
# if (ENABLE_GLCACHE)
2731
# add_executable(testGLCache test_glcache.c)

test/test_prefetchAlgo.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// Created by Zhelong Zhao on 2023.08.16.
3+
//
4+
#include "../libCacheSim/utils/include/mymath.h"
5+
#include "common.h"
6+
7+
// static const uint64_t req_cnt_true = 113872, req_byte_true = 4205978112;
8+
static const uint64_t req_cnt_true = 113872, req_byte_true = 4368040448;
9+
10+
static void _verify_profiler_results(const cache_stat_t *res,
11+
uint64_t num_of_sizes,
12+
uint64_t req_cnt_true,
13+
const uint64_t *miss_cnt_true,
14+
uint64_t req_byte_true,
15+
const uint64_t *miss_byte_true) {
16+
for (uint64_t i = 0; i < num_of_sizes; i++) {
17+
g_assert_cmpuint(req_cnt_true, ==, res[i].n_req);
18+
g_assert_cmpuint(miss_cnt_true[i], ==, res[i].n_miss);
19+
g_assert_cmpuint(req_byte_true, ==, res[i].n_req_byte);
20+
g_assert_cmpuint(miss_byte_true[i], ==, res[i].n_miss_byte);
21+
}
22+
}
23+
24+
static void print_results(const cache_t *cache, const cache_stat_t *res) {
25+
printf("%s uint64_t cache_size[] = {", cache->cache_name);
26+
printf("%ld", res[0].cache_size);
27+
for (uint64_t i = 1; i < CACHE_SIZE / STEP_SIZE; i++) {
28+
printf(", %ld", res[i].cache_size);
29+
}
30+
printf("};\n");
31+
32+
printf("uint64_t miss_cnt_true[] = {");
33+
printf("%ld", res[0].n_miss);
34+
for (uint64_t i = 1; i < CACHE_SIZE / STEP_SIZE; i++) {
35+
printf(", %ld", res[i].n_miss);
36+
}
37+
printf("};\n");
38+
39+
printf("uint64_t miss_byte_true[] = {");
40+
printf("%ld", res[0].n_miss_byte);
41+
for (uint64_t i = 1; i < CACHE_SIZE / STEP_SIZE; i++) {
42+
printf(", %ld", res[i].n_miss_byte);
43+
}
44+
printf("};\n");
45+
}
46+
47+
static void test_Mithril(gconstpointer user_data) {
48+
uint64_t miss_cnt_true[] = {79796, 78480, 76126, 75256,
49+
72336, 72062, 71936, 71667};
50+
uint64_t miss_byte_true[] = {3471357440, 3399726080, 3285093888, 3245231616,
51+
3092759040, 3077801472, 3075234816, 3061489664};
52+
53+
reader_t *reader = (reader_t *)user_data;
54+
common_cache_params_t cc_params = {
55+
.cache_size = CACHE_SIZE, .hashpower = 20, .default_ttl = DEFAULT_TTL};
56+
cache_t *cache = create_test_cache("Mithril", cc_params, reader, NULL);
57+
g_assert_true(cache != NULL);
58+
cache_stat_t *res = simulate_at_multi_sizes_with_step_size(
59+
reader, cache, STEP_SIZE, NULL, 0, 0, _n_cores());
60+
61+
print_results(cache, res);
62+
_verify_profiler_results(res, CACHE_SIZE / STEP_SIZE, req_cnt_true,
63+
miss_cnt_true, req_byte_true, miss_byte_true);
64+
cache->cache_free(cache);
65+
my_free(sizeof(cache_stat_t), res);
66+
}
67+
68+
int main(int argc, char *argv[]) {
69+
g_test_init(&argc, &argv, NULL);
70+
srand(0); // for reproducibility
71+
set_rand_seed(rand());
72+
73+
reader_t *reader;
74+
75+
// do not use these two because object size change over time and
76+
// not all algorithms can handle the object size change correctly
77+
// reader = setup_csv_reader_obj_num();
78+
// reader = setup_vscsi_reader();
79+
80+
reader = setup_oracleGeneralBin_reader();
81+
// reader = setup_vscsi_reader_with_ignored_obj_size();
82+
g_test_add_data_func("/libCacheSim/cacheAlgo_Mithril", reader, test_Mithril);
83+
84+
return g_test_run();
85+
}

0 commit comments

Comments
 (0)