mmap分配虚拟内存失败



在ftrace中得到以下输出:

mmap(0x200000000000, 17179869184, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Cannot allocate memory)

我代码:

void alloc_page_full_reverse()
{
printf("Allocating default pagesize pages > 128TB n");
mmap_chunks_higher(24575, 0);
printf("Allocating default pagesize pages < 128TB n");
/* Note: Allocating a 16GB chunk less due to heap space required 
for other mappings */
mmap_chunks_lower(8190, 0);
}
int mmap_chunks_higher(unsigned long no_of_chunks, unsigned long hugetlb_arg)
{
unsigned long i;
char *hptr;
char *hint;
int mmap_args = 0;
for (i = 0; i < no_of_chunks; i++){
hint = hind_addr();
hptr = mmap(hint, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | hugetlb_arg, -1, 0); // MAP_CHUNK_SIZE = 16GB
if (hptr == MAP_FAILED){
printf("n Map failed at address %p < 384TB in iteration = %d n", hptr, i);
exit(-1);   
}
if (validate_addr(hptr, 1)){
printf("n Address failed, not in > 128Tb iterator = %dn", i);
exit(-1);
}
}
printf("> 128Tb: n chunks allocated= %d n", i);
}
static char *hind_addr(void)
{
int bits = 48 + rand() % 15;
return (char *) (1UL << bits);
}

需要在mmap之前了解如何验证**voidmmap(voidaddr, size_t length, int prot, int flags, int fd, off_t offset);它的所有参数都被验证,EX: size_t length被验证

我仍然想确保在执行mmap之前我有足够的内存

没有一个接口允许进程检查这个,这是有充分理由的。假设存在这样一个系统调用,内核告诉一个进程它可以分配1 GB的内存。但是,在进程实际请求分配内存时,内核可能无法分配该内存。所以,这些信息是没有用的。

相反,您应该尝试分配内存,并处理ENOMEM。

最新更新