# tejas-k-06/cache-simulator architecture

> Architecture diagram of the GitHub repository tejas-k-06/cache-simulator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tejas-k-06/cache-simulator
- Repository: https://github.com/tejas-k-06/cache-simulator

## Overview

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: [`src/multilevel/CacheHierarchy.java`](https://github.com/tejas-k-06/cache-simulator/blob/main/src/multilevel/CacheHierarchy.java) (Hierarchy), [`src/main/Main.java`](https://github.com/tejas-k-06/cache-simulator/blob/main/src/main/Main.java) (Main), [`src/cache/Cache.java`](https://github.com/tejas-k-06/cache-simulator/blob/main/src/cache/Cache.java) (Cache base), [`src/config/CacheFactory.java`](https://github.com/tejas-k-06/cache-simulator/blob/main/src/config/CacheFactory.java) (Cache factory)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry"]
  node_src_main_main["Main<br/>entrypoint<br/>[Main.java]"]
end

subgraph group_config["Config"]
  node_src_config_configloader["Config loader<br/>json loader<br/>[ConfigLoader.java]"]
  node_src_config_cacheconfig["Cache config<br/>config model<br/>[CacheConfig.java]"]
  node_src_config_cachefactory["Cache factory<br/>builder<br/>[CacheFactory.java]"]
end

subgraph group_cache["Cache"]
  node_src_cache_cache["Cache base<br/>abstract cache<br/>[Cache.java]"]
  node_src_cache_directmappedcache["Direct mapped<br/>cache impl"]
  node_src_cache_setassociativecache["Set associative<br/>cache impl"]
  node_src_cache_fullyassociativecache["Fully associative<br/>cache impl"]
  node_src_cache_cacheblock["Cache block<br/>line/block<br/>[CacheBlock.java]"]
end

subgraph group_policy["Policy"]
  node_src_policy_replacementpolicy["Replacement<br/>strategy base"]
  node_src_policy_lrupolicy["LRU<br/>replacement policy<br/>[LRUPolicy.java]"]
  node_src_policy_fifopolicy["FIFO<br/>replacement policy<br/>[FIFOPolicy.java]"]
end

subgraph group_write["Writes"]
  node_src_write_writepolicy["Write policy<br/>strategy base<br/>[WritePolicy.java]"]
  node_src_write_writeback["Write back<br/>write policy<br/>[WriteBack.java]"]
  node_src_write_writethrough["Write through<br/>write policy<br/>[WriteThrough.java]"]
end

subgraph group_runtime["Runtime"]
  node_src_multilevel_cachehierarchy["Hierarchy<br/>routing layer"]
  node_src_stats_simulationstats["Stats<br/>metrics"]
end

subgraph group_input["Input"]
  node_src_trace_traceloader["Trace loader<br/>trace parser<br/>[TraceLoader.java]"]
  node_src_trace_memoryaccess["Access record<br/>trace model<br/>[MemoryAccess.java]"]
end

subgraph group_memory["Memory"]
  node_src_memory_mainmemory[("Main memory<br/>backing store<br/>[MainMemory.java]")]
end

subgraph group_errors["Errors"]
  node_src_exception_cacheconfigexception["Config error<br/>exception"]
  node_src_exception_invalidaddressexception["Address error<br/>exception"]
end

node_src_main_main -->|"loads config"| node_src_config_configloader
node_src_main_main -->|"loads trace"| node_src_trace_traceloader
node_src_main_main -->|"builds cache"| node_src_config_cachefactory
node_src_main_main -->|"runs sim"| node_src_multilevel_cachehierarchy
node_src_main_main -->|"reports stats"| node_src_stats_simulationstats
node_src_config_configloader -->|"produces"| node_src_config_cacheconfig
node_src_config_configloader -->|"fails on bad config"| node_src_exception_cacheconfigexception
node_src_config_cachefactory -->|"creates"| node_src_cache_directmappedcache
node_src_config_cachefactory -->|"creates"| node_src_cache_setassociativecache
node_src_config_cachefactory -->|"creates"| node_src_cache_fullyassociativecache
node_src_cache_directmappedcache -->|"extends"| node_src_cache_cache
node_src_cache_setassociativecache -->|"extends"| node_src_cache_cache
node_src_cache_fullyassociativecache -->|"extends"| node_src_cache_cache
node_src_cache_cache -->|"stores"| node_src_cache_cacheblock
node_src_multilevel_cachehierarchy -->|"uses"| node_src_cache_cache
node_src_multilevel_cachehierarchy -->|"falls through"| node_src_memory_mainmemory
node_src_multilevel_cachehierarchy -->|"applies"| node_src_policy_replacementpolicy
node_src_multilevel_cachehierarchy -->|"applies"| node_src_write_writepolicy
node_src_policy_lrupolicy -->|"implements"| node_src_policy_replacementpolicy
node_src_policy_fifopolicy -->|"implements"| node_src_policy_replacementpolicy
node_src_write_writeback -->|"implements"| node_src_write_writepolicy
node_src_write_writethrough -->|"implements"| node_src_write_writepolicy
node_src_trace_traceloader -->|"parses"| node_src_trace_memoryaccess
node_src_trace_traceloader -->|"fails on bad address"| node_src_exception_invalidaddressexception
node_src_multilevel_cachehierarchy -->|"updates"| node_src_stats_simulationstats

click node_src_main_main "https://github.com/tejas-k-06/cache-simulator/blob/main/src/main/Main.java"
click node_src_config_configloader "https://github.com/tejas-k-06/cache-simulator/blob/main/src/config/ConfigLoader.java"
click node_src_config_cacheconfig "https://github.com/tejas-k-06/cache-simulator/blob/main/src/config/CacheConfig.java"
click node_src_config_cachefactory "https://github.com/tejas-k-06/cache-simulator/blob/main/src/config/CacheFactory.java"
click node_src_cache_cache "https://github.com/tejas-k-06/cache-simulator/blob/main/src/cache/Cache.java"
click node_src_cache_directmappedcache "https://github.com/tejas-k-06/cache-simulator/blob/main/src/cache/DirectMappedCache.java"
click node_src_cache_setassociativecache "https://github.com/tejas-k-06/cache-simulator/blob/main/src/cache/SetAssociativeCache.java"
click node_src_cache_fullyassociativecache "https://github.com/tejas-k-06/cache-simulator/blob/main/src/cache/FullyAssociativeCache.java"
click node_src_cache_cacheblock "https://github.com/tejas-k-06/cache-simulator/blob/main/src/cache/CacheBlock.java"
click node_src_policy_replacementpolicy "https://github.com/tejas-k-06/cache-simulator/blob/main/src/policy/ReplacementPolicy.java"
click node_src_policy_lrupolicy "https://github.com/tejas-k-06/cache-simulator/blob/main/src/policy/LRUPolicy.java"
click node_src_policy_fifopolicy "https://github.com/tejas-k-06/cache-simulator/blob/main/src/policy/FIFOPolicy.java"
click node_src_write_writepolicy "https://github.com/tejas-k-06/cache-simulator/blob/main/src/write/WritePolicy.java"
click node_src_write_writeback "https://github.com/tejas-k-06/cache-simulator/blob/main/src/write/WriteBack.java"
click node_src_write_writethrough "https://github.com/tejas-k-06/cache-simulator/blob/main/src/write/WriteThrough.java"
click node_src_multilevel_cachehierarchy "https://github.com/tejas-k-06/cache-simulator/blob/main/src/multilevel/CacheHierarchy.java"
click node_src_stats_simulationstats "https://github.com/tejas-k-06/cache-simulator/blob/main/src/stats/SimulationStats.java"
click node_src_memory_mainmemory "https://github.com/tejas-k-06/cache-simulator/blob/main/src/memory/MainMemory.java"
click node_src_trace_traceloader "https://github.com/tejas-k-06/cache-simulator/blob/main/src/trace/TraceLoader.java"
click node_src_trace_memoryaccess "https://github.com/tejas-k-06/cache-simulator/blob/main/src/trace/MemoryAccess.java"
click node_src_exception_cacheconfigexception "https://github.com/tejas-k-06/cache-simulator/blob/main/src/exception/CacheConfigException.java"
click node_src_exception_invalidaddressexception "https://github.com/tejas-k-06/cache-simulator/blob/main/src/exception/InvalidAddressException.java"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_main_main,node_src_trace_traceloader,node_src_trace_memoryaccess toneBlue
class node_src_config_configloader,node_src_config_cacheconfig,node_src_config_cachefactory,node_src_memory_mainmemory toneAmber
class node_src_cache_cache,node_src_cache_directmappedcache,node_src_cache_setassociativecache,node_src_cache_fullyassociativecache,node_src_cache_cacheblock,node_src_exception_cacheconfigexception,node_src_exception_invalidaddressexception toneMint
class node_src_policy_replacementpolicy,node_src_policy_lrupolicy,node_src_policy_fifopolicy toneRose
class node_src_write_writepolicy,node_src_write_writeback,node_src_write_writethrough toneIndigo
class node_src_multilevel_cachehierarchy,node_src_stats_simulationstats toneTeal
```

## Components

### Entry

Program startup and orchestration

- [Main](https://github.com/tejas-k-06/cache-simulator/blob/main/src/main/Main.java): entrypoint · Coordinates configuration, trace loading, cache setup, simulation, and reporting · `src/main/Main.java`

### Config

Cache configuration loading and construction

- [Config loader](https://github.com/tejas-k-06/cache-simulator/blob/main/src/config/ConfigLoader.java): json loader · Reads cache_config.json and builds cache settings · `src/config/ConfigLoader.java`
- [Cache config](https://github.com/tejas-k-06/cache-simulator/blob/main/src/config/CacheConfig.java): config model · Structured cache parameters used to build the simulator · `src/config/CacheConfig.java`
- [Cache factory](https://github.com/tejas-k-06/cache-simulator/blob/main/src/config/CacheFactory.java): builder · Instantiates cache implementations from configuration · `src/config/CacheFactory.java`

### Cache

Cache topologies and line storage

- [Cache base](https://github.com/tejas-k-06/cache-simulator/blob/main/src/cache/Cache.java): abstract cache · Common cache behavior shared by all cache topologies · `src/cache/Cache.java`
- [Direct mapped](https://github.com/tejas-k-06/cache-simulator/blob/main/src/cache/DirectMappedCache.java): cache impl · Direct-mapped cache topology · `src/cache/DirectMappedCache.java`
- [Set associative](https://github.com/tejas-k-06/cache-simulator/blob/main/src/cache/SetAssociativeCache.java): cache impl · Set-associative cache topology · `src/cache/SetAssociativeCache.java`
- [Fully associative](https://github.com/tejas-k-06/cache-simulator/blob/main/src/cache/FullyAssociativeCache.java): cache impl · Fully associative cache topology · `src/cache/FullyAssociativeCache.java`
- [Cache block](https://github.com/tejas-k-06/cache-simulator/blob/main/src/cache/CacheBlock.java): line/block · Represents a cached block with metadata such as tag and validity · `src/cache/CacheBlock.java`

### Policy

Replacement strategy subsystem

- [Replacement](https://github.com/tejas-k-06/cache-simulator/blob/main/src/policy/ReplacementPolicy.java): strategy base · Abstract eviction policy interface · `src/policy/ReplacementPolicy.java`
- [LRU](https://github.com/tejas-k-06/cache-simulator/blob/main/src/policy/LRUPolicy.java): replacement policy · Least-recently-used eviction strategy · `src/policy/LRUPolicy.java`
- [FIFO](https://github.com/tejas-k-06/cache-simulator/blob/main/src/policy/FIFOPolicy.java): replacement policy · First-in-first-out eviction strategy · `src/policy/FIFOPolicy.java`

### Writes

Write handling subsystem

- [Write policy](https://github.com/tejas-k-06/cache-simulator/blob/main/src/write/WritePolicy.java): strategy base · Abstract write-handling interface · `src/write/WritePolicy.java`
- [Write back](https://github.com/tejas-k-06/cache-simulator/blob/main/src/write/WriteBack.java): write policy · Defers memory writes until eviction or flush · `src/write/WriteBack.java`
- [Write through](https://github.com/tejas-k-06/cache-simulator/blob/main/src/write/WriteThrough.java): write policy · Propagates writes immediately to lower memory · `src/write/WriteThrough.java`

### Runtime

Hierarchy execution and statistics

- [Hierarchy](https://github.com/tejas-k-06/cache-simulator/blob/main/src/multilevel/CacheHierarchy.java): routing layer · Routes accesses through L1/L2/L3 and down to memory · `src/multilevel/CacheHierarchy.java`
- [Stats](https://github.com/tejas-k-06/cache-simulator/blob/main/src/stats/SimulationStats.java): metrics · Aggregates hits, misses, and simulation counters · `src/stats/SimulationStats.java`

### Input

Trace ingestion and access records

- [Trace loader](https://github.com/tejas-k-06/cache-simulator/blob/main/src/trace/TraceLoader.java): trace parser · Reads memory-access traces from plain-text files · `src/trace/TraceLoader.java`
- [Access record](https://github.com/tejas-k-06/cache-simulator/blob/main/src/trace/MemoryAccess.java): trace model · Represents one read or write access with an address · `src/trace/MemoryAccess.java`

### Memory

Backing store

- [Main memory](https://github.com/tejas-k-06/cache-simulator/blob/main/src/memory/MainMemory.java): backing store · Terminal store for misses and write-backs · `src/memory/MainMemory.java`

### Errors

Configuration and trace failures

- [Config error](https://github.com/tejas-k-06/cache-simulator/blob/main/src/exception/CacheConfigException.java): exception · Raised for invalid cache configuration · `src/exception/CacheConfigException.java`
- [Address error](https://github.com/tejas-k-06/cache-simulator/blob/main/src/exception/InvalidAddressException.java): exception · Raised for malformed or invalid trace addresses · `src/exception/InvalidAddressException.java`

## Connections

- 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
