Report: Cache Simulation

The second assignment in my computer systems course was probably the one I most enjoyed this year. It was challenging but I learned a lot and had fun working with the large data trace. I scored 95%. Below are some excerpts from the handout and my solution.

 

“The aim of this assignment is to write a parameterized cache simulator that reads a memory address trace and provides statistics about cache accesses and hits. The simulator is to be written in C…

…Assume the following specifications in designing the cache simulator:

Fixed parameters:
The following parameters always stay the same:
(i) Memory addresses are 32 bits
(ii) Cache Organization: Unified (data and instruction cache blocks are kept in the same cache)

Variable parameters: The following parameters are passed as command-line arguments to the cache simulator:
(i) Cache Size: 256B to 8KB; powers of 2 only
(ii) Cache Block Size: 16, 32, 64, 128, or 256 bytes
(iii) Cache Mapping: Direct Mapped or Fully Associative
(iv) Cache Replacement Policy if Fully Associative Mapping: FIFO or LRU

For example, if the command-line parameters specify following options: Cache size: 2KB, Block Size: 64B, Cache Mapping: Direct Mapped then the cache will have the following properties:
Parameters                                      Values
Cache Mapping                         Direct Mapped
Replacement Policy                Not Applicable
Cache Size                                  2KB
Block Size                                    64B
Number of blocks                    32 (2KB/64B)
Num bits for block offset       6 (64B block)
Num bits for index                   5 (32 blocks)
Num bits for tag                        21 (32-6-5)

…The memory trace file consists of a sequence of memory accesses and it is an input to the cache simulator. For each memory access the trace file provides: a) whether it is an instruction or data access and b) 32-bit memory address in hex. The addresses in trace file are byte addresses and not the block addresses. An example of the trace file is as follows:

I 8cda3fa8
I 8158bf94
D 8cd94c50
I 8cd94d64
D 8cd94c54

For your convenience the output of the program is already provided in print_statistics(…) function. You need to populate the parameters of the function with appropriate values. Following is the full list of all variables you need to populate.
1. num_blocks: total number of blocks in a cache
2. num_bits_for_block_offset: number of bits required to access offset within a block
3. num_bits_for_index: number of bits required to represent an index
4. num_bits_for_tag: number of bits required for tag
5. result.instruction_accesses: total number of cache accesses made by instructions
6. result.instruction_hits: total number of hits incurred for instruction accesses
7. result.data_accesses: total number of cache accesses made by data
8. result.data_hits: total number of hits incurred for data accesses 

Summary: In this assignment, you are required to write a cache simulator which reads a trace of memory references and reports cache statistics. The cache configuration is determined by parameters that are passed as command line arguments. Following parameters may be changed via command line parameters. cache size, block size, cache mapping, replacement policy if fully associative cache.”

 

Due to University regulations code is only available on request, if you’re interested please contact me: james@james-odonnell.com