|
| 1 | +# libCacheSim-python |
| 2 | + |
| 3 | +libCacheSim-python is a high-performance Python binding for the libCacheSim library, providing cache simulation and analysis capabilities. It uses CMake, pybind11, and scikit-build-core for building Python extensions around a C++ core library. |
| 4 | + |
| 5 | +Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here. |
| 6 | + |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +### Prerequisites and System Dependencies |
| 10 | +- Install system dependencies: `sudo bash src/libCacheSim/scripts/install_dependency.sh` -- takes 66 seconds. NEVER CANCEL. |
| 11 | +- Install Python build dependencies: `sudo apt install -y python3-numpy python3-pytest pybind11-dev` |
| 12 | +- **CRITICAL**: Git submodules are required: `git submodule update --init --recursive` -- takes 2.4 seconds. |
| 13 | + |
| 14 | +### Build Process (Manual CMake - RECOMMENDED) |
| 15 | +**NEVER CANCEL builds** - CMake configuration + build takes 20-35 seconds total. Set timeout to 120+ seconds. |
| 16 | + |
| 17 | +1. **Standard build (Debug)**: |
| 18 | + ```bash |
| 19 | + mkdir build && cd build |
| 20 | + cmake .. -G Ninja |
| 21 | + ninja |
| 22 | + ``` |
| 23 | + - Configuration: ~2.4 seconds |
| 24 | + - Build: ~3 seconds |
| 25 | + - Total: ~6 seconds |
| 26 | + |
| 27 | +2. **Optimized build (Release)**: |
| 28 | + ```bash |
| 29 | + mkdir build && cd build |
| 30 | + cmake .. -G Ninja -DCMAKE_BUILD_TYPE=Release |
| 31 | + ninja |
| 32 | + ``` |
| 33 | + - Configuration: ~17 seconds (includes libCacheSim build with 175 targets) |
| 34 | + - Build: ~18 seconds (includes LTO optimization) |
| 35 | + - Total: ~35 seconds |
| 36 | + |
| 37 | +3. **Test the build**: |
| 38 | + ```bash |
| 39 | + export PYTHONPATH=$(pwd)/build:$PYTHONPATH |
| 40 | + python3 -c "import libcachesim_python as lcs; print('✓ Import successful')" |
| 41 | + ``` |
| 42 | + |
| 43 | +### Python Package Installation (pip install -e .) |
| 44 | +**WARNING**: pip install may experience network timeouts in CI environments. This is NORMAL. |
| 45 | +- If `pip install -e .` fails with network timeouts, use the manual CMake build approach above. |
| 46 | +- pip installation typically takes 30-60 seconds when network is stable. |
| 47 | +- Use longer timeouts (120+ seconds) and retry if needed. |
| 48 | + |
| 49 | +### Testing |
| 50 | +- **Manual validation** (always works): |
| 51 | + ```bash |
| 52 | + export PYTHONPATH=$(pwd)/build:$PYTHONPATH |
| 53 | + python3 -c " |
| 54 | + import libcachesim_python as lcs |
| 55 | + common_params = lcs.CommonCacheParams(cache_size=1024) |
| 56 | + cache = lcs.LRU_init(common_params) |
| 57 | + req = lcs.Request() |
| 58 | + req.obj_id = 1 |
| 59 | + req.obj_size = 10 |
| 60 | + req.op = lcs.OP_GET |
| 61 | + cache.get(req) |
| 62 | + print(f'Cache has {cache.get_n_obj()} objects') |
| 63 | + " |
| 64 | + ``` |
| 65 | + |
| 66 | +- **pytest** (if available): |
| 67 | + ```bash |
| 68 | + python3 -m pytest tests/ -v -m "not optional" |
| 69 | + ``` |
| 70 | + - NOTE: System pytest is version 7.4.4, but pyproject.toml requires 8.0+ |
| 71 | + - Use manual validation instead if pytest version conflicts occur |
| 72 | + |
| 73 | +### Performance Validation |
| 74 | +- **Debug build**: ~65,000 requests/second |
| 75 | +- **Release build**: ~571,000 requests/second |
| 76 | +- Test with: `python3 -c "import time; import libcachesim_python as lcs; start=time.time(); common_params=lcs.CommonCacheParams(cache_size=10000); cache=lcs.LRU_init(common_params); [cache.get(lcs.Request(obj_id=i%1000, obj_size=10, op=lcs.OP_GET)) for i in range(100000)]; print(f'Rate: {100000/(time.time()-start):.0f} req/s')"` |
| 77 | + |
| 78 | +## Critical Build Issues & Solutions |
| 79 | + |
| 80 | +### Network Timeouts |
| 81 | +**COMMON**: pip install may timeout due to PyPI network issues. |
| 82 | +- **Solution**: Use manual CMake build instead of pip install |
| 83 | +- **NOT a code issue** - this is an infrastructure limitation |
| 84 | + |
| 85 | +### pybind11 Compatibility |
| 86 | +**FIXED**: If you see `py::set_error` compilation errors: |
| 87 | +- This was already fixed in src/exception.cpp (lines 24, 26) |
| 88 | +- The fix changed `py::set_error(exc, msg)` to `exc(msg)` for pybind11 2.10+ compatibility |
| 89 | + |
| 90 | +### Optional Features (Advanced Algorithms) |
| 91 | +Optional cache algorithms (LRB, GLCache, 3LCache) require additional dependencies: |
| 92 | +- **XGBoost** and **LightGBM** libraries |
| 93 | +- Install with: `bash scripts/install_deps.sh` or `bash scripts/install_deps_user.sh` |
| 94 | +- Build with: `cmake -DENABLE_LRB=ON -DENABLE_GLCACHE=ON -DENABLE_3L_CACHE=ON` |
| 95 | +- **Skip optional features** for basic development - they are not required |
| 96 | + |
| 97 | +## Development Workflow |
| 98 | + |
| 99 | +### Standard Development Process |
| 100 | +1. **Initialize**: `git submodule update --init --recursive` |
| 101 | +2. **Install system deps**: `sudo bash src/libCacheSim/scripts/install_dependency.sh` |
| 102 | +3. **Build**: `mkdir build && cd build && cmake .. -G Ninja && ninja` |
| 103 | +4. **Test**: `export PYTHONPATH=$(pwd)/build:$PYTHONPATH && python3 -c "import libcachesim_python"` |
| 104 | +5. **Validate changes**: Run manual performance test |
| 105 | + |
| 106 | +### After Making Code Changes |
| 107 | +1. **Rebuild**: `ninja` (from build directory) -- takes 2-3 seconds |
| 108 | +2. **Test import**: `python3 -c "import libcachesim_python"` |
| 109 | +3. **Run validation scenarios**: Test cache algorithms manually |
| 110 | +4. **Performance check**: Verify >500k requests/second in Release mode |
| 111 | + |
| 112 | +### Common Validation Scenarios |
| 113 | +Always test these scenarios after making changes: |
| 114 | +1. **Basic cache creation**: LRU, FIFO, LFU, ARC, S3FIFO |
| 115 | +2. **Request processing**: Insert/get operations with different object sizes |
| 116 | +3. **Performance**: Ensure >500k requests/second (Release build) |
| 117 | +4. **Memory usage**: Verify reasonable memory consumption |
| 118 | + |
| 119 | +## Code Navigation |
| 120 | + |
| 121 | +### Key Directories |
| 122 | +- `src/`: Python binding source code (C++) |
| 123 | +- `src/libCacheSim/`: Git submodule containing the core C++ library |
| 124 | +- `tests/`: Python test suite |
| 125 | +- `examples/`: Usage examples |
| 126 | +- `benchmark/`: Performance benchmarks |
| 127 | +- `scripts/`: Build and installation scripts |
| 128 | + |
| 129 | +### Core Source Files |
| 130 | +- `src/export.cpp`: Main pybind11 module definition |
| 131 | +- `src/export_cache.cpp`: Cache algorithm exports |
| 132 | +- `src/export_reader.cpp`: Trace reader exports |
| 133 | +- `src/exception.cpp`: Exception handling (recently fixed for pybind11 compatibility) |
| 134 | +- `CMakeLists.txt`: Build configuration |
| 135 | +- `pyproject.toml`: Python package configuration |
| 136 | + |
| 137 | +### Native Tools |
| 138 | +The build also produces native C++ binaries in `src/libCacheSim/build/bin/`: |
| 139 | +- `cachesim`: Command-line cache simulator |
| 140 | +- `traceAnalyzer`: Trace analysis tool |
| 141 | +- `MRC`: Miss ratio curve generation |
| 142 | +- Use: `./cachesim trace_path csv LRU 100MB` |
| 143 | + |
| 144 | +## Timing Expectations |
| 145 | + |
| 146 | +### Build Times (NEVER CANCEL - set timeouts 120+ seconds) |
| 147 | +- Git submodule init: 2.4 seconds |
| 148 | +- System dependency install: 66 seconds |
| 149 | +- CMake configuration (Debug): 2.4 seconds |
| 150 | +- CMake configuration (Release): 17 seconds (includes full libCacheSim build) |
| 151 | +- Build (Debug): 3 seconds |
| 152 | +- Build (Release): 18 seconds (includes LTO) |
| 153 | +- **Total fresh build**: 75-110 seconds depending on build type |
| 154 | + |
| 155 | +### Test Times |
| 156 | +- Manual validation: <1 second |
| 157 | +- Performance test (100k requests): 0.2 seconds |
| 158 | +- pytest (when working): 10-30 seconds |
| 159 | + |
| 160 | +### Network Dependencies |
| 161 | +- pip install: 30-300 seconds (may timeout, use manual build instead) |
| 162 | +- Git submodule clone: 2-5 seconds |
| 163 | + |
| 164 | +## Troubleshooting |
| 165 | + |
| 166 | +### Build Failures |
| 167 | +- **pybind11 not found**: `sudo apt install pybind11-dev` |
| 168 | +- **Network timeouts**: Use manual CMake build instead of pip install |
| 169 | +- **Missing submodules**: `git submodule update --init --recursive` |
| 170 | +- **XGBoost/LightGBM missing**: Disable optional features or install via `scripts/install_deps.sh` |
| 171 | + |
| 172 | +### Import Failures |
| 173 | +- **Module not found**: `export PYTHONPATH=$(pwd)/build:$PYTHONPATH` |
| 174 | +- **Symbol errors**: Rebuild with `ninja` from build directory |
| 175 | +- **Version mismatch**: Check pybind11 version compatibility |
| 176 | + |
| 177 | +### Performance Issues |
| 178 | +- **Slow performance**: Use Release build (`cmake -DCMAKE_BUILD_TYPE=Release`) |
| 179 | +- **Memory leaks**: Check exception handling and object lifecycle |
| 180 | + |
| 181 | +Always use the manual CMake build approach for reliable development - the pip install method may fail due to network issues beyond your control. |
0 commit comments