33[ ![ Python Release] ( https://github.com/1a1a11a/libCacheSim/actions/workflows/pypi-release.yml/badge.svg )] ( https://github.com/1a1a11a/libCacheSim/actions/workflows/pypi-release.yml )
44[ ![ Python Versions] ( https://img.shields.io/pypi/pyversions/libcachesim.svg?logo=python&logoColor=white )] ( https://pypi.org/project/libcachesim )
55[ ![ PyPI Version] ( https://img.shields.io/pypi/v/libcachesim.svg? )] ( https://pypi.org/project/libcachesim )
6- ![ PyPI - Downloads] ( https://img.shields.io/pypi/dd/libcachesim )
7-
6+ [ ![ PyPI - Downloads] ( https://img.shields.io/pypi/dd/libcachesim )] ( https://pypistats.org/packages/libcachesim )
87
98Python bindings for libCacheSim, a high-performance cache simulator and analysis library.
109
@@ -63,24 +62,35 @@ print(cache.get(req)) # True (second access)
6362
6463### Trace Processing
6564
65+ To simulate with traces, we need to read the request of traces correctly. ` open_trace ` is an unified interface for trace reading, which accepet three parameters:
66+
67+ - ` trace_path ` : trace path, can be relative or absolutive path.
68+ - ` type ` (optional): if not given, we will automatically infer the type of trace according to the suffix of the trace file.
69+ - ` params ` (optional): if not given, default params are applied.
70+
6671``` python
6772import libcachesim as lcs
6873
6974# Open trace and process efficiently
70- reader = lcs.open_trace(" ./data/cloudPhysicsIO.oracleGeneral.bin" , lcs.TraceType.ORACLE_GENERAL_TRACE )
75+ reader = lcs.open_trace(
76+ trace_path = " ./data/cloudPhysicsIO.oracleGeneral.bin" ,
77+ type = lcs.TraceType.ORACLE_GENERAL_TRACE ,
78+ params = lcs.ReaderInitParam(ignore_obj_size = True )
79+ )
7180cache = lcs.S3FIFO(cache_size = 1024 * 1024 )
7281
7382# Process entire trace efficiently (C++ backend)
74- miss_ratio = cache.process_trace(reader)
75- print (f " Miss ratio: { miss_ratio :.4f } " )
83+ obj_miss_ratio, byte_miss_ratio = cache.process_trace(reader)
84+ print (f " Object miss ratio: { obj_miss_ratio :.4f } , Byte miss ratio: { byte_miss_ratio :.4f } " )
7685
86+ cache = lcs.S3FIFO(cache_size = 1024 * 1024 )
7787# Process with limits and time ranges
78- miss_ratio = cache.process_trace(
88+ obj_miss_ratio, byte_miss_ratio = cache.process_trace(
7989 reader,
80- start_req = 100 ,
90+ start_req = 0 ,
8191 max_req = 1000
8292)
83- print (f " Miss ratio: { miss_ratio :.4f } " )
93+ print (f " Object miss ratio: { obj_miss_ratio :.4f } , Byte miss ratio: { byte_miss_ratio :.4f } " )
8494```
8595
8696## Custom Cache Policies
@@ -147,8 +157,8 @@ print(f"Cache hit: {hit}") # Should be False (miss)
147157``` python
148158import libcachesim as lcs
149159from collections import deque
160+ from contextlib import suppress
150161
151- # Create a custom FIFO cache
152162cache = lcs.PythonHookCachePolicy(cache_size = 1024 , cache_name = " CustomFIFO" )
153163
154164def init_hook (cache_size ):
@@ -164,15 +174,13 @@ def eviction_hook(fifo_queue, obj_id, obj_size):
164174 return fifo_queue[0 ] # Return first item (oldest)
165175
166176def remove_hook (fifo_queue , obj_id ):
167- if fifo_queue and fifo_queue[ 0 ] == obj_id :
168- fifo_queue.popleft( )
177+ with suppress( ValueError ) :
178+ fifo_queue.remove(obj_id )
169179
170180# Set the hooks and test
171181cache.set_hooks(init_hook, hit_hook, miss_hook, eviction_hook, remove_hook)
172182
173- req = lcs.Request()
174- req.obj_id = 1
175- req.obj_size = 100
183+ req = lcs.Request(obj_id = 1 , obj_size = 100 )
176184hit = cache.get(req)
177185print (f " Cache hit: { hit} " ) # Should be False (miss)
178186```
@@ -225,6 +233,7 @@ req = lcs.Request()
225233req.obj_id = 1
226234req.obj_size = 100
227235hit = lru_cache.get(req)
236+ print (hit)
228237```
229238
230239## Examples and Testing
@@ -238,8 +247,8 @@ def compare_algorithms(trace_path):
238247 algorithms = [' LRU' , ' S3FIFO' , ' Sieve' , ' ARC' ]
239248 for algo_name in algorithms:
240249 cache = getattr (lcs, algo_name)(cache_size = 1024 * 1024 )
241- miss_ratio = cache.process_trace(reader)
242- print (f " { algo_name} \t\t { miss_ratio :.4f } " )
250+ obj_miss_ratio, byte_miss_ratio = cache.process_trace(reader)
251+ print (f " { algo_name} \t\t Obj: { obj_miss_ratio :.4f } , Byte: { byte_miss_ratio :.4f } " )
243252
244253compare_algorithms(" ./data/cloudPhysicsIO.vscsi" )
245254```
@@ -299,8 +308,8 @@ caches = [
299308]
300309
301310for i, cache in enumerate (caches):
302- miss_ratio_oracle = cache.process_trace(oracle_reader)
303- miss_ratio_csv = cache.process_trace(csv_reader)
311+ miss_ratio_oracle = cache.process_trace(oracle_reader)[ 0 ]
312+ miss_ratio_csv = cache.process_trace(csv_reader)[ 0 ]
304313 print (f " Cache { i} miss ratio: { miss_ratio_oracle:.4f } , { miss_ratio_csv:.4f } " )
305314```
306315
0 commit comments