Skip to content

About

L1/L2 Cache simulator with victim cache

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Cache Simulator

A high-fidelity CPU cache simulator implementing a multi-level memory hierarchy with L1, Victim Cache, and L2 caches.

Demo

# Simulate a 1KB L1 cache with 64-byte blocks, 2-way associativity
./cachesim -c 10 -b 6 -s 1 < traces/gcc.trace

Sample Output:

Cache Settings
--------------
L1 (C,B,S): (10,6,1). Replace policy: MIP
Victim cache entries: 2
L2 (C,B,S): (15,6,3). Replace policy: LIP. Early Restart: Disabled

Cache Statistics
----------------
Reads: 1000000
Writes: 500000
L1 accesses: 1500000
L1 hits: 1350000
L1 misses: 150000
L1 hit ratio: 0.900
L1 miss ratio: 0.100
L1 average access time (AAT): 3.456
...

No demo GIF yet? Record your terminal with asciinema or create a screenshot of sample output and add it to a screenshots/ folder.

Features

  • Multi-Level Cache Hierarchy: L1 Cache → Victim Cache → L2 Cache → DRAM
  • Configurable Parameters: Cache size (C), block size (B), associativity (S)
  • 4 Replacement Policies:
    • MIP (MRU Insertion Policy)
    • LIP (LRU Insertion Policy)
    • FIFO (First-In-First-Out)
    • Random (PRNG-based eviction)
  • Write Strategies:
    • L1: Write-back, Write-allocate (WBWA)
    • L2: Write-through, Write-no-allocate (WTWNA)
  • Victim Cache: Fully-associative buffer for evicted L1 blocks
  • Comprehensive Statistics: Hit/miss ratios, average access time (AAT)
  • Early Restart Support: Optional L2 optimization

Tech Stack

Component Technology
Language C++ (C++11)
Build GNU Make
Compiler g++ / clang++
Platform Linux, macOS

Getting Started

Prerequisites

  • C++ compiler with C++11 support (g++ 4.8+ or clang++ 3.3+)
  • GNU Make
  • Memory trace files (format: R/W 0xADDRESS)

Installation

# Clone the repository
git clone git@github.com:Samiurr10/Cache-Simulator.git
cd Cache-Simulator

# Build the simulator
make

# (Optional) Build with debug symbols
make DEBUG=1

# (Optional) Build with AddressSanitizer
make SANITIZE=1

Running the Simulator

./cachesim [OPTIONS] < trace_file

Usage

Command-Line Options

Option Description Default
-c C L1 cache size: 2^C bytes 10 (1KB)
-b B Block size: 2^B bytes 6 (64B)
-s S L1 associativity: 2^S ways 1 (2-way)
-v V Victim cache entries (0, 1, or 2) 2
-C C L2 cache size: 2^C bytes 15 (32KB)
-S S L2 associativity: 2^S ways 3 (8-way)
-P P L2 replacement policy (mip/lip/fifo/random) LIP
-D Disable L2 cache -
-E Enable Early Restart on L2 -
-h Show help message -

Examples

# Basic simulation with default settings
./cachesim < traces/gcc.trace

# Direct-mapped L1 cache (1-way)
./cachesim -s 0 < traces/mcf.trace

# 4KB L1, 4-way set associative, no victim cache
./cachesim -c 12 -s 2 -v 0 < traces/libquantum.trace

# Disable L2 cache (L1 + Victim only)
./cachesim -D < traces/lbm.trace

# Enable Early Restart optimization
./cachesim -E < traces/milc.trace

Trace File Format

Each line contains an access type and hexadecimal address:

R 0x7fff5fbff8c0
W 0x7fff5fbff8c8
R 0x7fff5fbff8d0
  • R = Read (load)
  • W = Write (store)

Project Structure

Cache-Simulator/
├── cachesim.cpp        # Core cache simulation logic
├── cachesim.hpp        # Data structures and configuration
├── cachesim_driver.cpp # Main driver and CLI parsing
├── Makefile            # Build configuration
├── run.sh              # Execution wrapper script
├── README.md           # This file
├── LICENSE             # MIT License
└── traces/             # (Add your trace files here)
    └── *.trace

Architecture

┌─────────────────────────────────────────────────────────────┐
│                        CPU Request                          │
└─────────────────────────┬───────────────────────────────────┘
                          ▼
                   ┌─────────────┐
                   │   L1 Cache  │  Write-back, Write-allocate
                   │  (C,B,S)    │  MIP Replacement
                   └──────┬──────┘
                          │ Miss
                          ▼
                 ┌────────────────┐
                 │  Victim Cache  │  Fully Associative
                 │   (0-2 entries)│  LRU Replacement
                 └───────┬────────┘
                         │ Miss
                         ▼
                   ┌─────────────┐
                   │   L2 Cache  │  Write-through, Write-no-allocate
                   │  (C,B,S)    │  LIP Replacement
                   └──────┬──────┘
                          │ Miss
                          ▼
                   ┌─────────────┐
                   │    DRAM     │  64 cycles + 2 cycles/word
                   └─────────────┘

Roadmap

  • Add support for additional replacement policies (PLRU, RRIP)
  • Implement prefetching strategies
  • Add cache coherence simulation for multi-core
  • Create visualization tool for cache behavior
  • Support for compressed trace formats
  • Add benchmark suite with standard traces

Troubleshooting

Build Errors

Problem: error: 'uint64_t' was not declared

# Solution: Ensure C++11 is enabled
make CXXFLAGS="-std=c++11"

Problem: Linker errors with math functions

# Solution: Math library should be linked automatically, but if not:
make LIBS="-lm"

Runtime Issues

Problem: Segmentation fault on large traces

# Build with AddressSanitizer to debug
make clean && make SANITIZE=1
./cachesim < trace.trace

Problem: Invalid configuration error

  • Ensure 4 <= B <= 7 (block size exponent)
  • Ensure L1 size < L2 size when L2 is enabled
  • Ensure L1 associativity <= L2 associativity
  • Victim cache entries must be 0, 1, or 2

Performance

Problem: Simulation is slow

# Build with optimizations
make clean && make FAST=1

Acknowledgments

  • ECE 4100 - Advanced Computer Architecture, Georgia Tech
  • Cache taxonomy based on the Conte Cache Taxonomy model

About

L1/L2 Cache simulator with victim cache

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages