Script to configure HugePages in Linux

 

 

Sometimes you have just finished the installation of the operating system (OEL in our case) and you want to configure the hugepages for the SGA of Oracle.

 

Here you can see one script to configure the 80% from the total memory in the machine (you can change this percentage if you want).

 

* Attention! –> You need to be sure that nothing more is running in your machine because if you have one Oracle Database you need to run this other script “Oracle Linux: Shell Script to Calculate
Values Recommended Linux HugePages / HugeTLB Configuration (Doc ID 401749.1)

[root@nodo01 ~]# chmod 700 /tmp/config_hugepages80.sh

[root@nodo01 ~]# cat /tmp/config_hugepages80.sh
#!/bin/bash

round() {
# Función para sacar la media de las cpus (de la función mira_cpus) ya que en algunas bash no funciona el típico:
# echo "(`lscpu |grep ^"CPU(s):" | awk -F: {'print $2'} | tr -d '[:blank:]'`)/2" | bc
echo $(printf %.$2f $(echo "scale=$2;(((10^$2)*$1)+0.5)/(10^$2)" | bc))
}

memlibre=`awk '/Hugepagesize:/{p=$2}/ 0 /{next}/ kB$/{v[sprintf("%9d GB %-s",int($2/1024/1024),$0)]=$0;next}{h[$0]=$2}END{for(k in v) print k;for (k in h) print sprintf("%9d GB %-s",p*h[k]/1024/1024,k)}' /proc/meminfo|sort -nr|grep --color=auto -E "^|.(Huge._[TF]|Mem).*:|" |grep MemFree | awk '{ print $4 }'`

pagesize=`awk '/Hugepagesize:/{p=$2}/ 0 /{next}/ kB$/{v[sprintf("%9d GB %-s",int($2/1024/1024),$0)]=$0;next}{h[$0]=$2}END{for(k in v) print k;for (k in h) print sprintf("%9d GB %-s",p*h[k]/1024/1024,k)}' /proc/meminfo|sort -nr|grep --color=auto -E "^|.(Huge._[TF]|Mem).*:|" |grep Hugepagesize | awk '{ print $4 }'`

hps=`echo $(round ${memlibre}/${pagesize} 0)`
pags=`echo $(round ${hps}*80/100 0)`

echo "${pags}" > /proc/sys/vm/nr_hugepages
echo "vm.nr_hugepages = ${pags}" >> /etc/sysctl.conf

sysctl -p >>/dev/null

awk '/Hugepagesize:/{p=$2}/ 0 /{next}/ kB$/{v[sprintf("%9d GB %-s",int($2/1024/1024),$0)]=$0;next}{h[$0]=$2}END{for(k in v) print k;for (k in h) print sprintf("%9d GB %-s",p*h[k]/1024/1024,k)}' /proc/meminfo|sort -nr|grep --color=auto -E "^|.(Huge._[TF]|Mem).*:|"

 

If you want to modify the percentage (80%) you can do it in the line “pags=” and changing the 80 number for another value.

 

Execute it:

[root@nodo01 ~]# /tmp/config_hugepages80.sh
32767 GB VmallocTotal: 34359738367 kB
39 GB MemTotal: 41186732 kB
34 GB DirectMap1G: 35651584 kB
28 GB HugePages_Total: 14416
28 GB HugePages_Free: 14416
10 GB MemAvailable: 10550832 kB
9 GB CommitLimit: 10025680 kB
7 GB MemFree: 7384284 kB
6 GB DirectMap2M: 7243776 kB
3 GB SwapTotal: 4194300 kB
3 GB SwapFree: 4194300 kB
3 GB Cached: 3452204 kB
1 GB Inactive(file): 1995756 kB
1 GB Inactive: 2011316 kB
1 GB Active(file): 1446100 kB
1 GB Active: 1462444 kB
0 GB SUnreclaim: 82040 kB
0 GB SReclaimable: 122556 kB
0 GB Slab: 204596 kB
0 GB Shmem: 16960 kB
0 GB PageTables: 2228 kB
0 GB Mapped: 33532 kB
0 GB KernelStack: 3696 kB
0 GB Inactive(anon): 15560 kB
0 GB HugePages_Surp: 0
0 GB HugePages_Rsvd: 0
0 GB Hugepagesize: 2048 kB
0 GB Dirty: 4 kB
0 GB DirectMap4k: 96256 kB
0 GB Committed_AS: 126564 kB
0 GB Buffers: 6608 kB
0 GB AnonPages: 15044 kB
0 GB Active(anon): 16344 kB

 

Don’t forget to reboot the system and check the modified values!

 

  • The awk for seeing the hugepages is courtesy from Franck Pachot: https://twitter.com/FranckPachot/status/1083023727460343808?s=19

 

Adding value with Arumel!!