Skip to content

Commit fffa5db

Browse files
committed
Use scikit build
1 parent 9406e05 commit fffa5db

23 files changed

Lines changed: 1783 additions & 17 deletions

.github/workflows/python.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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: Build main libCacheSim project
20+
run: |
21+
cmake -G Ninja -B build
22+
ninja -C build
23+
24+
- name: Install Python dependencies
25+
run: |
26+
pip install --upgrade pip
27+
pip install -r requirements.txt
28+
pip install pytest
29+
30+
- name: Build libCacheSim-python
31+
run: |
32+
cd libCacheSim-python
33+
pip install -e .
34+
35+
- name: Run tests
36+
run: |
37+
cd libCacheSim-python
38+
pytest tests/

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ else()
247247
message(STATUS "Building without test")
248248
endif()
249249

250+
# Export variables for scikit-build -> build/export_vars.cmake
251+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libCacheSim-python/export)
250252

251253
# libCacheSim unified library compilation and installation
252254
# Create a single library that combines all modular libraries

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: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
cmake_minimum_required(VERSION 3.15...3.27)
2+
3+
# Include exported variables from cache
4+
set(PARENT_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../build")
5+
set(EXPORT_FILE "${PARENT_BUILD_DIR}/export_vars.cmake")
6+
7+
if(EXISTS "${EXPORT_FILE}")
8+
include("${EXPORT_FILE}")
9+
message(STATUS "Loaded variables from export_vars.cmake")
10+
else()
11+
message(FATAL_ERROR "export_vars.cmake not found at ${EXPORT_FILE}. Please build the main project first (e.g. cd .. && cmake -G Ninja -B build)")
12+
endif()
13+
14+
# Force enable -fPIC
15+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
16+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
17+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
18+
19+
project(libCacheSim-python VERSION "${LIBCACHESIM_VERSION}")
20+
21+
if(LOG_LEVEL_LOWER STREQUAL "default")
22+
if(CMAKE_BUILD_TYPE_LOWER MATCHES "debug")
23+
add_compile_definitions(LOGLEVEL=6)
24+
else()
25+
add_compile_definitions(LOGLEVEL=7)
26+
endif()
27+
elseif(LOG_LEVEL_LOWER STREQUAL "verbose")
28+
add_compile_definitions(LOGLEVEL=5)
29+
elseif(LOG_LEVEL_LOWER STREQUAL "debug")
30+
add_compile_definitions(LOGLEVEL=6)
31+
elseif(LOG_LEVEL_LOWER STREQUAL "info")
32+
add_compile_definitions(LOGLEVEL=7)
33+
elseif(LOG_LEVEL_LOWER STREQUAL "warn")
34+
add_compile_definitions(LOGLEVEL=8)
35+
elseif(LOG_LEVEL_LOWER STREQUAL "error")
36+
add_compile_definitions(LOGLEVEL=9)
37+
else()
38+
add_compile_definitions(LOGLEVEL=7)
39+
endif()
40+
41+
# Find python and pybind11
42+
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
43+
find_package(pybind11 CONFIG REQUIRED)
44+
45+
# Include directories for dependencies
46+
include_directories(${GLib_INCLUDE_DIRS})
47+
include_directories(${GLib_CONFIG_INCLUDE_DIR})
48+
include_directories(${XGBOOST_INCLUDE_DIR})
49+
include_directories(${LIGHTGBM_PATH})
50+
include_directories(${ZSTD_INCLUDE_DIR})
51+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/bin)
52+
53+
# Find the main libCacheSim library
54+
set(MAIN_PROJECT_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../build")
55+
set(MAIN_PROJECT_LIB_PATH "${MAIN_PROJECT_BUILD_DIR}/liblibCacheSim.a")
56+
57+
if(EXISTS "${MAIN_PROJECT_LIB_PATH}")
58+
message(STATUS "Found pre-built libCacheSim library at ${MAIN_PROJECT_LIB_PATH}")
59+
60+
# Import the main library as an imported target
61+
add_library(libCacheSim_main STATIC IMPORTED)
62+
set_target_properties(libCacheSim_main PROPERTIES
63+
IMPORTED_LOCATION "${MAIN_PROJECT_LIB_PATH}"
64+
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/include;${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/utils/include;${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim"
65+
)
66+
67+
# Link dependencies that the main library needs
68+
target_link_libraries(libCacheSim_main INTERFACE ${dependency_libs})
69+
set(LIBCACHESIM_TARGET libCacheSim_main)
70+
71+
else()
72+
message(FATAL_ERROR "Pre-built libCacheSim library not found. Please build the main project first: cd .. && cmake -G Ninja -B build && ninja -C build")
73+
endif()
74+
75+
python_add_library(_libcachesim MODULE
76+
src/pylibcachesim.cpp
77+
${CMAKE_CURRENT_SOURCE_DIR}/../libCacheSim/bin/cli_reader_utils.c
78+
WITH_SOABI
79+
)
80+
81+
set_target_properties(_libcachesim PROPERTIES
82+
POSITION_INDEPENDENT_CODE ON
83+
INSTALL_RPATH_USE_LINK_PATH TRUE
84+
BUILD_WITH_INSTALL_RPATH TRUE
85+
INSTALL_RPATH "$ORIGIN"
86+
)
87+
88+
target_compile_definitions(_libcachesim PRIVATE VERSION_INFO=${PROJECT_VERSION})
89+
90+
target_link_libraries(_libcachesim PRIVATE
91+
${LIBCACHESIM_TARGET}
92+
pybind11::headers
93+
pybind11::module
94+
-Wl,--no-as-needed -ldl
95+
)
96+
97+
# install to wheel directory
98+
install(TARGETS _libcachesim LIBRARY DESTINATION libcachesim)

libCacheSim-python/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
Test
18+
19+
```
20+
python -m pytest .
21+
```
22+
23+
## Usage
24+
25+
```python
26+
import libcachesim as cachesim
27+
28+
# Create a cache with FIFO eviction policy
29+
cache = cachesim.FIFO(cache_size=1024*1024)
30+
31+
# Create a request
32+
req = cachesim.Request()
33+
req.obj_id = 1
34+
req.obj_size = 100
35+
36+
# Check if object is in cache
37+
hit = cache.get(req)
38+
print(f"Cache hit: {hit}")
39+
```
40+
41+
## Features
42+
43+
- [x] Support for multiple eviction policies (FIFO, LRU, ARC, Clock, etc.)
44+
- [ ] trace analysis tools
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Helper functions are removed since we don't export source files anymore
2+
3+
set(EXPORT_FILE "${CMAKE_BINARY_DIR}/export_vars.cmake")
4+
file(WRITE "${EXPORT_FILE}" "")
5+
6+
# ==============================================================================
7+
# Export project metadata
8+
# ==============================================================================
9+
file(APPEND "${EXPORT_FILE}" "set(LIBCACHESIM_VERSION \"${${PROJECT_NAME}_VERSION}\")\n")
10+
11+
# ==============================================================================
12+
# Export essential include directory variables
13+
# ==============================================================================
14+
foreach(var IN ITEMS GLib_INCLUDE_DIRS GLib_CONFIG_INCLUDE_DIR XGBOOST_INCLUDE_DIR LIGHTGBM_PATH ZSTD_INCLUDE_DIR)
15+
file(APPEND "${EXPORT_FILE}" "set(${var} \"${${var}}\")\n")
16+
endforeach()
17+
18+
# ==============================================================================
19+
# Export dependency library variables
20+
# ==============================================================================
21+
file(APPEND "${EXPORT_FILE}" "set(dependency_libs \"${dependency_libs}\")\n")
22+
23+
# ==============================================================================
24+
# Export essential build option variables
25+
# ==============================================================================
26+
file(APPEND "${EXPORT_FILE}" "set(LOG_LEVEL_LOWER \"${LOG_LEVEL_LOWER}\")\n")
27+
28+
message(STATUS "Exported essential variables to ${EXPORT_FILE}")
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# libCacheSim Python Binding Export
2+
3+
This directory contains the export mechanism for sharing variables between the main libCacheSim project and the Python binding.
4+
5+
## Overview
6+
7+
The `export/CMakeLists.txt` file serves as a bridge between the main libCacheSim project and the Python binding, ensuring that all necessary variables (source files, include directories, compiler flags, etc.) are properly exported and can be imported by the Python binding's CMakeLists.txt.
8+
9+
## How It Works
10+
11+
### 1. Variable Export Process
12+
13+
The export mechanism works in the following steps:
14+
15+
1. **Path Conversion**: Converts relative source file paths to absolute paths using the `convert_to_absolute_paths` function
16+
2. **Variable Collection**: Gathers all necessary variables from the main project
17+
3. **File Generation**: Writes all variables to `export_vars.cmake` in the build directory
18+
4. **Import**: The Python binding's CMakeLists.txt includes this file to access all variables
19+
20+
### 2. Exported Variables
21+
22+
The following categories of variables are exported:
23+
24+
#### Source Files
25+
- `ABS_cache_sources` - Cache-related source files
26+
- `ABS_dataStructure_sources` - Data structure source files
27+
- `ABS_traceReader_sources` - Trace reader source files
28+
- `ABS_profiler_sources` - Profiler source files
29+
- `ABS_utils_sources` - Utility source files
30+
- `ABS_traceAnalyzer_sources` - Trace analyzer source files
31+
- `ABS_mrcProfiler_sources` - MRC profiler source files
32+
33+
#### Project Metadata
34+
- `LIBCACHESIM_VERSION` - Version information
35+
36+
#### Include Directories
37+
- `libCacheSim_include_dir` - Main include directory
38+
- `libCacheSim_binary_include_dir` - Binary include directory
39+
- `GLib_INCLUDE_DIRS` - GLib include directories
40+
- `XGBOOST_INCLUDE_DIR` - XGBoost include directory
41+
- `LIGHTGBM_PATH` - LightGBM include directory
42+
- `ZSTD_INCLUDE_DIR` - ZSTD include directory
43+
44+
#### Dependencies
45+
- `dependency_libs` - Dependency libraries
46+
47+
#### Compiler Flags
48+
- `LIBCACHESIM_C_FLAGS` - C compiler flags
49+
- `LIBCACHESIM_CXX_FLAGS` - C++ compiler flags
50+
51+
#### Build Options
52+
- `USE_HUGEPAGE` - Hugepage usage
53+
- `ENABLE_TESTS` - Test enablement
54+
- `ENABLE_GLCACHE` - GLCache enablement
55+
- `SUPPORT_TTL` - TTL support
56+
- `OPT_SUPPORT_ZSTD_TRACE` - ZSTD trace support
57+
- `ENABLE_LRB` - LRB enablement
58+
- `ENABLE_3L_CACHE` - 3L Cache enablement
59+
- `LOG_LEVEL_LOWER` - Log level
60+
61+
## Usage
62+
63+
### In Main Project
64+
65+
The main project's CMakeLists.txt includes this export directory:
66+
67+
```cmake
68+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libCacheSim-python/export)
69+
```
70+
71+
### In Python Binding
72+
73+
The Python binding's CMakeLists.txt imports the exported variables:
74+
75+
```cmake
76+
set(PARENT_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../build")
77+
set(EXPORT_FILE "${PARENT_BUILD_DIR}/export_vars.cmake")
78+
79+
if(EXISTS "${EXPORT_FILE}")
80+
include("${EXPORT_FILE}")
81+
message(STATUS "Loaded variables from export_vars.cmake")
82+
else()
83+
message(FATAL_ERROR "export_vars.cmake not found")
84+
endif()
85+
```

0 commit comments

Comments
 (0)