Aleksey Demakov
Aleksey Demakov

Reputation: 426

Huge pages on Mac OS X

The Mac OS X mmap man page says that it is possible to allocate superpages and I gather it is the same thing as Linux huge pages.

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/mmap.2.html

However the following simple test fails on Mac OS X (Yosemite 10.10.5):

#include <stdio.h>
#include <sys/mman.h>
#include <mach/vm_statistics.h>

int
main()
{
    void *p = mmap((void *) 0x000200000000, 8 * 1024 * 1024,
                   PROT_READ | PROT_WRITE,
                   MAP_ANON | MAP_FIXED | MAP_PRIVATE,
                   VM_FLAGS_SUPERPAGE_SIZE_2MB, 0);
    printf("%p\n", p);
    if (p == MAP_FAILED)
        perror(NULL);
    return 0;
}

The output is:

0xffffffffffffffff
Cannot allocate memory

The result is the same with MAP_FIXED removed from the flags and NULL supplied as the address argument. Replacing VM_FLAGS_SUPERPAGE_SIZE_2MB with -1 results in the expected result, that is no error occurs, but obviously the allocated memory space uses regular 4k pages then.

What might be a problem with allocating superpages this way?

Upvotes: 7

Views: 7932

Answers (1)

mppf
mppf

Reputation: 1845

This minor modification to the posted example works for me on Mac OS 10.10.5:

#include <stdio.h>
#include <sys/mman.h>
#include <mach/vm_statistics.h>

int
main()
{
    void *p = mmap(NULL,
                   8*1024*1024,
                   PROT_READ | PROT_WRITE,
                   MAP_ANON | MAP_PRIVATE,
                   VM_FLAGS_SUPERPAGE_SIZE_2MB, // mach flags in fd argument
                   0);
    printf("%p\n", p);
    if (p == MAP_FAILED)
        perror(NULL);
    return 0;
}

Upvotes: -1

Related Questions