首页 > 代码库 > CSAPP 六个重要实验 lab4
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 source code differences among the two Java implementations?
两者src dst的数据类型不同.一个是int 另一个时Integer
(摸着良心说,目前为止,我仅钟爱于C,java仅仅写过hello world... 对于这个问题不能给出很漂亮的回答.)
2. Pick a single pair of results that most surprised you. What is it about the results that surprised you? (That is,
from the 32 pairs of measurement results, pick one pair whose relationship is least like what you would have
guessed.)
3. [Optional extra credit] None of these programs appear to actually do anything, so one is tempted to optimize
them by simply eliminating all code (resulting in an empty main()). Is that a correct optimization? Related to
that, try compiling this C program, with and without optimization, and then time running it:
我认为省略main的参数不会对程序有所优化(个人观点)
这里需要根据实验指导书去copy书上的一段代码,自己编译运行测试
#include <stdio.h> #define SIZE 10000 int main() { int i,j,k; int sum = 1; for(i = 0;i < SIZE; i++) { for(j = 0;j < SIZE;j++) { for(k = 0;k < SIZE;k++) { sum = -sum; } } } // printf("Hello world!\n"); printf("Sum is %d\n",sum); return 0; }
感觉intel i5 8G RAM 的配置都跑不动
反正我等了好久都出不来结果, 追究这东东意义不大了 , next station
Part II: Inferring Mystery Cache Geometries
这个实验是以CSAPP第六章第五节的cache为基础的,没搞清楚cache玩不转的
Instructions
Specifically, each of these "processors" is provided as an object file (.o file) against which you will link your code. See the file mystery-cache.h for documentation of the function interface that these object files export.
Your job is to fill in the function stubs in cache-test-skel.c which, when linked with one of these cache object files, will determine and then output the cache size, associativity, and block size. Some of the provided object files are named with this information (e.g. cache_64c_2a_16b.o is a 64 KB capacity, 2- way set -associative cache with 16B blocks) to help you check your work.There are also 4 mystery cache object files, whose parameters you must discover on your own.
You can assume that the mystery caches have sizes that are powers of 2 and use a least recently used replacement policy.
You cannot assume anything else about the cache parameters except what you can infer from the cache size. Finally, the mystery caches are all pretty realistic in their geometries, so use this fact to sanity check your results.
Your Tasks
Complete the 3 functions in cache-test-skel.c which have /* YOUR CODE GOES HERE */ comments in them.
Additionally, determine the geometry of each of the four mystery caches and list these in a comment, along with your name, at the top of your modified cache-test-skel.c.
我在做第三个的时候卡了很久,在这里特别要感谢Chenbo Li,是看了他的github 我才得以解脱...
先释出题解,过段时间再给出分析 : )
/* YOUR NAME HERE CSE 351 - Winter 2013 Lab 4 - Mystery Caches Mystery Cache Geometries: mystery0: block size = 64 bytes cache size = 262144 bytes associativity = 1 mystery1: block size = 8 bytes cache size = 16384 bytes associativity = 4 mystery2: block size = 16 bytes cache size = 65536 bytes associativity = 16 mystery3: block size = 2 bytes cache size = 131072 bytes associativity = 2 */ #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) { int block_counter = 0; flush_cache(); access_cache(0); for(block_counter = 0; access_cache(block_counter);block_counter++) { } return block_counter; } /* Returns the size (in B) of the cache. */ int get_cache_size(int block_size) { int possible_cache_size; int tmp = 0; flush_cache(); for(possible_cache_size = 1; 1 ;possible_cache_size<<=1) { for(tmp = 0;tmp <= possible_cache_size;tmp += block_size) { access_cache(tmp); } if(!access_cache(0)) { break; } } return possible_cache_size; } /* Returns the associativity of the cache. */ int get_cache_assoc(int cache_size) { int tmp = cache_size; int addr = 0; flush_cache(); for(addr = 0; 1; addr += cache_size) { access_cache(addr); for(tmp = 0;tmp <= addr;tmp += cache_size) { if(!access_cache(tmp)) { return addr/cache_size; } } } return -1; } 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; }
古典美 格威德(又译格维得) 英国 1906年 画布油画 40.60×30.50厘米
这幅作品正是格威德擅长描绘的古典美人着古典服装,依靠在大理石上的女性形象。观众透过薄如蝉翼的服饰,看到了少女的美丽。少女楚楚动人的容貌让人浮想联翩。观看这幅作品,有一种仰视纪念碑似的感觉,这是画家的有意安排。
CSAPP 六个重要实验 lab4