Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why mmap a 4GB file on 32-bit armv7l succeeded?

Tags:

c++

memory

mmap

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).

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;
        }   
}

Compile with -D_FILE_OFFSET_BITS=64 flag:

g++  -D_FILE_OFFSET_BITS=64 test.cc

And the result yields:

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

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.

My questions would be:

  1. Is this behavior expected for mmap?
  2. If not, where did I do wrong?

Edit:

With physical addres extension (PAE) 32-bit systems can addres much more than 2^32 bytes, if that is available.

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
like image 702
c3V6a2Vy Avatar asked May 24 '16 09:05

c3V6a2Vy


1 Answers

PAE is irrelevant. This is not about accessing large amounts of physical memory.

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.

like image 112
David Schwartz Avatar answered Nov 15 '22 15:11

David Schwartz