What are those pages and why would I want them to be huge?
When a process uses some memory, the CPU is marking the RAM as used by that process. For efficiency, the CPU allocate RAM by chunks of 4K bytes (it’s the default value on many platforms). Those chunks are named pages. Those pages can be swapped to disk, etc.

Since the process address space are virtual, the CPU and the operating system have to remember which page belong to which process, and where it is stored.
Obviously, the more pages you have, the more time it takes to find where the memory is mapped. When a process uses 1GB of memory, that’s 262144 entries to look up (1GB / 4K). If one Page Table Entry consume 8bytes, that’s 2MB (262144 * 8) to look-up.

How can I enable them?
The size of the huge pages in linux may change depending on the arch you are on

Architecture  huge page size
i386 4K and 4M (2M in PAE mode)
ia64 4K, 8K, 64K, 256K, 1M, 4M, 16M, 256M
ppc64 4K and 16M

You need to take the number below as a base

$ grep Hugepagesize /proc/meminfo
Hugepagesize:     2048 kB

and the calculate how much kB you want to dedicate to the process (the system will not be able to use them anymore, caveat!!)

# echo 512 > /proc/sys/vm/nr_hugepages

Is used for a runtime change (will disappear at reboot time).
To have a persistent change you have to use

# echo "vm.nr_hugepages=512" >> /etc/sysctl.conf

You can the check with

$ grep HugePages_Total /proc/meminfo
HugePages_Total:   512

Now you have to configure which user will be used for the huge pages (it will own the process of the app you want to use them with) in the /etc/security/limits.conf file.

userid          soft    memlock        PGSIZE*NUMBER
userid          hard    memlock        PGSIZE*NUMBER

How can I get statistics on the huge pages?
I wrote this simple bash script which can be used, having the name of the process, to know if that process is using or not the huge pages.

if (($# < 1))
        echo "$(basename $0) : Process to verify is needed";
        exit 255;
for pid in $(ps -ef | grep $PROCESS | grep -v grep | awk '{print $2}')
     HUGE=$(grep huge /proc/$pid/numa_maps 2>/dev/null)
     if [ $RES -eq 0 ]
       echo -e "\e[0;32m[YES] Pid $pid is using hugepages"
       echo -e "\e[0;91m[NO] Pid $pid is not using hugepages"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s