Skip to content

Commit dbacecb

Browse files
committed
Add simluate api
1 parent 32b536f commit dbacecb

24 files changed

Lines changed: 1694 additions & 106 deletions

.github/workflows/python.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Python
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
11+
- name: Set up Python
12+
uses: actions/setup-python@v4
13+
with:
14+
python-version: "3.10"
15+
16+
- name: Prepare
17+
run: bash scripts/install_dependency.sh
18+
19+
- name: Install XGBoost and LightGBM
20+
run: |
21+
pip install xgboost lightgbm
22+
# Set environment variables to help CMake find the libraries
23+
echo "XGBOOST_ROOT=$(python -c 'import xgboost; print(xgboost.__file__)')" >> $GITHUB_ENV
24+
echo "LIGHTGBM_ROOT=$(python -c 'import lightgbm; print(lightgbm.__file__)')" >> $GITHUB_ENV
25+
26+
- name: Install Python dependencies
27+
run: |
28+
pip install --upgrade pip
29+
pip install -r requirements.txt
30+
pip install pytest
31+
32+
- name: Build libCacheSim-python
33+
run: |
34+
cd libCacheSim-python
35+
pip install -e .
36+
37+
- name: Run tests
38+
run: |
39+
cd libCacheSim-python
40+
pytest tests/

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,8 @@ configure_file(${PROJECT_SOURCE_DIR}/${PROJECT_NAME}.cmake.in ${CMAKE_CURRENT_BI
363363
# this overwrites the default config.h
364364
# configure_file(${PROJECT_SOURCE_DIR}/libCacheSim/include/config.h.in ${PROJECT_SOURCE_DIR}/libCacheSim/include/config.h @ONLY)
365365
install(DIRECTORY ${PROJECT_SOURCE_DIR}/libCacheSim/include/ DESTINATION include)
366-
install(FILES ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc DESTINATION lib/pkgconfig)
367-
install(FILES ${CMAKE_BINARY_DIR}/Find${PROJECT_NAME}.cmake DESTINATION ${CMAKE_ROOT}/Modules/ COMPONENT dev)
366+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc DESTINATION lib/pkgconfig)
367+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/Find${PROJECT_NAME}.cmake DESTINATION ${CMAKE_ROOT}/Modules/ COMPONENT dev)
368368

369369
install(TARGETS ${PROJECT_NAME}
370370
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}

libCacheSim-python/.gitignore

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Automatically generated by `hgimportsvn`
2+
.svn
3+
.hgsvn
4+
5+
# Ignore local virtualenvs
6+
lib/
7+
bin/
8+
include/
9+
.Python/
10+
11+
# These lines are suggested according to the svn:ignore property
12+
# Feel free to enable them by uncommenting them
13+
*.pyc
14+
*.pyo
15+
*.swp
16+
*.class
17+
*.orig
18+
*~
19+
.hypothesis/
20+
21+
# autogenerated
22+
src/_pytest/_version.py
23+
# setuptools
24+
.eggs/
25+
26+
doc/*/_build
27+
doc/*/.doctrees
28+
build/
29+
dist/
30+
*.egg-info
31+
htmlcov/
32+
issue/
33+
env/
34+
.env/
35+
.venv/
36+
/pythonenv*/
37+
3rdparty/
38+
.tox
39+
.cache
40+
.pytest_cache
41+
.mypy_cache
42+
.coverage
43+
.coverage.*
44+
coverage.xml
45+
.ropeproject
46+
.idea
47+
.hypothesis
48+
.pydevproject
49+
.project
50+
.settings
51+
.vscode
52+
__pycache__/
53+
.python-version
54+
55+
# generated by pip
56+
pip-wheel-metadata/
57+
58+
# pytest debug logs generated via --debug
59+
pytestdebug.log

libCacheSim-python/CMakeLists.txt

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# Modified from https://github.com/pybind/scikit_build_example/blob/master/CMakeLists.txt
2+
cmake_minimum_required(VERSION 3.15...3.27)
3+
4+
set(CMAKE_C_COMPILER "gcc")
5+
set(CMAKE_CXX_COMPILER "g++")
6+
set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
7+
set(CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
8+
# enable -fPIC
9+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
10+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
11+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
12+
13+
project(
14+
${SKBUILD_PROJECT_NAME}
15+
VERSION ${SKBUILD_PROJECT_VERSION}
16+
LANGUAGES C CXX)
17+
18+
# use customized cmake module
19+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/../cmake/Modules/")
20+
21+
# set the logging level to DEBUG by default
22+
set(LOG_LEVEL DEBUG CACHE STRING "change the logging level")
23+
24+
string(TOLOWER "${LOG_LEVEL}" LOG_LEVEL_LOWER)
25+
26+
if(LOG_LEVEL_LOWER STREQUAL "vvverbose")
27+
add_compile_definitions(LOGLEVEL=3)
28+
elseif(LOG_LEVEL_LOWER STREQUAL "vverbose")
29+
add_compile_definitions(LOGLEVEL=4)
30+
elseif(LOG_LEVEL_LOWER STREQUAL "verbose")
31+
add_compile_definitions(LOGLEVEL=5)
32+
elseif(LOG_LEVEL_LOWER STREQUAL "debug")
33+
add_compile_definitions(LOGLEVEL=6)
34+
elseif(LOG_LEVEL_LOWER STREQUAL "info")
35+
add_compile_definitions(LOGLEVEL=7)
36+
elseif(LOG_LEVEL_LOWER STREQUAL "warn")
37+
add_compile_definitions(LOGLEVEL=8)
38+
elseif(LOG_LEVEL_LOWER STREQUAL "error")
39+
add_compile_definitions(LOGLEVEL=9)
40+
41+
# default none is info
42+
elseif(LOG_LEVEL_LOWER STREQUAL "none")
43+
add_compile_definitions(LOGLEVEL=7)
44+
else()
45+
message(WARNING "unknown log level ${LOG_LEVEL}, use INFO as default")
46+
add_compile_definitions(LOGLEVEL=7)
47+
endif()
48+
49+
option(ENABLE_LRB "enable LRB" ON)
50+
option(ENABLE_GLCACHE "enable GLCache" ON)
51+
option(ENABLE_3L_CACHE "enable 3LCache" ON)
52+
add_compile_definitions(ENABLE_GLCACHE=1)
53+
add_compile_definitions(ENABLE_LRB=1)
54+
add_compile_definitions(ENABLE_3L_CACHE=1)
55+
56+
# find python and pybind11
57+
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
58+
find_package(pybind11 CONFIG REQUIRED)
59+
60+
# find glib
61+
find_package(PkgConfig REQUIRED)
62+
pkg_check_modules(GLIB2 REQUIRED glib-2.0)
63+
64+
# ------------------------------------------------------------
65+
include_directories(
66+
${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/include
67+
${GLIB2_INCLUDE_DIRS}
68+
)
69+
70+
file(GLOB UTILS_SOURCES
71+
"${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/utils/*.c"
72+
)
73+
74+
add_library(utils STATIC ${UTILS_SOURCES})
75+
set_target_properties(utils PROPERTIES
76+
LANGUAGE C
77+
POSITION_INDEPENDENT_CODE ON
78+
)
79+
target_compile_options(utils PRIVATE -fPIC)
80+
target_link_libraries(utils PRIVATE ${GLIB2_LIBRARIES})
81+
82+
file(GLOB_RECURSE CACHE_SOURCES
83+
"${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/cache/*.c"
84+
"${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/cache/eviction/*.c"
85+
"${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/cache/admission/*.c"
86+
"${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/cache/prefetch/*.c"
87+
)
88+
89+
# Try to find XGBoost and LightGBM (optional)
90+
find_package(xgboost QUIET)
91+
if(XGBOOST_FOUND)
92+
message(STATUS "XGBoost found, enabling GLCache")
93+
include_directories(${XGBOOST_INCLUDE_DIR})
94+
set(XGBOOST_LIBRARIES xgboost::xgboost)
95+
add_compile_definitions(ENABLE_XGBOOST=1)
96+
else()
97+
message(WARNING "XGBoost not found, disabling GLCache")
98+
add_compile_definitions(ENABLE_XGBOOST=0)
99+
set(XGBOOST_LIBRARIES "")
100+
endif()
101+
102+
find_path(LIGHTGBM_PATH LightGBM QUIET)
103+
find_library(LIGHTGBM_LIB _lightgbm QUIET)
104+
if(LIGHTGBM_PATH AND LIGHTGBM_LIB)
105+
message(STATUS "LightGBM found, enabling LRB and 3LCache")
106+
include_directories(${LIGHTGBM_PATH})
107+
set(LIGHTGBM_LIBRARIES ${LIGHTGBM_LIB})
108+
add_compile_definitions(ENABLE_LIGHTGBM=1)
109+
else()
110+
message(WARNING "LightGBM not found, disabling LRB and 3LCache")
111+
add_compile_definitions(ENABLE_LIGHTGBM=0)
112+
set(LIGHTGBM_LIBRARIES "")
113+
endif()
114+
115+
# Only include sources if dependencies are available
116+
if(ENABLE_XGBOOST)
117+
file(GLOB_RECURSE LRB_SOURCES
118+
"${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/cache/eviction/LRB/*.cpp"
119+
)
120+
file(GLOB_RECURSE 3L_CACHE_SOURCES
121+
"${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/cache/eviction/3LCache/*.cpp"
122+
)
123+
else()
124+
set(LRB_SOURCES "")
125+
set(3L_CACHE_SOURCES "")
126+
endif()
127+
128+
file(GLOB_RECURSE CACHE_CPP_SOURCES
129+
"${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/cache/eviction/LHD/*"
130+
"${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/cache/eviction/cpp/*"
131+
)
132+
133+
file(GLOB_RECURSE DS_SOURCES
134+
"${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/dataStructure/*.c"
135+
)
136+
137+
file(GLOB_RECURSE READER_SOURCES
138+
"${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/traceReader/*.c"
139+
)
140+
141+
# ------------------------------------------------------------
142+
143+
add_library(cachesim_c STATIC
144+
${UTILS_SOURCES}
145+
${CACHE_SOURCES}
146+
${LRB_SOURCES}
147+
${3L_CACHE_SOURCES}
148+
${DS_SOURCES}
149+
${READER_SOURCES}
150+
)
151+
152+
set_target_properties(cachesim_c PROPERTIES
153+
LANGUAGE C
154+
POSITION_INDEPENDENT_CODE ON
155+
)
156+
157+
target_compile_options(cachesim_c PRIVATE -fPIC)
158+
target_link_libraries(cachesim_c PRIVATE ${GLIB2_LIBRARIES})
159+
160+
add_library(cachesim_cpp STATIC ${CACHE_CPP_SOURCES})
161+
set_target_properties(cachesim_cpp PROPERTIES
162+
LANGUAGE CXX
163+
POSITION_INDEPENDENT_CODE ON
164+
)
165+
target_compile_options(cachesim_cpp PRIVATE -fPIC)
166+
target_link_libraries(cachesim_cpp PRIVATE ${GLIB2_LIBRARIES})
167+
168+
# create python module
169+
python_add_library(_libcachesim MODULE
170+
src/pylibcachesim.cpp
171+
WITH_SOABI
172+
)
173+
174+
set_target_properties(_libcachesim PROPERTIES
175+
POSITION_INDEPENDENT_CODE ON
176+
INSTALL_RPATH_USE_LINK_PATH TRUE
177+
BUILD_WITH_INSTALL_RPATH TRUE
178+
INSTALL_RPATH "$ORIGIN"
179+
)
180+
181+
target_compile_options(_libcachesim PRIVATE -fPIC)
182+
target_compile_definitions(_libcachesim PRIVATE VERSION_INFO=${PROJECT_VERSION})
183+
184+
target_link_libraries(_libcachesim PRIVATE
185+
cachesim_c
186+
cachesim_cpp
187+
pybind11::headers
188+
${GLIB2_LIBRARIES}
189+
${XGBOOST_LIBRARIES}
190+
${LIGHTGBM_LIBRARIES}
191+
-Wl,--no-as-needed -ldl
192+
)
193+
194+
# install to wheel directory
195+
install(TARGETS _libcachesim cachesim_c
196+
LIBRARY DESTINATION libcachesim
197+
)

libCacheSim-python/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# libCacheSim Python Binding
2+
3+
Python bindings for libCacheSim, a high-performance cache simulator.
4+
5+
## Installation
6+
7+
```bash
8+
pip install .
9+
```
10+
11+
## Development
12+
13+
```bash
14+
pip install -e .
15+
```
16+
17+
## Usage
18+
19+
```python
20+
import libcachesim as cachesim
21+
22+
# Create a cache with FIFO eviction policy
23+
cache = cachesim.FIFO(cache_size=1024*1024)
24+
25+
# Create a request
26+
req = cachesim.Request()
27+
req.obj_id = 1
28+
req.obj_size = 100
29+
30+
# Check if object is in cache
31+
hit = cache.get(req)
32+
print(f"Cache hit: {hit}")
33+
```
34+
35+
## Features
36+
37+
- [x] Support for multiple eviction policies (FIFO, LRU, ARC, Clock, etc.)
38+
- [ ] trace analysis tools
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from __future__ import annotations
2+
3+
from ._libcachesim import (
4+
Cache,
5+
Reader,
6+
Request,
7+
__doc__,
8+
__version__,
9+
create_cache,
10+
open_trace,
11+
)
12+
from .const import TraceType
13+
from .eviction import (
14+
ARC,
15+
FIFO,
16+
LRB,
17+
LRU,
18+
S3FIFO,
19+
Clock,
20+
Sieve,
21+
ThreeLCache,
22+
TinyLFU,
23+
TwoQ,
24+
)
25+
26+
__all__ = [
27+
"ARC",
28+
"FIFO",
29+
"LRB",
30+
"LRU",
31+
"S3FIFO",
32+
"Cache",
33+
"Clock",
34+
"Reader",
35+
"Request",
36+
"Sieve",
37+
"ThreeLCache",
38+
"TinyLFU",
39+
"TraceType",
40+
"TwoQ",
41+
"__doc__",
42+
"__version__",
43+
"create_cache",
44+
"open_trace",
45+
# TODO(haocheng): add more eviction policies
46+
]

0 commit comments

Comments
 (0)