|
3 | 3 | #include <cstdio> |
4 | 4 | #include <cstdlib> |
5 | 5 | #include <cstring> |
| 6 | +#include <string> |
| 7 | +#include <unordered_map> |
| 8 | +#include <functional> |
| 9 | +#include <algorithm> |
6 | 10 |
|
7 | 11 | #include "libCacheSim/cache.h" |
8 | 12 | #include "libCacheSim/evictionAlgo.h" |
9 | 13 |
|
10 | 14 | // Note: we don't need to pass in the trace path here |
11 | 15 | // 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, |
13 | 17 | const uint64_t cache_size, |
14 | | - const char *eviction_params, |
| 18 | + const std::string& eviction_params, |
15 | 19 | const bool consider_obj_metadata) { |
16 | 20 | common_cache_params_t cc_params = { |
17 | 21 | .cache_size = cache_size, |
18 | 22 | .default_ttl = 86400 * 300, |
19 | 23 | .hashpower = 24, |
20 | 24 | .consider_obj_metadata = consider_obj_metadata, |
21 | 25 | }; |
22 | | - cache_t *cache; |
23 | 26 |
|
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); }}, |
160 | 76 | #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); }}, |
163 | 78 | #endif |
164 | 79 | #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); }}, |
168 | 82 | #endif |
169 | 83 | #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); }}, |
172 | 85 | #endif |
173 | 86 | #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); }}, |
188 | 94 | #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; |
192 | 110 | } |
193 | 111 |
|
194 | | - return cache; |
| 112 | + ERROR("do not support algorithm %s\n", eviction_algo.c_str()); |
| 113 | + abort(); |
195 | 114 | } |
0 commit comments