当我使用要分配大页的内存大小时,即当我定义 LENGTH = 4*1024 时,出现段错误。当我定义4*1024*1024时,没有段错误。造成这种情况的根本原因是什么?
代码如下:
#define _POSIX_C_SOURCE 199309
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#define PROTECTION (PROT_READ | PROT_WRITE)
#define LENGTH (4*1024)
//#define LENGTH (4*1024*1024)
#define LINE_SIZE 64
#define ASSOC 16
#define CACHE_SIZE (4*1024*1024)
#define WAY_SIZE (CACHE_SIZE/ASSOC)
#ifndef MAP_HUGETLB
#define MAP_HUGETLB 0x40000
#endif
#define ADDR (void *) (0x0UL)
#define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB)
int main (int argc, char *argv[]){
...
// allocate a buffer with the same size as the LLC using huge pages
buf = mmap(ADDR, LENGTH, PROTECTION, FLAGS, 0, 0);
if (buf == MAP_FAILED) {
perror("mmap");
exit(1);
}
set_S = atoi(argv[1]);
set_M = atoi(argv[2]);
printf("test 0\n");
initialize(set_S);
printf("test 1\n");
initialize(set_M);
printf("test 2\n");
head_S = &buf[set_S*LINE_SIZE];
printf("test 3\n");
head_M = &buf[set_M*LINE_SIZE];
...
}
请注意,我的计算机默认没有启用大页面,因此我以 root 身份将其打开:
echo 20 > /proc/sys/vm/nr_hugepages
这是我打开大页面后/proc/meminfo中的大页面相关信息:
HugePages_Total: 20
HugePages_Free: 20
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
程序输出:
$./a.out 1 1
test 0
Segmentation fault (core dumped)
$uname 输出:
Linux mymachine 3.13.0-43-generic #72-Ubuntu SMP Mon Dec 8 19:35:06 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
编辑1:添加初始化函数:
void initialize(int set){
int j, k;
char ** ptr1, ** ptr2;
char * tmp;
// re-initialize pointer array of size "size"
// implementation of Sattolo's random cyclic permutation
for (j=0; j<ASSOC; j++){
ptr1 = (char **)&buf[set*LINE_SIZE+j*WAY_SIZE];
*ptr1 = (char*)ptr1;
}
// permute each set
for (j=ASSOC-1; j>=1; j--){
k = rand()%j;
ptr1 = (char **)&buf[set*LINE_SIZE+j*WAY_SIZE];
ptr2 = (char **)&buf[set*LINE_SIZE+k*WAY_SIZE];
tmp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = tmp;
}
}
最佳答案
访问大于 mmap 长度的 set*LINE_SIZE+(ASSOC-1)*WAY_SIZE = set*64 + CACHE_SIZE - WAY_SIZE = set*64 + 4*1024*1024 - WAY_SIZE
时 LENGTH = 4*1024
在 initialize
循环中,您正在越界访问内存。这就是定义 LENGTH = 4*1024 时出现段错误的原因。
关于c - 更改大页分配的大小时由于initialize()函数导致的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27707319/