Ldco Unit 6 Notes
Ldco Unit 6 Notes
Instructor
Dr. R. S. Khule,
Department of Information Technology,
Matoshri College of Engineering and Research Centre,
Nashik.
Recap
• Cache Memory –
Principle of Locality
Organization
Mapping functions
Module Objectives
•
1. First way is that the CPU should fetch the required data or instruction
and use it and that’s it but what, when the same data or instruction is
required again.CPU again has to access the same main memory location
for it and we already know that main memory is the slowest to access.
2. The second way is to store the data or instruction in the cache memory
so that if it is needed soon again in the near future it could be fetched in
a much faster way.
Cache Operation
• It is based on the principle of locality of reference. There are two
ways with which data or instruction is fetched from main memory
and get stored in cache memory. These two ways are the following:
• Temporal Locality –
Temporal locality means current data or instruction that is being
fetched may be needed soon. So we should store that data or
instruction in the cache memory so that we can avoid again
searching in main memory for the same data.
• When CPU accesses the current main memory location for reading required data or
instruction, it also gets stored in the cache memory which is based on the fact that
same data or instruction may be needed in near future. This is known as temporal
locality. If some data is referenced, then there is a high probability that it will be
referenced again in the near future.
• Spatial Locality –
Spatial locality means instruction or data near to the current memory location that
is being fetched, may be needed soon in the near future. This is slightly different
from the temporal locality. Here we are talking about nearly located memory
locations while in temporal locality we were talking about the actual memory
location that was being fetched.
• Sequentiality or Sequential locality- Given that a reference
has been made to a particular location s it is likely that within
the next several references a reference to the location of s + 1
will be made. Sequentiality is a restricted type of spatial
locality and can be regarded as a subset of it.
Cache Performance
• The performance of the cache is measured in terms of hit ratio. When CPU
refers to memory and find the data or instruction within the Cache
Memory, it is known as cache hit. If the desired data or instruction is not
found in the cache memory and CPU refers to the main memory to find
that data or instruction, it is known as a cache miss.
• Hit + Miss = Total CPU Reference Hit Ratio(h) = Hit / (Hit+Miss)
• Average access time of any memory system consists of two levels: Cache
and Main Memory. If Tc is time to access cache memory and Tm is the
time to access main memory then we can write:
• Tavg = Average time to access memory, Tavg = h * Tc + (1-h)*(Tm + Tc)
Cache Organization
• Cache is close to CPU and faster than main memory. But at
the same time is smaller than main memory. The cache
organization is about mapping data in memory to a location
in cache.
• A Simple Solution:
One way to go about this mapping is to consider last few
bits of long memory address to find small cache address,
and place them at the found address.
• Problems With Simple Solution:
The problem with this approach is, we loose the
information about high order bits and have no way to find
out the lower order bits belong to which higher order bits.
Solution is Tag:
To handle above problem,
more information is stored in
cache to tell which block of
memory is stored in cache. We
store additional information
as Tag
What is a Cache Block?
Since programs have Spatial Locality. (Once a location is retrieved, it
is highly probable that the nearby locations would be retrieved in
near future). So a cache is organized in the form of blocks. Typical
cache block sizes are 32 bytes or 64 bytes
Cache Mapping
• Mapping functions determine how memory blocks are placed
in the cache. The memory system has to quickly determine
if a given address is in the cache.
• There are three popular methods of mapping addresses
to cache locations
– Fully Associative – Search the entire cache for an address
– Direct – Each address has a specific place in the cache
– Set Associative – Each address can be in any of a small set
of cache locations
.
Cache Lines
• The cache memory is divided into blocks or lines.
Currently lines can range from 16 to 64 bytes
• Data is copied to and from the cache one line at a
time
• The lower log2(line size) bits of an address specify a
particular byte in a line
Line Offset
address
Line Example
0110010100
These boxes 0110010101
represent RAM
addresses 0110010110 With a line size of 4, the offset is
the log2(4) = 2 bits
0110010111
0110011000
The lower 2 bits specify
0110011001 which byte in the line
0110011010
0110011011
0110011100
0110011101
0110011110
0110011111
Direct Mapping
• Each location in RAM has one specific place in cache
where the data will be held
• Consider the cache to be like an array. Part of the address
is used as index into the cache to identify where the data
will be held
• Since a data block from RAM can only be in one specific
line in the cache, it must always replace the one block
that was already there.
• There is no need for a replacement algorithm
Direct Cache Addressing
• The lower log2(line size) bits define which byte in
the block
• The next log2(number of lines) bits defines which
line of the cache
• The remaining upper bits are the tag field
27 bits 5 bits
Tag Offset
Example Address
Associative
A. tag= 20, offset=4
Mapping 24 bit B. tag=19, offset=5
addresses 128K C. tag=18, offset=6
bytes of cache D. tag=16, offset=8
64 byte cache
lines
Set Associative Mapping