Skip to content

Commit 6ccc5df

Browse files
authored
[Fix] compile error of 3L-Cache (1a1a11a#244)
* [Fix] compile error of 3L-Cache * Clean up * Fix mem leak
1 parent 916b6d1 commit 6ccc5df

10 files changed

Lines changed: 112 additions & 145 deletions

File tree

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

Lines changed: 5 additions & 6 deletions
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++;
@@ -191,12 +190,12 @@ int32_t ThreeLCacheCache::rank() {
191190
if (initial_queue_length == 0) {
192191
initial_queue_length = in_cache.metas.size();
193192
}
194-
// 防止有trace在小缓存小只能存1-2个对象
193+
// prevent the trace from having only 1-2 objects in the small cache
195194
if (sample_rate >= initial_queue_length * 0.01 + eviction_rate)
196195
sample_rate = initial_queue_length > 2
197196
? initial_queue_length * 0.01 + eviction_rate
198197
: 1;
199-
// 新对象的采样
198+
// the sampling of the new object
200199
sampled_objects = quick_demotion();
201200

202201
if (new_obj_size < _currentSize * reserved_space / 10) {
@@ -241,7 +240,7 @@ int32_t ThreeLCacheCache::rank() {
241240
int32_t eviciton_sum = 0, p99 = 0;
242241
for (int i = 0; i < 16; i++)
243242
eviciton_sum += object_distribution_n_eviction[i];
244-
// 粗粒度划分频率
243+
// coarse-grained frequency division
245244
for (int i = 0; i < 16; i++) {
246245
p99 += object_distribution_n_eviction[i];
247246
if (p99 >= 0.99 * eviciton_sum) {
@@ -283,7 +282,7 @@ int32_t ThreeLCacheCache::rank() {
283282

284283
vector<int32_t> ThreeLCacheCache::quick_demotion() {
285284
vector<int32_t> sampled_objects;
286-
int i, j = 0;
285+
int i = 0, j = 0;
287286
while (new_obj_size > (uint64_t)(_currentSize * reserved_space / 100) &&
288287
j < (int)(sample_rate * 1.5) && (size_t)i < new_obj_keys.size()) {
289288
auto it = key_map.find(new_obj_keys[i])->second;
@@ -395,7 +394,7 @@ void ThreeLCacheCache::prediction(vector<int32_t> sampled_objects) {
395394
keys[idx_row] = meta._key;
396395
poses[idx_row] = pos;
397396
indices[idx_feature] = 0;
398-
// 年龄
397+
// age
399398
data[idx_feature++] = current_seq - meta._past_timestamp;
400399

401400
past_timestamps[idx_row] = meta._past_timestamp;

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

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77
#include <cmath>
88
#include <deque>
99
#include <fstream>
10+
#include <iostream>
1011
#include <list>
12+
#include <map>
1113
#include <random>
1214
#include <sstream>
15+
#include <string>
1316
#include <unordered_map>
1417
#include <unordered_set>
1518
#include <vector>
1619

20+
#include "cache.h"
1721
#include "dataStructure/sparsepp/spp.h"
1822
#include "libCacheSim/cache.h"
19-
using namespace webcachesim;
23+
#include "request.h"
24+
2025
using namespace std;
26+
using namespace webcachesim;
2127

2228
using spp::sparse_hash_map;
2329

@@ -40,6 +46,10 @@ struct MetaExtra {
4046
_past_distances = vector<int32_t>(1, distance);
4147
}
4248

49+
MetaExtra(const MetaExtra &other)
50+
: _past_distances(other._past_distances),
51+
_past_distance_idx(other._past_distance_idx) {}
52+
4353
void update(const int32_t &distance) {
4454
uint8_t distance_idx = _past_distance_idx % max_n_past_distances;
4555
if (_past_distances.size() < max_n_past_distances)
@@ -71,13 +81,38 @@ class Meta {
7181
_sample_times = 0;
7282
}
7383

74-
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; }
75111

76112
void emplace_sample(uint64_t &sample_t, uint8_t max_num = 1) {
77113
if (_sample_times == 0) _sample_times = sample_t;
78114
}
79115

80-
void free() { delete _extra; }
81116
void update(const uint64_t &past_timestamp) {
82117
if (max_n_past_distances > 0) {
83118
int32_t _distance = past_timestamp - _past_timestamp;
@@ -177,7 +212,7 @@ class TrainingData {
177212
int32_t counter = indptr.back();
178213

179214
indices.emplace_back(0);
180-
// 等待时间
215+
// waiting time
181216
data.emplace_back(sample_timestamp - meta._past_timestamp);
182217
++counter;
183218
int j = 0;
@@ -220,20 +255,20 @@ struct KeyMapEntryT {
220255
int32_t list_pos;
221256
};
222257

223-
class ThreeLCacheCache : public Cache {
258+
class ThreeLCacheCache : public webcachesim::Cache {
224259
public:
225260
uint64_t current_seq = -1;
226261
int32_t n_feature;
227262
sparse_hash_map<uint64_t, float> pred_map;
228-
// 用于记录对象的预测结果, 同时记录id, 以保证状态切换
263+
// used to record the prediction result, and the id of the object
229264
vector<HeapUint> pred_times;
230-
// 驱逐候选对象采样步长与区间
265+
// the step length and interval of the eviction candidate object sampling
231266
uint64_t scan_length = 0;
232-
// 新对象
267+
// new object
233268
vector<uint64_t> new_obj_keys;
234-
// 新对象占用地缓存空间
269+
// the size of the new object
235270
uint64_t new_obj_size = 0;
236-
// 驱逐对象的数量
271+
// the number of the eviction object
237272
int evict_nums = 0;
238273
uint16_t sample_rate = 1024;
239274
uint8_t eviction_rate = 2;
@@ -245,14 +280,14 @@ class ThreeLCacheCache : public Cache {
245280
int32_t initial_queue_length = 0;
246281
uint64_t origin_current_seq = 0;
247282
uint8_t reserved_space = 2;
248-
// 采样指针
283+
// the sampling pointer
249284
int32_t samplepointer = 0;
250285
uint8_t hsw = 2;
251286
uint64_t MAX_EVICTION_BOUNDARY[2] = {0, 0};
252287
int32_t max_out_cache_size = 2;
253-
// 窗口满了后
288+
// the window is full
254289
uint8_t is_full = 0;
255-
// 对象命中率的时间基线
290+
// the time baseline of the object hit rate
256291
uint64_t n_req = 0;
257292
uint64_t n_hit = 0;
258293
uint64_t n_window_hit = 0;
@@ -353,6 +388,10 @@ class ThreeLCacheCache : public Cache {
353388

354389
void update_stat_periodic() override;
355390

391+
void setSize(const uint64_t &cs) { _cacheSize = cs; }
392+
393+
bool exist(const int64_t &key) { return key_map.find(key) != key_map.end(); }
394+
356395
pair<uint64_t, int32_t> evict_predobj();
357396

358397
void remove_from_outcache_metas(Meta &meta, unsigned int &pos,
@@ -376,6 +415,13 @@ class ThreeLCacheCache : public Cache {
376415
}
377416
return distribution;
378417
}
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+
}
379425
};
380426

381427
} // namespace ThreeLCache

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
#include <string>
55

66
#include "ThreeLCache.hpp"
7-
#include "dataStructure/hashtable/hashtable.h"
87
#include "libCacheSim/cache.h"
9-
#include "libCacheSim/evictionAlgo.h"
108

119
#ifdef __cplusplus
1210
extern "C" {
@@ -17,7 +15,7 @@ typedef struct {
1715
char *objective;
1816
SimpleRequest ThreeLCache_req;
1917

20-
pair<uint64_t, int32_t> to_evict_pair;
18+
std::pair<uint64_t, int32_t> to_evict_pair;
2119
cache_obj_t obj_tmp;
2220
} ThreeLCache_params_t;
2321

@@ -92,6 +90,11 @@ cache_t *ThreeLCache_init(const common_cache_params_t ccache_params,
9290
ThreeLCache_params_t *params = my_malloc(ThreeLCache_params_t);
9391
cache->eviction_params = params;
9492

93+
// init
94+
params->ThreeLCache_cache = nullptr;
95+
params->objective = nullptr;
96+
params->to_evict_pair = std::make_pair(0, 0);
97+
9598
ThreeLCache_parse_params(cache, DEFAULT_PARAMS);
9699
if (cache_specific_params != NULL) {
97100
ThreeLCache_parse_params(cache, cache_specific_params);
@@ -132,6 +135,7 @@ static void ThreeLCache_free(cache_t *cache) {
132135
free(cache->to_evict_candidate);
133136
if (params->objective != NULL) {
134137
free(params->objective);
138+
params->objective = NULL;
135139
}
136140
my_free(sizeof(ThreeLCache_params_t), params);
137141
cache_struct_free(cache);
@@ -236,7 +240,7 @@ static cache_obj_t *ThreeLCache_to_evict(cache_t *cache, const request_t *req) {
236240
auto *params = static_cast<ThreeLCache_params_t *>(cache->eviction_params);
237241
auto *ThreeLCache =
238242
static_cast<ThreeLCache::ThreeLCacheCache *>(params->ThreeLCache_cache);
239-
// ThreeLCache rank变成了evict_preobj
243+
// ThreeLCache rank becomes evict_predobj
240244
params->to_evict_pair = ThreeLCache->evict_predobj();
241245
auto &meta = ThreeLCache->in_cache.metas[params->to_evict_pair.second];
242246

@@ -328,7 +332,7 @@ static void ThreeLCache_parse_params(cache_t *cache,
328332
const char *cache_specific_params) {
329333
ThreeLCache_params_t *params = (ThreeLCache_params_t *)cache->eviction_params;
330334
char *params_str = strdup(cache_specific_params);
331-
char *end;
335+
char *original_params_str = params_str; // preserve the original pointer
332336

333337
while (params_str != NULL && params_str[0] != '\0') {
334338
/* different parameters are separated by comma,
@@ -344,6 +348,7 @@ static void ThreeLCache_parse_params(cache_t *cache,
344348
if (strcasecmp(key, "objective") == 0) {
345349
if (params->objective != NULL) {
346350
free(params->objective);
351+
params->objective = NULL;
347352
}
348353
params->objective = strdup(value);
349354
if (params->objective == NULL) {
@@ -352,13 +357,15 @@ static void ThreeLCache_parse_params(cache_t *cache,
352357
} else if (strcasecmp(key, "print") == 0) {
353358
printf("current parameters: %s\n",
354359
ThreeLCache_current_params(cache, params));
360+
free(original_params_str);
355361
exit(0);
356362
} else {
357363
ERROR("%s does not have parameter %s\n", cache->cache_name, key);
364+
free(original_params_str);
358365
exit(1);
359366
}
360367
}
361-
free(params_str);
368+
free(original_params_str);
362369
}
363370
#ifdef __cplusplus
364371
}

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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +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_executable(test3LCache test_3lcache.c)
72-
target_link_libraries(test3LCache ${core_libs} ${dependency_libs})
73-
add_test(NAME test3LCache COMMAND test3LCache WORKING_DIRECTORY .)
74-
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

0 commit comments

Comments
 (0)