libCacheSim

repository·develop·Indexed 43 days ago

https://github.com/1a1a11a/libcachesim

A high-performance C++ library and toolset for simulating and analyzing cache behaviors. It supports state-of-the-art caching algorithms including LRU, FIFO, S3-FIFO, and Sieve, and provides high-throughput trace replay, miss ratio curve (MRC) profiling, and a plugin system for custom cache logic. The project offers a command-line simulator, a C API, and bindings for Python and Node.js (libcachesim-node v1.0.1).

Tokens
47.8K
Snippets
152
Records
213
Agent score
55%

What's inside libCacheSim

  1. Overview of libCacheSim components

    develop

    libCacheSim is a high-performance library for building and running cache simulations. It consists of three primary components:

    • cachesim: A high-performance cache simulator used for running cache simulations.
    • traceAnalyzer: A high-performance and versatile analyzer for cache traces.
    • libCacheSim: The core library used for building custom cache simulators.

    To get started, you should first follow the Install & Build guide, then proceed to the specific guide for the component you intend to use (e.g., cachesim, traceAnalyzer, etc.).

  2. Quickstart Guides for libCacheSim

    develop

    libCacheSim provides several entry points depending on your needs. You can use the command-line simulator, trace utilities, or the Python bindings for high-level access.

    Available quickstart paths:

    • Command-line cache simulator: For running simulations via CLI.
    • Trace Utilities: For managing and manipulating trace files.
    • Trace Analyzer: For analyzing simulation results.
    • MRC Profiler: For Miss Rate Curve profiling.
    • Plugin System: For extending simulator functionality.
    • Python Binding: For easy API access via Python (available in a separate repository).
  3. Overview of libCacheSim data structures

    develop

    The dataStructure module in libCacheSim provides a collection of optimized data structures used for cache simulation tasks. These include structures for priority management, tree-based lookups, probabilistic membership testing, and hashing.

    Available data structures:

    • priority queue (pqueue.h/.c): For managing elements based on priority.
    • splay tree (splay.h/.c): A self-adjusting binary search tree.
    • bloom filter (bloom.h/.c): A space-efficient probabilistic data structure for membership testing.
    • minimal increment counting bloom filter (minimalIncrementCBF.h/.c): A specialized version of a Bloom filter for counting.
    • ketama (ketama/*.c): Implements consistent hashing.
    • hash (hash/*.c): General hashing utilities.
    • hashtable (hashtable.c): A standard hash table implementation.
  4. What is libCacheSim

    develop

    libCacheSim is a high-performance library and suite of tools for cache simulation and trace analysis. It consists of three primary components:

    • cachesim: A high-performance simulator for running cache simulations.
    • traceAnalyzer: A versatile analyzer for cache traces.
    • libCacheSim: The core high-performance library used for building custom cache simulators.

    Key features include high throughput (over 20M requests/sec), high memory efficiency, out-of-the-box parallelism, and support for state-of-the-art eviction, admission, and prefetching algorithms.

  5. Explore third-party forks of libCacheSim

    develop

    The libCacheSim ecosystem includes numerous third-party forks that extend the core functionality with specialized algorithms, research implementations, and hardware-specific models. Developers looking for specific advanced features (like reinforcement learning, hybrid DRAM/Flash caches, or LLM-trace readers) can find them in these forks:

    • WiredTiger Emulation: fedorova/libCacheSim adds an emulation of WiredTiger's eviction algorithm.
    • Reinforcement Learning (LOH): ddkkpp/libCacheSim implements the LOH (Learning-based Object Handling) policy.
    • Memory Tiering & TLB Filtering: access-bits/libCacheSim provides TLB and access-bit filtering for memory tiering.
    • RingLFU: tzussman/libCacheSim implements a bucketed frequency policy.
    • Hybrid Caches: shermanjlim/libCacheSim includes a DRAM-plus-flash hybrid cache (HYBRID.cpp).
    • Learned Caches (LightGBM/XGBoost): ouyhlan/libCacheSim implements ZGCache and ZCCache.
    • Embedding-based Policies: Aryan470/libCacheSim provides policies using an EmbeddingManager (e.g., LRU with Forgiveness).
    • Prefetcher/Eviction Interference: varungohil/libCacheSim includes instrumentation for studying prefetcher interference.
    • Cost-aware Simulation: midsterx/libCacheSim modifies the simulator to emit cost and gib_missed metrics for IBM Object Storage traces.
    • Compute-aware Eviction: LauYeeYu/libCacheSim adds compute-aware policies and an LLM-trace reader.
    • Two-Level Cache Hierarchies: gws8820/2-Level-libCacheSim allows setting independent replacement algorithms and sizes for each level of a hierarchy.
  6. Advanced Usage and Library Integration

    develop

    For deep integration or customization, libCacheSim offers advanced guides covering:

    • Library Usage Guide: How to integrate the C library into your own projects.
    • How to Add a New Algorithm: Instructions for implementing custom cache logic.
    • API Reference (C): Detailed documentation of the C API.
    • Performance Tuning: Best practices for optimizing simulation speed.
    • Memory Usage Profiling: Techniques for monitoring the simulator's memory footprint.
  7. Developer Documentation and Installation

    develop

    Developers looking to build, debug, or contribute to libCacheSim can refer to the following:

    • Install & Build: Step-by-step instructions for setting up the environment and compiling the project.
    • Debugging Guide: Tools and techniques for troubleshooting simulations.
    • Contributing: Guidelines for submitting code to the project.
  8. Understand libCacheSim trace formats and interchange standards

    develop

    The oracleGeneral format has become a de-facto interchange format for cache traces. Several third-party projects use or implement parsers for libCacheSim's trace formats, including:

    • Binary Trace Formats: Used by projects like Pelikan (cachesim-rs) which implements the same integer encoding for the op column as libCacheSim's req_op_e.
    • oracleGeneral: A common format used by Caffeine (Java), Otter (Go), Theine (Python), and go-sieve (Go).
    • CSV Formats: Supported by the libcachesimCSV identifier in some implementations.

    If you are building a tool that needs to interact with existing cache datasets, you will likely need to implement a decoder for oracleGeneral or libcachesimCSV.

  9. Understand traceAnalyzer output statistics

    develop

    When running traceAnalyzer, the output provides a summary of the trace characteristics. Key metrics include:

    • Request/Object Counts: Total number of requests and unique objects.
    • Data Volume: Total GiB for requests and objects.
    • Compulsory Miss Ratio: Ratio of compulsory misses per request and per byte.
    • Object Size: Weighted average object size by request and by object.
    • Frequency: Mean access frequency.
    • Time Span: Total duration of the trace.
    • Rates: Minimum and maximum request rates and object rates over a specific window (e.g., 300s).
    • X-hit: Distribution of objects accessed exactly X times.
    • Popularity: Fraction of accesses to the most popular objects.
    • Zipf Fit: (For certain traces) Zipf alpha and R2 values.
  10. Understand Massif analysis report structure

    develop

    The ms_print output provides two main components for memory analysis:

    1. Memory Usage Bar Chart: A visual representation of memory usage over time (where the x-axis represents instructions executed).
    2. Heap Profile Snapshots: Detailed tables showing memory allocation at specific points.

    Report Columns

    When inspecting the snapshot tables, look for these columns:

    • n: Snapshot number
    • time(i): Instruction count
    • total(B): Total memory allocated
    • useful-heap(B): Memory actually used by the application
    • extra-heap(B): Memory overhead (e.g., malloc metadata)
    • stacks(B): Memory used by stacks

    Identifying Hotspots

    The report uses a tree structure to show the call stack responsible for allocations. For example, a high percentage of memory attributed to create_chained_hashtable_v2 indicates that hash table overhead is a primary memory consumer.

  11. Concurrent Cache Simulation APIs

    develop

    The concurrent cache simulator demonstrates how to simulate multiple caches simultaneously using the following core APIs from libCacheSim:

    • simulate_with_multi_caches(): Used to simulate multiple distinct cache configurations at the same time.
    • simulate_at_multi_sizes(): Used to simulate caches across a range of different sizes.

    Additionally, this example demonstrates how to integrate a CSV reader to process cache traces in CSV format. For full technical specifications, refer to simulator.h in the libCacheSim include directory.

  12. How the Plugin V2 Architecture works

    develop

    The Plugin V2 architecture allows for standalone cache implementations to be injected into the libCacheSim execution flow.

    Mental Model:

    1. Implementation Layer: You write your logic in a high-level language (e.g., C++ using classes like StandaloneLRU for complex data structures like doubly-linked lists and hash maps).
    2. Interface Layer: You wrap that logic in a C-compatible interface by implementing specific Hook Functions (cache_init_hook, cache_hit_hook, etc.).
    3. Integration Layer: The plugin_cache.c framework in libCacheSim calls these hooks at specific lifecycle events (hits, misses, evictions).

    Key Constraints:

    • Compatibility: Must export C functions to be loadable by the framework.
    • Thread Safety: The standard plugin architecture (as shown in the LRU example) does not implement thread safety and is intended for single-threaded use cases.
    • Memory Management: It is recommended to handle memory management within the implementation (e.g., via C++ destructors) to ensure cache_free_hook cleans up correctly.