Skip to content

Commit 8bb69b9

Browse files
authored
Add LeakSanitizer to CI (1a1a11a#195)
* Add LSan to CI * Fix mem leak * Formatting test code
1 parent 22962c3 commit 8bb69b9

20 files changed

Lines changed: 890 additions & 532 deletions

.github/workflows/build.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@ jobs:
3232
- uses: actions/checkout@v2
3333
- name: Prepare
3434
run: bash scripts/install_dependency.sh
35-
- name: Configure CMake
36-
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
35+
- name: Configure CMake with LSan
36+
run: |
37+
cmake -B ${{github.workspace}}/build \
38+
-DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \
39+
-DCMAKE_C_FLAGS="-fsanitize=leak" \
40+
-DCMAKE_CXX_FLAGS="-fsanitize=leak"
3741
- name: Build
3842
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
39-
- name: Test
43+
- name: Test with LSan
4044
working-directory: ${{github.workspace}}/build
41-
run: ctest -C ${{env.BUILD_TYPE}}
45+
run: |
46+
export ASAN_OPTIONS="detect_leaks=1:halt_on_error=1:verbosity=1"
47+
ctest -C ${{env.BUILD_TYPE}} --output-on-failure
4248
4349
selfhosted:
4450
runs-on: self-hosted

libCacheSim/cache/eviction/LFUDA.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ static void free_list_node(void *list_node);
5858
*/
5959
cache_t *LFUDA_init(const common_cache_params_t ccache_params,
6060
const char *cache_specific_params) {
61-
cache_t *cache = cache_struct_init("LFUDA", ccache_params, cache_specific_params);
61+
cache_t *cache =
62+
cache_struct_init("LFUDA", ccache_params, cache_specific_params);
6263
cache->cache_init = LFUDA_init;
6364
cache->cache_free = LFUDA_free;
6465
cache->get = LFUDA_get;
@@ -102,6 +103,7 @@ cache_t *LFUDA_init(const common_cache_params_t ccache_params,
102103
static void LFUDA_free(cache_t *cache) {
103104
LFUDA_params_t *params = (LFUDA_params_t *)(cache->eviction_params);
104105
g_hash_table_destroy(params->freq_map);
106+
free(params);
105107
cache_struct_free(cache);
106108
}
107109

@@ -216,7 +218,9 @@ static cache_obj_t *LFUDA_insert(cache_t *cache, const request_t *req) {
216218
memset(new_node, 0, sizeof(freq_node_t));
217219
new_node->freq = cache_obj->lfu.freq;
218220
g_hash_table_insert(params->freq_map, key, new_node);
219-
params->max_freq = params->max_freq < cache_obj->lfu.freq ? cache_obj->lfu.freq : params->max_freq;
221+
params->max_freq = params->max_freq < cache_obj->lfu.freq
222+
? cache_obj->lfu.freq
223+
: params->max_freq;
220224
} else {
221225
DEBUG_ASSERT(new_node->freq == cache_obj->lfu.freq);
222226
}
@@ -354,7 +358,7 @@ static void update_min_freq(LFUDA_params_t *params) {
354358
break;
355359
}
356360
}
357-
// If cache is empty, min_freq will be unchanged.
361+
// If cache is empty, min_freq will be unchanged.
358362
DEBUG_ASSERT(params->min_freq >= old_min_freq);
359363
}
360364

libCacheSim/cache/eviction/LIRS.c

Lines changed: 46 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,30 @@ 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 >
641+
params->hirs_limit) {
622642
evictHIR(cache);
623643
}
624-
params->LRU_q->insert(params->LRU_q, req_local);
644+
params->LRU_q->insert(params->LRU_q, req_local_evictLIR);
625645

626646
cache_obj_t *obj_to_update =
627-
params->LRU_q->find(params->LRU_q, req_local, false);
647+
params->LRU_q->find(params->LRU_q, req_local_evictLIR, false);
628648
obj_to_update->LIRS.is_LIR = false;
629649
obj_to_update->LIRS.in_cache = true;
630650

631-
params->hirs_count += req_local->obj_size;
632-
cache->occupied_byte += (req_local->obj_size + cache->obj_md_size);
651+
params->hirs_count += req_local_evictLIR->obj_size;
652+
cache->occupied_byte += (req_local_evictLIR->obj_size + cache->obj_md_size);
633653
cache->n_obj += 1;
634654
}
635655

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

644664
// 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();
665+
if (req_local_evictHIR == NULL) {
666+
req_local_evictHIR = new_request();
648667
}
649-
copy_cache_obj_to_request(req_local, obj_to_evict);
668+
copy_cache_obj_to_request(req_local_evictHIR, obj_to_evict);
650669

651670
params->hirs_count -= obj_to_evict->obj_size;
652671
params->LRU_q->evict(params->LRU_q, NULL);
653672

654673
cache_obj_t *obj_to_update =
655-
params->LRU_s->find(params->LRU_s, req_local, false);
674+
params->LRU_s->find(params->LRU_s, req_local_evictHIR, false);
656675
if (obj_to_update != NULL) {
657676
obj_to_update->LIRS.in_cache = false;
658-
params->LRU_nh->insert(params->LRU_nh, req_local);
677+
params->LRU_nh->insert(params->LRU_nh, req_local_evictHIR);
659678
params->nonresident += obj_to_update->obj_size;
660679
}
661680

662-
cache->occupied_byte -= (req_local->obj_size + cache->obj_md_size);
681+
cache->occupied_byte -= (req_local_evictHIR->obj_size + cache->obj_md_size);
663682
cache->n_obj -= 1;
664683

665684
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: 4 additions & 2 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

@@ -450,8 +451,9 @@ static void SLRU_parse_params(cache_t *cache,
450451
params->n_seg = n_seg;
451452
params->lru_max_n_bytes = calloc(params->n_seg, sizeof(int64_t));
452453
for (int i = 0; i < n_seg; i++) {
453-
params->lru_max_n_bytes[i] = (int64_t)(
454-
(double)seg_size_array[i] / seg_size_sum * cache->cache_size);
454+
params->lru_max_n_bytes[i] =
455+
(int64_t)((double)seg_size_array[i] / seg_size_sum *
456+
cache->cache_size);
455457
}
456458
} else if (strcasecmp(key, "print") == 0) {
457459
printf("current parameters: %s\n", SLRU_current_params(cache, params));

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
}

0 commit comments

Comments
 (0)