88from collections import deque
99from libcachesim import PluginCache , CommonCacheParams , Request , S3FIFO , FIFO , SyntheticReader
1010
11+
1112# NOTE(haocheng): we only support ignore object size for now
1213class StandaloneS3FIFO :
13- def __init__ (self ,
14- small_size_ratio : float = 0.1 ,
15- ghost_size_ratio : float = 0.9 ,
16- move_to_main_threshold : int = 2 ,
17- cache_size : int = 1024 ):
14+ def __init__ (
15+ self ,
16+ small_size_ratio : float = 0.1 ,
17+ ghost_size_ratio : float = 0.9 ,
18+ move_to_main_threshold : int = 2 ,
19+ cache_size : int = 1024 ,
20+ ):
1821 self .cache_size = cache_size
1922 small_fifo_size = int (small_size_ratio * cache_size )
2023 main_fifo_size = cache_size - small_fifo_size
@@ -27,15 +30,15 @@ def __init__(self,
2730 self .small_fifo = FIFO (small_fifo_size )
2831 self .main_fifo = FIFO (main_fifo_size )
2932 self .ghost_fifo = FIFO (ghost_fifo_size )
30-
33+
3134 # Frequency tracking
3235 self .freq = {}
33-
36+
3437 # Other parameters
3538 self .max_freq = 3
3639 self .move_to_main_threshold = move_to_main_threshold
3740
38- self .has_evicted = False # Mark if we start to evict, only after full we will start eviction
41+ self .has_evicted = False # Mark if we start to evict, only after full we will start eviction
3942 self .hit_on_ghost = False
4043
4144 def cache_hit (self , req : Request ):
@@ -46,7 +49,7 @@ def cache_hit(self, req: Request):
4649
4750 if self .main_fifo .find (req , update_cache = False ):
4851 self .freq [req .obj_id ] += 1
49-
52+
5053 def cache_miss (self , req : Request ):
5154 if not self .hit_on_ghost :
5255 obj = self .ghost_fifo .find (req , update_cache = False )
@@ -56,14 +59,13 @@ def cache_miss(self, req: Request):
5659 self .ghost_fifo .remove (req .obj_id )
5760 self .ghost_set .remove (req .obj_id )
5861
59-
6062 # NOTE(haocheng): first we need to know this miss object has record in ghost or not
6163 if not self .hit_on_ghost :
6264 if req .obj_size >= self .small_fifo .cache_size :
6365 # If object is too large, we do not process it
6466 return
6567
66- # If is initialization state, we need to insert to small fifo,
68+ # If is initialization state, we need to insert to small fifo,
6769 # then we can insert to main fifo
6870 if not self .has_evicted and self .small_fifo .get_occupied_byte () >= self .small_fifo .cache_size :
6971 obj = self .main_fifo .insert (req )
@@ -76,7 +78,7 @@ def cache_miss(self, req: Request):
7678 self .main_set .add (req .obj_id )
7779 self .hit_on_ghost = False
7880 self .freq [obj .obj_id ] = 0
79-
81+
8082 def cache_evict_small (self , req : Request ):
8183 has_evicted = False
8284 evicted_id = None
@@ -100,7 +102,7 @@ def cache_evict_small(self, req: Request):
100102 self .small_set .remove (evicted_id )
101103 assert flag , "Should be able to remove"
102104 return real_evicted_id
103-
105+
104106 def cache_evict_main (self , req : Request ):
105107 has_evicted = False
106108 evicted_id = None
@@ -134,15 +136,15 @@ def cache_evict(self, req: Request):
134136 self .ghost_set .remove (req .obj_id )
135137
136138 self .has_evicted = True
137- cond = ( self .main_fifo .get_occupied_byte () > self .main_fifo .cache_size )
138- if ( cond or (self .small_fifo .get_occupied_byte () == 0 ) ):
139+ cond = self .main_fifo .get_occupied_byte () > self .main_fifo .cache_size
140+ if cond or (self .small_fifo .get_occupied_byte () == 0 ):
139141 obj_id = self .cache_evict_main (req )
140142 else :
141143 obj_id = self .cache_evict_small (req )
142144
143145 if obj_id is not None :
144146 del self .freq [obj_id ]
145-
147+
146148 return obj_id
147149
148150 def cache_remove (self , obj_id ):
@@ -151,28 +153,35 @@ def cache_remove(self, obj_id):
151153 removed |= self .ghost_fifo .remove (obj_id )
152154 removed |= self .main_fifo .remove (obj_id )
153155 return removed
154-
156+
157+
155158def cache_init_hook (common_cache_params : CommonCacheParams ):
156159 return StandaloneS3FIFO (cache_size = common_cache_params .cache_size )
157160
161+
158162def cache_hit_hook (cache , request : Request ):
159163 cache .cache_hit (request )
160164
165+
161166def cache_miss_hook (cache , request : Request ):
162167 cache .cache_miss (request )
163168
169+
164170def cache_eviction_hook (cache , request : Request ):
165171 evicted_id = None
166172 while evicted_id is None :
167173 evicted_id = cache .cache_evict (request )
168174 return evicted_id
169175
176+
170177def cache_remove_hook (cache , obj_id ):
171178 cache .cache_remove (obj_id )
172179
180+
173181def cache_free_hook (cache ):
174182 pass
175183
184+
176185cache = PluginCache (
177186 cache_size = 1024 ,
178187 cache_init_hook = cache_init_hook ,
@@ -181,17 +190,18 @@ def cache_free_hook(cache):
181190 cache_eviction_hook = cache_eviction_hook ,
182191 cache_remove_hook = cache_remove_hook ,
183192 cache_free_hook = cache_free_hook ,
184- cache_name = "S3FIFO" )
193+ cache_name = "S3FIFO" ,
194+ )
185195
186196URI = "cache_dataset_oracleGeneral/2007_msr/msr_hm_0.oracleGeneral.zst"
187197dl = lcs .DataLoader ()
188198dl .load (URI )
189199
190200# Step 2: Open trace and process efficiently
191201reader = lcs .TraceReader (
192- trace = dl .get_cache_path (URI ),
193- trace_type = lcs .TraceType .ORACLE_GENERAL_TRACE ,
194- reader_init_params = lcs .ReaderInitParam (ignore_obj_size = True )
202+ trace = dl .get_cache_path (URI ),
203+ trace_type = lcs .TraceType .ORACLE_GENERAL_TRACE ,
204+ reader_init_params = lcs .ReaderInitParam (ignore_obj_size = True ),
195205)
196206
197207ref_s3fifo = S3FIFO (cache_size = 1024 , small_size_ratio = 0.1 , ghost_size_ratio = 0.9 , move_to_main_threshold = 2 )
@@ -208,4 +218,4 @@ def cache_free_hook(cache):
208218
209219assert req_miss_ratio == ref_req_miss_ratio
210220assert byte_miss_ratio == ref_byte_miss_ratio
211- print ("All requests processed successfully. Plugin cache matches reference S3FIFO cache." )
221+ print ("All requests processed successfully. Plugin cache matches reference S3FIFO cache." )
0 commit comments