-
Notifications
You must be signed in to change notification settings - Fork 1
Bindings for cache admission policies #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0ff840e
export admissioner struct
AlexSutila e389e05
Add bindings for existing admission algorithms
AlexSutila a7ede08
Proof of concept implementation of PluginAdmissioner (still uncertain…
AlexSutila a8910ca
Add custom deleter for plugin admissioner
AlexSutila ef33885
Add examples
AlexSutila 2639d40
Add tests for admission policies
AlexSutila 5879829
Fix type hints and support admissioner argument for all cache impleme…
AlexSutila 7d15f3c
Fix arg naming and typing for admissioner bindings
AlexSutila 5402c7e
Run each admission test with various cache types
AlexSutila 6ca8a64
include documentation for admissioner bindings and PluginAdmissioner
AlexSutila a6e0d57
Fix incorrect argument passing of 'admissioner'
AlexSutila 6746f68
Comment on type for argument for plugin system hook functions
AlexSutila 90de763
Apply suggestion from @gemini-code-assist[bot]
haochengxia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,176 @@ | ||
# Cache Simulation | ||
|
||
[TBD] | ||
## Basic Usage | ||
|
||
The cache classes are the core of cache simulation. When an instance of a cache is creates (e.g., `LRU`, `S3FIFO`), we can configure the cache size and any cache-specific parameters such as promotion thresholds. | ||
|
||
```py | ||
import libcachesim as lcs | ||
|
||
# Initialize cache | ||
cache = lcs.S3FIFO( | ||
cache_size=1024 * 1024, | ||
# Cache specific parameters | ||
small_size_ratio=0.2, | ||
ghost_size_ratio=0.8, | ||
move_to_main_threshold=2, | ||
) | ||
``` | ||
|
||
Admission policies are optional - if none is provided, the cache will simply admit all objects according to the replacement policy. An admissioner (e.g., `BloomFilterAdmissioner`) can be placed infront of the cache by specifying the `admissioner` argument. | ||
|
||
```py | ||
import libcachesim as lcs | ||
|
||
# Initialize admissioner | ||
admissioner = lcs.BloomFilterAdmissioner() | ||
|
||
# Step 2: Initialize cache | ||
cache = lcs.S3FIFO( | ||
cache_size=1024 * 1024, | ||
# Cache specific parameters | ||
small_size_ratio=0.2, | ||
ghost_size_ratio=0.8, | ||
move_to_main_threshold=2, | ||
# Optionally provide admissioner | ||
admissioner=admissioner, | ||
) | ||
``` | ||
|
||
Then we can run cache simulations using real world workloads leveraging trace readers (see [Trace Reader](reader.md) for more on using `TraceReader`): | ||
|
||
```py | ||
# Process entire trace efficiently (C++ backend) | ||
req_miss_ratio, byte_miss_ratio = cache.process_trace(reader) | ||
print(f"Request miss ratio: {req_miss_ratio:.4f}, Byte miss ratio: {byte_miss_ratio:.4f}") | ||
``` | ||
|
||
## Caches | ||
The following cache classes all inherit from `CacheBase` and share a common interface, sharing the following arguments in all cache classes unless otherwise specified: | ||
|
||
- `cache_size: int` | ||
- `default_ttl: int` (optional) | ||
- `hashpower: int` (optional) | ||
- `consider_obj_metadata: bool` (optional) | ||
- `admissioner: AdmissionerBase` (optional) | ||
|
||
### LHD | ||
**Lest Hit Density** evicts objects based on each objects expected hits-per-space-consumed (hit density). | ||
|
||
- *No additional parameters beyond the common arguments* | ||
|
||
### LRU | ||
**Least Recently Used** evicts the object that has not been accessed for the longest time. | ||
|
||
- *No additional parameters beyond the common arguments* | ||
|
||
### FIFO | ||
**First-In, First-Out** evicts objects in order regardless of frequency or recency. | ||
|
||
- *No additional parameters beyond the common arguments* | ||
|
||
### LFU | ||
**Least Frequently Used** evicts the object with the lowest access frequency. | ||
|
||
- *No additional parameters beyond the common arguments* | ||
|
||
### Arc | ||
**Adaptive Replacement Cache** a hybrid algorithm which balances recency and frequency. | ||
|
||
- *No additional parameters beyond the common arguments* | ||
|
||
### Clock | ||
**Clock** is an low-complexity approximation of `LRU`. | ||
|
||
- `int_freq: int` - Initial frequency counter value which is used for new objects (default: `0`) | ||
- `n_bit_counter: int` - Number of bits used for the frequency counter (default: `1`) | ||
|
||
### Random | ||
**Random** evicts objects at random. | ||
|
||
- *No additional parameters beyond the common arguments* | ||
|
||
### S3FIFO | ||
[TBD] | ||
|
||
### Sieve | ||
[TBD] | ||
|
||
### LIRS | ||
[TBD] | ||
|
||
### TwoQ | ||
[TBD] | ||
|
||
### SLRU | ||
[TBD] | ||
|
||
### WTinyLFU | ||
[TBD] | ||
|
||
### LeCaR | ||
[TBD] | ||
|
||
### LFUDA | ||
[TBD] | ||
|
||
### ClockPro | ||
[TBD] | ||
|
||
### Cacheus | ||
[TBD] | ||
|
||
### Belady | ||
[TBD] | ||
|
||
### BeladySize | ||
[TBD] | ||
|
||
### LRUProb | ||
[TBD] | ||
|
||
### FlashProb | ||
[TBD] | ||
|
||
### GDSF | ||
[TBD] | ||
|
||
### Hyperbolic | ||
[TBD] | ||
|
||
### ThreeLCache | ||
[TBD] | ||
|
||
### GLCache | ||
[TBD] | ||
|
||
### LRB | ||
[TBD] | ||
|
||
## Admission Policies | ||
|
||
### BloomFilterAdmissioner | ||
Uses a Bloom filter to decide admissions based on how many times an object has been seen. | ||
|
||
- *No parameters* | ||
|
||
### ProbAdmissioner | ||
Admits objects with a fixed probability. | ||
|
||
- `prob: float` (optional) - Probability of admitting an object (default: `0.5`) | ||
|
||
### SizeAdmissioner | ||
Admits objects only if they are below a specified size threshold. | ||
|
||
- `size_threshold: int` (optional) - Maximum allowed object size (in bytes) for admission (default: `9_223_372_036_854_775_807`, or `INT64_MAX`) | ||
|
||
### SizeProbabilisticAdmissioner | ||
Admits objects with a probability that decreases with object size, favoring smaller objects over large. | ||
|
||
- `exponent: float` (optional) - Exponent controlling how aggressively larger objects are filtered out (default: `1e-6`) | ||
|
||
### AdaptSizeAdmissioner | ||
Implements **AdaptSize**, a feedback-driven policy that periodically adjusts its size threshold. | ||
|
||
- `max_iteration: int` (optional) - Maximum number of iterators for parameter tuning (default: `15`) | ||
- `reconf_interval: int` (optional) - Interval (with respect to request count) at which the threshold is re-evaluated (default: `30_000`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from libcachesim import BloomFilterAdmissioner, SyntheticReader, LRU | ||
|
||
BloomFilter = BloomFilterAdmissioner() | ||
haochengxia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lru_without_admission = LRU( | ||
cache_size=1024, | ||
# admissioner=BloomFilter | ||
) | ||
lru_with_admission = LRU( | ||
cache_size=1024, | ||
admissioner=BloomFilter | ||
) | ||
|
||
reader = SyntheticReader( | ||
num_of_req=100_000, | ||
num_objects=10_000, | ||
obj_size=100, | ||
alpha=0.8, | ||
dist="zipf", | ||
) | ||
|
||
without_admission_hits = 0 | ||
with_admission_hits = 0 | ||
|
||
for req in reader: | ||
if lru_without_admission.get(req): | ||
without_admission_hits += 1 | ||
if lru_with_admission.get(req): | ||
with_admission_hits += 1 | ||
|
||
print(f'Obtained {without_admission_hits} without using cache admission') | ||
print(f'Obtained {with_admission_hits} using cache admission') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from libcachesim import PluginAdmissioner, SyntheticReader, LRU | ||
import random | ||
|
||
''' | ||
A toy example where we admit ten percent of all requests | ||
at random. The admit rate is tracked and printed in the | ||
free hook to serve as a final sanity check. | ||
''' | ||
|
||
|
||
class AdmissionerStats: | ||
admitted_requests: int = 0 | ||
total_requests: int = 0 | ||
|
||
|
||
def init_hook(): | ||
return AdmissionerStats() | ||
|
||
|
||
def admit_hook(data, request): | ||
admit = random.randint(1, 10) == 5 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if admit: | ||
data.admitted_requests += 1 | ||
data.total_requests += 1 | ||
return admit | ||
|
||
|
||
def clone_hook(): | ||
pass | ||
haochengxia marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
|
||
def update_hook(data, request, cs): | ||
pass | ||
|
||
|
||
def free_hook(data): | ||
print(f'Admit rate: {100 * data.admitted_requests / data.total_requests}%') | ||
haochengxia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
custom_admissioner = PluginAdmissioner( | ||
"AdmitTenPercent", | ||
init_hook, | ||
admit_hook, | ||
clone_hook, | ||
update_hook, | ||
free_hook, | ||
) | ||
lru_cache = LRU( | ||
cache_size=1024, | ||
admissioner=custom_admissioner | ||
) | ||
|
||
reader = SyntheticReader( | ||
num_of_req=100_000, | ||
num_objects=10_000, | ||
obj_size=100, | ||
alpha=0.8, | ||
dist="zipf", | ||
) | ||
|
||
for req in reader: | ||
lru_cache.get(req) | ||
|
||
# Invokes free_hook, percentage should be ~10% | ||
del lru_cache |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.