Assignment 3 Cache Simulator Solution



  • Overview

The goal of this assignment is to help you understand caches better. You are required to write a cache simulator using the C programming language. The programs have to run on iLab machines. We are providing real program memory traces as input to your cache simulator. The format and structure of the memory traces are described below.

  • Memory Access Traces

The input to the cache simulator is a memory access trace, which we have generated by executing real programs. The trace contains memory addresses accessed during program execution. Your cache simulator will have to use these addresses to determine if the access is a hit or a miss, and the actions to perform in each case. The memory trace le consists of multiple lines. Each line of the trace le corresponds to a memory accesses performed by the program. Each line consists of two columns, which are space separated. The second column reports 48-bit memory address that has been accessed by the program while the rst column indicates whether the memory access is a read (R) or a write (W). The trace le always ends with a #eof string. We have provided you three input trace les (some of them are larger in size). You can safely assume the trace les always have proper format. Here is a sample trace le.

  • 0x9cb3d40 W 0x9cb3d40

  • 0x9cb3d44 W 0x9cb3d44

  • 0xbf8ef498


  • Cache Simulator

You will implement a cache simulator to evaluate di erent con gurations of caches. Your program should be able to support traces with any number of lines. The followings are the requirements for your cache simulator:

You simulate one level cache.

The cache size, associativity, the replacement policy, and the block size are the input parameters. Cache size and block size are speci ed in bytes.

Replacement algorithm: First In First Out (FIFO). When a block needs to be replaced, the cache evicts the block that was accessed rst. It does not take into account whether the block is frequently or recently accessed.

You have to simulate a write through cache.

  • Cache Simulator Interface

You have to name your cache simulator rst. Your program should support the following us-age interface: ./ rst <cache size><block size><cache policy><associativity><trace le>


  1. <cache size>is the total size of the cache in bytes. This number should be a power of 2.

  1. <block size>is a power of 2 integer that speci es the size of the cache block in bytes.

  1. <cache policy>Here is valid cache policy is fo.

  1. <associativity>is one of:

direct – simulate a direct mapped cache.

assoc – simulate a fully associative cache.

assoc:n – simulate an n way associative cache. n will be a power of 2.

E) <trace le>is the name of the trace le.

NOTE: Your program should check if all the inputs are in valid format, if not print error and then terminate the program.

  • Sample Output

As the output, your program should print out the number of memory reads (per cache block), memory writes (per cache block), cache hits, and cache misses. You should follow the exact same format shown below (pay attention to case sensitivity of the letters), otherwise, the autograder can not grade your program properly.

$./ rst 32 4 fo assoc:2 trace2.txt

Memory reads: 3499

Memory writes: 2861

Cache hits: 6501

Cache misses: 3499

In this example above, we are simulating 2-way set associate cache of size 32 bytes. Each cache block is 4 bytes. The trace le name is trace2.txt.

NOTE: Some of the trace les are quite large. So it might take a few minutes for the autograder to grade for all the testcases.

  • Other Details

  1. (a) When your program starts, there is nothing in the cache. So, all cache lines are empty (invalid).

(b) you can assume that the memory size is 2pow48 . Therefore, memory addresses are 48 bit (zero extend the addresses in the trace le if theyre less than 48-bit in length).

(c) the number of bits in the tag, cache address, and byte address are determined by the cache size and the block size;

  1. For a write-through cache, there is the question of what should happen in case of a write miss. In this assignment, the assumption is that the block is rst read from memory (one read memory), and then followed by a memory write.

3- You do not need to simulate the memory in this assignment. Because, the traces doesnt contain any information on data values transferred between the memory and the caches.

  1. You have to compile your program with the following ags: -Wall -Werror -fsanitize=address

  • Extra credit (20 points)

As an extra credit, you should implement LRU (Least Recently Used) cache policy. Your program should output exactly the same format output as it shown before.

Here is an example of running your program with LRU policy.

$./ rst 32 4 lru assoc:2 trace2.txt

Memory reads: 3292

Memory writes: 2861

Cache hits: 6708

Cache misses: 3292

  • Submission

You have to e-submit the assignment using Sakai . Put all les (source code + Make le) into a directory named rst, which itself is a sub-directory under pa3 . Then, create a tar le (follow the instructions in the previous assignments to create the tar le). Your submission should be only a tar le named pa3.tar. You have to e-submit the assignment using Sakai. Your submission should be a tar le named pa3.tar. To create this le, put everything that you are submitting into a directory named pa3. Then, cd into the directory containing pa3 (that is, pa3s parent directory) and run the following command: $tar cvf pa3.tar pa3

To check that you have correctly created the tar le, you should copy it (pa3.tar) into an empty directory and run the following command: $tar xvf pa3.tar

This is how the folder structure should be.


{ rst

rst.c rst.h Make le

Source code: all source code les necessary for building your programs. e.g. rst.c and rst.h.

Make le: There should be at least three rules in your Make le:

all: make a complete build of your program ( rst).

rst: build the executables ( rst).

clean: prepare for rebuilding from scratch.

  • Autograder

First mode

Testing when you are writing code with a pa3 folder.

  1. Lets say you have a pa3 folder with the directory structure as described in the assignment.

  1. Copy the folder to the directory of the autograder

  1. Run the autograder with the following command

$python auto

It will run the test cases and print your scores.

Second mode

This mode is to test your nal submission (i.e, pa3.tar) 1. Copy pa3.tar to the autograder directory

  1. Run the autograder with pa3.tar as the argument as below: $python auto pa3.tar

10 Grading guidelines

  1. We should be able build your program by just running make.

  1. Your program should follow the format speci ed above for the usage interface.

  1. Your program should strictly follow the input and output speci cations mentioned above. (Note: This is perhaps the most important guideline: failing to follow it might result in you losing all or most of your points for this assignment. Make sure your programs output format is exactly as speci ed. Any deviation will cause the automated grader to mark your output as incorrect. REQUESTS FOR RE-EVALUATIONS OF PROGRAMS REJECTED DUE TO IMPROPER FORMAT WILL NOT BE ENTERTAINED.)

error: Content is protected !!