Professional Documents
Culture Documents
William Stallings
Computer Organization
and Architecture
9th Edition
+
Chapter 4
Cache Memory
Key Characteristics of Computer
Memory Systems
◼ Capacity
◼ Memory is typically expressed in terms of bytes (or words)
◼ Unit of transfer
◼ For internal memory the unit of transfer is equal to the number of
electrical lines into and out of the memory module (continue)
+
Characteristics of Memory
Systems
This equal to the word length (often larger; 64, 128, 256 bits)
This principle can be applied to more than tow levels (Fig. 4.1)
Typical Cache
Organization (book
Elements of Cache Design
Logical
and
Physical
Caches
Cache Size
◼ We would like the size to be small enough so that the
◼ Overall average cost per bit is close to that of main memory alone
Direct
Mapping
Direct Mapping (mechanism)
◼ The mapping function is easily implemented using the main
memory address
◼ Fig. 4.9 illustrate the general mechanism, each MM address is
divided into 3 fields
◼ Least significant w bits identify a unique word or byte within a block
of MM
◼ Remaining s bits specify one of the 2s block of main memory
◼ Cache logic interprets s bits as:
◼ Tag of s-r (MS portion), identifies which particular block (MM) is
currently is being stored in that line c
◼ A line field of r bits (identify one of the m = 2r lines of the cache
◼ Direct mapping summary
◼ Address length = (s + w) bits
◼ Number of addressable units = 2s+w words or bytes
◼ Block size = line size = 2w words or bytes
◼ Number of blocks in main memory = 2s+ w/2w = 2s
◼ Number of lines in cache = m = 2r
◼ Size of tag = (s – r) bits
Direct Mapping Cache Organization
Example 4.2 for all 3 cases
◼ The example include the following elements:
◼ The cache can hold 64 Kbytes
◼ Data are transferred between MM and the cache in blocks of 4
byte each (i.e. Block size = Line size = 4 bytes). So cache is
organized as 16K = 214 lines of 4 bytes each
◼ The main memory consists of 16 Mbytes, with each byte directly
addressable by 24-bit address (224 = 16M).
◼ Thus for mapping purposes, we can consider MM to consist of 4M
blocks of 4 bytes each
Mapping
Example
+
Direct Mapping Advs / disAdvs
+Victim Cache
◼ Advantages direct mapping
◼ Simple and inexpensive to implement
◼ Disadvantage of direct mapping
◼ Thrashing:- If a program access 2 blocks that map to the same
line repeatedly, then continually swapping, and this will low the
hit ratio (cache misses)
Victim Cache
◼ Originally proposed as an approach to reduce the conflict
misses of direct mapped caches without affecting its fast access
◼ Fully associative cache
◼ Typical size is 4 to 16 cache lines
◼ Residing between direct mapped L1 cache and the next level of
memory
+
Fully Associative Mapping
◼ Each memory block can be loaded into any line of the cache
◼ The memory address is interprets as a Tag and a Word field
◼ The Tag field uniquely identifies a block of main memory
◼ The word field identifies the word within the requested block
◼ To determine whether a block is in cache, the cache control
logic must simultaneously examine every line’s tag for a
mach
◼ Fully associative mapping summary
◼ Address length = (s + w) bits
◼ Number of addressable units = 2s+w words or bytes
◼ Block size = line size = 2w words or bytes
◼ Number of blocks in main memory = 2s+ w/2w = 2s
◼ Number of lines in cache = undetermined
◼ Size of tag = s bits
◼ Fig. 4.11 illustrate the logic of Fully Associative Cache Org.
Fully Associative Cache Organization
+
Fully Associative Mapping Example
◼ Remember our example
◼ The cache can hold 64 Kbytes
◼ Data are transferred between MM and the cache in blocks of 4
byte each (i.e. Block size = Line size = 4 bytes). So cache is
organized as 16K = 214 lines of 4 bytes each
◼ The main memory consists of 16 Mbytes, with each byte directly
addressable by 24-bit address (224 = 16M).
◼ Thus for mapping purposes, we can consider MM to consist of 4M
blocks of 4 bytes each
Mapping
Example
+
Set Associative Mapping
◼ Compromise that exhibits the strengths of both the direct and
associative approaches while reducing their disadvantages
◼ Cache consists of (divided into) a number of sets
◼ Each set contains a number of lines
◼ A given block maps to any line in a given set
◼ e.g. 2 lines per set
◼ This referred to 2- way set-associative mapping
◼ A given block can be in one of 2 lines in only one set
◼ Finally
◼ Each block maps into a specific set (direct mapping)
◼ And, maps into any line in the specific set (fully associative )
+
Mapping From
Main Memory
to Cache:
k-Way
Set Associative
+
Set Associative Mapping
Set associative cache organization
◼ Here the cache control interprets a memory address as three
fields:- Tag, Set, and Word
◼ Fig. 4.14 illustrates the cache control logic.
◼ Note that the Tag size with fully associative is quit large, while with set
associative is much smaller
◼ Set Associative Mapping Summary
◼ Address length = (s + w) bits
◼ Number of addressable units = 2s+w words or bytes
◼ Block size = line size = 2w words or bytes
◼ Number of blocks in main memory = 2s+w/2w=2s
◼ Number of lines in set = k
◼ Number of sets = v = 2d
◼ Number of lines in cache = m= kv = k * 2d
◼ Size of cache = k * 2d+wwords or bytes
◼ Size of tag = (s – d) bits
k-Way
Set
Associative
Cache
Organization
Example 4.2c for set associative
◼ Remember the example include the following elements:
◼ The cache can hold 64 Kbytes, MM consists of 16 Mbytes
◼ Block size = Line size = 4 bytes, assume 2-way set associative
◼ Fig. 4.14 shows the set associative mapping and the address
format
◼ The main memory address length = s + w = 24-bit (2s+w = 16 Mbytes
= 224 byte) divided into:
◼ w = 2-bits for word identity (line= block = 4 bytes = 2w = 22) and s =
24 – 2 = 22-bit ( s + w = 24)
◼ Number of line in set = k = 2
◼ Number of sets v = 2d = 16Klines/ 2 (#of lines in se) =8k sets = 213
sets, so d = 13 set number
◼ Tag = (22 – 13 = 9), or = MM blocks/ Cache sets = 4M/8K
◼ For direct mapping there is only one possible line for any
particular block and no choice is possible
◼ First-in-first-out (FIFO)
◼ Replace that block in the set that has been in the cache longest
◼ Easily implemented as a round-robin or circular buffer technique
If at least one write operation has been A more complex problem occurs when
performed on a word in that line of the multiple processors are attached to the
cache then main memory must be same bus and each processor has its own
updated by writing the line of cache out local cache - if a word is altered in one
to the block of memory before bringing cache it could conceivably invalidate a
in the new block word in other caches
+ Write Policy:- Write Through and
Write Back
◼ Write through
◼ Simplest technique
◼ All write operations are made to main memory as well as to the cache
◼ The main disadvantage of this technique is that it generates substantial
memory traffic and may create a bottleneck
◼ Write back
◼ Minimizes memory writes, Updates are made only in the cache
◼ When an update occurs, a dirty bit associated with the line is set
◼ If a block is replaced, it written back to MM if dirty bit is set
◼ Problem: Portions of main memory are invalid and hence accesses by
I/O modules can be allowed only through the cache. And this`
◼ Makes for complex circuitry and a potential bottleneck
◼ See example 4.3 in text
◼ Cache coherency (read in text)
+
Line size
◼ When a block of data is retrieved and placed in the cache
not only the desired word but also some number of adjacent
words are retrieved
◼ As the block size increases more useful data are brought into
the cache. Two specific effects come into play:
◼ Larger blocks reduce the number of blocks that fit into a cache
◼ As a block becomes larger each additional word is farther from
the requested word
◼ As the block size increases the hit ratio will at first increase
because of the principle of locality
◼ Two-level cache:
◼ Internal cache designated as level 1 (L1)
◼ External cache designated as level 2 (L2)
◼ Potential savings due to the use of an L2 cache depends on the
hit rates in both the L1 and L2 caches
◼ The use of multilevel caches complicates all of the design issues
related to caches, including size, replacement algorithm, write
policy, line size, and set size continue
+
Number of caches (Multilevel Caches)
◼ Question ?, inclusion of an off-chip cache is desirable ?. yes
and most designs includes both on-chip and off –chip
◼ If there is no off-chip (L2), processor will access DRAM or ROM if
the request word is not in on-chip (L1) across the bus
◼ Typically bus speed is slow and MM access time is slow
◼ On other hand if L2 (static RAM) is used, the missing information
can be quickly retrieved
◼ How fast?
◼ Pentium 4 cache organization
◼ How expensive?
◼ Cache memory principles ◼ ARM cache organization