Skip to content

Commit 2788e11

Browse files
committed
Fix mem leak excepting Quark
1 parent 5f55fa7 commit 2788e11

13 files changed

Lines changed: 76 additions & 42 deletions

File tree

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on: [push, pull_request]
44

55
env:
66
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
7-
BUILD_TYPE: Release
7+
BUILD_TYPE: Debug
88

99
jobs:
1010
# macos:
@@ -45,7 +45,7 @@ jobs:
4545
run: |
4646
# Set ASan/LSan options for runtime detection
4747
export ASAN_OPTIONS="detect_leaks=1:halt_on_error=1:verbosity=1"
48-
ctest -C ${{env.BUILD_TYPE}}
48+
ctest -C ${{env.BUILD_TYPE}} --output-on-failure
4949
5050
selfhosted:
5151
runs-on: self-hosted

libCacheSim/cache/eviction/LFUDA.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ cache_t *LFUDA_init(const common_cache_params_t ccache_params,
102102
static void LFUDA_free(cache_t *cache) {
103103
LFUDA_params_t *params = (LFUDA_params_t *)(cache->eviction_params);
104104
g_hash_table_destroy(params->freq_map);
105+
free(params);
105106
cache_struct_free(cache);
106107
}
107108

libCacheSim/cache/eviction/LIRS.c

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ typedef struct LIRS_params {
2727
uint64_t nonresident;
2828
} LIRS_params_t;
2929

30+
/**
31+
* @brief local request for thread local storage
32+
*/
33+
static __thread request_t *req_local_hit_RD_HIRinQ = NULL;
34+
static __thread request_t *req_local_evictLIR = NULL;
35+
static __thread request_t *req_local_evictHIR = NULL;
36+
3037
// ***********************************************************************
3138
// **** ****
3239
// **** function declarations ****
@@ -125,6 +132,20 @@ static void LIRS_free(cache_t *cache) {
125132
params->LRU_s->cache_free(params->LRU_s);
126133
params->LRU_q->cache_free(params->LRU_q);
127134
params->LRU_nh->cache_free(params->LRU_nh);
135+
136+
if (req_local_hit_RD_HIRinQ != NULL) {
137+
free_request(req_local_hit_RD_HIRinQ);
138+
req_local_hit_RD_HIRinQ = NULL;
139+
}
140+
if (req_local_evictLIR != NULL) {
141+
free_request(req_local_evictLIR);
142+
req_local_evictLIR = NULL;
143+
}
144+
if (req_local_evictHIR != NULL) {
145+
free_request(req_local_evictHIR);
146+
req_local_evictHIR = NULL;
147+
}
148+
128149
my_free(sizeof(LIRS_params_t), params);
129150
cache_struct_free(cache);
130151
}
@@ -553,9 +574,10 @@ static cache_obj_t *hit_RD_HIRinS(cache_t *cache, cache_obj_t *cache_obj_s,
553574
LIRS_params_t *params = (LIRS_params_t *)(cache->eviction_params);
554575

555576
if (cache_obj_q != NULL) {
556-
params->hirs_count -= cache_obj_q->obj_size;
577+
int64_t obj_size = cache_obj_q->obj_size;
578+
params->hirs_count -= obj_size;
557579
params->LRU_q->remove(params->LRU_q, cache_obj_q->obj_id);
558-
cache->occupied_byte -= cache_obj_q->obj_size;
580+
cache->occupied_byte -= obj_size;
559581
cache->n_obj--;
560582
}
561583

@@ -583,19 +605,17 @@ static cache_obj_t *hit_NR_HIRinS(cache_t *cache, cache_obj_t *cache_obj_s) {
583605

584606
static cache_obj_t *hit_RD_HIRinQ(cache_t *cache, cache_obj_t *cache_obj_q) {
585607
LIRS_params_t *params = (LIRS_params_t *)(cache->eviction_params);
586-
587-
static __thread request_t *req_local = NULL;
588-
if (req_local == NULL) {
589-
req_local = new_request();
608+
if (req_local_hit_RD_HIRinQ == NULL) {
609+
req_local_hit_RD_HIRinQ = new_request();
590610
}
591-
copy_cache_obj_to_request(req_local, cache_obj_q);
611+
copy_cache_obj_to_request(req_local_hit_RD_HIRinQ, cache_obj_q);
592612

593613
while (params->lirs_count + cache_obj_q->obj_size > params->lirs_limit) {
594614
evictLIR(cache);
595615
}
596-
params->LRU_s->insert(params->LRU_s, req_local);
616+
params->LRU_s->insert(params->LRU_s, req_local_hit_RD_HIRinQ);
597617
cache_obj_t *obj_to_update =
598-
params->LRU_s->find(params->LRU_s, req_local, false);
618+
params->LRU_s->find(params->LRU_s, req_local_hit_RD_HIRinQ, false);
599619
obj_to_update->LIRS.is_LIR = false;
600620
obj_to_update->LIRS.in_cache = true;
601621

@@ -606,30 +626,29 @@ static void evictLIR(cache_t *cache) {
606626
LIRS_params_t *params = (LIRS_params_t *)(cache->eviction_params);
607627
cache_obj_t *obj_to_evict = params->LRU_s->to_evict(params->LRU_s, NULL);
608628

609-
static __thread request_t *req_local = NULL;
610-
if (req_local == NULL) {
611-
req_local = new_request();
629+
if (req_local_evictLIR == NULL) {
630+
req_local_evictLIR = new_request();
612631
}
613-
copy_cache_obj_to_request(req_local, obj_to_evict);
632+
copy_cache_obj_to_request(req_local_evictLIR, obj_to_evict);
614633
params->lirs_count -= obj_to_evict->obj_size;
615634
params->LRU_s->evict(params->LRU_s, NULL);
616635

617-
cache->occupied_byte -= (req_local->obj_size + cache->obj_md_size);
636+
cache->occupied_byte -= (req_local_evictLIR->obj_size + cache->obj_md_size);
618637
cache->n_obj -= 1;
619638

620-
if ((uint64_t)req_local->obj_size <= params->hirs_limit) {
621-
while ((uint64_t)params->hirs_count + req_local->obj_size > params->hirs_limit) {
639+
if ((uint64_t)req_local_evictLIR->obj_size <= params->hirs_limit) {
640+
while ((uint64_t)params->hirs_count + req_local_evictLIR->obj_size > params->hirs_limit) {
622641
evictHIR(cache);
623642
}
624-
params->LRU_q->insert(params->LRU_q, req_local);
643+
params->LRU_q->insert(params->LRU_q, req_local_evictLIR);
625644

626645
cache_obj_t *obj_to_update =
627-
params->LRU_q->find(params->LRU_q, req_local, false);
646+
params->LRU_q->find(params->LRU_q, req_local_evictLIR, false);
628647
obj_to_update->LIRS.is_LIR = false;
629648
obj_to_update->LIRS.in_cache = true;
630649

631-
params->hirs_count += req_local->obj_size;
632-
cache->occupied_byte += (req_local->obj_size + cache->obj_md_size);
650+
params->hirs_count += req_local_evictLIR->obj_size;
651+
cache->occupied_byte += (req_local_evictLIR->obj_size + cache->obj_md_size);
633652
cache->n_obj += 1;
634653
}
635654

@@ -642,24 +661,23 @@ static bool evictHIR(cache_t *cache) {
642661
cache_obj_t *obj_to_evict = params->LRU_q->to_evict(params->LRU_q, NULL);
643662

644663
// update the corresponding block in S to be non-resident
645-
static __thread request_t *req_local = NULL;
646-
if (req_local == NULL) {
647-
req_local = new_request();
664+
if (req_local_evictHIR == NULL) {
665+
req_local_evictHIR = new_request();
648666
}
649-
copy_cache_obj_to_request(req_local, obj_to_evict);
667+
copy_cache_obj_to_request(req_local_evictHIR, obj_to_evict);
650668

651669
params->hirs_count -= obj_to_evict->obj_size;
652670
params->LRU_q->evict(params->LRU_q, NULL);
653671

654672
cache_obj_t *obj_to_update =
655-
params->LRU_s->find(params->LRU_s, req_local, false);
673+
params->LRU_s->find(params->LRU_s, req_local_evictHIR, false);
656674
if (obj_to_update != NULL) {
657675
obj_to_update->LIRS.in_cache = false;
658-
params->LRU_nh->insert(params->LRU_nh, req_local);
676+
params->LRU_nh->insert(params->LRU_nh, req_local_evictHIR);
659677
params->nonresident += obj_to_update->obj_size;
660678
}
661679

662-
cache->occupied_byte -= (req_local->obj_size + cache->obj_md_size);
680+
cache->occupied_byte -= (req_local_evictHIR->obj_size + cache->obj_md_size);
663681
cache->n_obj -= 1;
664682

665683
return true;

libCacheSim/cache/eviction/LRU.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ cache_t *LRU_init(const common_cache_params_t ccache_params,
8686
*
8787
* @param cache
8888
*/
89-
static void LRU_free(cache_t *cache) { cache_struct_free(cache); }
89+
static void LRU_free(cache_t *cache) {
90+
LRU_params_t *params = (LRU_params_t *)cache->eviction_params;
91+
free(params);
92+
cache_struct_free(cache);
93+
}
9094

9195
/**
9296
* @brief this function is the user facing API

libCacheSim/cache/eviction/SLRU.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ static void SLRU_free(cache_t *cache) {
202202
free(params->lru_tails);
203203
free(params->lru_n_objs);
204204
free(params->lru_n_bytes);
205+
free(params);
205206
cache_struct_free(cache);
206207
}
207208

libCacheSim/mrcProfiler/mrcProfiler.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,7 @@ void mrcProfiler::MRCProfilerMINISIM::run() {
324324
hit_size_vec[i] = sum_obj_size_req - result[i].n_miss_byte;
325325
}
326326
}
327+
// clean up
328+
my_free(sizeof(cache_stat_t) * mrc_size_vec.size(), result);
329+
free_request(req);
327330
}

libCacheSim/traceReader/reader.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ int64_t get_num_of_req(reader_t *const reader) {
512512
while (read_one_req(reader_copy, req) == 0) {
513513
n_req++;
514514
}
515+
free_request(req);
516+
close_reader(reader_copy);
515517
} else {
516518
ERROR("should not reach here\n");
517519
abort();

test/test_admissionAlgo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int main(int argc, char *argv[]) {
120120
g_test_add_data_func("/libCacheSim/admissionAlgo_AdaptSize", reader, test_AdaptSize);
121121
g_test_add_data_func("/libCacheSim/admissionAlgo_Size", reader, test_Size);
122122
g_test_add_data_func("/libCacheSim/admissionAlgo_SizeProb", reader, test_SizeProb);
123-
g_test_add_data_func("/libCacheSim/admissionAlgo_BloomFilter", reader, test_BloomFilter);
123+
g_test_add_data_func_full("/libCacheSim/admissionAlgo_BloomFilter", reader, test_BloomFilter, test_teardown);
124124

125125
return g_test_run();
126126
}

test/test_dataStructure.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ void test_chained_hashtable_v2(gconstpointer user_data) {
2727
print_chained_hashtable_v2(hashtable);
2828
// cache_obj_t *obj = chained_hashtable_rand_obj_v2(hashtable);
2929
// printf("random object %lu\n", obj->obj_id);
30+
31+
// clean up
32+
free_request(req);
33+
free_chained_hashtable_v2(hashtable);
3034
}
3135

3236
int main(int argc, char *argv[]) {
3337
g_test_init(&argc, &argv, NULL);
34-
reader_t *reader;
3538

36-
reader = setup_plaintxt_reader_num();
3739
g_test_add_data_func("/libCacheSim/test_chained_hashtable_v2", NULL, test_chained_hashtable_v2);
3840

3941
return g_test_run();

test/test_dist.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,26 @@ void test_distUtils_basic(gconstpointer user_data) {
2222
for (i = (long)get_num_of_req(reader) - 1, j = 0; j < N_TEST; i--, j++) {
2323
g_assert_cmpint(dist[i], ==, rd_true[j]);
2424
}
25+
free(dist);
2526

2627
dist = get_stack_dist(reader, FUTURE_STACK_DIST, &array_size);
2728
g_assert_cmpint(array_size, ==, get_num_of_req(reader));
2829
for (i = 6, j = 0; j < N_TEST; i++, j++) {
2930
g_assert_cmpint(dist[i], ==, frd_true[j]);
3031
}
32+
free(dist);
3133

3234
dist = get_access_dist(reader, DIST_SINCE_LAST_ACCESS, &array_size);
3335
g_assert_cmpint(array_size, ==, get_num_of_req(reader));
3436
for (i = (long)get_num_of_req(reader) - 1, j = 0; j < N_TEST; i--, j++) {
3537
g_assert_cmpint(dist[i], ==, last_dist_true[j]);
3638
}
37-
39+
free(dist);
3840
// dist = get_next_access_dist(reader);
3941
// for (i = 6, j = 0; j < N_TEST; i++, j++) {
4042
// g_assert_cmpint(dist[i], ==, next_dist_true[j]);
4143
// }
44+
// free(dist);
4245
}
4346

4447
void test_distUtils_more1(gconstpointer user_data) {

0 commit comments

Comments
 (0)