What's the max file mapping size in 64bits machine

后端 未结 5 1292
情话喂你
情话喂你 2021-01-12 15:55

I\'m new to 64-bits architecture. Could you tell me what\'s MAX file size supported by file mapping in 64 bits linux machine. I want to open more than 20GB files by file map

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-12 16:20

    Agree with MarkR, you are dereference an invalid address.

    // A bug in these lines.
    unsigned char* pCur = pBegin + GBSIZE;      
    printf("%c",*pCur);  
    
    unsigned char* pEnd = pBegin + NUMSIZE;  
    unsigned char* pLast = pEnd - 1;
    unsigned char* pCur = pLast;
    

    I modified your code to use HUGE TLB flags as the following.

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #define MAP_HUGETLB     0x40000         /*  create a huge page mapping */
    #define MAP_HUGE_SHIFT  26
    #define MAP_HUGE_1GB    (30 << MAP_HUGE_SHIFT)
    
    #define KSIZE 1024L
    #define MSIZE (1024L*1024L)
    #define GSIZE (1024L*1024L*1024L)
    #define TSIZE (1024L*GSIZE)
    
    #define INIT_MEM 0
    // Fail on my MacBook Pro (Retina, 13-inch, Early 2015)
    // Darwin Kernel Version 16.5.0:x86_64
    // #define NUMSIZE  (16L * TSIZE)
    
    // mmap ok; init: got killed; signal 9
    // #define NUMSIZE  (8L * TSIZE)
    
    // Got killed signal 9
    // #define NUMSIZE  (1L * TSIZE)
    
    // OK
    // #define NUMSIZE  (200L * GSIZE)
    
    // OK
    #define NUMSIZE  (20L * GSIZE)
    typedef unsigned long long ETYPE;
    
    #define MEMSIZE (NUMSIZE*sizeof(ETYPE))
    #define PGSIZE (16*KSIZE)
    
    void init(ETYPE* ptr) {
            *ptr = (ETYPE)ptr;
    }
    
    int verify(ETYPE* ptr) {
            if (*ptr != (ETYPE)ptr) {
                    fprintf(stderr, "ERROR: 0x%016llx != %p.\n", *ptr, ptr);
                    return -1;
            }
            else {
                    fprintf(stdout, "OK: 0x%016llx = %p.\n", *ptr, ptr);
            }
            return 0;
    }
    int main(int argc, char *argv[])
    {
        int i;
        int fd;
        ETYPE *pBegin;
    
        int flags = MAP_SHARED | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_1GB;
        printf("mmap memory size:%lu GB\n", MEMSIZE/GSIZE);
        pBegin = (ETYPE*) mmap(0, MEMSIZE, PROT_READ | PROT_WRITE, flags, -1, 0);
        if (pBegin == MAP_FAILED) {
            perror("Error mmapping the file");
            exit(EXIT_FAILURE);
        }
    
        ETYPE* pEnd = pBegin + NUMSIZE;
        ETYPE* pCur = pBegin;
    
    #if INIT_MEM
        while (pCur < pEnd) {
                init(pCur);
                // ++pCur;  //slow if init all addresses.
                pCur += (PGSIZE/sizeof(ETYPE)); 
        }
    #endif
    
        init(&pBegin[0]);
        init(&pBegin[NUMSIZE-1]);
    
        verify(&pBegin[0]);
        verify(&pBegin[NUMSIZE-1]);
    
        if (munmap(pBegin, MEMSIZE) == -1) {
            perror("Error un-mmapping the file");
        }
        return 0;
    }
    

提交回复
热议问题