• Lab 4: Cache Geometries


    这次的lab特别简单,直接贴代码了

    /*
    Coursera HW/SW Interface
    Lab 4 - Mystery Caches
    
    Mystery Cache Geometries (for you to keep notes):
    mystery0:
        block size =
        cache size =
        associativity =
    mystery1:
        block size =
        cache size =
        associativity =
    mystery2:
        block size =
        cache size =
        associativity =
    mystery3:
        block size =
        cache size =
        associativity =
    */
    
    #include <stdlib.h>
    #include <stdio.h>
    
    #include "mystery-cache.h"
    
    /*
     * NOTE: When using access_cache() you do not need to provide a "real"
     * memory addresses. You can use any convenient integer value as a
     * memory address, you should not be able to cause a segmentation
     * fault by providing a memory address out of your programs address
     * space as the argument to access_cache.
     */
    
    /*
       Returns the size (in B) of each block in the cache.
    */
    int get_block_size(void) {
      /* YOUR CODE GOES HERE */
      cache_init(16000, 4);
      flush_cache();
      int back = 0x0000ff00;
      access_cache(back);
      while(access_cache(back)) {
        back+= 1;
      }
    
      flush_cache();
      int front = 0x0000ff00;
      access_cache(front);
      while(access_cache(front)) {
        front-= 1;
      }
    
      return back-front-1;
      
    }
    
    /*
       Returns the size (in B) of the cache.
    */
    int get_cache_size(int size) {
      /* YOUR CODE GOES HERE */
      // printf("%d
    ", s);
      flush_cache();
      int start = 0x00FFff00;
      int i = size / 2;
      int p;
    
      do {
        i *= 2;
        p = start;
        flush_cache();
        access_cache(start);
        while(p < start + i) {
          p += size;
          access_cache(p);
        }
        if (!access_cache(start))
          break;
      } while(1);
    
      return i;
    }
    
    /*
       Returns the associativity of the cache.
    */
    int get_cache_assoc(int size) {
      /* YOUR CODE GOES HERE */
    
      return get_cache_size(size) / size; 
    }
    
    //// DO NOT CHANGE ANYTHING BELOW THIS POINT
    int main(void) {
      int size;
      int assoc;
      int block_size;
    
      /* The cache needs to be initialized, but the parameters will be
         ignored by the mystery caches, as they are hard coded.  You can
         test your geometry paramter discovery routines by calling
         cache_init() w/ your own size and block size values. */
      cache_init(0,0);
    
      block_size=get_block_size();
      size=get_cache_size(block_size);
      assoc=get_cache_assoc(size);
    
      printf("Cache block size: %d bytes
    ", block_size);
      printf("Cache size: %d bytes
    ", size);
      printf("Cache associativity: %d
    ", assoc);
    
      return EXIT_SUCCESS;
    }

     get_block_size 中代码的意思是找到下一行的起始点,上一行的终结点,然后两者相减再减1就是block size

    get_cache_size 中代码的意思是不断访问一行行cache,一旦访问到某一行时最初那行变为未访问状态,就表明这行block被替换了,说明已经经过了cache size。

    get_cache_assoc 与 get_cache_size 相同,不过每次加的都是 cache size,这样每次访问都只会在一组内访问,一旦最初那组出现未访问,就说明被替代了。这样就能得出一组的行数。

    2015-09-28

  • 相关阅读:
    19. 各种提权姿势总结
    18. 各种数据库查询基础
    Kafka——分布式消息系统
    跳表
    Linux的io机制
    Make命令
    ElasticSearch之二——集群
    ElasticSearch之一——索引
    Scribe日志收集工具
    Thrift 的原理和使用
  • 原文地址:https://www.cnblogs.com/whuyt/p/4843795.html
Copyright © 2020-2023  润新知