-
Notifications
You must be signed in to change notification settings - Fork 2
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 5 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from libcachesim import BloomFilterAdmissioner, SyntheticReader, LRU | ||
|
|
||
| BloomFilter = BloomFilterAdmissioner() | ||
| 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 | ||
|
Contributor
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
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,88 @@ | ||
| from abc import ABC | ||
| from .libcachesim_python import ( | ||
| Admissioner, | ||
| Request, | ||
| create_bloomfilter_admissioner, | ||
| create_prob_admissioner, | ||
| create_size_admissioner, | ||
| create_size_probabilistic_admissioner, | ||
| create_adaptsize_admissioner, | ||
| create_plugin_admissioner, | ||
| ) | ||
|
|
||
|
|
||
| class AdmissionerBase(ABC): | ||
| _admissioner: Admissioner # Internal C++ admissioner object | ||
|
|
||
| def __init__(self, _admissioner: Admissioner): | ||
| self._admissioner = _admissioner | ||
|
|
||
| def clone(self): | ||
| return self._admissioner.clone() | ||
|
|
||
| def update(self, req: Request, cache_size: int): | ||
| return self._admissioner.update(req, cache_size) | ||
|
|
||
| def admit(self, req: Request): | ||
| return self._admissioner.admit(req) | ||
|
|
||
| def free(self): | ||
| return self._admissioner.free() | ||
|
|
||
|
|
||
| class BloomFilterAdmissioner(AdmissionerBase): | ||
| def __init__(self): | ||
| admissioner = create_bloomfilter_admissioner(None) | ||
| super().__init__(admissioner) | ||
|
|
||
|
|
||
| class ProbAdmissioner(AdmissionerBase): | ||
| def __init__(self, prob: float = None): | ||
haochengxia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| params = f"prob={prob}" if prob is not None else None | ||
| admissioner = create_prob_admissioner(params) | ||
| super().__init__(admissioner) | ||
|
|
||
|
|
||
| class SizeAdmissioner(AdmissionerBase): | ||
| def __init__(self, size_threshold: int = None): | ||
| params = f"size={size_threshold}" if size_threshold is not None else None | ||
| admissioner = create_size_admissioner(params) | ||
| super().__init__(admissioner) | ||
|
|
||
|
|
||
| class SizeProbabilisticAdmissioner(AdmissionerBase): | ||
| def __init__(self, exponent: float = None): | ||
| params = f"exponent={exponent}" if exponent is not None else None | ||
| admissioner = create_size_probabilistic_admissioner(params) | ||
| super().__init__(admissioner) | ||
|
|
||
|
|
||
| class AdaptSizeAdmissioner(AdmissionerBase): | ||
| def __init__(self, max_iteration: int = None, reconf_interval: int = None): | ||
| params = ",".join( | ||
| f'{arg}={val}' for arg, val in { | ||
| 'max-iteration': max_iteration, | ||
| 'reconf-interval': reconf_interval, | ||
| }.items() if val is not None | ||
| ) or None | ||
|
|
||
| admissioner = create_adaptsize_admissioner(params) | ||
| super().__init__(admissioner) | ||
|
|
||
|
|
||
| class PluginAdmissioner(AdmissionerBase): | ||
| def __init__(self, | ||
| admissioner_name, | ||
| admissioner_init_hook, | ||
| admissioner_admit_hook, | ||
| admissioner_clone_hook, | ||
| admissioner_update_hook, | ||
| admissioner_free_hook): | ||
| admissioner = create_plugin_admissioner( | ||
| admissioner_name, | ||
| admissioner_init_hook, | ||
| admissioner_admit_hook, | ||
| admissioner_clone_hook, | ||
| admissioner_update_hook, | ||
| admissioner_free_hook) | ||
| super().__init__(admissioner) | ||
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
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.