Site Tools


Sidebar

software:reserving_hugepages

What?

Simple workflow to reserve hugepages and detect which process uses them.

# mount the fs
mkdir -p /mnt/hugetlbfs
mount -t hugetlbfs none /mnt/hugetlbfs -o pagesize=2m

# reserve the pages
echo 128 > /proc/sys/vm/nr_hugepages

# use code from hugepages.txt to reserve:
cat >hugepages.c<<EOT
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/mman.h>

#ifndef SHM_HUGETLB
#define SHM_HUGETLB 04000
#endif

#define LENGTH (256UL*1024*1024)

#define dprintf(x)  printf(x)

/* Only ia64 requires this */
#ifdef __ia64__
#define ADDR (void *)(0x8000000000000000UL)
#define SHMAT_FLAGS (SHM_RND)
#else
#define ADDR (void *)(0x0UL)
#define SHMAT_FLAGS (0)
#endif

int main(void)
{
        int shmid,key;
        unsigned long i;
        char *shmaddr;

        if ((shmid = shmget(2, LENGTH,
                            SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W)) < 0) {
                perror("shmget");
                exit(1);
        }
        printf("shmid: 0x%x\n", shmid);

        shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS);
        if (shmaddr == (char *)-1) {
                perror("Shared memory attach failure");
                shmctl(shmid, IPC_RMID, NULL);
                exit(2);
        }
        printf("shmaddr: %p\n", shmaddr);

        dprintf("Starting the writes:\n");
        for (i = 0; i < LENGTH; i++) {
                shmaddr[i] = (char)(i);
                if (!(i % (1024 * 1024)))
                        dprintf(".");
        }
        dprintf("\n");

        dprintf("Starting the Check...");
        for (i = 0; i < LENGTH; i++)
                if (shmaddr[i] != (char)i)
                        printf("\nIndex %lu mismatched\n", i);
        dprintf("Done.\n");

        printf("Press <enter> to quit: ");
        fflush(stdout);
        while ((key = getchar()) != '\n' && key != EOF) {
        }

        if (shmdt((const void *)shmaddr) != 0) {
                perror("Detach failure");
                shmctl(shmid, IPC_RMID, NULL);
                exit(3);
        }

        shmctl(shmid, IPC_RMID, NULL);

        return 0;
}
EOT

gcc hugepages.c

./a.out

# in a different terminal to check which process uses hugepages:
for i in /proc/*/smaps; do
    if [[ $(grep '^KernelPageSize' $i | grep -v '4 kB$') ]]; then
        echo -ne "file $i contains a pagesize different from 4kB:\n"; grep '^KernelPageSize' $i | grep -v '4 kB$';
    fi;
done
software/reserving_hugepages.txt ยท Last modified: 2022/11/13 12:06 by 127.0.0.1