Skip to content

Commit

Permalink
Merge pull request #17 from zztaki/support-Mithril
Browse files Browse the repository at this point in the history
add prefetch module && mv Mithril to prefetch
  • Loading branch information
1a1a11a authored Aug 21, 2023
2 parents 0f4d135 + 9747163 commit 5fa68ef
Show file tree
Hide file tree
Showing 21 changed files with 1,938 additions and 201 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ message(STATUS "========================================================")
# library compilation #
########################################
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
set(ALL_MODULES cachelib admission evictionC evictionCPP traceReader profiler dataStructure ds_hash utils)
set(ALL_MODULES cachelib admission prefetch evictionC evictionCPP traceReader profiler dataStructure ds_hash utils)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libCacheSim/cache)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libCacheSim/dataStructure)
Expand Down
1 change: 1 addition & 0 deletions libCacheSim/bin/cachesim/cache_init.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include <strings.h>
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
Expand Down
39 changes: 36 additions & 3 deletions libCacheSim/bin/cachesim/cli_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "../../include/libCacheSim/const.h"
#include "../../include/libCacheSim/dist.h"
#include "../../include/libCacheSim/prefetchAlgo.h"
#include "../../utils/include/mystr.h"
#include "../../utils/include/mysys.h"
#include "../cli_reader_utils.h"
Expand Down Expand Up @@ -43,6 +44,9 @@ enum argp_option_short {
OPTION_NUM_THREAD = 0x106,
OPTION_SAMPLE_RATIO = 's',
OPTION_REPORT_INTERVAL = 0x108,

OPTION_PREFETCH_ALGO = 'p',
OPTION_PREFETCH_PARAMS = 0x109,
};

/*
Expand All @@ -61,12 +65,17 @@ static struct argp_option options[] = {
"Sample ratio, 1 means no sampling, 0.01 means sample 1% of objects", 2},

{NULL, 0, NULL, 0, "cache related parameters:", 0},
{"eviction-params", OPTION_EVICTION_PARAMS, "\"n-seg=\"4", 0,
{"eviction-params", OPTION_EVICTION_PARAMS, "\"n-seg=4\"", 0,
"optional params for each eviction algorithm, e.g., n-seg=4", 4},
{"admission", OPTION_ADMISSION_ALGO, "bloom-filter", 0,
"Admission algorithm: size/bloom-filter/prob", 4},
{"admission-params", OPTION_ADMISSION_PARAMS, "\"prob=0.8\"", 0,
"params for admission algorithm", 4},
{"prefetch", OPTION_PREFETCH_ALGO, "Mithril", 0,
"Prefetching algorithm: Mithril", 4},
{"prefetch-params", OPTION_PREFETCH_PARAMS, "\"block-size=65536\"", 0,
"optional params for each prefetching algorithm, e.g., block-size=65536",
4},

{0, 0, 0, 0, "Other options:"},
{"ignore-obj-size", OPTION_IGNORE_OBJ_SIZE, "false", 0,
Expand All @@ -85,8 +94,7 @@ static struct argp_option options[] = {
"Whether consider per object metadata size in the simulated cache", 10},
{"verbose", OPTION_VERBOSE, "1", 0, "Produce verbose output", 10},

{0}
};
{0}};

/*
PARSER. Field 2 in ARGP.
Expand All @@ -113,11 +121,19 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
case OPTION_ADMISSION_ALGO:
arguments->admission_algo = arg;
break;
case OPTION_PREFETCH_ALGO:
arguments->prefetch_algo = arg;
break;
case OPTION_ADMISSION_PARAMS:
arguments->admission_params = strdup(arg);
replace_char(arguments->admission_params, ';', ',');
replace_char(arguments->admission_params, '_', '-');
break;
case OPTION_PREFETCH_PARAMS:
arguments->prefetch_params = strdup(arg);
replace_char(arguments->prefetch_params, ';', ',');
replace_char(arguments->prefetch_params, '_', '-');
break;
case OPTION_OUTPUT_PATH:
strncpy(arguments->ofilepath, arg, OFILEPATH_LEN);
break;
Expand Down Expand Up @@ -195,7 +211,9 @@ static void init_arg(struct arguments *args) {
args->trace_path = NULL;
args->eviction_params = NULL;
args->admission_algo = NULL;
args->prefetch_algo = NULL;
args->admission_params = NULL;
args->prefetch_params = NULL;
args->trace_type_str = NULL;
args->trace_type_params = NULL;
args->verbose = true;
Expand Down Expand Up @@ -322,6 +340,11 @@ void parse_cmd(int argc, char *argv[], struct arguments *args) {
args->caches[idx]->admissioner =
create_admissioner(args->admission_algo, args->admission_params);
}

if (args->prefetch_algo != NULL) {
args->caches[idx]->prefetcher = create_prefetcher(
args->prefetch_algo, args->prefetch_params, args->cache_sizes[j]);
}
}
}

Expand Down Expand Up @@ -492,6 +515,16 @@ void print_parsed_args(struct arguments *args) {
n += snprintf(output_str + n, OUTPUT_STR_LEN - n - 1,
", admission-params: %s", args->admission_params);

if (args->prefetch_algo != NULL) {
n += snprintf(output_str + n, OUTPUT_STR_LEN - n - 1, ", prefetch: %s",
args->prefetch_algo);
}

if (args->prefetch_params != NULL) {
n += snprintf(output_str + n, OUTPUT_STR_LEN - n - 1,
", prefetch-params: %s", args->prefetch_params);
}

if (args->eviction_params != NULL)
n += snprintf(output_str + n, OUTPUT_STR_LEN - n - 1,
", eviction-params: %s", args->eviction_params);
Expand Down
3 changes: 2 additions & 1 deletion libCacheSim/bin/cachesim/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct arguments {
char *eviction_algo[N_MAX_ALGO];
int n_eviction_algo;
char *admission_algo;
char *prefetch_algo;
uint64_t cache_sizes[N_MAX_CACHE_SIZE];
int n_cache_size;
int warmup_sec;
Expand All @@ -35,6 +36,7 @@ struct arguments {
char *trace_type_params;
char *eviction_params;
char *admission_params;
char *prefetch_params;
double sample_ratio;
int n_thread;
int64_t n_req; /* number of requests to process */
Expand Down Expand Up @@ -64,4 +66,3 @@ void print_parsed_args(struct arguments *args);
#ifdef __cplusplus
}
#endif

2 changes: 2 additions & 0 deletions libCacheSim/cache/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

include_directories(admission)
include_directories(eviction)
include_directories(prefetch)

add_subdirectory(admission)
add_subdirectory(eviction)
add_subdirectory(prefetch)

add_library(cachelib cache.c cacheObj.c)
target_link_libraries(cachelib dataStructure)
3 changes: 3 additions & 0 deletions libCacheSim/cache/admission/adaptsize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ void free_adaptsize_admissioner(admissioner_t *admissioner) {
static_cast<adaptsize_admission_params_t *>(admissioner->params);

free(pa);
if (admissioner->init_params) {
free(admissioner->init_params);
}
free(admissioner);
}

Expand Down
6 changes: 5 additions & 1 deletion libCacheSim/cache/admission/bloomfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ typedef struct bloomfilter_admission {
bool bloomfilter_admit(admissioner_t *admissioner, const request_t *req) {
bf_admission_params_t *bf = admissioner->params;
gpointer key = GINT_TO_POINTER(req->obj_id);
gpointer n_times = g_hash_table_lookup(bf->seen_times, GSIZE_TO_POINTER(req->obj_id));
gpointer n_times =
g_hash_table_lookup(bf->seen_times, GSIZE_TO_POINTER(req->obj_id));
if (n_times == NULL) {
g_hash_table_insert(bf->seen_times, key, GINT_TO_POINTER(1));
return false;
Expand All @@ -37,6 +38,9 @@ void free_bloomfilter_admissioner(admissioner_t *admissioner) {
struct bloomfilter_admission *bf = admissioner->params;
g_hash_table_destroy(bf->seen_times);
free(bf);
if (admissioner->init_params) {
free(admissioner->init_params);
}
free(admissioner);
}

Expand Down
3 changes: 3 additions & 0 deletions libCacheSim/cache/admission/prob.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ void free_prob_admissioner(admissioner_t *admissioner) {
prob_admission_params_t *pa = admissioner->params;

free(pa);
if (admissioner->init_params) {
free(admissioner->init_params);
}
free(admissioner);
}

Expand Down
3 changes: 3 additions & 0 deletions libCacheSim/cache/admission/size.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ void free_size_admissioner(admissioner_t *admissioner) {
size_admission_params_t *pa = admissioner->params;

free(pa);
if (admissioner->init_params) {
free(admissioner->init_params);
}
free(admissioner);
}

Expand Down
54 changes: 40 additions & 14 deletions libCacheSim/cache/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../include/libCacheSim/cache.h"

#include "../dataStructure/hashtable/hashtable.h"
#include "../include/libCacheSim/prefetchAlgo.h"

/** this file contains both base function, which should be called by all
*eviction algorithms, and the queue related functions, which should be called
Expand Down Expand Up @@ -33,6 +34,7 @@ cache_t *cache_struct_init(const char *const cache_name,
cache->cache_size = params.cache_size;
cache->eviction_params = NULL;
cache->admissioner = NULL;
cache->prefetcher = NULL;
cache->future_stack_dist = NULL;
cache->future_stack_dist_array_size = 0;
cache->default_ttl = params.default_ttl;
Expand Down Expand Up @@ -71,6 +73,7 @@ cache_t *cache_struct_init(const char *const cache_name,
void cache_struct_free(cache_t *cache) {
free_hashtable(cache->hashtable);
if (cache->admissioner != NULL) cache->admissioner->free(cache->admissioner);
if (cache->prefetcher != NULL) cache->prefetcher->free(cache->prefetcher);
my_free(sizeof(cache_t), cache);
}

Expand All @@ -95,6 +98,10 @@ cache_t *create_cache_with_new_size(const cache_t *old_cache,
if (old_cache->admissioner != NULL) {
cache->admissioner = old_cache->admissioner->clone(old_cache->admissioner);
}
if (old_cache->prefetcher != NULL) {
cache->prefetcher =
old_cache->prefetcher->clone(old_cache->prefetcher, new_size);
}
cache->future_stack_dist = old_cache->future_stack_dist;
cache->future_stack_dist_array_size = old_cache->future_stack_dist_array_size;
return cache;
Expand Down Expand Up @@ -146,6 +153,13 @@ cache_obj_t *cache_find_base(cache_t *cache, const request_t *req,
const bool update_cache) {
cache_obj_t *cache_obj = hashtable_find(cache->hashtable, req);

// "update_cache = true" means that it is a real user request, use handle_find
// to update prefetcher's state
if (cache->prefetcher && cache->prefetcher->handle_find && update_cache) {
bool hit = (cache_obj != NULL);
cache->prefetcher->handle_find(cache, req, hit);
}

if (cache_obj != NULL) {
#ifdef SUPPORT_TTL
if (cache_obj->exp_time != 0 && cache_obj->exp_time < req->clock_time) {
Expand Down Expand Up @@ -193,26 +207,27 @@ bool cache_get_base(cache_t *cache, const request_t *req) {
cache->get_occupied_byte(cache), cache->cache_size);

cache_obj_t *obj = cache->find(cache, req, true);
bool hit = (obj != NULL);

if (obj != NULL) {
if (hit) {
VVERBOSE("req %ld, obj %ld --- cache hit\n", cache->n_req, req->obj_id);
return true;
}

if (cache->can_insert(cache, req) == false) {
} else if (!cache->can_insert(cache, req)) {
VVERBOSE("req %ld, obj %ld --- cache miss cannot insert\n", cache->n_req,
req->obj_id);
return false;
} else {
while (cache->get_occupied_byte(cache) + req->obj_size +
cache->obj_md_size >
cache->cache_size) {
cache->evict(cache, req);
}
cache->insert(cache, req);
}

while (cache->get_occupied_byte(cache) + req->obj_size + cache->obj_md_size >
cache->cache_size) {
cache->evict(cache, req);
if (cache->prefetcher && cache->prefetcher->prefetch) {
cache->prefetcher->prefetch(cache, req);
}

cache->insert(cache, req);

return false;
return hit;
}

/**
Expand All @@ -237,8 +252,8 @@ cache_obj_t *cache_insert_base(cache_t *cache, const request_t *req) {
}
#endif

#if defined(TRACK_EVICTION_V_AGE) || \
defined(TRACK_DEMOTION) || defined(TRACK_CREATE_TIME)
#if defined(TRACK_EVICTION_V_AGE) || defined(TRACK_DEMOTION) || \
defined(TRACK_CREATE_TIME)
cache_obj->create_time = CURR_TIME(cache, req);
#endif

Expand All @@ -263,6 +278,17 @@ void cache_evict_base(cache_t *cache, cache_obj_t *obj,
record_eviction_age(cache, obj, CURR_TIME(cache, req) - obj->create_time);
}
#endif
if (cache->prefetcher && cache->prefetcher->handle_evict) {
request_t *check_req = new_request();
check_req->obj_id = obj->obj_id;
check_req->obj_size = obj->obj_size;
// check_req->ttl = 0; // re-add objects should be?
cache_remove_obj_base(cache, obj, remove_from_hashtable);
cache->prefetcher->handle_evict(cache, check_req);
my_free(sizeof(request_t), check_req);
return;
}

cache_remove_obj_base(cache, obj, remove_from_hashtable);
}

Expand Down
1 change: 1 addition & 0 deletions libCacheSim/cache/eviction/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ set(sourceC
other/S3LRU.c

Sieve.c

)

if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/priv")
Expand Down
Loading

0 comments on commit 5fa68ef

Please sign in to comment.