-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcache.py
More file actions
693 lines (563 loc) · 21.9 KB
/
cache.py
File metadata and controls
693 lines (563 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
from abc import ABC
from typing import Callable, Optional
from .libcachesim_python import (
CommonCacheParams,
Request,
CacheObject,
Cache,
# Core cache algorithms
LHD_init,
LRU_init,
FIFO_init,
LFU_init,
ARC_init,
Clock_init,
Random_init,
LIRS_init,
TwoQ_init,
SLRU_init,
# Advanced algorithms
S3FIFO_init,
Sieve_init,
WTinyLFU_init,
LeCaR_init,
LFUDA_init,
ClockPro_init,
Cacheus_init,
# Optimal algorithms
Belady_init,
BeladySize_init,
# Probabilistic algorithms
LRU_Prob_init,
flashProb_init,
# Size-based algorithms
Size_init,
GDSF_init,
# Hyperbolic algorithms
Hyperbolic_init,
# Plugin cache
pypluginCache_init,
# Process trace function
c_process_trace,
)
from .protocols import ReaderProtocol
class CacheBase(ABC):
"""Base class for all cache implementations"""
_cache: Cache # Internal C++ cache object
def __init__(self, _cache: Cache):
self._cache = _cache
def get(self, req: Request) -> bool:
return self._cache.get(req)
def find(self, req: Request, update_cache: bool = True) -> Optional[CacheObject]:
return self._cache.find(req, update_cache)
def can_insert(self, req: Request) -> bool:
return self._cache.can_insert(req)
def insert(self, req: Request) -> CacheObject:
return self._cache.insert(req)
def need_eviction(self, req: Request) -> bool:
return self._cache.need_eviction(req)
def evict(self, req: Request) -> CacheObject:
return self._cache.evict(req)
def remove(self, obj_id: int) -> bool:
return self._cache.remove(obj_id)
def to_evict(self, req: Request) -> CacheObject:
return self._cache.to_evict(req)
def get_occupied_byte(self) -> int:
return self._cache.get_occupied_byte()
def get_n_obj(self) -> int:
return self._cache.get_n_obj()
def set_cache_size(self, new_size: int) -> None:
self._cache.set_cache_size(new_size)
def print_cache(self) -> str:
return self._cache.print_cache()
def process_trace(self, reader: ReaderProtocol, start_req: int = 0, max_req: int = -1) -> tuple[float, float]:
"""Process trace with this cache and return miss ratios"""
if hasattr(reader, "c_reader") and reader.c_reader:
# C++ reader with _reader attribute
if hasattr(reader, "_reader"):
return c_process_trace(self._cache, reader._reader, start_req, max_req)
else:
raise ValueError("C++ reader missing _reader attribute")
else:
# Python reader - use Python implementation
return self._process_trace_python(reader, start_req, max_req)
def _process_trace_python(
self, reader: ReaderProtocol, start_req: int = 0, max_req: int = -1
) -> tuple[float, float]:
"""Python fallback for processing traces"""
reader.reset()
if start_req > 0:
reader.skip_n_req(start_req)
n_req = 0
n_hit = 0
bytes_req = 0
bytes_hit = 0
for req in reader:
if not req.valid:
break
n_req += 1
bytes_req += req.obj_size
if self.get(req):
n_hit += 1
bytes_hit += req.obj_size
if max_req > 0 and n_req >= max_req:
break
obj_miss_ratio = 1.0 - (n_hit / n_req) if n_req > 0 else 0.0
byte_miss_ratio = 1.0 - (bytes_hit / bytes_req) if bytes_req > 0 else 0.0
return obj_miss_ratio, byte_miss_ratio
# Properties
@property
def cache_size(self) -> int:
return self._cache.cache_size
@property
def cache_name(self) -> str:
return self._cache.cache_name
def _create_common_params(
cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
) -> CommonCacheParams:
"""Helper to create common cache parameters"""
return CommonCacheParams(
cache_size=cache_size,
default_ttl=default_ttl,
hashpower=hashpower,
consider_obj_metadata=consider_obj_metadata,
)
# ------------------------------------------------------------------------------------------------
# Core cache algorithms
# ------------------------------------------------------------------------------------------------
class LHD(CacheBase):
"""Least Hit Density cache (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=LHD_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
class LRU(CacheBase):
"""Least Recently Used cache (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=LRU_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
class FIFO(CacheBase):
"""First In First Out cache (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=FIFO_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
class LFU(CacheBase):
"""Least Frequently Used cache (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=LFU_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
class ARC(CacheBase):
"""Adaptive Replacement Cache (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=ARC_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
class Clock(CacheBase):
"""Clock replacement algorithm
Special parameters:
init_freq: initial frequency of the object (default: 0)
n_bit_counter: number of bits for the counter (default: 1)
"""
def __init__(
self,
cache_size: int,
default_ttl: int = 86400 * 300,
hashpower: int = 24,
consider_obj_metadata: bool = False,
init_freq: int = 0,
n_bit_counter: int = 1,
):
cache_specific_params = f"init-freq={init_freq}, n-bit-counter={n_bit_counter}"
super().__init__(
_cache=Clock_init(
_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params
)
)
class Random(CacheBase):
"""Random replacement cache (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=Random_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
# Advanced algorithms
class S3FIFO(CacheBase):
"""S3-FIFO cache algorithm
Special parameters:
small_size_ratio: ratio of small cache size to total cache size (default: 0.1)
ghost_size_ratio: ratio of ghost cache size to total cache size (default: 0.9)
move_to_main_threshold: threshold for moving objects from ghost to main cache (default: 2)
"""
def __init__(
self,
cache_size: int,
default_ttl: int = 86400 * 300,
hashpower: int = 24,
consider_obj_metadata: bool = False,
small_size_ratio: float = 0.1,
ghost_size_ratio: float = 0.9,
move_to_main_threshold: int = 2,
):
cache_specific_params = f"small-size-ratio={small_size_ratio}, ghost-size-ratio={ghost_size_ratio}, move-to-main-threshold={move_to_main_threshold}"
super().__init__(
_cache=S3FIFO_init(
_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params
)
)
class Sieve(CacheBase):
"""Sieve cache algorithm (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=Sieve_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
class LIRS(CacheBase):
"""Low Inter-reference Recency Set (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=LIRS_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
def insert(self, req: Request) -> Optional[CacheObject]:
return super().insert(req)
class TwoQ(CacheBase):
"""2Q replacement algorithm
Special parameters:
a_in_size_ratio: ratio of Ain queue size to total cache size (default: 0.25)
a_out_size_ratio: ratio of Aout queue size to total cache size (default: 0.5)
"""
def __init__(
self,
cache_size: int,
default_ttl: int = 86400 * 300,
hashpower: int = 24,
consider_obj_metadata: bool = False,
a_in_size_ratio: float = 0.25,
a_out_size_ratio: float = 0.5,
):
cache_specific_params = f"Ain-size-ratio={a_in_size_ratio}, Aout-size-ratio={a_out_size_ratio}"
super().__init__(
_cache=TwoQ_init(
_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params
)
)
class SLRU(CacheBase):
"""Segmented LRU (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=SLRU_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
class WTinyLFU(CacheBase):
"""Window TinyLFU
Special parameters:
main_cache: the type of the main cache (default: "SLRU")
window_size: ratio of the window size to the main cache size (default: 0.01)
"""
def __init__(
self,
cache_size: int,
default_ttl: int = 86400 * 300,
hashpower: int = 24,
consider_obj_metadata: bool = False,
main_cache: str = "SLRU",
window_size: float = 0.01,
):
cache_specific_params = f"main-cache={main_cache}, window-size={window_size}"
super().__init__(
_cache=WTinyLFU_init(
_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params
)
)
class LeCaR(CacheBase):
"""Learning Cache Replacement
Special parameters:
update_weight (bool): whether to update the weight (default: True)
lru_weight (float): the initial weight (probability) of the LRU (default: 0.5), 1 - lru_weight = lfu_weight, i.e, the probability of the LRU being selected
"""
def __init__(
self,
cache_size: int,
default_ttl: int = 86400 * 300,
hashpower: int = 24,
consider_obj_metadata: bool = False,
update_weight: bool = True,
lru_weight: float = 0.5,
):
cache_specific_params = f"update-weight={int(update_weight)}, lru-weight={lru_weight}"
super().__init__(
_cache=LeCaR_init(
_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params
)
)
class LFUDA(CacheBase):
"""LFU with Dynamic Aging (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=LFUDA_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
class ClockPro(CacheBase):
"""Clock-Pro replacement algorithm
Special parameters:
init_ref: initial reference count (default: 0)
init_ratio_cold: initial ratio of cold pages (default: 1)
"""
def __init__(
self,
cache_size: int,
default_ttl: int = 86400 * 300,
hashpower: int = 24,
consider_obj_metadata: bool = False,
init_ref: int = 0,
init_ratio_cold: float = 0.5,
):
cache_specific_params = f"init-ref={init_ref}, init-ratio-cold={init_ratio_cold}"
super().__init__(
_cache=ClockPro_init(
_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params
)
)
class Cacheus(CacheBase):
"""Cacheus algorithm (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=Cacheus_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
# Optimal algorithms
class Belady(CacheBase):
"""Belady's optimal algorithm (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=Belady_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
class BeladySize(CacheBase):
"""Belady's optimal algorithm with size consideration
Special parameters:
n_samples: number of samples for the size consideration (default: 128)
"""
def __init__(
self,
cache_size: int,
default_ttl: int = 86400 * 300,
hashpower: int = 24,
consider_obj_metadata: bool = False,
n_samples: int = 128,
):
cache_specific_params = f"n-samples={n_samples}"
super().__init__(
_cache=BeladySize_init(
_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params
)
)
class LRUProb(CacheBase):
"""LRU with Probabilistic Replacement
Special parameters:
prob: probability of promoting an object to the head of the queue (default: 0.5)
"""
def __init__(
self,
cache_size: int,
default_ttl: int = 86400 * 300,
hashpower: int = 24,
consider_obj_metadata: bool = False,
prob: float = 0.5,
):
cache_specific_params = f"prob={prob}"
super().__init__(
_cache=LRU_Prob_init(
_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params
)
)
class FlashProb(CacheBase):
"""FlashProb replacement algorithm
Special parameters:
ram_size_ratio: ratio of the RAM size to the total cache size (default: 0.05)
disk_admit_prob: probability of admitting a disk page to the RAM (default: 0.2)
ram_cache: the type of the RAM cache (default: "LRU")
disk_cache: the type of the disk cache (default: "FIFO")
"""
def __init__(
self,
cache_size: int,
default_ttl: int = 86400 * 300,
hashpower: int = 24,
consider_obj_metadata: bool = False,
ram_size_ratio: float = 0.05,
disk_admit_prob: float = 0.2,
ram_cache: str = "LRU",
disk_cache: str = "FIFO",
):
cache_specific_params = f"ram-size-ratio={ram_size_ratio}, disk-admit-prob={disk_admit_prob}, ram-cache={ram_cache}, disk-cache={disk_cache}"
super().__init__(
_cache=flashProb_init(
_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params
)
)
class Size(CacheBase):
"""Size-based replacement algorithm (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=Size_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
class GDSF(CacheBase):
"""GDSF replacement algorithm (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=GDSF_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
class Hyperbolic(CacheBase):
"""Hyperbolic replacement algorithm (no special parameters)"""
def __init__(
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
):
super().__init__(
_cache=Hyperbolic_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
)
# Extra deps
class ThreeLCache(CacheBase):
"""Three-Level Cache
Special parameters:
objective: the objective of the ThreeLCache (default: "byte-miss-ratio")
"""
def __init__(
self,
cache_size: int,
default_ttl: int = 86400 * 300,
hashpower: int = 24,
consider_obj_metadata: bool = False,
objective: str = "byte-miss-ratio",
):
# Try to import ThreeLCache_init
try:
from .libcachesim_python import ThreeLCache_init
except ImportError:
raise ImportError(
'ThreeLCache is not installed. Please install it with `CMAKE_ARGS="-DENABLE_3L_CACHE=ON" pip install libcachesim --force-reinstall`'
)
cache_specific_params = f"objective={objective}"
super().__init__(
_cache=ThreeLCache_init(
_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params
)
)
class GLCache(CacheBase):
"""GLCache replacement algorithm
Special parameters:
segment-size: the size of the segment (default: 100)
n-merge: the number of merges (default: 2)
type: the type of the GLCache (default: "learned")
rank-intvl: the interval for ranking (default: 0.02)
merge-consecutive-segs: whether to merge consecutive segments (default: True)
train-source-y: the source of the training data (default: "online")
retrain-intvl: the interval for retraining (default: 86400)
"""
def __init__(
self,
cache_size: int,
default_ttl: int = 86400 * 300,
hashpower: int = 24,
consider_obj_metadata: bool = False,
segment_size: int = 100,
n_merge: int = 2,
type: str = "learned",
rank_intvl: float = 0.02,
merge_consecutive_segs: bool = True,
train_source_y: str = "online",
retrain_intvl: int = 86400,
):
# Try to import GLCache_init
try:
from .libcachesim_python import GLCache_init
except ImportError:
raise ImportError(
'GLCache is not installed. Please install it with `CMAKE_ARGS="-DENABLE_GLCACHE=ON" pip install libcachesim --force-reinstall`'
)
cache_specific_params = f"segment-size={segment_size}, n-merge={n_merge}, type={type}, rank-intvl={rank_intvl}, merge-consecutive-segs={merge_consecutive_segs}, train-source-y={train_source_y}, retrain-intvl={retrain_intvl}"
super().__init__(
_cache=GLCache_init(
_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params
)
)
class LRB(CacheBase):
"""LRB replacement algorithm
Special parameters:
objective: the objective of the LRB (default: "byte-miss-ratio")
"""
def __init__(
self,
cache_size: int,
default_ttl: int = 86400 * 300,
hashpower: int = 24,
consider_obj_metadata: bool = False,
objective: str = "byte-miss-ratio",
):
# Try to import LRB_init
try:
from .libcachesim_python import LRB_init
except ImportError:
raise ImportError(
'LRB is not installed. Please install it with `CMAKE_ARGS="-DENABLE_LRB=ON" pip install libcachesim --force-reinstall`'
)
cache_specific_params = f"objective={objective}"
super().__init__(
_cache=LRB_init(
_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params
)
)
# Plugin cache for custom Python implementations
class PluginCache(CacheBase):
"""Python plugin cache for custom implementations"""
def __init__(
self,
cache_size: int,
cache_init_hook: Callable,
cache_hit_hook: Callable,
cache_miss_hook: Callable,
cache_eviction_hook: Callable,
cache_remove_hook: Callable,
cache_free_hook: Optional[Callable] = None,
cache_name: str = "PythonHookCache",
default_ttl: int = 86400 * 300,
hashpower: int = 24,
consider_obj_metadata: bool = False,
):
self.common_cache_params = _create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata)
super().__init__(
_cache=pypluginCache_init(
self.common_cache_params,
cache_name,
cache_init_hook,
cache_hit_hook,
cache_miss_hook,
cache_eviction_hook,
cache_remove_hook,
cache_free_hook,
)
)