How tejas-k-06/cache-simulator works
1. Overall shape
This is a Java command-line cache simulator. The runtime flow is: Main loads cache settings from JSON, loads a memory-access trace, builds the cache objects, then runs the trace through the cache hierarchy and collects hit/miss statistics.
2. Entry point and orchestration
src/main/Main.java is the top-level coordinator. It does not implement cache behavior itself; instead, it wires together configuration loading, trace loading, cache construction, simulation execution, and stats reporting.
3. Configuration boundary
src/config/ConfigLoader.java reads config/cache_config.json and converts it into CacheConfig objects. CacheFactory uses that config to instantiate the right cache implementation and attach policies. This is the main architectural boundary between static setup and runtime simulation.
4. Cache core
src/cache/Cache.java is the abstract base for all cache types. The concrete implementations are:
- DirectMappedCache
- SetAssociativeCache
- FullyAssociativeCache
These subclasses encapsulate indexing, lookup, eviction, and line/block management. CacheBlock represents the cached unit of data and likely carries metadata such as valid/dirty state and tag information.
5. Replacement strategy subsystem
Replacement behavior is separated into src/policy/ReplacementPolicy.java with concrete strategies LRUPolicy and FIFOPolicy. This keeps eviction logic independent from cache topology, so the same policy can be reused across cache types where applicable.
6. Write policy subsystem
Write handling is split behind src/write/WritePolicy.java, with WriteBack and WriteThrough as implementations. This is the main boundary for how write accesses propagate to lower levels or main memory, and it affects dirty-state handling and memory traffic.
7. Memory hierarchy
src/multilevel/CacheHierarchy.java models the L1 → L2 → L3 chain described in the README. It is the routing layer that decides which cache level handles an access and how misses flow downward toward MainMemory. This is the key data path for the simulator.
8. Main memory model
src/memory/MainMemory.java is the terminal backing store. It represents the non-cache memory source/sink that services misses and write-backs. Architecturally, it is the bottom of the hierarchy rather than a full system simulator.
9. Trace ingestion
src/trace/TraceLoader.java parses plain-text trace files from traces/. Each line becomes a MemoryAccess record containing the access type (R/W) and address. This is the input stream that drives the simulation, and the repo includes both sample and generated traces for different workload shapes.
10. Simulation output and metrics
src/stats/SimulationStats.java aggregates runtime results such as hits, misses, and related counters. This is the reporting boundary between the simulation engine and the final output shown to the user.
11. Error handling
src/exception/InvalidAddressException.java and CacheConfigException.java define the main failure modes: malformed trace addresses and invalid cache configurations. These exceptions protect the simulation from bad input at both the trace layer and the config layer.
12. Architectural summary for diagramming
For a diagram, the clearest flow is:
Main → ConfigLoader / TraceLoader → CacheFactory → CacheHierarchy → Cache implementations → MainMemory, with ReplacementPolicy, WritePolicy, and SimulationStats attached as cross-cutting subsystems. The repo is organized cleanly around those boundaries, with cache, policy, write, memory, trace, config, stats, and exception as the main packages.
Main entry points: CacheHierarchy.java Hierarchy · Main.java Main · Cache.java Cache base · CacheFactory.java Cache factory
Components 22
Entry
Program startup and orchestration
- Main · Coordinates configuration, trace loading, cache setup, simulation, and reporting
src/main/Main.java
Config
Cache configuration loading and construction
- Config loader · Reads cache_config.json and builds cache settings
src/config/ConfigLoader.java - Cache config · Structured cache parameters used to build the simulator
src/config/CacheConfig.java - Cache factory · Instantiates cache implementations from configuration
src/config/CacheFactory.java
Cache
Cache topologies and line storage
- Cache base · Common cache behavior shared by all cache topologies
src/cache/Cache.java - Direct mapped · Direct-mapped cache topology
src/cache/DirectMappedCache.java - Set associative · Set-associative cache topology
src/cache/SetAssociativeCache.java - Fully associative · Fully associative cache topology
src/cache/FullyAssociativeCache.java - Cache block · Represents a cached block with metadata such as tag and validity
src/cache/CacheBlock.java
Policy
Replacement strategy subsystem
- Replacement · Abstract eviction policy interface
src/policy/ReplacementPolicy.java - LRU · Least-recently-used eviction strategy
src/policy/LRUPolicy.java - FIFO · First-in-first-out eviction strategy
src/policy/FIFOPolicy.java
Writes
Write handling subsystem
- Write policy · Abstract write-handling interface
src/write/WritePolicy.java - Write back · Defers memory writes until eviction or flush
src/write/WriteBack.java - Write through · Propagates writes immediately to lower memory
src/write/WriteThrough.java
Runtime
Hierarchy execution and statistics
Input
Trace ingestion and access records
- Trace loader · Reads memory-access traces from plain-text files
src/trace/TraceLoader.java - Access record · Represents one read or write access with an address
src/trace/MemoryAccess.java
Memory
Backing store
- Main memory · Terminal store for misses and write-backs
src/memory/MainMemory.java
Errors
Configuration and trace failures
- Config error · Raised for invalid cache configuration
src/exception/CacheConfigException.java - Address error · Raised for malformed or invalid trace addresses
src/exception/InvalidAddressException.java
Connections 25
- Main → Config loader: loads config · Reads simulator settings before startup
- Main → Trace loader: loads trace · Parses the workload to simulate
- Main → Cache factory: builds cache · Constructs cache objects from configuration
- Main → Hierarchy: runs sim · Executes the access stream through the cache hierarchy
- Main → Stats: reports stats · Prints or aggregates simulation results
- Config loader → Cache config: produces · Maps JSON into cache configuration objects
- Config loader → Config error: fails on bad config · Signals invalid configuration input
- Cache factory → Direct mapped: creates · Builds direct-mapped cache instances
- Cache factory → Set associative: creates · Builds set-associative cache instances
- Cache factory → Fully associative: creates · Builds fully associative cache instances
- Direct mapped → Cache base: extends · Shares the cache abstraction
- Set associative → Cache base: extends · Shares the cache abstraction
- Fully associative → Cache base: extends · Shares the cache abstraction
- Cache base → Cache block: stores · Maintains cache blocks/lines
- Hierarchy → Cache base: uses · Delegates access handling to cache levels
- Hierarchy → Main memory: falls through · Misses and write-backs reach main memory
- Hierarchy → Replacement: applies · Uses replacement strategy during eviction
- Hierarchy → Write policy: applies · Uses write policy for stores and dirty data
- LRU → Replacement: implements · Concrete LRU strategy
- FIFO → Replacement: implements · Concrete FIFO strategy
- Write back → Write policy: implements · Concrete write-back strategy
- Write through → Write policy: implements · Concrete write-through strategy
- Trace loader → Access record: parses · Converts trace lines into access records
- Trace loader → Address error: fails on bad address · Signals malformed trace input
- Hierarchy → Stats: updates · Accumulates hit/miss counters during simulation