计算机结构(Computer Architecture) --------- 缓存(Cache & TLB)

  • 简介:

    • Build memory as a hierarchy of levels, with the fastest memory close to the processor, and the slower, less expensive memory below that.

      Four major technologies used to construct memory hierarchy:

计算机结构(Computer Architecture) --------- 缓存(Cache & TLB)_第1张图片

通常架构:

计算机结构(Computer Architecture) --------- 缓存(Cache & TLB)_第2张图片

形象化解释:You are reading book, “Register” is like your hand and take a book, “Cache” is a desk that you can put some books on it , “DRAM” is like a bookshelf that stores a lot of books, if you still can’t find a book which you want ,you can go to “disk”, it is a library.

 

  • Cahce:

    • 通常memory hierarchy 会采用2-3层Cache。
    • Cache的结构:
      • 以32位操作系为例,r + m + n = 32bit:
        • Index: Cache有  行或者是 组。Cache是被分为一行一行(Cache Line),有的架构会每几行被分为一组。

        • Tag:内存(DRAM)是分成一块一块的(block),tag用确定Cache中某一行存放的是内存中的哪一个block的数据。

        • Block offset:Cache中每一行存放的block数据大小为  byte,由word和Byte offset组成。Byte offset由操作系统决定,例如:32位操作系统,Byte offset = 4 byte, 那么word个数就为 个。计算机结构(Computer Architecture) --------- 缓存(Cache & TLB)_第3张图片

    • Cache映射策略:
      1. 直接映射Cache (Direct-mapping Cache) : 内存中的某一个block只能对应放在Cache中的固定的某一行。计算机结构(Computer Architecture) --------- 缓存(Cache & TLB)_第4张图片
      2. 组相联Cache (Set-associative Cache) :Cache分组,内存中的某一个block只能放入Cache中的固定一组,但在组里可以放任意一行。即组间采用Direct-mapping,组內采用Full-associative。计算机结构(Computer Architecture) --------- 缓存(Cache & TLB)_第5张图片
      3. 全相联Cache (Full-associative Cache):内存中的某一个block能放入Cache中的任意一行。此时Cache地址结构里没有Index栏位,全部并成tag栏位。
      4. 具体栗子:entries即是cache line,可以看到Index为8bit,所以Cache有256个entries。每一个entry有2^{4}个Word,每个word为4Byte = 32 bits。剩下的Tag即为18bits。
    • 当cpu指令來,先按Index在Cache中找到对应的entry,取出Tag和Vaild bit 判断数据是否存在Cache,如果Hit就取出Data中的数据,不存在就通过address到内存中取入cache。计算机结构(Computer Architecture) --------- 缓存(Cache & TLB)_第6张图片

 

  • Cache大小计算:
    • How many total bits are required for a direct-mapped cache with 16KB of data and 4-word blocks, assuming 32-bit address?

      Answer:

      Because it is 32-bit address,so a word is 4 Byte , Byte offset is 2 bit,4-word blocks ,so Block offset is 4 bit;

      Number of entry =  16KB/2^{4}B = 1024 = 2^{10}   -->  so Index is 10 bit ;

      Tag bit = 32 - 10 - 2(word) – 2(byte offset) = 18 bit ;

      Block size = 4*4*8 = 128 bit;

      Vaild field size : 1bit;

      So Cache size: 2^{10} *(18 + 1 + 128) = 147Kbits;

    • Assume a cache of 4K-entry caches, a 4-word blocks, four-way set associative and a 32-bit address.

      Answer:

      Because it is 32-bit address,so a word is 4 Byte , Byte offset is 2 bit,4-word blocks,Block offset is 4 bit;

      The bit of index and tag : 32 – 4 = 28 bit ,(4为 block offset)

      The number of set : 4K/4 = 1K = 2^{10}  --> so Index is 10 bit;

      Tag bit = 28 -10 = 18 bit;

      Valid field size : 1bit;

      So cache size : 2^{10} *(18 + 1 + 128)*4 = 588Kbits;(乘4,一组里有4个entry)。

 

 

  • TLB:Translation-lookaside buffer

    • 本质就是Cache中的page table。
    • 为了加快存取速度,在Cache中,会创建一个最近常使用的page构成的table,相当于把page table的一部分内容放入Cache中,减少到内存中读取page table的效能损耗。和page table不同的是,TLB将 virtual page number 存放在了tag中,data中放的就是physical page number,同样也会有Dirty、ref、valid bit。TLB相当于page table的子集,如果Virtual address在TLB中hit,在page table中也一定hit。
    • 计算机结构(Computer Architecture) --------- 缓存(Cache & TLB)_第7张图片
      • Page Fault 发生情况:
        1. 不属于page fault:
          1. CPU指令 \mapsto 查TLB hit \mapsto查Cache hit \mapsto data返回CPU完成指令。
          2. CPU指令 \mapsto 查TLB hit \mapsto 查Cache miss \mapsto 根据TLB address 从 memory中读入Cache \mapsto data返回CPU完成指令。
          3. CPU指令 \mapsto 查TLB miss \mapsto 查Page table hit \mapsto 查Cache hit \mapsto 更新TLB,data返回CPU完成指令。
          4. CPU指令 \mapsto 查TLB miss \mapsto 查Page table hit \mapsto 查Cache miss \mapsto 从 memory中读入Cache \mapsto 更新TLB,data返回CPU完成指令。
        2. page fault:
          1. CPU指令 \mapsto 查TLB miss \mapsto 查Page table miss \mapsto 发生Page Fault 根据换页算法,将disk中的需求page替换进memory。

 

 

 

仅为个人理解,如有不足,请指教。 https://blog.csdn.net/weixin_35811044

你可能感兴趣的:(计算机结构)