Skip to content

Commit 2cf1216

Browse files
committed
Format with ruff
1 parent 2a3e315 commit 2cf1216

File tree

9 files changed

+333
-204
lines changed

9 files changed

+333
-204
lines changed

examples/basic_usage.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@
77

88
# Step 2: Open trace and process efficiently
99
reader = lcs.TraceReader(
10-
trace = dl.get_cache_path(URI),
11-
trace_type = lcs.TraceType.ORACLE_GENERAL_TRACE,
12-
reader_init_params = lcs.ReaderInitParam(ignore_obj_size=False)
10+
trace=dl.get_cache_path(URI),
11+
trace_type=lcs.TraceType.ORACLE_GENERAL_TRACE,
12+
reader_init_params=lcs.ReaderInitParam(ignore_obj_size=False),
1313
)
1414

1515
# Step 3: Initialize cache
16-
cache = lcs.S3FIFO(cache_size=1024*1024)
16+
cache = lcs.S3FIFO(cache_size=1024 * 1024)
1717

1818
# Step 4: Process entire trace efficiently (C++ backend)
1919
obj_miss_ratio, byte_miss_ratio = cache.process_trace(reader)
2020
print(f"Object miss ratio: {obj_miss_ratio:.4f}, Byte miss ratio: {byte_miss_ratio:.4f}")
2121

2222
# Step 4.1: Process with limited number of requests
23-
cache = lcs.S3FIFO(cache_size=1024*1024)
24-
obj_miss_ratio, byte_miss_ratio = cache.process_trace(
25-
reader,
26-
start_req=0,
27-
max_req=1000
28-
)
29-
print(f"Object miss ratio: {obj_miss_ratio:.4f}, Byte miss ratio: {byte_miss_ratio:.4f}")
23+
cache = lcs.S3FIFO(cache_size=1024 * 1024)
24+
obj_miss_ratio, byte_miss_ratio = cache.process_trace(reader, start_req=0, max_req=1000)
25+
print(f"Object miss ratio: {obj_miss_ratio:.4f}, Byte miss ratio: {byte_miss_ratio:.4f}")

examples/plugin_cache/s3fifo.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
from collections import deque
99
from libcachesim import PluginCache, CommonCacheParams, Request, S3FIFO, FIFO, SyntheticReader
1010

11+
1112
# NOTE(haocheng): we only support ignore object size for now
1213
class 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+
155158
def cache_init_hook(common_cache_params: CommonCacheParams):
156159
return StandaloneS3FIFO(cache_size=common_cache_params.cache_size)
157160

161+
158162
def cache_hit_hook(cache, request: Request):
159163
cache.cache_hit(request)
160164

165+
161166
def cache_miss_hook(cache, request: Request):
162167
cache.cache_miss(request)
163168

169+
164170
def 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+
170177
def cache_remove_hook(cache, obj_id):
171178
cache.cache_remove(obj_id)
172179

180+
173181
def cache_free_hook(cache):
174182
pass
175183

184+
176185
cache = 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

186196
URI = "cache_dataset_oracleGeneral/2007_msr/msr_hm_0.oracleGeneral.zst"
187197
dl = lcs.DataLoader()
188198
dl.load(URI)
189199

190200
# Step 2: Open trace and process efficiently
191201
reader = 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

197207
ref_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

209219
assert req_miss_ratio == ref_req_miss_ratio
210220
assert 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.")

examples/trace_analysis.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,27 @@
66
dl.load(URI)
77

88
reader = lcs.TraceReader(
9-
trace = dl.get_cache_path(URI),
10-
trace_type = lcs.TraceType.ORACLE_GENERAL_TRACE,
11-
reader_init_params = lcs.ReaderInitParam(ignore_obj_size=False)
9+
trace=dl.get_cache_path(URI),
10+
trace_type=lcs.TraceType.ORACLE_GENERAL_TRACE,
11+
reader_init_params=lcs.ReaderInitParam(ignore_obj_size=False),
1212
)
1313

1414
analysis_option = lcs.AnalysisOption(
15-
req_rate=True, # Keep basic request rate analysis
16-
access_pattern=False, # Disable access pattern analysis
17-
size=True, # Keep size analysis
18-
reuse=False, # Disable reuse analysis for small datasets
19-
popularity=False, # Disable popularity analysis for small datasets (< 200 objects)
20-
ttl=False, # Disable TTL analysis
21-
popularity_decay=False, # Disable popularity decay analysis
22-
lifetime=False, # Disable lifetime analysis
23-
create_future_reuse_ccdf=False, # Disable experimental features
24-
prob_at_age=False, # Disable experimental features
25-
size_change=False, # Disable size change analysis
26-
)
15+
req_rate=True, # Keep basic request rate analysis
16+
access_pattern=False, # Disable access pattern analysis
17+
size=True, # Keep size analysis
18+
reuse=False, # Disable reuse analysis for small datasets
19+
popularity=False, # Disable popularity analysis for small datasets (< 200 objects)
20+
ttl=False, # Disable TTL analysis
21+
popularity_decay=False, # Disable popularity decay analysis
22+
lifetime=False, # Disable lifetime analysis
23+
create_future_reuse_ccdf=False, # Disable experimental features
24+
prob_at_age=False, # Disable experimental features
25+
size_change=False, # Disable size change analysis
26+
)
2727

2828
analysis_param = lcs.AnalysisParam()
2929

30-
analyzer = lcs.TraceAnalyzer(
31-
reader, "example_analysis", analysis_option=analysis_option, analysis_param=analysis_param
32-
)
30+
analyzer = lcs.TraceAnalyzer(reader, "example_analysis", analysis_option=analysis_option, analysis_param=analysis_param)
3331

34-
analyzer.run()
32+
analyzer.run()

libcachesim/cache.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ def __init__(
284284
def insert(self, req: Request) -> Optional[CacheObject]:
285285
return super().insert(req)
286286

287+
287288
class TwoQ(CacheBase):
288289
"""2Q replacement algorithm
289290
@@ -454,18 +455,24 @@ def __init__(
454455

455456
class LRUProb(CacheBase):
456457
"""LRU with Probabilistic Replacement
457-
458+
458459
Special parameters:
459460
prob: probability of promoting an object to the head of the queue (default: 0.5)
460461
"""
461462

462463
def __init__(
463-
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False,
464+
self,
465+
cache_size: int,
466+
default_ttl: int = 86400 * 300,
467+
hashpower: int = 24,
468+
consider_obj_metadata: bool = False,
464469
prob: float = 0.5,
465470
):
466471
cache_specific_params = f"prob={prob}"
467472
super().__init__(
468-
_cache=LRU_Prob_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params)
473+
_cache=LRU_Prob_init(
474+
_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata), cache_specific_params
475+
)
469476
)
470477

471478

@@ -551,7 +558,9 @@ def __init__(
551558
try:
552559
from .libcachesim_python import ThreeLCache_init
553560
except ImportError:
554-
raise ImportError('ThreeLCache is not installed. Please install it with `CMAKE_ARGS="-DENABLE_3L_CACHE=ON" pip install libcachesim --force-reinstall`')
561+
raise ImportError(
562+
'ThreeLCache is not installed. Please install it with `CMAKE_ARGS="-DENABLE_3L_CACHE=ON" pip install libcachesim --force-reinstall`'
563+
)
555564

556565
cache_specific_params = f"objective={objective}"
557566
super().__init__(
@@ -592,7 +601,9 @@ def __init__(
592601
try:
593602
from .libcachesim_python import GLCache_init
594603
except ImportError:
595-
raise ImportError('GLCache is not installed. Please install it with `CMAKE_ARGS="-DENABLE_GLCACHE=ON" pip install libcachesim --force-reinstall`')
604+
raise ImportError(
605+
'GLCache is not installed. Please install it with `CMAKE_ARGS="-DENABLE_GLCACHE=ON" pip install libcachesim --force-reinstall`'
606+
)
596607

597608
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}"
598609
super().__init__(
@@ -621,7 +632,9 @@ def __init__(
621632
try:
622633
from .libcachesim_python import LRB_init
623634
except ImportError:
624-
raise ImportError('LRB is not installed. Please install it with `CMAKE_ARGS="-DENABLE_LRB=ON" pip install libcachesim --force-reinstall`')
635+
raise ImportError(
636+
'LRB is not installed. Please install it with `CMAKE_ARGS="-DENABLE_LRB=ON" pip install libcachesim --force-reinstall`'
637+
)
625638

626639
cache_specific_params = f"objective={objective}"
627640
super().__init__(

libcachesim/synthetic_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def read_one_req(self) -> Request:
9090
req = Request()
9191
if self.current_pos >= self.num_of_req:
9292
req.valid = False
93-
return req # return invalid request
93+
return req # return invalid request
9494

9595
obj_id = self.obj_ids[self.current_pos]
9696
req.obj_id = obj_id

libcachesim/trace_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def get_num_of_req(self) -> int:
169169

170170
def read_one_req(self) -> Request:
171171
req = Request()
172-
ret = self._reader.read_one_req(req) # return 0 if success
172+
ret = self._reader.read_one_req(req) # return 0 if success
173173
if ret != 0:
174174
raise RuntimeError("Failed to read one request")
175175
return req

0 commit comments

Comments
 (0)