What are Linux shared memory ‘segments’ vs. ‘pages’

linux-kernelmemory

The Linux kernel docs say:

shmall:

This parameter sets the total amount of shared memory pages that can
be used system wide. Hence, SHMALL should always be at least
ceil(shmmax/PAGE_SIZE).

But I can't find a definition for page. shmmax, for its part, defines the max size of a shared memory segment. So I don't understand this paragraph nor the ceil(shmmax/PAGE_SIZE) part.

(I'm researching this in order to determine how to set up my server for postgres.)

Best Answer

Page: A fixed length of contiguous block of virtual memory.

Segment: A segment is your interface into the shared memory. A segment is made up of one or more pages. If you (or your process) haven't created a segment, you're not using shared memory.

ceil: AKA 'ceiling'. A well-defined math function that returns the next highest integer (aka rounding up). See Wikipedia: Floor and ceiling functions

PAGE_SIZE is the number of bytes the OS is using to split up its' chunks of memory. You can find the size with getconf:

# getconf PAGE_SIZE
PAGE_SIZE                          4096

shmmax is the maximum size of any individual segment in bytes (not pages).

shmall needs to be at least 'ceil(shmmax/PAGE_SIZE)' because if it was any less you could not create a segment that's shmmax in size. You would run out of pages to use.

Let's say you want to use no more than 8MiB (MB being base 10, MiB - mebibytes being base 2, what your computer actually uses when calculating the sizes) for shared memory on the system.

To find the number of pages 8MiB is you simply divide by PAGE_SIZE.

8MiB / 4096 bytes = 2048.

Set shmall to 2048.

Now let's say that you know you only need a single segment that's 512K (KiB, not KB) in size for postgres. You have all the data to calculate the minimum number you should set shmall to.

ceil(512KiB / 4096 bytes) = 128

The smallest you should set shmall to would be 128 pages. There's nothing stopping you from setting it higher. Shmall is simply a limit specifying that you will not use more than that amount of memory for shared memory whether there's one segment or ten.

Related Topic