Skip to content

Commit ad01277

Browse files
committed
Support LHD
1 parent 36f4bf9 commit ad01277

File tree

9 files changed

+42
-385
lines changed

9 files changed

+42
-385
lines changed

examples/basic_usage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030
move_to_main_threshold=2,
3131
)
3232
req_miss_ratio, byte_miss_ratio = cache.process_trace(reader, start_req=0, max_req=1000)
33-
print(f"Request miss ratio: {req_miss_ratio:.4f}, Byte miss ratio: {byte_miss_ratio:.4f}")
33+
print(f"Request miss ratio: {req_miss_ratio:.4f}, Byte miss ratio: {byte_miss_ratio:.4f}")

libcachesim/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .cache import (
2020
CacheBase,
2121
# Core algorithms
22+
LHD,
2223
LRU,
2324
FIFO,
2425
LFU,
@@ -74,6 +75,7 @@
7475
# Cache base class
7576
"CacheBase",
7677
# Core cache algorithms
78+
"LHD",
7779
"LRU",
7880
"FIFO",
7981
"LFU",

libcachesim/cache.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
CacheObject,
77
Cache,
88
# Core cache algorithms
9+
LHD_init,
910
LRU_init,
1011
FIFO_init,
1112
LFU_init,
@@ -152,6 +153,17 @@ def _create_common_params(
152153
# ------------------------------------------------------------------------------------------------
153154
# Core cache algorithms
154155
# ------------------------------------------------------------------------------------------------
156+
class LHD(CacheBase):
157+
"""Least Hit Density cache (no special parameters)"""
158+
159+
def __init__(
160+
self, cache_size: int, default_ttl: int = 86400 * 300, hashpower: int = 24, consider_obj_metadata: bool = False
161+
):
162+
super().__init__(
163+
_cache=LHD_init(_create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata))
164+
)
165+
166+
155167
class LRU(CacheBase):
156168
"""Least Recently Used cache (no special parameters)"""
157169

scripts/install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ if [ $? -ne 0 ]; then
3636
fi
3737

3838
python scripts/sync_version.py
39-
python -m pip install -e . -vvv
39+
python -m pip3 install -e . -vvv
4040

4141
# Test that the import works
4242
echo "Testing import..."

scripts/smart_build.py

Lines changed: 0 additions & 126 deletions
This file was deleted.

src/export_cache.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,20 @@ void export_cache(py::module& m) {
279279
"req"_a)
280280
.def(
281281
"find",
282-
[](cache_t& self, const request_t& req, const bool update_cache) {
282+
[](cache_t& self, const request_t& req, const bool update_cache) -> py::object {
283283
cache_obj_t* obj = self.find(&self, &req, update_cache);
284-
return obj ? py::cast(obj, py::return_value_policy::reference) : py::none();
284+
// Return None if obj is null (not found)
285+
if (obj == nullptr) {
286+
return py::none();
287+
}
288+
// NOTE(haocheng): For LHD only, return a dummy object for hit
289+
if (obj == reinterpret_cast<cache_obj_t *>(0x1)) {
290+
cache_obj_t* obj = new cache_obj_t();
291+
obj->obj_id = req.obj_id;
292+
obj->obj_size = req.obj_size;
293+
return py::cast(std::unique_ptr<cache_obj_t, CacheObjectDeleter>(obj));
294+
}
295+
return py::cast(obj, py::return_value_policy::reference);
285296
},
286297
"req"_a, "update_cache"_a = true)
287298
.def(

src/libCacheSim

tests/test_cache.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
from libcachesim import (
1111
# Basic algorithms
12+
LHD,
1213
LRU,
1314
FIFO,
1415
LFU,
@@ -71,6 +72,7 @@ class TestCacheBasicFunctionality:
7172
@pytest.mark.parametrize(
7273
"cache_class",
7374
[
75+
LHD,
7476
LRU,
7577
FIFO,
7678
LFU,
@@ -109,10 +111,14 @@ def test_cache_initialization(self, cache_class):
109111
pytest.skip(f"Cache {cache_class.__name__} failed to initialize: {e}")
110112

111113
@pytest.mark.parametrize(
112-
"cache_class", [LRU, FIFO, LFU, ARC, Clock, Random, S3FIFO, Sieve, LIRS, TwoQ, SLRU, WTinyLFU]
114+
"cache_class", [LHD, LRU, FIFO, LFU, ARC, Clock, Random, S3FIFO, Sieve, LIRS, TwoQ, SLRU, WTinyLFU]
113115
)
114116
def test_basic_get_and_insert(self, cache_class):
115117
"""Test basic get and insert operations"""
118+
if cache_class == LHD:
119+
pytest.skip("LHD's insert always returns None")
120+
121+
116122
cache = cache_class(1024 * 1024) # 1MB cache
117123

118124
# Create a request
@@ -141,6 +147,7 @@ def test_basic_get_and_insert(self, cache_class):
141147
@pytest.mark.parametrize(
142148
"cache_class",
143149
[
150+
LHD,
144151
LRU,
145152
FIFO,
146153
LFU,
@@ -192,6 +199,7 @@ def test_cache_eviction(self, cache_class):
192199
@pytest.mark.parametrize(
193200
"cache_class",
194201
[
202+
LHD,
195203
LRU,
196204
FIFO,
197205
LFU,
@@ -239,6 +247,7 @@ def test_cache_find_method(self, cache_class):
239247
@pytest.mark.parametrize(
240248
"cache_class",
241249
[
250+
LHD,
242251
LRU,
243252
FIFO,
244253
LFU,

0 commit comments

Comments
 (0)