为什么在32位armv7l上成功映射4GB文件? [英] Why mmap a 4GB file on 32-bit armv7l succeeded?
问题描述
我从mmap(2)
手册页和搜索结果中得到的印象是,mmap
仅限于系统的可用地址空间,减去系统保留的地址空间.因此,在32位armv7l上,我假设它约为3GB =(4GB-1GB).
I had the impression from the mmap(2)
man page and search results, that mmap
is only limited to system's available address spaces, minus the system reserved address spaces. So on 32-bit armv7l, I assume it's around 3GB = (4GB - 1GB).
但是看来我实际上可以mmap
一个5 GB的文件而没有任何问题:
But it seemed like I could actually mmap
a 5 GB file without any problem:
int main(int argc, char** argv) {
// stats
char * path = argv[1];
struct stat sb;
stat(path, &sb);
std::cout << "File size: " << sb.st_size << std::endl;
// open
int fd = open(path, O_RDONLY, S_IRWXU);
std::cout << "File descriptor: " << fd << std::endl;
int i;
for (i =0; i<10; ++i){
void *pa = mmap(
nullptr, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
std::cout << "PA: " << pa
<< ", MAP_FAILED: "
<< (pa == MAP_FAILED) << ", Status: "
<< strerror(errno) << std::endl;
}
}
使用-D_FILE_OFFSET_BITS=64
标志进行编译:
g++ -D_FILE_OFFSET_BITS=64 test.cc
结果如下:
File size: 5045966585
File descriptor: 3
PA: 0x89f80000, MAP_FAILED: 0, Status: Success
PA: 0x5d34a000, MAP_FAILED: 0, Status: Success
PA: 0x30714000, MAP_FAILED: 0, Status: Success
PA: 0x3ade000, MAP_FAILED: 0, Status: Success
PA: 0xffffffff, MAP_FAILED: 1, Status: Cannot allocate memory
PA: 0xffffffff, MAP_FAILED: 1, Status: Cannot allocate memory
PA: 0xffffffff, MAP_FAILED: 1, Status: Cannot allocate memory
PA: 0xffffffff, MAP_FAILED: 1, Status: Cannot allocate memory
PA: 0xffffffff, MAP_FAILED: 1, Status: Cannot allocate memory
PA: 0xffffffff, MAP_FAILED: 1, Status: Cannot allocate memory
从结果来看,mmap在经历真正的麻烦之前已经成功了4次.但这不应该成功,因为文件约为5GB.
From the results, mmap succeeded for 4 times before going into real troubles. But it shouldn't have been succeeded since the file is ~5GB.
我的问题是:
-
mmap
会发生这种情况吗? - 如果没有,我在哪里做错了?
- Is this behavior expected for
mmap
? - If not, where did I do wrong?
如果有物理地址扩展(PAE),则32位系统可以添加的地址远远超过2 ^ 32字节.
With physical addres extension (PAE) 32-bit systems can addres much more than 2^32 bytes, if that is available.
此CPU不支持PAE
There's no PAE support for this CPU
$> cat /proc/cpuinfo
Processor : ARMv7 Processor rev 4 (v7l)
processor : 0
BogoMIPS : 1436.46
processor : 1
BogoMIPS : 1436.46
Features : swp half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 4
Hardware : sun7i
Revision : 0000
Serial : 09c11b9d52544848804857831651664b
推荐答案
PAE不相关.这与访问大量物理内存无关.
PAE is irrelevant. This is not about accessing large amounts of physical memory.
问题是您的mmap
函数采用32位值表示映射大小.因此,您的64位大小会被截断,实际上您分配的虚拟内存不到1 GB.
The problem is that your mmap
function takes a 32-bit value for the size of the mapping. So your 64-bit size gets truncated and you're actually allocating less than 1 GB of virtual memory.
这篇关于为什么在32位armv7l上成功映射4GB文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!