CPU Caching Concepts

7,907 views

Published on

Introduction to basics of caching concepts.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,907
On SlideShare
0
From Embeds
0
Number of Embeds
1,854
Actions
Shares
0
Downloads
184
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

CPU Caching Concepts

  1. 1. Understand CPU Caching Concepts<br />Abhijit K Rao<br />IIIT - Bangalore<br />
  2. 2. Concept of Caching<br />Need for Cache has come about due to reasons : <br />The concept of Locality of reference. <br /> -> 5 percent of the data is accessed 95 percent of the times, so <br /> makes sense to cache the 5 percent of the data.<br />The gap between CPU and main memory speeds. <br /> -> In analogy to producer consumer problem, the CPU is the <br /> consumer and RAM, hard disks act as producers. Slow <br /> producers limit the performance of the consumer. <br />
  3. 3. Locality of Reference<br />Spatial locality : If a particular memory location say nth location is referenced at a particular time, then it is likely that (n+1) th memory location will be referenced in the near future<br />The actual piece of data that was requested is called the critical word, and the surrounding group of bytes that gets fetched along with it is called a cache line or cache block.<br />Temporal Locality: If at one point in time say T a particular memory location is referenced, then it is likely that the same location will be referenced again at time T+delta. <br /> This is very similar to the concept of working set, i.e., set of pages which the CPU frequently accesses.<br />
  4. 4. CPU Cache and its operation<br />A CPU cache is a smaller, faster memory which stores copies of the data from the most frequently used main memory locations. The concept of locality of reference drives caching concept, we cache the most frequently used, data, instruction for faster data access. <br />CPU cache could be data cache, instruction cache. Unlike RAM, cache is not expandable.<br />The CPU first checks in the L1 cache for data, if it does not find it at L1, it moves over to L2 and finally L3. If not found at L3, it’s a cache miss and RAM is searched next, followed by the hard drive.<br />If the CPU finds the requested data in cache, it’s a cache hit, and if not, it’s a cache miss. <br />
  5. 5. Levels of caching and speed, size comparisons<br />
  6. 6. Cache organization<br />When the processor needs to read or write a location in main <br /> memory, it first checks whether that memory location is in the cache.<br /> This is accomplished by comparing the address of the memory location<br /> to all tags in the cache that might contain that address.<br />If the processor finds that the memory location is in the cache, we say <br /> that a cache hit has occurred; otherwise, we speak of a cache miss. <br />
  7. 7. Cache Entry structure<br />Cache row entries usually have the following structure:<br />The data blocks (cache line) contain the actual data fetched from the main memory. The memory address is split into the tag, the index and the displacement (offset), while the valid bit denotes that this particular entry has valid data.<br /><ul><li>The index length is bits and describes which row the data has been put in.
  8. 8. The displacement length is and specifies which block of the ones we have stored we need.
  9. 9. The tag length is address − index − displacement </li></li></ul><li>Cache organization - 1<br /> Cache is divided into blocks. The blocks form the basic unit of cache <br /> organization. RAM is also organized into blocks of the same size as the <br /> cache's blocks<br /> When the CPU requests a byte from a particular RAM block, it needs to <br /> be able to determine three things very quickly:<br />1. Whether or not the needed block is actually in the cache<br /> 2. The location of the block within the cache<br /> 3. The location of the desired byte within the block<br />
  10. 10. Mapping RAM blocks to cache block<br />Fully associative : Any RAM block can be stored in any available block frame. The problem with this scheme is that if you want to retrieve a specific block from the cache, you have to check the tag of every single block frame in the entire cache because the desired block could be in any of the frames<br />Direct mapping : In a direct-mapped cache, each block frame can cache only a certain subset of the blocks in main memory. ForEg. Ram block X whose modulo results in 1 are always stored in Cache block 1.<br /> The problem with this approach is certain cache blocks could remain unused and there could be frequent eviction of cache entries for certain cache blocks.<br />N way associative : Ram block X, could be either mapped to Cache Block X or Y. <br />
  11. 11. Handling Cache Miss<br />In order to make room for the new entry on a cache miss, the cache has to evict one of the existing entries. <br />The heuristic that it uses to choose the entry to evict is called the replacement policy. The fundamental problem with any replacement policy is that it must predict which existing cache entry is least likely to be used in the future. Some of the replacement policies are :<br />Random Eviction: Removal of any cache entry by random choice.<br />LIFO: Evicting the latest cache entry.<br />FIFO: Evicting the oldest cache entry.<br />LRU: Evicting the Least recently used cache entry. <br />
  12. 12. Mirroring Cache to Main memory<br />If data are written to the cache, they must at some point be written to main memory and other higher order cache as well. The timing of this write is controlled by what is known as the write policy.<br />A Write-through cache, every write to the cache causes a write to main memory and higher order cache like L2, L3.<br />Write-back or copy-back cache, writes are not immediately mirrored to the main memory. Instead, the cache tracks which locations will be evicted. Such entries are written to main memory, higher order cache just before eviction of the cache entry<br />
  13. 13. Stale data in cache<br />The data in main memory being cached may be changed by other entities (e.g. peripherals using direct memory access or multi-core processor), in which case the copy in the cache may become out-of-date or stale. <br />Alternatively, when the CPU in a multi-core processor updates the data in the cache, copies of data in caches associated with other cores will become stale. <br />Communication protocols between the cache managers. Which keep the data consistent are known as cache coherence protocols. Eg. Snoopy based, directory based, token based.<br />
  14. 14. State of the Art today<br />Current day research on cache design, handling cache coherence, is more biased to multicore architectures.<br />References<br />Wikipedia : http://en.wikipedia.org/wiki/CPU_cache<br />ArsTechnica : http://arstechnica.com/<br />http://software.intel.com<br />What Every Programmer Should Know About Memory -<br /> - Ulrich Drepper, Red Hat, Inc.<br />
  15. 15. Q/A<br />

×