Skip to content

Commit d65e551

Browse files
committed
Optimize cache_init.hpp: replace char* with std::string, use map instead of if-else chain, fix strlen safety issues
1 parent 4313fb0 commit d65e551

3 files changed

Lines changed: 86 additions & 166 deletions

File tree

libCacheSim-python/src/cache_init.hpp

Lines changed: 83 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -3,193 +3,112 @@
33
#include <cstdio>
44
#include <cstdlib>
55
#include <cstring>
6+
#include <string>
7+
#include <unordered_map>
8+
#include <functional>
9+
#include <algorithm>
610

711
#include "libCacheSim/cache.h"
812
#include "libCacheSim/evictionAlgo.h"
913

1014
// Note: we don't need to pass in the trace path here
1115
// Cache size is precomputed in the Python side
12-
static inline cache_t *create_sim_cache(const char *eviction_algo,
16+
static inline cache_t *create_sim_cache(const std::string& eviction_algo,
1317
const uint64_t cache_size,
14-
const char *eviction_params,
18+
const std::string& eviction_params,
1519
const bool consider_obj_metadata) {
1620
common_cache_params_t cc_params = {
1721
.cache_size = cache_size,
1822
.default_ttl = 86400 * 300,
1923
.hashpower = 24,
2024
.consider_obj_metadata = consider_obj_metadata,
2125
};
22-
cache_t *cache;
2326

24-
if (strcasecmp(eviction_algo, "lru") == 0) {
25-
cache = LRU_init(cc_params, eviction_params);
26-
} else if (strcasecmp(eviction_algo, "fifo") == 0) {
27-
cache = FIFO_init(cc_params, eviction_params);
28-
} else if (strcasecmp(eviction_algo, "arc") == 0) {
29-
cache = ARC_init(cc_params, eviction_params);
30-
} else if (strcasecmp(eviction_algo, "arcv0") == 0) {
31-
cache = ARCv0_init(cc_params, eviction_params);
32-
} else if (strcasecmp(eviction_algo, "lhd") == 0) {
33-
cache = LHD_init(cc_params, eviction_params);
34-
} else if (strcasecmp(eviction_algo, "random") == 0) {
35-
cache = Random_init(cc_params, eviction_params);
36-
} else if (strcasecmp(eviction_algo, "randomTwo") == 0) {
37-
cache = RandomTwo_init(cc_params, eviction_params);
38-
} else if (strcasecmp(eviction_algo, "lfu") == 0) {
39-
cache = LFU_init(cc_params, eviction_params);
40-
} else if (strcasecmp(eviction_algo, "gdsf") == 0) {
41-
cache = GDSF_init(cc_params, eviction_params);
42-
} else if (strcasecmp(eviction_algo, "lfuda") == 0) {
43-
cache = LFUDA_init(cc_params, eviction_params);
44-
} else if (strcasecmp(eviction_algo, "twoq") == 0 ||
45-
strcasecmp(eviction_algo, "2q") == 0) {
46-
cache = TwoQ_init(cc_params, eviction_params);
47-
} else if (strcasecmp(eviction_algo, "slru") == 0) {
48-
cache = SLRU_init(cc_params, eviction_params);
49-
} else if (strcasecmp(eviction_algo, "slruv0") == 0) {
50-
cache = SLRUv0_init(cc_params, eviction_params);
51-
} else if (strcasecmp(eviction_algo, "hyperbolic") == 0) {
52-
cc_params.hashpower = MAX(cc_params.hashpower - 8, 16);
53-
cache = Hyperbolic_init(cc_params, eviction_params);
54-
} else if (strcasecmp(eviction_algo, "lecar") == 0) {
55-
cache = LeCaR_init(cc_params, eviction_params);
56-
} else if (strcasecmp(eviction_algo, "lecarv0") == 0) {
57-
cache = LeCaRv0_init(cc_params, eviction_params);
58-
} else if (strcasecmp(eviction_algo, "RandomLRU") == 0) {
59-
cache = RandomLRU_init(cc_params, eviction_params);
60-
} else if (strcasecmp(eviction_algo, "cacheus") == 0) {
61-
cache = Cacheus_init(cc_params, eviction_params);
62-
} else if (strcasecmp(eviction_algo, "size") == 0) {
63-
cache = Size_init(cc_params, eviction_params);
64-
} else if (strcasecmp(eviction_algo, "lfucpp") == 0) {
65-
cache = LFUCpp_init(cc_params, eviction_params);
66-
} else if (strcasecmp(eviction_algo, "tinyLFU") == 0) {
67-
if (eviction_params == NULL) {
68-
cache = WTinyLFU_init(cc_params, eviction_params);
69-
} else {
70-
const char *window_size = strstr(eviction_params, "window-size=");
71-
if (window_size == NULL) {
72-
char dest[30];
73-
strncpy(dest, eviction_params, sizeof(dest) - 1);
74-
dest[sizeof(dest) - 1] = '\0';
75-
size_t param_len = strlen(dest);
76-
char *new_params = (char *)malloc(param_len + 20);
77-
if (param_len > 0) {
78-
snprintf(new_params, param_len + 20, "%s,window-size=0.01",
79-
eviction_params);
80-
} else { // if eviction_params is "", no comma is needed
81-
snprintf(new_params, 20, "window-size=0.01");
82-
}
83-
cache = WTinyLFU_init(cc_params, new_params);
84-
free(new_params);
85-
} else {
86-
cache = WTinyLFU_init(cc_params, eviction_params);
87-
}
88-
}
89-
} else if (strcasecmp(eviction_algo, "wtinyLFU") == 0) {
90-
cache = WTinyLFU_init(cc_params, eviction_params);
91-
/* TODO(haocheng): add belady support, since we remove the trace path,
92-
* format info needs to be passed in through another parameter */
93-
// } else if (strcasecmp(eviction_algo, "belady") == 0 &&
94-
// strcasestr(trace_path, "lcs") == NULL) {
95-
// if (strcasestr(trace_path, "oracleGeneral") == NULL) {
96-
// WARN("belady is only supported for oracleGeneral and lcs trace\n");
97-
// WARN("to convert a trace to lcs format\n");
98-
// WARN("./bin/traceConv input_trace trace_format output_trace\n");
99-
// WARN("./bin/traceConv ../data/cloudPhysicsIO.txt txt\n");
100-
// exit(1);
101-
// }
102-
// cache = Belady_init(cc_params, eviction_params);
103-
} else if (strcasecmp(eviction_algo, "nop") == 0) {
104-
cache = nop_init(cc_params, eviction_params);
105-
// } else if (strcasecmp(eviction_algo, "beladySize") == 0) {
106-
// if (strcasestr(trace_path, "oracleGeneral") == NULL &&
107-
// strcasestr(trace_path, "lcs") == NULL) {
108-
// WARN("beladySize is only supported for oracleGeneral and lcs
109-
// trace\n"); WARN("to convert a trace to lcs format\n");
110-
// WARN("./bin/traceConv input_trace trace_format output_trace\n");
111-
// WARN("./bin/traceConv ../data/cloudPhysicsIO.txt txt\n");
112-
// exit(1);
113-
// }
114-
// cc_params.hashpower = MAX(cc_params.hashpower - 8, 16);
115-
// cache = BeladySize_init(cc_params, eviction_params);
116-
} else if (strcasecmp(eviction_algo, "fifo-reinsertion") == 0 ||
117-
strcasecmp(eviction_algo, "clock") == 0 ||
118-
strcasecmp(eviction_algo, "second-chance") == 0) {
119-
cache = Clock_init(cc_params, eviction_params);
120-
} else if (strcasecmp(eviction_algo, "clockpro") == 0) {
121-
cache = ClockPro_init(cc_params, eviction_params);
122-
} else if (strcasecmp(eviction_algo, "lirs") == 0) {
123-
cache = LIRS_init(cc_params, eviction_params);
124-
} else if (strcasecmp(eviction_algo, "fifomerge") == 0 ||
125-
strcasecmp(eviction_algo, "fifo-merge") == 0) {
126-
cache = FIFO_Merge_init(cc_params, eviction_params);
127-
// } else if (strcasecmp(eviction_algo, "fifo-reinsertion") == 0) {
128-
// cache = FIFO_Reinsertion_init(cc_params, eviction_params);
129-
} else if (strcasecmp(eviction_algo, "flashProb") == 0) {
130-
// used to measure application level write amp
131-
cache = flashProb_init(cc_params, eviction_params);
132-
} else if (strcasecmp(eviction_algo, "sfifo") == 0) {
133-
cache = SFIFO_init(cc_params, eviction_params);
134-
} else if (strcasecmp(eviction_algo, "sfifov0") == 0) {
135-
cache = SFIFOv0_init(cc_params, eviction_params);
136-
} else if (strcasecmp(eviction_algo, "lru-prob") == 0) {
137-
cache = LRU_Prob_init(cc_params, eviction_params);
138-
} else if (strcasecmp(eviction_algo, "fifo-belady") == 0) {
139-
cache = FIFO_Belady_init(cc_params, eviction_params);
140-
} else if (strcasecmp(eviction_algo, "lru-belady") == 0) {
141-
cache = LRU_Belady_init(cc_params, eviction_params);
142-
} else if (strcasecmp(eviction_algo, "sieve-belady") == 0) {
143-
cache = Sieve_Belady_init(cc_params, eviction_params);
144-
} else if (strcasecmp(eviction_algo, "s3lru") == 0) {
145-
cache = S3LRU_init(cc_params, eviction_params);
146-
} else if (strcasecmp(eviction_algo, "s3fifo") == 0 ||
147-
strcasecmp(eviction_algo, "s3-fifo") == 0) {
148-
cache = S3FIFO_init(cc_params, eviction_params);
149-
} else if (strcasecmp(eviction_algo, "s3fifov0") == 0 ||
150-
strcasecmp(eviction_algo, "s3-fifov0") == 0) {
151-
cache = S3FIFOv0_init(cc_params, eviction_params);
152-
} else if (strcasecmp(eviction_algo, "s3fifod") == 0) {
153-
cache = S3FIFOd_init(cc_params, eviction_params);
154-
} else if (strcasecmp(eviction_algo, "qdlp") == 0) {
155-
cache = QDLP_init(cc_params, eviction_params);
156-
} else if (strcasecmp(eviction_algo, "CAR") == 0) {
157-
cache = CAR_init(cc_params, eviction_params);
158-
} else if (strcasecmp(eviction_algo, "sieve") == 0) {
159-
cache = Sieve_init(cc_params, eviction_params);
27+
using cache_init_func = std::function<cache_t*(const common_cache_params_t&, const char*)>;
28+
29+
static const std::unordered_map<std::string, cache_init_func> cache_init_map = {
30+
{"lru", [](const common_cache_params_t& params, const char* evict_params) { return LRU_init(params, evict_params); }},
31+
{"fifo", [](const common_cache_params_t& params, const char* evict_params) { return FIFO_init(params, evict_params); }},
32+
{"arc", [](const common_cache_params_t& params, const char* evict_params) { return ARC_init(params, evict_params); }},
33+
{"arcv0", [](const common_cache_params_t& params, const char* evict_params) { return ARCv0_init(params, evict_params); }},
34+
{"lhd", [](const common_cache_params_t& params, const char* evict_params) { return LHD_init(params, evict_params); }},
35+
{"random", [](const common_cache_params_t& params, const char* evict_params) { return Random_init(params, evict_params); }},
36+
{"randomtwo", [](const common_cache_params_t& params, const char* evict_params) { return RandomTwo_init(params, evict_params); }},
37+
{"lfu", [](const common_cache_params_t& params, const char* evict_params) { return LFU_init(params, evict_params); }},
38+
{"gdsf", [](const common_cache_params_t& params, const char* evict_params) { return GDSF_init(params, evict_params); }},
39+
{"lfuda", [](const common_cache_params_t& params, const char* evict_params) { return LFUDA_init(params, evict_params); }},
40+
{"twoq", [](const common_cache_params_t& params, const char* evict_params) { return TwoQ_init(params, evict_params); }},
41+
{"2q", [](const common_cache_params_t& params, const char* evict_params) { return TwoQ_init(params, evict_params); }},
42+
{"slru", [](const common_cache_params_t& params, const char* evict_params) { return SLRU_init(params, evict_params); }},
43+
{"slruv0", [](const common_cache_params_t& params, const char* evict_params) { return SLRUv0_init(params, evict_params); }},
44+
{"lecar", [](const common_cache_params_t& params, const char* evict_params) { return LeCaR_init(params, evict_params); }},
45+
{"lecarv0", [](const common_cache_params_t& params, const char* evict_params) { return LeCaRv0_init(params, evict_params); }},
46+
{"randomlru", [](const common_cache_params_t& params, const char* evict_params) { return RandomLRU_init(params, evict_params); }},
47+
{"cacheus", [](const common_cache_params_t& params, const char* evict_params) { return Cacheus_init(params, evict_params); }},
48+
{"size", [](const common_cache_params_t& params, const char* evict_params) { return Size_init(params, evict_params); }},
49+
{"lfucpp", [](const common_cache_params_t& params, const char* evict_params) { return LFUCpp_init(params, evict_params); }},
50+
{"wtinylfu", [](const common_cache_params_t& params, const char* evict_params) { return WTinyLFU_init(params, evict_params); }},
51+
{"nop", [](const common_cache_params_t& params, const char* evict_params) { return nop_init(params, evict_params); }},
52+
{"fifo-reinsertion", [](const common_cache_params_t& params, const char* evict_params) { return Clock_init(params, evict_params); }},
53+
{"clock", [](const common_cache_params_t& params, const char* evict_params) { return Clock_init(params, evict_params); }},
54+
{"second-chance", [](const common_cache_params_t& params, const char* evict_params) { return Clock_init(params, evict_params); }},
55+
{"clockpro", [](const common_cache_params_t& params, const char* evict_params) { return ClockPro_init(params, evict_params); }},
56+
{"lirs", [](const common_cache_params_t& params, const char* evict_params) { return LIRS_init(params, evict_params); }},
57+
{"fifomerge", [](const common_cache_params_t& params, const char* evict_params) { return FIFO_Merge_init(params, evict_params); }},
58+
{"fifo-merge", [](const common_cache_params_t& params, const char* evict_params) { return FIFO_Merge_init(params, evict_params); }},
59+
{"flashprob", [](const common_cache_params_t& params, const char* evict_params) { return flashProb_init(params, evict_params); }},
60+
{"sfifo", [](const common_cache_params_t& params, const char* evict_params) { return SFIFO_init(params, evict_params); }},
61+
{"sfifov0", [](const common_cache_params_t& params, const char* evict_params) { return SFIFOv0_init(params, evict_params); }},
62+
{"lru-prob", [](const common_cache_params_t& params, const char* evict_params) { return LRU_Prob_init(params, evict_params); }},
63+
{"fifo-belady", [](const common_cache_params_t& params, const char* evict_params) { return FIFO_Belady_init(params, evict_params); }},
64+
{"lru-belady", [](const common_cache_params_t& params, const char* evict_params) { return LRU_Belady_init(params, evict_params); }},
65+
{"sieve-belady", [](const common_cache_params_t& params, const char* evict_params) { return Sieve_Belady_init(params, evict_params); }},
66+
{"s3lru", [](const common_cache_params_t& params, const char* evict_params) { return S3LRU_init(params, evict_params); }},
67+
{"s3fifo", [](const common_cache_params_t& params, const char* evict_params) { return S3FIFO_init(params, evict_params); }},
68+
{"s3-fifo", [](const common_cache_params_t& params, const char* evict_params) { return S3FIFO_init(params, evict_params); }},
69+
{"s3fifov0", [](const common_cache_params_t& params, const char* evict_params) { return S3FIFOv0_init(params, evict_params); }},
70+
{"s3-fifov0", [](const common_cache_params_t& params, const char* evict_params) { return S3FIFOv0_init(params, evict_params); }},
71+
{"s3fifod", [](const common_cache_params_t& params, const char* evict_params) { return S3FIFOd_init(params, evict_params); }},
72+
{"qdlp", [](const common_cache_params_t& params, const char* evict_params) { return QDLP_init(params, evict_params); }},
73+
{"car", [](const common_cache_params_t& params, const char* evict_params) { return CAR_init(params, evict_params); }},
74+
{"sieve", [](const common_cache_params_t& params, const char* evict_params) { return Sieve_init(params, evict_params); }},
75+
{"tinylfu", [](const common_cache_params_t& params, const char* evict_params) { return WTinyLFU_init(params, evict_params); }},
16076
#ifdef ENABLE_3L_CACHE
161-
} else if (strcasecmp(eviction_algo, "3LCache") == 0) {
162-
cache = ThreeLCache_init(cc_params, eviction_params);
77+
{"3lcache", [](const common_cache_params_t& params, const char* evict_params) { return ThreeLCache_init(params, evict_params); }},
16378
#endif
16479
#ifdef ENABLE_GLCACHE
165-
} else if (strcasecmp(eviction_algo, "GLCache") == 0 ||
166-
strcasecmp(eviction_algo, "gl-cache") == 0) {
167-
cache = GLCache_init(cc_params, eviction_params);
80+
{"glcache", [](const common_cache_params_t& params, const char* evict_params) { return GLCache_init(params, evict_params); }},
81+
{"gl-cache", [](const common_cache_params_t& params, const char* evict_params) { return GLCache_init(params, evict_params); }},
16882
#endif
16983
#ifdef ENABLE_LRB
170-
} else if (strcasecmp(eviction_algo, "lrb") == 0) {
171-
cache = LRB_init(cc_params, eviction_params);
84+
{"lrb", [](const common_cache_params_t& params, const char* evict_params) { return LRB_init(params, evict_params); }},
17285
#endif
17386
#ifdef INCLUDE_PRIV
174-
} else if (strcasecmp(eviction_algo, "mclock") == 0) {
175-
cache = MClock_init(cc_params, eviction_params);
176-
} else if (strcasecmp(eviction_algo, "lp-sfifo") == 0) {
177-
cache = LP_SFIFO_init(cc_params, eviction_params);
178-
} else if (strcasecmp(eviction_algo, "lp-arc") == 0) {
179-
cache = LP_ARC_init(cc_params, eviction_params);
180-
} else if (strcasecmp(eviction_algo, "lp-twoq") == 0) {
181-
cache = LP_TwoQ_init(cc_params, eviction_params);
182-
} else if (strcasecmp(eviction_algo, "qdlpv0") == 0) {
183-
cache = QDLPv0_init(cc_params, eviction_params);
184-
} else if (strcasecmp(eviction_algo, "s3fifodv2") == 0) {
185-
cache = S3FIFOdv2_init(cc_params, eviction_params);
186-
} else if (strcasecmp(eviction_algo, "myMQv1") == 0) {
187-
cache = myMQv1_init(cc_params, eviction_params);
87+
{"mclock", [](const common_cache_params_t& params, const char* evict_params) { return MClock_init(params, evict_params); }},
88+
{"lp-sfifo", [](const common_cache_params_t& params, const char* evict_params) { return LP_SFIFO_init(params, evict_params); }},
89+
{"lp-arc", [](const common_cache_params_t& params, const char* evict_params) { return LP_ARC_init(params, evict_params); }},
90+
{"lp-twoq", [](const common_cache_params_t& params, const char* evict_params) { return LP_TwoQ_init(params, evict_params); }},
91+
{"qdlpv0", [](const common_cache_params_t& params, const char* evict_params) { return QDLPv0_init(params, evict_params); }},
92+
{"s3fifodv2", [](const common_cache_params_t& params, const char* evict_params) { return S3FIFOdv2_init(params, evict_params); }},
93+
{"mymqv1", [](const common_cache_params_t& params, const char* evict_params) { return myMQv1_init(params, evict_params); }},
18894
#endif
189-
} else {
190-
ERROR("do not support algorithm %s\n", eviction_algo);
191-
abort();
95+
};
96+
97+
std::string algo_lower = eviction_algo;
98+
std::transform(algo_lower.begin(), algo_lower.end(), algo_lower.begin(), ::tolower);
99+
100+
auto it = cache_init_map.find(algo_lower);
101+
if (it != cache_init_map.end()) {
102+
cache_t* cache = nullptr;
103+
104+
if (algo_lower == "hyperbolic") {
105+
cc_params.hashpower = MAX(cc_params.hashpower - 8, 16);
106+
}
107+
cache = it->second(cc_params, eviction_params.empty() ? nullptr : eviction_params.c_str());
108+
109+
return cache;
192110
}
193111

194-
return cache;
112+
ERROR("do not support algorithm %s\n", eviction_algo.c_str());
113+
abort();
195114
}

libCacheSim-python/src/pylibcachesim.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ PYBIND11_MODULE(_libcachesim, m) {
174174
[](const std::string& eviction_algo, const uint64_t cache_size,
175175
const std::string& eviction_params, bool consider_obj_metadata) {
176176
cache_t* ptr =
177-
create_sim_cache(eviction_algo.c_str(), cache_size,
178-
eviction_params.c_str(), consider_obj_metadata);
177+
create_sim_cache(eviction_algo, cache_size,
178+
eviction_params, consider_obj_metadata);
179179
return std::unique_ptr<cache_t, CacheDeleter>(ptr);
180180
},
181181
py::arg("eviction_algo"), py::arg("cache_size"),

libCacheSim-python/tests/test_eviction.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def test_eviction_algo_generic(eviction_algo, cache_size_ratio, mock_reader):
6868
req_count += 1
6969

7070
miss_ratio = miss_count / req_count
71+
print("Check eviction algo: ", eviction_algo, "with cache size ratio: ", cache_size_ratio)
7172
reference_miss_ratio = get_reference_data(eviction_algo, cache_size_ratio)
7273
if reference_miss_ratio is None:
7374
pytest.skip(f"No reference data for {eviction_algo} with cache size ratio {cache_size_ratio}")

0 commit comments

Comments
 (0)