Skip to content

PMota173/cache-simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Cache Simulator

This project is a cache memory simulator implementation, developed in Python for the Computer Architecture and Organization course. The program simulates the behavior of different cache configurations (mapping types, replacement policies) and analyzes their performance.

Project Description

The simulator reads a sequence of 32-bit memory addresses from an input file. For each address, it determines if the access results in a hit (data is already in the cache) or a miss (data needs to be fetched from main memory).

At the end of the simulation, the program displays detailed performance statistics, classifying misses into three categories:

  • Compulsory Miss: Occurs on the first reference to a memory block. It is unavoidable.
  • Capacity Miss: Occurs when the cache is full and the desired block was discarded to make room for another, even if there was no set conflict.
  • Conflict Miss: Occurs in direct mapping or set-associative caches when two blocks compete for the same set and one is discarded, even if there are empty spaces in other sets.

Features

  • Mapping Types:

  • Direct Mapping (assoc = 1)

  • Set-Associative Mapping (assoc > 1)

  • Fully Associative Mapping (special case with nsets = 1)

  • Replacement Policies:

  • LRU (Least Recently Used)

  • FIFO (First-In, First-Out)

  • Random

  • Output Statistics:

  • Total count of accesses, hits, and misses.

  • Detailed miss counts (compulsory, capacity, conflict).

  • Hit and miss rate calculations for each category.

🛠️ Technologies Used

  • Python 3: Main language.
  • Standard libraries: sys, math, random.

🚀 How to Run

To run the simulator, use the Python 3 interpreter and pass the simulation parameters via the command line.

1. Clone the Repository

git clone https://github.com/PMota173/cache-simulator.git
cd cache-simulator

2. Execute the Simulator

The script requires 6 arguments, following this format:

python cache_simulator.py <nsets> <bsize> <assoc> <subst> <output_flag> <bin_file>

Parameters:

  • <nsets>: Number of sets (lines) in the cache.

  • <bsize>: Block size (in bytes).

  • <assoc>: Associativity level (number of ways per set).

  • <subst>: Replacement policy to use:

  • L or l for LRU.

  • F or f for FIFO.

  • R or r for Random.

  • <output_flag>: Output format:

  • 0 for the full, human-readable format.

  • 1 for a compact format, ideal for scripts/CSV.

  • <bin_file>: Path to the binary input file.

Execution Example:

python cache_simulator.py 64 8 2 l ../Testes/bin_100.bin

Program Output

Depending on the <output_flag>, the program will display statistics in two ways:

output_flag = 0 (Full Format):

Total accesses:          100000
Total hits:              87491
Total misses:            12509
Total compulsory misses: 3134
Total capacity misses:   0
Total conflict misses:   9375
Hit rate:                87.49%
Miss rate:               12.51%
Compulsory miss rate:    25.05%
Capacity miss rate:      0.00%
Conflict miss rate:      74.95%

output_flag = 1 (Compact Format):

100000 0.8749 0.1251 0.25 0.00 0.75

(Values correspond to: Total Accesses, Hit Rate, Miss Rate, Compulsory Miss Rate, Capacity Miss Rate, Conflict Miss Rate)


Authors

About

A configurable cache simulator developed for the Computer Architecture course.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages