Introduction to Cache
- Cache depends on two principles
- Temporal Locality
- If an item is referenced, it will tend to be referenced again soon.
- Spatial Locality
- If an item is referenced, the items near it will also tend to be referenced.
- Cache is designed based on these two principles
- Items which are accessed are stored in cache for a while
- Items near an item which was needed are also stored in cache for a while.
- Cache is an array of memory.
- It runs close or at processor speed.
- It is organized as a series of blocks.
- A block is a "row" or line in the cache.
- It holds at least a word.
- In some cache configurations it can hold quite a bit more than a word.
- But multiples of words.
- When an item from memory is needed, the cache is searched.
- A cache hit or hit occurs when an item is found in cache.
- Since cache may be slower than the processor there is a hit time or the time required to find an element in the cache.
- A cache miss or miss occurs when an item is not found.
- If a miss occurs there is a miss penalty or the time required to fetch the item from the next lower memory level.
- This may be another layer of cache
- Or it may be main memory.
- Or possibly even lower
- The percentage of hits to searches is called the hit rate, P(hit)
- And the percentage of misses to searches is the miss rate, P(miss)
- P(hit) + P(miss) = 1
- There are three reasons for cache misses
- Compulsory misses occur when the item has never been accessed or placed in cache.
- Capacity misses occur when there is not enough space to hold everything in cache.
- Conflict misses occur when two items will be stored in the same location in cache.
- Different cache configurations have been created to deal with these different types of misses.
- Direct Mapped Cache
- This is probably the simplest type of cache.
- Think of assigned spaces in a parking lot.
- There are n locations for data storage.
- For now restrict the block size to be 1.
- An item from address a is stored at location a%n
- Example:
- Assume WOMBAT has a 12 bit address.
- Assume a 16 bit direct mapped cache with block size of 1.
- 4 bits are needed to label the locations in cache.
- 8 bits are left over
- Remember 2 bit of the address are ignored, we are only dealing with word boundries.
- We will use the next 4 bits for the index
- We will call the remaining 6 bits the tag
- We will need a valid bit to indicate if the entry is valid or not.
-
- So any address ending in 0100 will map to cache location 4
- Or ending in 1001 will map to location 9
-
- Give three addresses which will map to location 6
- In order to tell which data is in the cache, we store both the data and the tag.
-
- Each cache location will require
- 2 bytes for the tag
- 4 bytes for the data
- Or a total of 6 bytes,
- So far 16x6 or 96 bytes.
- 1 bit for valid x 16 = 2 bytes
- Or 98 bytes.
- To fetch an item
- Compute the index and the tag of the target address
- Check to see if the computed tag matches the stored tag in the cache at the computed index
- If it does,
- Cache Hit
- return the data from the cache to the processor.
- If it does not,
- Cache Miss.
- fetch the data from memory
- Store it in the cache
- Return it to the processor
- Given an example of a program which causes a compulsory miss
- Give an example that causes a collision miss.
- Take a look at the image on page 389.
-