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\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

时间: 2024-08-03 00:27:50

Lab 4: Cache Geometries的相关文章

CSAPP 六个重要实验 lab4

CSAPP && lab4 实验材料: http://download.csdn.net/detail/u011368821/7926305 实验指导书: http://download.csdn.net/detail/u011368821/7926323 实验环境: Linux 3.13.11 Ubuntu 14.0 Part I: An Experiment in C and Java Q&A Answer these questions: 1.  What are the s

深入理解计算机系统 (CS:APP) - 高速缓存实验 Cache Lab 解析

原文地址:https://billc.io/2019/05/csapp-cachelab/ 这个实验是这学期的第四个实验.作为缓存这一章的配套实验,设计得非常精妙.难度上来讲,相比之前的修改现成文件,直接写一个程序也更高了一些.需要注意的是检查程序在编译时开启了 -Werror,需要保证没有警告才能成功编译. 从官方文档得知需要完善 csim.c 和 trans.c 文件,第一个是模拟一个高速缓存的程序并从由 valgrind 程序生成的 trace 文件中统计 hit, miss 和 evic

6.824 Lab 5: Caching Extents

Introduction In this lab you will modify YFS to cache extents, reducing the load on the extent server and improving YFS performance. The main challenge is to ensure consistency of extents cached at different yfs_clients: to make sure that each yfs_cl

Openflow Lab

GETTING STARTED OPENFLOW OPENVSWITCH TUTORIAL LAB : SETUP For a more up to date tutorial as anything more then 6 months old is outdated in the world of SDN Please see:OpenDaylight OpenStack Integration with DevStack on Fedora 20 I wrote a Python Open

[转]Openflow Lab

GETTING STARTED OPENFLOW OPENVSWITCH TUTORIAL LAB : SETUP For a more up to date tutorial as anything more then 6 months old is outdated in the world of SDN Please see: OpenDaylight OpenStack Integration with DevStack on Fedora 20 I wrote a Python Ope

嵌入式LAB 1:启动

嵌入式系统Lab 1 启动 1. 画出你所实际实施的连接示意图 2.给出实际拍摄的板卡连接照片 3.给出所用的器材的列表 pcduino(含电源).USB串口线.网线.SD卡.无线网卡 显示屏.鼠标.键盘.支持L2TP路由器.MacBook Air 4.给出得到的pcDuino启动时的输出文字,并逐行解释 U-Boot 2009.08 (Dec 25 2014 - 21:37:33) # U-Boot: Universal Boot Loader, 负责嵌入式 Linux 系统的引导 CPU:

CSAPP2e: Proxy lab 解答

这次的Proxy lab 是要求实现一个简单的web 代理.与此相关的章节是网络编程和并发编程,其实之前零零星星的看过一些讲HTTP协议的书,但是对于套接字这些都是一知半解,跟着课堂学完这两章突然柳暗花明,再看一些更详细更深入的书,像<HTTP权威指南>,<计算机网络-自顶向下的方法>就明白的多了. 下面就说一下这次lab,共有3个部分,第一部分是实现一个单线程代理,接收客户端请求,连接服务器然后转发.第二部分是实现并发,为每一个请求新建一个进程.第三部分是最有趣的,为每个请求建立

Remote DNS Cache Poisoning——山东大学网络攻防实验

实验描述 ①实验概观 本次实验的目标是让学生获得远程DNS缓存中毒攻击的第一手经验.DNS(Domain Name System)是互联网的电话簿,其将主机名转换为IP地址,反之亦然.这个转换通过DNS解析完成,且对用户是透明的.DNS Pharming攻击以各种方式操纵此解析过程,意图将用户误导到代替的目的地,而这往往是恶意的.本次实验我们聚焦一种特殊的DNS Pharming攻击,叫作DNS缓存中毒攻击. ②实验环境: (1)操作系统 实验使用seed-ubuntu-12.04系统,可在se

Centos 6.5上构建Git Lab服务及问题总结

前言: 官方上面说并不推荐在CentOS上面安装GitLab,无奈公司的服务器都是CentOS的系统..... 不过,好在经历了三天才算把GitLab搭建起来,查了N多参考教程,发现一个问题,并不是每个教程都是通用的.总会遇到某些未知的错误是其他作者没有碰到的,毕竟每台服务器的环境是不一样的,问题百出,很是让人很是头痛. 下面总结了自己在CentOS搭建GitLab的教程以及某些错误的解决办法,算是对自己的总结吧!或许那天就用到了...... --------------------------