码迷,mamicode.com
首页 > 系统相关 > 详细

Lab 4: Cache Geometries

时间:2015-09-28 14:37:52      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:

这次的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\n", 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\n", block_size);
  printf("Cache size: %d bytes\n", size);
  printf("Cache associativity: %d\n", 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

 

Lab 4: Cache Geometries

标签:

原文地址:http://www.cnblogs.com/whuyt/p/4843795.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!