


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















We now consider the memory system of the computer. Up to this point, we have considered the memory as a single monolithic element. A 128 MB memory would be viewed as follows. We now consider the actual structure of memory, which has evolved in response to commercial pressure to have large memories that are also fast, physically compact, and relatively cheap. As an example, we quote a recent (7/7/2005) price for memory from Gateway Computers: $130 for 512 MB of 533 MHz SDRAM. The multi–level memory structure is one response to the requirement for a very large, very fast memory. A complete memory hierarchy uses several levels of cache memory , which is best considered as a small fast memory attached to a larger slower memory. Under proper conditions, such a memory can be made to act as a larger memory with a speed almost that of the faster memory. In this chapter we consider these designs. Here is the central problem addressed by the memory hierarchy: memory is too slow. We now have central processing units with clocks rated at 3.2 GHz, with rumors of 5.0 GHZ units being in development. We now ask about the impact of a 100 GHz unit, were it now available. Such a unit, having a clock approximately 30 times faster than current units, would be only slightly faster than current models, due to the inability of memory to keep up. This phenomenon is called memory starvation – memory is just too slow for the CPU. The central idea of a multi–level memory is to have a faster primary memory front for a slower secondary memory. All modern computers employ two variants of such a memory strategy, called cache memory and virtual memory. Although these two strategies evolved for different reasons, their structures share a lot in common. The following figure shows how the two play together in a typical computer. The study of a virtual memory system focuses on the management of main and secondary memory by the operating system as a single large address space. This is covered in courses on Operating Systems. The study of cache memory focuses on the management of a high speed cache placed between the main memory and the CPU. Page 1 of 26 Chapter 6 Revised July 19, 2005
Locality Both virtual memory and cache memory depend on two locality properties of computer programs. These properties are collectively called “ locality ”. There are two basic types of locality: temporal and spatial locality. These have simple definitions: temporal locality the principle that recently accessed memory locations are very likely to be accessed again. spatial locality the principle that the next memory location to be accessed will likely be very close to the location last accessed. For instruction access, this can be called “sequential locality”. The locality principle was discovered by Peter J. Denning as a part of his Ph.D. research at MIT, where he addressed the problem called “memory thrashing” in which virtual memory systems spent most of their time handling memory access and very little time doing actual computation. Dr. Denning describes his work in the July 2005 issue of Communications of the ACM. We should all hope for a Ph.D. project with such wide applicability. Effective Access Time for Multilevel Memory Each of the two memory systems (cache memory and virtual memory) can be viewed as a two–level memory. We have a faster primary memory with an access time denoted by TP, and a slower secondary memory with an access time denoted by TS. Work with problems of this time is made more complicated by the names by which we refer to computer memory; our main memory (of which we might have 128 – 512 MB) is also called “primary memory”, a term that can cause confusion. Here are the proper uses of the terms. System Primary Level Secondary Level Cache Memory The Cache Main Memory Virtual Memory Main Memory The Disk When considering a problem, there is one rule that can always be used: TP < TS. For the simpler problem of cache memory, we have TP 5 to 10 nanoseconds and TS 80 nsec. We now consider the multilevel memory problem in the abstract, deriving results that are valid for both cache memory and virtual memory systems. As noted above, the success of such a system is due to the principle of locality. The measure of the effectiveness of this system is the hit ratio , reflected in the effective access time of the memory system. In a standard memory system, an addressable item is referenced by its address. In a two level memory system, the primary memory is first checked for the address. If the addressed item is present in the primary memory, we have a hit , otherwise we have a miss. The hit ratio is defined as the number of hits divided by the total number of memory accesses; 0.0 h 1.0. Given a faster primary memory with an access time TP and a slower secondary memory with access time TS, we compute the effective access time as a function of the hit ratio. The applicable formula is TE = h TP + (1.0 – h ) TS. As an aside, we note that the following results are easily proved if TP < TS:
A Strategy for Primary-Memory Misses In a multilevel memory system, we stipulate that the CPU first searches the primary memory for the desired word (data or instruction) before searching the secondary memory. To be a bit more specific about the above, we state that.
In the first scenario, the average access time for the memory is 90 nanoseconds, actually slower than the secondary memory. In the second scenario, the average access time is 25 nanoseconds, not impressive but better than the secondary memory. We shall now discuss the general problem of primary memory management. This author would prefer to continue the use of generic language in order to emphasize the point that the strategy applies equally well to both the cache memory problem and the virtual memory problem. He has decided that holding tenaciously to such an approach would lead to more confusion than it is worth, so now focuses on a specific cache management strategy. A Direct-Mapped Cache in the MARIE We need a more focused discussion of the “slots and blocks” issue of primary memory management, so we focus on the simplest example: the management of a cache memory that fronts for the main memory in a computer. Ignoring the fact that the MARIE memory is much smaller than most existing cache memories, we postulate the following scenario. Main memory: 4096 words of 16 bits each Cache memory: 64 words of 16 bits each, organized into four “slots” or blocks holding sixteen words each. Mapping strategy: Direct mapped. As just noted, each block in the cache memory holds 16 words. It is organized as follows. As is the case with any memory fronted by a cache, the memory address space is also divided into blocks of 16 words each. Since there are 4096 (2^12 ) words in the memory, this implies 256 (2^8 ) blocks of 16 (2^4 ) words each. It is a requirement that the memory blocks be the same size as the cache blocks. The address of a word in main memory is thus considered as MemBlock16 + Offset Page 5 of 26 Chapter 6 Revised July 19, 2005
Tags and Valid Bits Suppose a program in the computer issues the address 0x006 by placing that 12-bit number in the Memory Address Register. The hardware must see if that address is in cache memory. As a 12–bit pattern, formatted according to the tag-cache block-offset scheme, we have: The figures for this address are: Offset = 0x Cache Block = 0x Tag = 0x We see that this memory address, if present in the cache memory, would be present in cache block 0 at offset 6. The trouble is that, from what we have specified already, the entry in that offset could just as easily be for address 0x146, 0x5C6, or any of the other 61 memory addresses that would map to that offset in cache 0. We need to store some additional address information with the entry to be sure we can identify it. Fortunately all entries in a given cache block correspond to the same memory block in main memory. The solution is to store the memory block number in a special register associated with the cache block. In this arrangement, every memory block stored in cache block 0 has a memory block such that (MemBlock mod 4 == 0) so that we need store only Tag = MemBlock / 4. Suppose now that the word in the specified offset (0x06) in cache block 0 is really the word at address 0x486 in main memory. This corresponds to a memory block number of 0x48. Recalling that 0x48 = 416 + 8, we see that the memory block is (44 + 2)4, so that (MemBlock mod 4) == 0 and (MemBlock / 4) == 44 + 2 = 0x12. The cache tag is 0x12. The CPU is seeking an entry in memory block 0, but it is memory block 0x48 that is in the cache block. This is a cache miss, so that main memory has to be accessed and a new block brought into the cache. The data in cache block 0 will be overwritten. The main question is whether or not the data in the cache should be saved back to main memory before the new data are read in. Managing this function is the job of the dirty bit. The rules for handling the dirty bit are simple:
We now associate the following structure with each of the cache blocks in the above example. Recall that a memory block size is 16 words, and that there are 4 cache blocks. Rules for this cache memory Before continuing with a more general description of the primary vs. secondary memory scenario, we explicitly close this small illustration with a complete set of rules. This rule set is specific to the above example: 12-bit memory addresses, with 4 cache blocks of 16 words.
With this relaxed association of the memory block number to the cache block, the situation with the address space as in the next figure. Since we no longer have an automatic association of memory block with the cache block, we must consider a tag array associated with the cache. Suppose the following. Cache Block Memory Tag 0 0xA 1 0x 2 0xBF 3 0x The association represented by this table can be expressed as follows: Cache block 0 holds addresses 0xA00 through 0xA0F, Cache block 1 holds addresses 0x220 through 0x22F, Cache block 2 holds addresses 0xBF0 through 0xBFF, and Cache block 3 holds addresses 0x330 through 0x33F. Since we have just dropped the forced association of memory blocks with cache blocks, each memory reference must cause a search of all four elements of this array to determine which of the four cache blocks, if any, was associated with the memory reference. Talk about a big waste of time. There must be a better way. Naturally, there is a better way. Content Addressable Memory (Associative Memory) The difficulty with the above cache scheme is that it uses a standard memory structure to store the memory tags. The only way to access such a memory structure is via the address of each memory cell; we send an address and ask the contents of the cell. Content addressable memory , also called associative memory , is memory that is accessed via its contents; in such a memory the address is almost an afterthought. The basic memory unit of such a memory is closely related to that of standard memory and is best considered to be a modified flip–flop. The next figure shows a typical cell in associative memory. The most important structure in this drawing is the provision for a single bit, called the “Bit to Match” to be compared to the contents of the cell. Page 10 of 26 Chapter 6 Revised July 19, 2005
Calling the bit to be matched “B”, consider the output of the above circuit. Q B QB Q^ B 0 0 0 1 0 1 1 0 1 0 1 0 1 1 0 1 The output of this circuit is 1 if and only if the contents of the memory are the same as the bit to be matched. As in all discussions of associative memory, we acknowledge that the memory cell must be loaded somehow, but generally ignore that mechanism. The following diagram shows the output mechanism of an associative memory unit comprising two words of three bits each. There are three bits to be matched. If either of the 3–bit words in this memory contains bits that match the pattern being sought, its corresponding match line will be asserted. For example, suppose that B 2 B 1 B 0 = 011, and that words 0 and 1 contain 111 and 011 respectively. Then M 0 = 0 (no match) and M 1 = 1 (match). A complete associative memory contains provisions for a few additional features that need not concern us, as our goal is only to gain a basic understanding of this memory type. The additional features that a complete system should provide include:
Miscellaneous Comments on Primary Memory / Secondary Memory We now make a few comments that apply to both virtual memory and cache memory. How to Break Caching The utility of a cache memory (really, of any primary/secondary memory combination) depends on the locality of references. This is especially true in array references, when the arrays are accessed in a infelicitous order. To see what is happening, let us consider the storage of two–dimensional arrays in a computer. For example consider A[2][3], an array with row indices 0 and 1 and column indices 0, 1, 2. There are two orders in which the array can be stored: row major order and column major order. Here they are: Row Major Column Major A[0][0] A[0][0] A[0][1] A[1][0] A[0][2] A[0][1] A[1][0] A[1][1] A[1][1] A[0][2] A[1][2] A[1][2] To see how array references can come to grief, recall the example of direct mapped caching as it might be applied to the MARIE. The address structure is as follows. In this scenario, word 0 of cache block 0 would contain addresses 0, 64, 128, 192, etc. Now suppose an array, declared as A[64][4] with the first element A[0][0] associated with an address that maps to cache block 0. Suppose the following code fragment For (J = 0; J < 64; J++) For (K = 0; K < 4; K++) A[J][K] = 0 If the array is stored in row major order , then we have the following associations for the first eight entries in the cache block 0 in the cache memory. Offset 0 A[0][0] A[16][0] A[20][0] 1 A[0][1] A[16][1] A[20][1] 2 A[0][2] A[16][2] A[20][2] 3 A[0][3] A[16][3] A[20][3] 4 A[1][0] A[17][0] A[21][0] 5 A[1][1] A[17][1] A[21][1] 6 A[1][2] A[17][2] A[21][2] 7 A[1][3] A[17][3] A[21][3] The array accesses for this row major order will make optimal use of the cache. The first sixteen references will be from different offsets in cache block 0, which will not again be accessed until 48 more array accesses have been made. Page 14 of 26 Chapter 6 Revised July 19, 2005
Cache Write Policies What to do when an item is written to primary memory? Unless the circuit takes extra time to compare the old contents to the new contents, it must assume that the contents of the block in the primary memory now differ from those in the secondary memory; the block is “dirty”. Eventually, the contents of the memory block in secondary memory must be changed to agree with the contents of the block in the primary memory; the question is when to do this. The choice of handling dirty blocks is called the “ cache write policy ” or just “ write policy ”. In the policy called “ write–through ”, the secondary memory block is updated at the same time as the primary memory block. As an immediate result, the primary memory block can never become “dirty”, because it always is consistent with its corresponding memory block in secondary memory. The main problem is that every write to primary memory is accompanied by a write to secondary memory, and thus proceeds at the slower pace. In a cache memory scenario, the choice on this policy depends on the frequency of memory accesses that are memory writes vs. memory reads. If the fraction is small enough (and it usually is), then the slow–down due to this policy is negligible. In a virtual memory scenario, such a policy would require a disk write for every memory write; resulting in a performance degradation that would not be tolerated. In the policy called “ write–back ”, the dirty bit is set on a primary memory block that has been changed, and the primary memory block is copied back to secondary memory only when the replacement policy (defined below) selects its slot to be filled by another memory block. This strategy is considerably faster than write–through, but suffers the problem that the primary memory and secondary memory may not be consistent. Obviously, virtual memory must use some variant of this policy. While we are at it, we should mention the “ cache coherence ” problem that can appear in multi–processor systems. For the moment, ignore virtual memory and focus on two processors, each with its own cache memory sharing a common main memory. The scenario is simple, and is a variant of a problem that appears in databases.
Replacement Policies We now consider the replacement policy for a primary memory. Suppose that the primary memory is full; every block contains valid data. If the system needs to read a new memory block from secondary into the primary memory, one of the blocks in primary memory must be overwritten. The replacement policy determines which block is to be overwritten. Remember the function of the “dirty bit”. If the primary memory block to be overwritten is dirty, its contents are always written back to the appropriate block in secondary memory. Direct Mapped Primary Memory This option is more often used with cache memories, but can also be applied in virtual memory. In this strategy, the primary memory block is already determined for every memory block from secondary memory; thus the replacement policy is fixed. In our discussion of a direct mapped cache for the MARIE, we postulated memory in which MemBlock = Address / 16 CacheBlock = MemBlock Mod 4 For this strategy, the replacement policy is automatic.
SDRAM – Synchronous Dynamic Random Access Memory As we mentioned above, the relative slowness of memory as compared to the CPU has long been a point of concern among computer designers. One recent development that is used to address this problem is SDRAM – synchronous dynamic access memory. The standard memory types we have discussed up to this point are SRAM Static Random Access Memory Typical access time: 5 – 10 nanoseconds Implemented with 6 transistors: costly and fairly large. DRAM Dynamic Random Access Memory Typical access time: 50 – 70 nanoseconds Implemented with one capacitor and one transistor: small and cheap. In a way, the desirable approach would be to make the entire memory to be SRAM. Such a memory would be about as fast as possible, but would suffer from a number of setbacks, including very large cost (given current economics a 256 MB memory might cost in excess of $20,000) and unwieldy size. The current practice, which leads to feasible designs, is to use large amounts of DRAM for memory. This leads to an obvious difficulty.
The main difference between level 1 (L1) cache and level 2 (L2) cache is that the L1 cache is on the CPU chip, while the L2 cache generally is on a different chip. For example, the IBM S390/G5 chip has 256 KB of L1 cache on the CPU chip and 4MB of L2 cache. It is likely that the existence of two (or more) levels of cache, rather than one large L1 cache, is due to two aspects of area constraints: area on the CPU chip that can be allocated to the L1 cache and the fact that larger SRAM memories tend to be slower than the smaller ones. L1 cache memory is uniformly implemented as SRAM (Static RAM), while L2 cache memory may be either SRAM or SDRAM. The SDRAM chip uses a number of tricks to deliver data at an acceptable rate. As an example, let’s consider a modern SDRAM chip capable of supporting a DDR data bus. In order to appreciate the SDRAM chip, we must begin with simpler chips and work up. We begin with noting an approach that actually imposes a performance hit – address multiplexing. Consider an NTE2164, a typical 64Kb chip. With 64K of addressable units, we would expect 16 address lines, as 64K = 2^16. In stead we find 8 address lines and two additional control lines RAS Row Address Strobe (Active Low) CAS Column Address Strobe (Active Low) Here is how it works. Recalling that 64K = 2^16 = 2^8 28 = 256 256, we organize the memory as a 256-by-256 square array. Every item in the memory is uniquely identified by two addresses – its row address and its column address. Here is the way that the 8-bit address is interpreted. RAS CAS Action 0 0 An error – this had better not happen. 0 1 It is a row address (say the high order 8-bits of the 16-bit address) 1 0 It is a column address (say the low order 8-bits of the 16-bit address) 1 1 It is ignored. Here is that way that the NTE2164 would be addressed.