Shang Jian Ding
Shang Jian Ding

Reputation: 2146

Check if a System V shared memory segment is backed by huge pages or regular pages

To allocate a System V shared memory segment, one can use shmget() with the SHM_HUGETLB flag.

Is there a way to check whether a System V shared memory segment is backed by huge pages or regular pages assuming that we do not know how the original creator of this memory segment used the shmget() system call.

Upvotes: 3

Views: 565

Answers (2)

jww
jww

Reputation: 102426

I don't believe System V shared memory segment influences the size of a page. That is a function of the os and cpu configuration. Also see What is the size of shared memory page? and friends.

On Linux you can call getpagesize(2) to determine the page size:

#include <unistd.h>
int size = getpagesize();

You can also call sysconf(3):

#include <unistd.h>
long size = sysconf(PAGESIZE);

One thing though... Glibc may not be able to determine the pagesize. You should check that size>0 and size is a multiple of 2. Treat anything else like an error and use a default page size:

#include <unistd.h>
long size = sysconf(PAGESIZE);
if (size <= 0)
    size = 4096;

Though -1 is a failure, I've had Glibc return bogus values on PowerPC, like 0 instead of failure, for a cache line size (the cache line size is either 64 or 128; never 0). Also see Bug 0014599, sysconf(_SC_LEVEL1_DCACHE_LINESIZE) returns 0 instead of 128.

Also see How can i calculate the size of shared memory available to the system, where another bogus value is returned on Red Hat systems.

Upvotes: 0

Shang Jian Ding
Shang Jian Ding

Reputation: 2146

Ok, I think I figured it out.

One method is to attach to the shared memory segment (or rely on a process which is already attached), and examine /proc/[PID]/smaps to find the shared memory segment of interest and look at the corresponding KernelPageSize field to see it matches up with the server's configured Hugepagesize

Upvotes: 2

Related Questions