Skip to content

Commit deb3134

Browse files
authored
fix some compiler issues (#261)
* Refactor: Rename variables to fix compiler error about "main" should not be used as a variable name - Updated variable names in QDLP, S3FIFO, S3FIFOd, S3FIFOv0, WTinyLFU, and S3LRU eviction algorithms to improve code readability. - Changed 'fifo' to 'small_fifo' and 'main' to 'main_cache' for consistency across the codebase. - Ensured that all references to cache objects are clear and descriptive, enhancing maintainability. * fix the issue where compiler cannot find glib when instealled at user directory * address comments * fix format
1 parent 3b29ba6 commit deb3134

File tree

7 files changed

+287
-276
lines changed

7 files changed

+287
-276
lines changed

CMakeLists.txt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ endif()
8888

8989

9090
# Define shared compiler flags for all targets
91-
set(LIBCACHESIM_C_FLAGS
92-
-Wall -Wextra -Werror
93-
-Wno-unused-variable -Wno-unused-function -Wno-unused-parameter -Wno-unused-but-set-variable
94-
-Wpedantic -Wformat=2 -Wformat-security -Wshadow -Wwrite-strings
91+
set(LIBCACHESIM_C_FLAGS
92+
-Wall -Wextra -Werror
93+
-Wno-unused-variable -Wno-unused-function -Wno-unused-parameter -Wno-unused-but-set-variable
94+
-Wpedantic -Wformat=2 -Wformat-security -Wshadow -Wwrite-strings
9595
-Wstrict-prototypes -Wold-style-definition -Wredundant-decls -Wnested-externs -Wmissing-include-dirs
9696
)
9797

98-
set(LIBCACHESIM_CXX_FLAGS
99-
-Wall -Wextra -Werror
100-
-Wno-deprecated-copy -Wno-unused-variable -Wno-unused-function -Wno-unused-parameter -Wno-unused-but-set-variable
98+
set(LIBCACHESIM_CXX_FLAGS
99+
-Wall -Wextra -Werror
100+
-Wno-deprecated-copy -Wno-unused-variable -Wno-unused-function -Wno-unused-parameter -Wno-unused-but-set-variable
101101
-Wno-pedantic -Wformat=2 -Wformat-security -Wshadow -Wwrite-strings -Wmissing-include-dirs
102102
)
103103

@@ -119,10 +119,18 @@ list(APPEND dependency_libs ${CMAKE_THREAD_LIBS_INIT})
119119
# Link standard math and dl libraries universally
120120
list(APPEND dependency_libs m dl)
121121

122-
find_package(GLib REQUIRED)
122+
# Find GLib using pkg-config
123+
find_package(PkgConfig REQUIRED)
124+
125+
# Find glib-2.0 using pkg-config
126+
pkg_check_modules(GLib REQUIRED glib-2.0)
127+
128+
# Add GLib library directories to the linker search path
129+
link_directories(${GLib_LIBRARY_DIRS})
130+
123131
# Don't add GLib includes globally - add them to specific targets
124132
# include_directories(${GLib_INCLUDE_DIRS})
125-
list(APPEND dependency_libs ${GLib_LIBRARY})
133+
list(APPEND dependency_libs ${GLib_LIBRARIES})
126134

127135
find_package(argp REQUIRED)
128136
# Don't add argp includes globally
@@ -336,4 +344,4 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/Find${PROJECT_NAME}.cmake
336344
install(EXPORT ${PROJECT_NAME}Targets
337345
FILE ${PROJECT_NAME}Targets.cmake
338346
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
339-
)
347+
)

libCacheSim/cache/eviction/QDLP.c

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ extern "C" {
2020
#endif
2121

2222
typedef struct {
23-
cache_t *fifo;
24-
cache_t *fifo_ghost;
23+
cache_t *small_cache;
24+
cache_t *ghost_cache;
2525
cache_t *main_cache;
2626
bool hit_on_ghost;
2727

28-
int64_t n_obj_admit_to_fifo;
28+
int64_t n_obj_admit_to_small;
2929
int64_t n_obj_admit_to_main;
3030
int64_t n_obj_move_to_main;
31-
int64_t n_byte_admit_to_fifo;
31+
int64_t n_byte_admit_to_small;
3232
int64_t n_byte_admit_to_main;
3333
int64_t n_byte_move_to_main;
3434

3535
int move_to_main_threshold;
36-
double fifo_size_ratio;
36+
double small_size_ratio;
3737
double ghost_size_ratio;
3838
char main_cache_type[32];
3939

@@ -100,22 +100,22 @@ cache_t *QDLP_init(const common_cache_params_t ccache_params,
100100
}
101101

102102
int64_t fifo_cache_size =
103-
(int64_t)ccache_params.cache_size * params->fifo_size_ratio;
103+
(int64_t)ccache_params.cache_size * params->small_size_ratio;
104104
int64_t main_cache_size = ccache_params.cache_size - fifo_cache_size;
105-
int64_t fifo_ghost_cache_size =
105+
int64_t ghost_cache_size =
106106
(int64_t)(ccache_params.cache_size * params->ghost_size_ratio);
107107

108108
common_cache_params_t ccache_params_local = ccache_params;
109109
ccache_params_local.cache_size = fifo_cache_size;
110-
params->fifo = FIFO_init(ccache_params_local, NULL);
110+
params->small_cache = FIFO_init(ccache_params_local, NULL);
111111

112-
if (fifo_ghost_cache_size > 0) {
113-
ccache_params_local.cache_size = fifo_ghost_cache_size;
114-
params->fifo_ghost = FIFO_init(ccache_params_local, NULL);
115-
snprintf(params->fifo_ghost->cache_name, CACHE_NAME_ARRAY_LEN,
112+
if (ghost_cache_size > 0) {
113+
ccache_params_local.cache_size = ghost_cache_size;
114+
params->ghost_cache = FIFO_init(ccache_params_local, NULL);
115+
snprintf(params->ghost_cache->cache_name, CACHE_NAME_ARRAY_LEN,
116116
"FIFO-ghost");
117117
} else {
118-
params->fifo_ghost = NULL;
118+
params->ghost_cache = NULL;
119119
}
120120

121121
ccache_params_local.cache_size = main_cache_size;
@@ -152,15 +152,15 @@ cache_t *QDLP_init(const common_cache_params_t ccache_params,
152152
}
153153

154154
#if defined(TRACK_EVICTION_V_AGE)
155-
if (params->fifo_ghost != NULL) {
156-
params->fifo_ghost->track_eviction_age = false;
155+
if (params->ghost_cache != NULL) {
156+
params->ghost->track_eviction_age = false;
157157
}
158-
params->fifo->track_eviction_age = false;
158+
params->small_cache->track_eviction_age = false;
159159
params->main_cache->track_eviction_age = false;
160160
#endif
161161

162162
snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "QDLP-%.4lf-%.4lf-%s-%d",
163-
params->fifo_size_ratio, params->ghost_size_ratio,
163+
params->small_size_ratio, params->ghost_size_ratio,
164164
params->main_cache_type, params->move_to_main_threshold);
165165

166166
return cache;
@@ -174,9 +174,9 @@ cache_t *QDLP_init(const common_cache_params_t ccache_params,
174174
static void QDLP_free(cache_t *cache) {
175175
QDLP_params_t *params = (QDLP_params_t *)cache->eviction_params;
176176
free_request(params->req_local);
177-
params->fifo->cache_free(params->fifo);
178-
if (params->fifo_ghost != NULL) {
179-
params->fifo_ghost->cache_free(params->fifo_ghost);
177+
params->small_cache->cache_free(params->small_cache);
178+
if (params->ghost_cache != NULL) {
179+
params->ghost_cache->cache_free(params->ghost_cache);
180180
}
181181
params->main_cache->cache_free(params->main_cache);
182182
free(cache->eviction_params);
@@ -204,7 +204,7 @@ static void QDLP_free(cache_t *cache) {
204204
*/
205205
static bool QDLP_get(cache_t *cache, const request_t *req) {
206206
QDLP_params_t *params = (QDLP_params_t *)cache->eviction_params;
207-
DEBUG_ASSERT(params->fifo->get_occupied_byte(params->fifo) +
207+
DEBUG_ASSERT(params->small_cache->get_occupied_byte(params->small_cache) +
208208
params->main_cache->get_occupied_byte(params->main_cache) <=
209209
cache->cache_size);
210210

@@ -234,7 +234,8 @@ static cache_obj_t *QDLP_find(cache_t *cache, const request_t *req,
234234

235235
// if update cache is false, we only check the fifo and main caches
236236
if (!update_cache) {
237-
cache_obj_t *obj = params->fifo->find(params->fifo, req, false);
237+
cache_obj_t *obj =
238+
params->small_cache->find(params->small_cache, req, false);
238239
if (obj != NULL) {
239240
return obj;
240241
}
@@ -247,14 +248,14 @@ static cache_obj_t *QDLP_find(cache_t *cache, const request_t *req,
247248

248249
/* update cache is true from now */
249250
params->hit_on_ghost = false;
250-
cache_obj_t *obj = params->fifo->find(params->fifo, req, true);
251+
cache_obj_t *obj = params->small_cache->find(params->small_cache, req, true);
251252
if (obj != NULL) {
252253
return obj;
253254
}
254255

255-
if (params->fifo_ghost != NULL &&
256-
params->fifo_ghost->remove(params->fifo_ghost, req->obj_id)) {
257-
// if object in fifo_ghost, remove will return true
256+
if (params->ghost_cache != NULL &&
257+
params->ghost_cache->remove(params->ghost_cache, req->obj_id)) {
258+
// if object in ghost, remove will return true
258259
params->hit_on_ghost = true;
259260
}
260261

@@ -287,12 +288,12 @@ static cache_obj_t *QDLP_insert(cache_t *cache, const request_t *req) {
287288
obj = params->main_cache->find(params->main_cache, req, false);
288289
} else {
289290
/* insert into the fifo */
290-
if (req->obj_size >= params->fifo->cache_size) {
291+
if (req->obj_size >= params->small_cache->cache_size) {
291292
return NULL;
292293
}
293-
params->n_obj_admit_to_fifo += 1;
294-
params->n_byte_admit_to_fifo += req->obj_size;
295-
obj = params->fifo->insert(params->fifo, req);
294+
params->n_obj_admit_to_small += 1;
295+
params->n_byte_admit_to_small += req->obj_size;
296+
obj = params->small_cache->insert(params->small_cache, req);
296297
}
297298

298299
#if defined(TRACK_EVICTION_V_AGE)
@@ -331,25 +332,25 @@ static cache_obj_t *QDLP_to_evict(cache_t *cache, const request_t *req) {
331332
static void QDLP_evict(cache_t *cache, const request_t *req) {
332333
QDLP_params_t *params = (QDLP_params_t *)cache->eviction_params;
333334

334-
cache_t *fifo = params->fifo;
335-
cache_t *ghost = params->fifo_ghost;
336-
cache_t *main = params->main_cache;
335+
cache_t *small_cache = params->small_cache;
336+
cache_t *ghost_cache = params->ghost_cache;
337+
cache_t *main_cache = params->main_cache;
337338

338-
if (fifo->get_occupied_byte(fifo) == 0) {
339+
if (small_cache->get_occupied_byte(small_cache) == 0) {
339340
#if defined(TRACK_EVICTION_V_AGE)
340-
cache_obj_t *obj = main->to_evict(main, req);
341+
cache_obj_t *obj = main_cache->to_evict(main_cache, req);
341342
record_eviction_age(cache, obj, CURR_TIME(cache, req) - obj->create_time);
342343
#endif
343344

344-
assert(main->get_occupied_byte(main) <= cache->cache_size);
345+
assert(main_cache->get_occupied_byte(main_cache) <= cache->cache_size);
345346
// evict from main cache
346-
main->evict(main, req);
347+
main_cache->evict(main_cache, req);
347348

348349
return;
349350
}
350351

351352
// evict from FIFO
352-
cache_obj_t *obj = fifo->to_evict(fifo, req);
353+
cache_obj_t *obj = small_cache->to_evict(small_cache, req);
353354
assert(obj != NULL);
354355
// need to copy the object before it is evicted
355356
copy_cache_obj_to_request(params->req_local, obj);
@@ -361,21 +362,22 @@ static void QDLP_evict(cache_t *cache, const request_t *req) {
361362

362363
params->main_cache->get(params->main_cache, params->req_local);
363364
#if defined(TRACK_EVICTION_V_AGE)
364-
main->find(main, params->req_local, false)->create_time = obj->create_time;
365+
main_cache->find(main_cache, params->req_local, false)->create_time =
366+
obj->create_time;
365367
} else {
366368
record_eviction_age(cache, obj, CURR_TIME(cache, req) - obj->create_time);
367369
#else
368370
} else {
369371
#endif
370372
// insert to ghost
371-
if (ghost != NULL) {
372-
ghost->get(ghost, params->req_local);
373+
if (ghost_cache != NULL) {
374+
ghost_cache->get(ghost_cache, params->req_local);
373375
}
374376
}
375377

376378
// remove from fifo, but do not update stat
377379
// bool removed = fifo->remove(fifo, params->req_local->obj_id);
378-
fifo->evict(fifo, req);
380+
small_cache->evict(small_cache, req);
379381
}
380382

381383
/**
@@ -394,30 +396,31 @@ static void QDLP_evict(cache_t *cache, const request_t *req) {
394396
static bool QDLP_remove(cache_t *cache, const obj_id_t obj_id) {
395397
QDLP_params_t *params = (QDLP_params_t *)cache->eviction_params;
396398
bool removed = false;
397-
removed = removed || params->fifo->remove(params->fifo, obj_id);
398-
removed = removed || (params->fifo_ghost &&
399-
params->fifo_ghost->remove(params->fifo_ghost, obj_id));
399+
removed = removed || params->small_cache->remove(params->small_cache, obj_id);
400+
removed =
401+
removed || (params->ghost_cache &&
402+
params->ghost_cache->remove(params->ghost_cache, obj_id));
400403
removed = removed || params->main_cache->remove(params->main_cache, obj_id);
401404

402405
return removed;
403406
}
404407

405408
static inline int64_t QDLP_get_occupied_byte(const cache_t *cache) {
406409
QDLP_params_t *params = (QDLP_params_t *)cache->eviction_params;
407-
return params->fifo->get_occupied_byte(params->fifo) +
410+
return params->small_cache->get_occupied_byte(params->small_cache) +
408411
params->main_cache->get_occupied_byte(params->main_cache);
409412
}
410413

411414
static inline int64_t QDLP_get_n_obj(const cache_t *cache) {
412415
QDLP_params_t *params = (QDLP_params_t *)cache->eviction_params;
413-
return params->fifo->get_n_obj(params->fifo) +
416+
return params->small_cache->get_n_obj(params->small_cache) +
414417
params->main_cache->get_n_obj(params->main_cache);
415418
}
416419

417420
static inline bool QDLP_can_insert(cache_t *cache, const request_t *req) {
418421
QDLP_params_t *params = (QDLP_params_t *)cache->eviction_params;
419422

420-
return req->obj_size <= params->fifo->cache_size &&
423+
return req->obj_size <= params->small_cache->cache_size &&
421424
cache_can_insert_default(cache, req);
422425
}
423426

@@ -429,7 +432,7 @@ static inline bool QDLP_can_insert(cache_t *cache, const request_t *req) {
429432
static const char *QDLP_current_params(QDLP_params_t *params) {
430433
static __thread char params_str[128];
431434
snprintf(params_str, 128, "fifo-size-ratio=%.4lf,main-cache=%s\n",
432-
params->fifo_size_ratio, params->main_cache->cache_name);
435+
params->small_size_ratio, params->main_cache->cache_name);
433436
return params_str;
434437
}
435438

@@ -452,7 +455,7 @@ static void QDLP_parse_params(cache_t *cache,
452455
}
453456

454457
if (strcasecmp(key, "fifo-size-ratio") == 0) {
455-
params->fifo_size_ratio = strtod(value, NULL);
458+
params->small_size_ratio = strtod(value, NULL);
456459
} else if (strcasecmp(key, "ghost-size-ratio") == 0) {
457460
params->ghost_size_ratio = strtod(value, NULL);
458461
} else if (strcasecmp(key, "move-to-main-threshold") == 0) {

0 commit comments

Comments
 (0)