【发布时间】:2010-05-26 03:31:47
【问题描述】:
我一直在 openSUSE 11.2 x86_64 上处理大型稀疏文件。当我尝试 mmap() 一个 1TB 的稀疏文件时,它会因 ENOMEM 而失败。我原以为 64 位地址空间足以映射 TB,但似乎不是。进一步试验,一个 1GB 的文件可以正常工作,但一个 2GB 的文件(以及更大的文件)会失败。我猜可能有一个设置需要调整,但广泛搜索却一无所获。
这里有一些显示问题的示例代码 - 有什么线索吗?
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
char * filename = argv[1];
int fd;
off_t size = 1UL << 40; // 30 == 1GB, 40 == 1TB
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666);
ftruncate(fd, size);
printf("Created %ld byte sparse file\n", size);
char * buffer = (char *)mmap(NULL, (size_t)size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if ( buffer == MAP_FAILED ) {
perror("mmap");
exit(1);
}
printf("Done mmap - returned 0x0%lx\n", (unsigned long)buffer);
strcpy( buffer, "cafebabe" );
printf("Wrote to start\n");
strcpy( buffer + (size - 9), "deadbeef" );
printf("Wrote to end\n");
if ( munmap(buffer, (size_t)size) < 0 ) {
perror("munmap");
exit(1);
}
close(fd);
return 0;
}
【问题讨论】:
-
作为一个兴趣点,你的程序适用于我最大 256GB (
1 << 38) 的大小,任何更高的返回EINVAL。这是在 RHEL4(内核 2.6.9-42.0.3.ELsmp)上。 -
ulimit -a 说什么?
-
谢谢,bmargulies - 就是这样。 ulimit -a 报告的虚拟内存为 1804800 KB(略高于 1.7GB)。 ulimit -v 1610612736 (1.5TB) 让我映射我的 1TB 稀疏文件。我会回答我自己的问题,这样我就可以“关闭”它......