Skip to content

Commit 1157e64

Browse files
authored
Refactor cache_init.h for better extensibility (#196)
* Refactor cache_init.h for better extensibility * Reformated cache_init.h using clang format * Added 3LCache, LRB, GLCache, and PRIV into jump table * Reformat with clang format
1 parent f320500 commit 1157e64

1 file changed

Lines changed: 98 additions & 120 deletions

File tree

libCacheSim/bin/cachesim/cache_init.h

Lines changed: 98 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,108 @@
1212
extern "C" {
1313
#endif
1414

15-
static inline cache_t *create_cache(const char *trace_path, const char *eviction_algo, const uint64_t cache_size,
16-
const char *eviction_params, const bool consider_obj_metadata) {
15+
static inline cache_t *create_cache(const char *trace_path,
16+
const char *eviction_algo,
17+
const uint64_t cache_size,
18+
const char *eviction_params,
19+
const bool consider_obj_metadata) {
1720
common_cache_params_t cc_params = {
18-
.cache_size = cache_size,
19-
.default_ttl = 86400 * 300,
20-
.hashpower = 24,
21-
.consider_obj_metadata = consider_obj_metadata,
22-
};
21+
.cache_size = cache_size,
22+
.default_ttl = 86400 * 300,
23+
.hashpower = 24,
24+
.consider_obj_metadata = consider_obj_metadata,
25+
};
2326
cache_t *cache;
2427

2528
/* the trace provided is small */
26-
if (trace_path != NULL && strstr(trace_path, "data/trace.") != NULL) cc_params.hashpower -= 8;
29+
if (trace_path != NULL && strstr(trace_path, "data/trace.") != NULL)
30+
cc_params.hashpower -= 8;
31+
typedef struct {
32+
const char *name;
33+
cache_t *(*init_func)(common_cache_params_t, const char *);
34+
} eviction_algo_entry_t;
2735

28-
if (strcasecmp(eviction_algo, "lru") == 0) {
29-
cache = LRU_init(cc_params, eviction_params);
30-
} else if (strcasecmp(eviction_algo, "fifo") == 0) {
31-
cache = FIFO_init(cc_params, eviction_params);
32-
} else if (strcasecmp(eviction_algo, "arc") == 0) {
33-
cache = ARC_init(cc_params, eviction_params);
34-
} else if (strcasecmp(eviction_algo, "arcv0") == 0) {
35-
cache = ARCv0_init(cc_params, eviction_params);
36-
} else if (strcasecmp(eviction_algo, "lhd") == 0) {
37-
cache = LHD_init(cc_params, eviction_params);
38-
} else if (strcasecmp(eviction_algo, "random") == 0) {
39-
cache = Random_init(cc_params, eviction_params);
40-
} else if (strcasecmp(eviction_algo, "randomTwo") == 0) {
41-
cache = RandomTwo_init(cc_params, eviction_params);
42-
} else if (strcasecmp(eviction_algo, "lfu") == 0) {
43-
cache = LFU_init(cc_params, eviction_params);
44-
} else if (strcasecmp(eviction_algo, "gdsf") == 0) {
45-
cache = GDSF_init(cc_params, eviction_params);
46-
} else if (strcasecmp(eviction_algo, "lfuda") == 0) {
47-
cache = LFUDA_init(cc_params, eviction_params);
48-
} else if (strcasecmp(eviction_algo, "twoq") == 0 || strcasecmp(eviction_algo, "2q") == 0) {
49-
cache = TwoQ_init(cc_params, eviction_params);
50-
} else if (strcasecmp(eviction_algo, "slru") == 0) {
51-
cache = SLRU_init(cc_params, eviction_params);
52-
} else if (strcasecmp(eviction_algo, "slruv0") == 0) {
53-
cache = SLRUv0_init(cc_params, eviction_params);
36+
static const eviction_algo_entry_t simple_algos[] = {
37+
{"lru", LRU_init},
38+
{"fifo", FIFO_init},
39+
{"arc", ARC_init},
40+
{"arcv0", ARCv0_init},
41+
{"lhd", LHD_init},
42+
{"random", Random_init},
43+
{"randomTwo", RandomTwo_init},
44+
{"lfu", LFU_init},
45+
{"gdsf", GDSF_init},
46+
{"lfuda", LFUDA_init},
47+
{"twoq", TwoQ_init},
48+
{"2q", TwoQ_init},
49+
{"slru", SLRU_init},
50+
{"slruv0", SLRUv0_init},
51+
{"lecar", LeCaR_init},
52+
{"lecarv0", LeCaRv0_init},
53+
{"RandomLRU", RandomLRU_init},
54+
{"cacheus", Cacheus_init},
55+
{"size", Size_init},
56+
{"lfucpp", LFUCpp_init},
57+
{"wtinyLFU", WTinyLFU_init},
58+
{"nop", nop_init},
59+
{"fifo-reinsertion", Clock_init},
60+
{"clock", Clock_init},
61+
{"second-chance", Clock_init},
62+
{"clockpro", ClockPro_init},
63+
{"lirs", LIRS_init},
64+
{"fifomerge", FIFO_Merge_init},
65+
{"fifo-merge", FIFO_Merge_init},
66+
{"flashProb", flashProb_init},
67+
{"sfifo", SFIFO_init},
68+
{"sfifov0", SFIFOv0_init},
69+
{"lru-prob", LRU_Prob_init},
70+
{"fifo-belady", FIFO_Belady_init},
71+
{"lru-belady", LRU_Belady_init},
72+
{"sieve-belady", Sieve_Belady_init},
73+
{"s3lru", S3LRU_init},
74+
{"s3fifo", S3FIFO_init},
75+
{"s3-fifo", S3FIFO_init},
76+
{"s3fifov0", S3FIFOv0_init},
77+
{"s3-fifov0", S3FIFOv0_init},
78+
{"s3fifod", S3FIFOd_init},
79+
{"qdlp", QDLP_init},
80+
{"CAR", CAR_init},
81+
#ifdef ENABLE_3L_CACHE
82+
{"3LCache", ThreeLCache_init},
83+
#endif
84+
#ifdef ENABLE_GLCACHE
85+
{"GLCache", GLCache_init},
86+
{"gl-cache", GLCache_init},
87+
#endif
88+
#ifdef ENABLE_LRB
89+
{"lrb", LRB_init},
90+
#endif
91+
#ifdef INCLUDE_PRIV
92+
{"mclock", MClock_init},
93+
{"lp-sfifo", LP_SFIFO_init},
94+
{"lp-arc", LP_ARC_init},
95+
{"lp-twoq", LP_TwoQ_init},
96+
{"qdlpv0", QDLPv0_init},
97+
{"s3fifodv2", S3FIFOdv2_init},
98+
{"myMQv1", myMQv1_init}
99+
#endif
100+
};
101+
102+
cache_t *(*init_func)(common_cache_params_t, const char *) = NULL;
103+
for (size_t i = 0; i < sizeof(simple_algos) / sizeof(simple_algos[0]); ++i) {
104+
if (strcasecmp(eviction_algo, simple_algos[i].name) == 0) {
105+
init_func = simple_algos[i].init_func;
106+
break;
107+
}
108+
}
109+
110+
// Initializing for algorithms which require special handling (not in
111+
// simple_algos)
112+
if (init_func) {
113+
cache = init_func(cc_params, eviction_params);
54114
} else if (strcasecmp(eviction_algo, "hyperbolic") == 0) {
55115
cc_params.hashpower = MAX(cc_params.hashpower - 8, 16);
56116
cache = Hyperbolic_init(cc_params, eviction_params);
57-
} else if (strcasecmp(eviction_algo, "lecar") == 0) {
58-
cache = LeCaR_init(cc_params, eviction_params);
59-
} else if (strcasecmp(eviction_algo, "lecarv0") == 0) {
60-
cache = LeCaRv0_init(cc_params, eviction_params);
61-
} else if (strcasecmp(eviction_algo, "RandomLRU") == 0) {
62-
cache = RandomLRU_init(cc_params, eviction_params);
63-
} else if (strcasecmp(eviction_algo, "cacheus") == 0) {
64-
cache = Cacheus_init(cc_params, eviction_params);
65-
} else if (strcasecmp(eviction_algo, "size") == 0) {
66-
cache = Size_init(cc_params, eviction_params);
67-
} else if (strcasecmp(eviction_algo, "lfucpp") == 0) {
68-
cache = LFUCpp_init(cc_params, eviction_params);
69117
} else if (strcasecmp(eviction_algo, "tinyLFU") == 0) {
70118
if (eviction_params == NULL) {
71119
cache = WTinyLFU_init(cc_params, eviction_params);
@@ -79,9 +127,8 @@ static inline cache_t *create_cache(const char *trace_path, const char *eviction
79127
cache = WTinyLFU_init(cc_params, eviction_params);
80128
}
81129
}
82-
} else if (strcasecmp(eviction_algo, "wtinyLFU") == 0) {
83-
cache = WTinyLFU_init(cc_params, eviction_params);
84-
} else if (strcasecmp(eviction_algo, "belady") == 0 && strcasestr(trace_path, "lcs") == NULL) {
130+
} else if (strcasecmp(eviction_algo, "belady") == 0 &&
131+
strcasestr(trace_path, "lcs") == NULL) {
85132
if (strcasestr(trace_path, "oracleGeneral") == NULL) {
86133
WARN("belady is only supported for oracleGeneral and lcs trace\n");
87134
WARN("to convert a trace to lcs format\n");
@@ -90,10 +137,9 @@ static inline cache_t *create_cache(const char *trace_path, const char *eviction
90137
exit(1);
91138
}
92139
cache = Belady_init(cc_params, eviction_params);
93-
} else if (strcasecmp(eviction_algo, "nop") == 0) {
94-
cache = nop_init(cc_params, eviction_params);
95140
} else if (strcasecmp(eviction_algo, "beladySize") == 0) {
96-
if (strcasestr(trace_path, "oracleGeneral") == NULL && strcasestr(trace_path, "lcs") == NULL) {
141+
if (strcasestr(trace_path, "oracleGeneral") == NULL &&
142+
strcasestr(trace_path, "lcs") == NULL) {
97143
WARN("beladySize is only supported for oracleGeneral and lcs trace\n");
98144
WARN("to convert a trace to lcs format\n");
99145
WARN("./bin/traceConv input_trace trace_format output_trace\n");
@@ -102,74 +148,6 @@ static inline cache_t *create_cache(const char *trace_path, const char *eviction
102148
}
103149
cc_params.hashpower = MAX(cc_params.hashpower - 8, 16);
104150
cache = BeladySize_init(cc_params, eviction_params);
105-
} else if (strcasecmp(eviction_algo, "fifo-reinsertion") == 0 || strcasecmp(eviction_algo, "clock") == 0 ||
106-
strcasecmp(eviction_algo, "second-chance") == 0) {
107-
cache = Clock_init(cc_params, eviction_params);
108-
} else if (strcasecmp(eviction_algo, "clockpro") == 0) {
109-
cache = ClockPro_init(cc_params, eviction_params);
110-
} else if (strcasecmp(eviction_algo, "lirs") == 0) {
111-
cache = LIRS_init(cc_params, eviction_params);
112-
} else if (strcasecmp(eviction_algo, "fifomerge") == 0 || strcasecmp(eviction_algo, "fifo-merge") == 0) {
113-
cache = FIFO_Merge_init(cc_params, eviction_params);
114-
// } else if (strcasecmp(eviction_algo, "fifo-reinsertion") == 0) {
115-
// cache = FIFO_Reinsertion_init(cc_params, eviction_params);
116-
} else if (strcasecmp(eviction_algo, "flashProb") == 0) {
117-
// used to measure application level write amp
118-
cache = flashProb_init(cc_params, eviction_params);
119-
} else if (strcasecmp(eviction_algo, "sfifo") == 0) {
120-
cache = SFIFO_init(cc_params, eviction_params);
121-
} else if (strcasecmp(eviction_algo, "sfifov0") == 0) {
122-
cache = SFIFOv0_init(cc_params, eviction_params);
123-
} else if (strcasecmp(eviction_algo, "lru-prob") == 0) {
124-
cache = LRU_Prob_init(cc_params, eviction_params);
125-
} else if (strcasecmp(eviction_algo, "fifo-belady") == 0) {
126-
cache = FIFO_Belady_init(cc_params, eviction_params);
127-
} else if (strcasecmp(eviction_algo, "lru-belady") == 0) {
128-
cache = LRU_Belady_init(cc_params, eviction_params);
129-
} else if (strcasecmp(eviction_algo, "sieve-belady") == 0) {
130-
cache = Sieve_Belady_init(cc_params, eviction_params);
131-
} else if (strcasecmp(eviction_algo, "s3lru") == 0) {
132-
cache = S3LRU_init(cc_params, eviction_params);
133-
} else if (strcasecmp(eviction_algo, "s3fifo") == 0 || strcasecmp(eviction_algo, "s3-fifo") == 0) {
134-
cache = S3FIFO_init(cc_params, eviction_params);
135-
} else if (strcasecmp(eviction_algo, "s3fifov0") == 0 || strcasecmp(eviction_algo, "s3-fifov0") == 0) {
136-
cache = S3FIFOv0_init(cc_params, eviction_params);
137-
} else if (strcasecmp(eviction_algo, "s3fifod") == 0) {
138-
cache = S3FIFOd_init(cc_params, eviction_params);
139-
} else if (strcasecmp(eviction_algo, "qdlp") == 0) {
140-
cache = QDLP_init(cc_params, eviction_params);
141-
} else if(strcasecmp(eviction_algo, "CAR") == 0) {
142-
cache = CAR_init(cc_params, eviction_params);
143-
} else if (strcasecmp(eviction_algo, "sieve") == 0) {
144-
cache = Sieve_init(cc_params, eviction_params);
145-
#ifdef ENABLE_3L_CACHE
146-
} else if (strcasecmp(eviction_algo, "3LCache") == 0) {
147-
cache = ThreeLCache_init(cc_params, eviction_params);
148-
#endif
149-
#ifdef ENABLE_GLCACHE
150-
} else if (strcasecmp(eviction_algo, "GLCache") == 0 || strcasecmp(eviction_algo, "gl-cache") == 0) {
151-
cache = GLCache_init(cc_params, eviction_params);
152-
#endif
153-
#ifdef ENABLE_LRB
154-
} else if (strcasecmp(eviction_algo, "lrb") == 0) {
155-
cache = LRB_init(cc_params, eviction_params);
156-
#endif
157-
#ifdef INCLUDE_PRIV
158-
} else if (strcasecmp(eviction_algo, "mclock") == 0) {
159-
cache = MClock_init(cc_params, eviction_params);
160-
} else if (strcasecmp(eviction_algo, "lp-sfifo") == 0) {
161-
cache = LP_SFIFO_init(cc_params, eviction_params);
162-
} else if (strcasecmp(eviction_algo, "lp-arc") == 0) {
163-
cache = LP_ARC_init(cc_params, eviction_params);
164-
} else if (strcasecmp(eviction_algo, "lp-twoq") == 0) {
165-
cache = LP_TwoQ_init(cc_params, eviction_params);
166-
} else if (strcasecmp(eviction_algo, "qdlpv0") == 0) {
167-
cache = QDLPv0_init(cc_params, eviction_params);
168-
} else if (strcasecmp(eviction_algo, "s3fifodv2") == 0) {
169-
cache = S3FIFOdv2_init(cc_params, eviction_params);
170-
} else if (strcasecmp(eviction_algo, "myMQv1") == 0) {
171-
cache = myMQv1_init(cc_params, eviction_params);
172-
#endif
173151
} else {
174152
ERROR("do not support algorithm %s\n", eviction_algo);
175153
abort();

0 commit comments

Comments
 (0)