Cache Memory is a smaller, faster type of memory located between the CPU and main memory (RAM) in a computer system.
• Its primary purpose is to temporarily store copies of frequently accessed data and instructions from main memory to reduce the time taken to access them by the CPU.
Below are the key points regarding cache memory:
Purpose:
Cache memory is used to store frequently accessed data and instructions, which the CPU can access quickly.
Hierarchy:
Cache memory is typically arranged in a hierarchy with multiple levels (L1, L2, L3), each with different sizes and speeds. The closer the cache to the CPU, the faster it is but also smaller in size.
Speed:
Cache memory operates at much faster speeds compared to main memory (RAM) and other types of storage.
Size:
Cache memory sizes vary, with typical sizes ranging from a few kilobytes to several megabytes.
Types:
There are different types of cache memory, including instruction cache (I-cache) and data cache (D-cache).
Cache Hit:
A cache hit occurs when the CPU requests data or instructions that are already stored in the cache, resulting in faster access times.
Cache Miss:
A cache miss occurs when the requested data or instructions are not found in the cache, leading to a longer access time as the CPU retrieves the data from main memory.
Cache Replacement Policy:
When the cache is full and a new piece of data needs to be stored, a cache replacement policy determines which existing data to evict from the cache. Common policies include Least Recently Used (LRU) and First-In-First-Out (FIFO).
Associativity:
Cache memory can be direct-mapped, set-associative, or fully associative, affecting how data is mapped and accessed within the cache.
Importance:
Cache memory significantly improves CPU performance by reducing the time it takes to access frequently used data and instructions.
Diagram of Cache Memory:
Cache Mapping:
Cache mapping refers to the technique used to determine the mapping between the addresses of data stored in the main memory and their corresponding locations in the cache memory.
• The goal of cache mapping is to efficiently manage the storage and retrieval of data in the cache, optimizing performance by reducing access times and maximizing hit rates.
There are three different types of mapping used for the purpose of cache memory which is as follows:
1.) Direct Mapping:
The simplest technique, known as direct mapping, maps each block of main memory into only one possible cache line. or In Direct mapping, assign each memory block to a specific line in the cache.
• If a line is previously taken up by a memory block when a new block needs to be loaded, the old block is trashed. An address space is split into two parts index field and a tag field. The cache is used to store the tag field whereas the rest is stored in the main memory. Direct mapping`s performance is directly proportional to the Hit ratio.
For purposes of cache access, each main memory address can be viewed as consisting of three fields. The least significant w bits identify a unique word or byte within a block of main memory. In most contemporary machines, the address is at the byte level. The remaining s bits specify one of the 2s blocks of main memory. The cache logic interprets these s bits as a tag of s-r bits (the most significant portion) and a line field of r bits. This latter field identifies one of the m=2r lines of the cache. Line offset is index bits in the direct mapping.
2.) Associative Mapping:
In this type of mapping, associative memory is used to store the content and addresses of the memory word.
• Any block can go into any line of the cache. This means that the word id bits are used to identify which word in the block is needed, but the tag becomes all of the remaining bits. This enables the placement of any word at any place in the cache memory.
• It is considered to be the fastest and most flexible mapping form. In associative mapping, the index bits are zero.
3.) Set- Associative Mapping:
This form of mapping is an enhanced form of direct mapping where the drawbacks of direct mapping are removed.
• Set associative addresses the problem of possible thrashing in the direct mapping method. It does this by saying that instead of having exactly one line that a block can map to in the cache, we will group a few lines together creating a set. Then a block in memory can map to any one of the lines of a specific set.
• It allows each word that is present in the cache can have two or more words in the main memory for the same index address.
• It combines the best of direct and associative cache mapping techniques. In set associative mapping the index bits are given by the set offset bits. In this case, the cache consists of a number of sets, each of which consists of a number of lines
Writing into cache:
Writing data into a cache involves several steps, and the process can vary depending on the cache architecture and the specific caching strategy being employed.
Generally, there are two main approaches to writing data into a cache: write-through and write-back.
- Write-Through Cache:
- In a write-through cache, when data is written to the cache, it is also simultaneously written to the corresponding main memory or next level of the memory hierarchy.
- The advantage of this approach is that it ensures that the data in the cache and the main memory are always consistent.
- However, the drawback is that it can lead to more frequent memory writes, which may impact performance, especially if the main memory access is slower than the cache.
- Write-Back Cache:
- In a write-back cache, data is initially written only to the cache when a write operation occurs. The corresponding entry in the main memory is not immediately updated.
- Instead, the cache controller marks the cache line as “dirty” to indicate that it has been modified and needs to be written back to the main memory at a later time.
- When the cache line is evicted from the cache (due to cache replacement or other reasons), the dirty data is then written back to the main memory.
- This approach can reduce the number of memory writes, improving performance, especially in situations where there are many write operations to the same cache line.
The process of writing into a cache typically involves the following steps:
- Address Lookup:
- The memory address associated with the data to be written is presented to the cache.
- The cache performs a lookup to determine if the data is already present in the cache and, if so, where it is located.
- Data Transfer:
- If the data is found in the cache (cache hit), the new data is written into the cache at the appropriate location.
- If the data is not found in the cache (cache miss), it may need to be fetched from the next level of the memory hierarchy before being written into the cache.
- Updating Cache Metadata:
- The cache metadata, such as tags and valid bits, are updated to reflect the new data.
- In the case of a write-back cache, the dirty bit may also be set to indicate that the data has been modified.
- Optional Main Memory Write:
- In the case of a write-through cache, the data is also written into the main memory immediately.
- In the case of a write-back cache, the data is only written into the main memory when the cache line is evicted from the cache.
Cache Initialization:
Cache initialization refers to the process of setting up the cache memory in a computer system before it starts executing programs or tasks. Cache initialization involves several steps to ensure that the cache is properly configured and ready to improve system performance.
Here’s an overview of the cache initialization process:
- Cache Configuration:
- Before initialization, the system needs to determine the size, associativity, and replacement policy of the cache. These parameters depend on factors such as the architecture of the processor, memory hierarchy, and performance requirements of the system.
- Invalidation:
- If the cache is not empty when the system starts, it needs to be invalidated to ensure that stale data is not present. This involves marking all cache lines as invalid or empty.
- Data Loading:
- Once the cache is invalidated, it needs to be populated with data from main memory or another level of cache. This can be done through various mechanisms, such as prefetching frequently used data or loading data on demand when it is accessed.
- Prefetching:
- To improve performance, the system may prefetch data into the cache before it is actually needed. Prefetching algorithms analyze program behavior and access patterns to predict which data will be accessed next and fetch it into the cache proactively.
- Initialization of Control Structures:
- Cache initialization also involves setting up control structures, such as tags, valid bits, and replacement counters, which are used to manage cache operations like data lookup, eviction, and replacement.
- Cache Warm-up:
- After initialization, the cache may go through a warm-up period during which it gradually fills up with data as the system executes programs. This allows the cache to adapt to the workload and optimize its contents based on actual usage patterns.
- Testing and Validation:
- Once the cache is initialized, the system may perform testing and validation procedures to ensure that it is functioning correctly and meeting performance expectations. This may involve running benchmark tests or stress tests to evaluate cache efficiency and stability.