深入理解计算机系统Cache实验源代码

这段代码展示了如何实现一个简单的Cache模拟器,包括初始化Cache、访问数据、回放跟踪文件等功能。通过命令行参数设置Cache的大小、块大小和关联度,并使用LRU替换策略。访问数据时,如果命中则增加命中计数,未命中则增加缺失计数,并可能触发驱逐操作。
摘要由CSDN通过智能技术生成
#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include "cachelab.h"


//#define DEBUG_ON
#define ADDRESS_LENGTH 64


/* Type: Memory address */
typedef unsigned long long int mem_addr_t;


/* Type: Cache line
   LRU is a counter used to implement LRU replacement policy  */
typedef struct cache_line
{
    char valid;
    mem_addr_t tag;
    unsigned long long int lru;
} cache_line_t;


typedef cache_line_t* cache_set_t;
typedef cache_set_t* cache_t;


/* Globals set by command line args */
int verbosity = 0; /* print trace if set */
int s = 0; /* set index bits */
int b = 0; /* block offset bits */
int E = 0; /* associativity */
char* trace_file = NULL;


/* Derived from command line args */
int S; /* number of sets */
int B; /* block size (bytes) */


/* Counters used to record cache statistics */
int miss_count = 0;
int hit_count = 0;
int eviction_count = 0;
unsigned long long int lru_counter = 1;


/* The cache we are simulating */
cache_t cache;
mem_addr_t set_index_mask;


/*
 * initCache - Allocate memory, write 0's for valid and tag and LRU
 *
 */
void initCache()
{
    cache = malloc(S*sizeof(cache_set_t));
    int i;
   
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值