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+
2025using namespace std ;
26+ using namespace webcachesim ;
2127
2228using 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
0 commit comments