关联集缓存低估命中率

Associative Set Cache Underestimating Hit Rate

本文关键字:命中率 缓存 关联      更新时间:2023-10-16

我正在尝试实现一个使用最近最少使用的替换技术的集合关联缓存。到目前为止,我的代码低估了缓存命中率,我不知道为什么。下面发布的是我的函数setAssoc,它接受一个表示缓存关联性的int值,以及一个作为一系列数据访问的对向量。

该函数使用两个2D数组,一个用于存储缓存块,另一个用于在缓存中存储每个块的"年龄"。

对于这个特定的实现,不必担心标记位或任何类似性质的东西;简单地使用地址除以块大小就足以确定块的数目,然后使用块的数目模集的数目来确定集的数目就足够了。

任何关于为什么我可能无法准确预测缓存命中数的见解都将不胜感激!

int setAssoc(int associativity, vector<pair<unsigned long long, int>>& memAccess){
  int blockNum, setNum;
  int hitRate = 0;
  int numOfSets = 16384 / (associativity * 32);
  int cache [numOfSets][associativity];//used to store blocks
  int age [numOfSets][associativity];//used to store ages
  int maxAge = 0;
  int hit;//use this to signal a hit in the cache
  //set up cache here
  for(int i = 0; i < numOfSets; i++){
    for(int j = 0; j < associativity; j++){
      cache[i][j] = -1;//initialize all blocks to -1
      age[i][j] = 0;//initialize all ages to 0

    }//end for int j
  }//end for int i


  for(int i = 0; i < memAccess.size(); i++){
    blockNum = int ((memAccess[i].first) / 32);
    setNum = blockNum % numOfSets;
    hit = 0;
    for(int j = 0; j < associativity; j++){
        age[setNum][j]++;//age each entry in the cache
        if(cache[setNum][j] == blockNum){
            hitRate++;//increment hitRate if block is in cache
            age[setNum][j] = 0;//reset age of block since it was just accessed
            hit = 1;
        }//end if
    }//end for int j
    if(!hit){
        for(int j = 0; j < associativity; j++){
            //loop to find the least recently used block
            if(age[setNum][j] > maxAge){
                maxAge = j;
            }//end if
        }//end for int j        
        cache[setNum][maxAge] = blockNum;
        age[setNum][maxAge] = 0;
    }

  }//end for int i
  return hitRate;
}//end setAssoc function

不确定这是否是这段代码中唯一的问题,但您似乎混淆了年龄和路号。通过分配maxAge = j,您可以在年龄变量中放入任意的路号(这将干扰LRU路的查找)。然后将其用作方式索引。

我建议将其分为两个变量:

if(!hit){
    for(int j = 0; j < associativity; j++){
        //loop to find the least recently used block
        if(age[setNum][j] > maxAge){
            maxAgeWay = j;
            maxAge = age[setNum][j];
        }//end if
    }//end for int j        
    cache[setNum][maxAgeWay] = blockNum;
    age[setNum][maxAgeWay] = 0;
}

(当然要进行正确的初始化和绑定检查)