x86-64 - 在不同级别的缓存中创建 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);
}
解决方案
推荐阅读
- rust - 如何在 Rust 中实现运行时选择的策略?
- python - Python重复命名
- python - python BeautifulSoup 和 Requests 的问题
- python - 在 Python 中过滤 JSON 元素不起作用
- node.js - 通过 chrome 终端将 Access-Control-Allow-Origin 添加到 fetch 方法的标题中
- c# - 从另一台 PC 访问 C# SQL 数据库
- mysql - 在 sequelize bulkCreate 时间戳不更新
- php - 使用 Nginx 托管超过 1 个简单的 wordpress 站点时出现重复的上游错误
- reactjs - 带有firebase auth的Django Rest框架权限
- java - 在 org.gradle.api.internal.artifacts 类型的对象上找不到参数 [{group=junit, name=junit, version=4.12}] 的方法 testCompile()