📄 victim.c
字号:
#include <stdio.h>#include <stdlib.h>#include "cache.h"#include "config.h"#include "misc.h"#include "victim.h"cache_t *victim_create(char *name, int nsets, int assoc, int block_size, enum cache_policy policy){ cache_t *cache; cache_set_t *set; cache_block_t *blk; int i, j; cache = (cache_t *)malloc(sizeof(cache_t)); /*initialize the cache arguments*/ cache->name = name; cache->nsets = nsets; cache->log2_nsets = log2(nsets); cache->assoc = assoc; cache->log2_block_size = log2(block_size); cache->policy = policy; cache->victim = NULL; cache->set = (cache_set_t *)malloc(nsets*sizeof(cache_set_t)); /*allocate space for 'nsets' sets*/ for (i=0; i<nsets; i++) /*in a cache*/ { set = &(cache->set[i]); /*address of the first set*/ set->block = (cache_block_t *)malloc(assoc*sizeof(cache_block_t)); /*allocate sapces for*/ blk = &(set->block[0]); /*address of the first block*/ /*'assoc' blocks in a set*/ for (j=0; j<assoc; j++) { blk->valid = FALSE; blk++; } } return cache;}/*There are three situations: 1. Primary cache has desired data-->normal *//* 2. Primary has not the desired data, but victim has --> *//* a.Supply CPU with the data; */ /* b.Promote the block in victim to primary *//* 3. Both primary and victim have not the data--> *//* a. Fetch from memory into the primary cache *//* b. The block that is replaced in the primary cache is */ /* moved to the victim cache. *//************************************************************************/voidvictim_sim(cache_t *primary_cache, cache_t *victim_cache, FILE *input){ unsigned nhits, nmisses, nrefs, cycle; unsigned addr,addr_temp; cache_block_t *primary_blk, *victim_blk; int block_size = 1 << primary_cache->log2_block_size; unsigned temp_tag; nhits = nmisses = nrefs = cycle = 0; while (fread(&addr, sizeof(unsigned), 1, input)) { nrefs++; primary_blk = cache_locate(primary_cache, addr); /*returns a pointer to the block that contains the data*/ if (primary_blk) /* Hit */ /*situation 1*/ { nhits++; cache_update_time(primary_cache, primary_blk, cycle); cycle += HIT_TIME; } else { victim_blk = cache_locate(victim_cache, addr); if(victim_blk) /*situation 2*/ { /*returns a pointer to the block that needs to be repalced*/ primary_blk = cache_get_repl_block(primary_cache, addr); temp_tag = CACHE_TAG(victim_cache, REPLACED_CACHE_ADDR(primary_cache,primary_blk,addr,block_size)); primary_blk->valid = TRUE; primary_blk->tag = CACHE_TAG(primary_cache, addr); primary_blk->time = cycle; victim_blk->valid = TRUE; victim_blk->tag = temp_tag; victim_blk->time = cycle; cycle += HIT_TIME; } else /* Miss */ /*situation 3*/ { nmisses++; primary_blk = cache_get_repl_block(primary_cache, addr); victim_blk = cache_get_repl_block(victim_cache, addr); victim_blk->valid = TRUE; addr_temp = REPLACED_CACHE_ADDR(primary_cache,primary_blk,addr,block_size); victim_blk->tag = CACHE_TAG(victim_cache, addr_temp); victim_blk->time = cycle; primary_blk->valid = TRUE; primary_blk->tag = CACHE_TAG(primary_cache, addr); primary_blk->time = cycle; cycle += MISS_PENALTY(block_size); } }#ifdef DEBUG cache_dump(primary_cache);#endif } /* end while */ printf("CONVENTIONAL CACHE WITH A VICTIM CACHE:\n"); printf("Cache configuration:\n"); cache_print_config(primary_cache); printf("Victim configuration:\n"); cache_print_config(victim_cache); cache_print_stats(primary_cache, nrefs, nhits, nmisses, cycle); newline(2);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -