Skip to content

Commit 1a977b8

Browse files
committed
Fix mem leak
1 parent 9f878f7 commit 1a977b8

10 files changed

Lines changed: 84 additions & 124 deletions

File tree

libCacheSim/cache/eviction/3LCache/ThreeLCache.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ void ThreeLCacheCache::erase_out_cache() {
145145
}
146146
}
147147
key_map.erase(meta._key);
148-
meta.free();
149148
}
150149
out_cache.metas.pop_front();
151150
out_cache.front_index++;

libCacheSim/cache/eviction/3LCache/ThreeLCache.hpp

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ struct MetaExtra {
4646
_past_distances = vector<int32_t>(1, distance);
4747
}
4848

49+
MetaExtra(const MetaExtra &other)
50+
: _past_distances(other._past_distances),
51+
_past_distance_idx(other._past_distance_idx) {}
52+
4953
void update(const int32_t &distance) {
5054
uint8_t distance_idx = _past_distance_idx % max_n_past_distances;
5155
if (_past_distances.size() < max_n_past_distances)
@@ -77,13 +81,38 @@ class Meta {
7781
_sample_times = 0;
7882
}
7983

80-
virtual ~Meta() = default;
84+
// deep copy
85+
Meta(const Meta &other)
86+
: _key(other._key),
87+
_size(other._size),
88+
_past_timestamp(other._past_timestamp),
89+
_freq(other._freq),
90+
_sample_times(other._sample_times) {
91+
if (other._extra) {
92+
_extra = new MetaExtra(*other._extra);
93+
}
94+
}
95+
96+
// copy assignment operator
97+
Meta &operator=(const Meta &other) {
98+
if (this != &other) {
99+
_key = other._key;
100+
_size = other._size;
101+
_past_timestamp = other._past_timestamp;
102+
_freq = other._freq;
103+
_sample_times = other._sample_times;
104+
if (_extra) delete _extra;
105+
_extra = other._extra ? new MetaExtra(*other._extra) : nullptr;
106+
}
107+
return *this;
108+
}
109+
110+
~Meta() { delete _extra; }
81111

82112
void emplace_sample(uint64_t &sample_t, uint8_t max_num = 1) {
83113
if (_sample_times == 0) _sample_times = sample_t;
84114
}
85115

86-
void free() { delete _extra; }
87116
void update(const uint64_t &past_timestamp) {
88117
if (max_n_past_distances > 0) {
89118
int32_t _distance = past_timestamp - _past_timestamp;
@@ -386,6 +415,13 @@ class ThreeLCacheCache : public webcachesim::Cache {
386415
}
387416
return distribution;
388417
}
418+
419+
~ThreeLCacheCache() {
420+
if (evcition_distribution) free(evcition_distribution);
421+
if (object_distribution_n_eviction) free(object_distribution_n_eviction);
422+
if (training_data) delete training_data;
423+
if (booster) LGBM_BoosterFree(booster);
424+
}
389425
};
390426

391427
} // namespace ThreeLCache

libCacheSim/cache/eviction/3LCache/ThreeLCache_Interface.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ cache_t *ThreeLCache_init(const common_cache_params_t ccache_params,
9090
ThreeLCache_params_t *params = my_malloc(ThreeLCache_params_t);
9191
cache->eviction_params = params;
9292

93+
// init
94+
params->ThreeLCache_cache = nullptr;
95+
params->objective = nullptr;
96+
params->to_evict_pair = std::make_pair(0, 0);
97+
9398
ThreeLCache_parse_params(cache, DEFAULT_PARAMS);
9499
if (cache_specific_params != NULL) {
95100
ThreeLCache_parse_params(cache, cache_specific_params);
@@ -130,6 +135,7 @@ static void ThreeLCache_free(cache_t *cache) {
130135
free(cache->to_evict_candidate);
131136
if (params->objective != NULL) {
132137
free(params->objective);
138+
params->objective = NULL;
133139
}
134140
my_free(sizeof(ThreeLCache_params_t), params);
135141
cache_struct_free(cache);
@@ -326,7 +332,7 @@ static void ThreeLCache_parse_params(cache_t *cache,
326332
const char *cache_specific_params) {
327333
ThreeLCache_params_t *params = (ThreeLCache_params_t *)cache->eviction_params;
328334
char *params_str = strdup(cache_specific_params);
329-
char *end;
335+
char *original_params_str = params_str; // preserve the original pointer
330336

331337
while (params_str != NULL && params_str[0] != '\0') {
332338
/* different parameters are separated by comma,
@@ -342,6 +348,7 @@ static void ThreeLCache_parse_params(cache_t *cache,
342348
if (strcasecmp(key, "objective") == 0) {
343349
if (params->objective != NULL) {
344350
free(params->objective);
351+
params->objective = NULL;
345352
}
346353
params->objective = strdup(value);
347354
if (params->objective == NULL) {
@@ -350,13 +357,15 @@ static void ThreeLCache_parse_params(cache_t *cache,
350357
} else if (strcasecmp(key, "print") == 0) {
351358
printf("current parameters: %s\n",
352359
ThreeLCache_current_params(cache, params));
360+
free(original_params_str);
353361
exit(0);
354362
} else {
355363
ERROR("%s does not have parameter %s\n", cache->cache_name, key);
364+
free(original_params_str);
356365
exit(1);
357366
}
358367
}
359-
free(params_str);
368+
free(original_params_str);
360369
}
361370
#ifdef __cplusplus
362371
}

libCacheSim/cache/eviction/LRB/LRB_Interface.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ static void LRB_parse_params(cache_t *cache,
320320
const char *cache_specific_params) {
321321
LRB_params_t *params = (LRB_params_t *)cache->eviction_params;
322322
char *params_str = strdup(cache_specific_params);
323-
char *end;
323+
char *original_params_str = params_str; // preserve the original pointer
324324

325325
while (params_str != NULL && params_str[0] != '\0') {
326326
/* different parameters are separated by comma,
@@ -343,13 +343,15 @@ static void LRB_parse_params(cache_t *cache,
343343
}
344344
} else if (strcasecmp(key, "print") == 0) {
345345
printf("current parameters: %s\n", LRB_current_params(cache, params));
346+
free(original_params_str);
346347
exit(0);
347348
} else {
348349
ERROR("%s does not have parameter %s\n", cache->cache_name, key);
350+
free(original_params_str);
349351
exit(1);
350352
}
351353
}
352-
free(params_str);
354+
free(original_params_str);
353355
}
354356
#ifdef __cplusplus
355357
}

test/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,3 @@ add_test(NAME testUtils COMMAND testUtils WORKING_DIRECTORY .)
6666
# target_link_libraries(testGLCache ${all_modules} ${dependency_libs})
6767
# add_test(NAME testGLCache COMMAND testGLCache WORKING_DIRECTORY .)
6868
# endif (ENABLE_GLCACHE)
69-
70-
if (ENABLE_3L_CACHE)
71-
add_test_executable(test3LCache test_3lcache.c)
72-
add_test(NAME test3LCache COMMAND test3LCache WORKING_DIRECTORY .)
73-
endif (ENABLE_3L_CACHE)

test/common.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,18 @@ static reader_t *setup_GLCacheTestData_reader(void) {
101101

102102
static reader_t *setup_3LCacheTestData_reader(void) {
103103
const char *url =
104-
"https://ftp.pdl.cmu.edu/pub/datasets/twemcacheWorkload/cacheDatasets/"
105-
"tencentBlock/"
106-
"tencentBlock.ns3964.oracleGeneral.zst";
104+
"https://ftp.pdl.cmu.edu/pub/datasets/twemcacheWorkload/"
105+
".w68.oracleGeneral.bin.zst";
107106
int ret = system(
108-
"if [ ! -f tencentBlock.ns3964.oracleGeneral.zst ]; then wget "
109-
"https://ftp.pdl.cmu.edu/pub/datasets/twemcacheWorkload/cacheDatasets/"
110-
"tencentBlock/"
111-
"tencentBlock.ns3964.oracleGeneral.zst; fi");
107+
"if [ ! -f .w68.oracleGeneral.bin.zst ]; then wget "
108+
"https://ftp.pdl.cmu.edu/pub/datasets/twemcacheWorkload/"
109+
".w68.oracleGeneral.bin.zst; fi");
112110
if (ret != 0) {
113111
ERROR("downloading data failed\n");
114112
}
115113

116-
reader_t *reader_oracle = setup_reader(
117-
"tencentBlock.ns3964.oracleGeneral.zst", ORACLE_GENERAL_TRACE, NULL);
114+
reader_t *reader_oracle =
115+
setup_reader(".w68.oracleGeneral.bin.zst", ORACLE_GENERAL_TRACE, NULL);
118116
return reader_oracle;
119117
}
120118

test/test_3lcache.c

Lines changed: 0 additions & 99 deletions
This file was deleted.

test/test_admissionAlgo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static void test_admission_algorithm(gconstpointer user_data,
104104
test_data->miss_byte_true);
105105

106106
cache->cache_free(cache);
107-
my_free(sizeof(cache_stat_t), res);
107+
my_free(sizeof(cache_stat_t) * (CACHE_SIZE / STEP_SIZE), res);
108108
}
109109

110110
// Individual test functions (ordered alphabetically)

test/test_evictionAlgo.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,17 @@ static const cache_test_data_t test_data_truth[] = {
195195
.req_byte_true = 4368040448,
196196
.miss_cnt_true = {90043, 83978, 81482, 77727, 72611, 72059, 67836, 65739},
197197
.miss_byte_true = {4068758016, 3792818176, 3639756288, 3379609600,
198-
3165339648, 3058814976, 2862775296, 2774183936}}};
198+
3165339648, 3058814976, 2862775296, 2774183936}},
199+
#if defined(ENABLE_3L_CACHE) && ENABLE_3L_CACHE == 1
200+
{.cache_name = "3LCache",
201+
.hashpower = 20,
202+
.req_cnt_true = 113872,
203+
.req_byte_true = 4368040448,
204+
.miss_cnt_true = {93374, 89783, 83572, 81722, 72494, 72104, 71972, 71704},
205+
.miss_byte_true = {4214303232, 4061242368, 3778040320, 3660569600,
206+
3100927488, 3078128640, 3075403776, 3061662720}},
207+
#endif /* ENABLE_3L_CACHE */
208+
};
199209

200210
static void _verify_profiler_results(const cache_stat_t *res,
201211
uint64_t num_of_sizes,
@@ -255,7 +265,7 @@ static void test_cache_algorithm(gconstpointer user_data,
255265
test_data->miss_byte_true);
256266

257267
cache->cache_free(cache);
258-
my_free(sizeof(cache_stat_t), res);
268+
my_free(sizeof(cache_stat_t) * (CACHE_SIZE / STEP_SIZE), res);
259269
}
260270

261271
// Individual test functions (ordered alphabetically)
@@ -364,6 +374,12 @@ static void test_SR_LRU(gconstpointer user_data) {
364374
test_cache_algorithm(user_data, &test_data_truth[24]);
365375
}
366376

377+
#if defined(ENABLE_3L_CACHE) && ENABLE_3L_CACHE == 1
378+
static void test_3LCache(gconstpointer user_data) {
379+
test_cache_algorithm(user_data, &test_data_truth[25]);
380+
}
381+
#endif /* ENABLE_3L_CACHE */
382+
367383
static void test_WTinyLFU(gconstpointer user_data) {
368384
// TODO: to be implemented
369385
}
@@ -414,6 +430,10 @@ int main(int argc, char *argv[]) {
414430
g_test_add_data_func("/libCacheSim/cacheAlgo_SLRU", reader, test_SLRU);
415431
g_test_add_data_func("/libCacheSim/cacheAlgo_SR_LRU", reader, test_SR_LRU);
416432

433+
#if defined(ENABLE_3L_CACHE) && ENABLE_3L_CACHE == 1
434+
g_test_add_data_func("/libCacheSim/cacheAlgo_3LCache", reader, test_3LCache);
435+
#endif /* ENABLE_3L_CACHE */
436+
417437
// Belady algorithms require reader that has next access information
418438
// and can only use oracleGeneral trace (which we're using)
419439
g_test_add_data_func("/libCacheSim/cacheAlgo_Belady", reader, test_Belady);

test/test_prefetchAlgo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static void test_prefetch_algorithm(gconstpointer user_data,
101101
test_data->miss_byte_true);
102102

103103
cache->cache_free(cache);
104-
my_free(sizeof(cache_stat_t), res);
104+
my_free(sizeof(cache_stat_t) * (CACHE_SIZE / STEP_SIZE), res);
105105
}
106106

107107
// Individual test functions (ordered alphabetically)

0 commit comments

Comments
 (0)