Consider a system with 1MB main memory and 4KB cache memory. It is a direct mapped cache with block size of 16 bytes. If a program is executed on this system which accesses four words with the physical addresses 0x30243, 0x41246, 0x14324, 0x14326 in a loop 50 times. Assuming the cache is initially empty how many cache misses occur in executing the given program on this system?
Solution
The Main memory size is 1MB = 2^20 and the given physical addresses of the words are 5 digits of hexadecimal numbers.
So it is a byte addressable memory and we are given the address of each byte in the memory.
Block size is given as 16 Bytes. So block offset needs 4 bits.
The cache size is 4KB. Number of blocks in the cache = 4KB/16B = 256 = 2^8. The LINE number needs 8 bits.
In a direct mapped cache the physical address is divided into 3 fields as (TAG, LINE, OFFSET).
The given physical addresses are in hexadecimal format and each has 5 digits. Each hexadecimal digit needs 4 binary bits.
Let the four physical addresses be A = 0x30243, B = 0x41246, C = 0x14324, D = 0x14326
If you convert these four physical addresses into binary,
A = 0x30243 = 0011 0000 0010 0100 0011. Here the least significant 4 bits are OFFSET bits(0011) and the next 8 bits which are in bold are the LINE number(0010 0100)
B = 0x41246 = 0100 0001 0010 0100 0110. Here the LINE number is (0010 0100)
C = 0x14324 = 0001 0100 0011 0010 0100. Here the LINE number is (0011 0010)
D = 0x14326 = 0001 0100 0011 0010 0110. Here the LINE number is (0011 0010)
Here the physical addresses A and B are mapped to the same cache line (0010 0100) but they are completely different addresses as we can see the TAG bits of them are not matching. TAG bits of A are 0011 0000 and the TAG bits of B are 0100 0001.
First the block corresponding word A will come and will go to cache. When B is accessed it will evict A from the cache as B is also mapped to the same cache line.
In the next iteration A will replace B from the 1st iteration, and when B comes it will replace A. This will keep going on for all the 50 iterations.
So, both A and B will result in cache misses for all the 50 iterations, which will give cache 50*2 = 100 misses.
But the physical addresses C and D belong to the same block as they both have the same LINE numbers and same TAG bits also.
So after accessing C the first time it results in a miss and the corresponding block is fetched from main memory into cache. When D is accessed first time that will result in a cache hit, as D is also in the same block as C. From then on all the accesses to both C and D will be hit for all the 50 iterations. So all the accesses to D are hits including its 1st access.
Out of 50 iterations C and D together cause only 1 cache miss.
That's why total cache misses are 100+1 = 101.