首页 > 解决方案 > 在不同级别的缓存中创建 40 - 60% 的缓存未命中

问题描述

我正在尝试在 L1 和 L2 缓存中创建大量缓存未命中,如果可能的话以破坏缓存。下面的示例是我正在处理的代码的一部分。在这段代码中,c_size 是 L1、L2 和 L3 缓存的大小。在我的系统上,我的 L1、L2、L3 分别为 32Kb、256Kb 和 2048kb。高速缓存行是 64 字节。我的目标是在我读取或/和写入内存时创建缓存未命中。我尝试过使用不同的数组大小,但很少有缓存未命中(少于预期)。目标是 50%。我不确定我做得不好。请,有人可以解释如何实现这一目标吗?

 // allocate memory at an aligned address
  int PAGE_SIZE = getpagesize();
  int n_pages = c_size / PAGE_SIZE;
  if (c_size % PAGE_SIZE != 0) n_pages++;
  size_t alloc_size = n_pages * PAGE_SIZE;
  void *x = mmap ((void *)mmap_addr, alloc_size, PROT_READ|PROT_WRITE,
             MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, (off_t)0);
  if (x == MAP_FAILED || x != mmap_addr) {
  fprintf(stderr, "Could not allocate memory at %p: %s\n", mmap_addr, 
  strerror(errno));
  exit (1);

  }


  char *addr;
  int rand_t,rval;
  uint64_t urand;
  int i,j;
  unsigned int A[32768];
  uint64_t temp = 0xff000000ul << 24;

  if (access_type == Write){
  for(i = 0; i < 64; i++){
  rand_t = rand();
  A[i]=rand_t & temp;
  rval = rand_t & temp; // random byte 
  addr = (char *) (mmap_addr+(rand_t % c_size));
 //*addr = rval;
*addr = A[i];

 //asm("rdrand %r14");
  //printf("%p:\t%x\tmem:%hhx\n", addr, rval, *addr);
}

标签: x86-64cpu-cache

解决方案


推荐阅读