Skip to content

Instantly share code, notes, and snippets.

@mjf
Last active December 29, 2015 13:39
Show Gist options
  • Save mjf/7678504 to your computer and use it in GitHub Desktop.
Save mjf/7678504 to your computer and use it in GitHub Desktop.
sysctlgen - Generate useful /etc/sysctl.conf options
#! /bin/sh
#
# sysctlgen - Generate useful /etc/sysctl.conf options
# Copyright (C) 2013 Matous J. Fialka, <http://mjf.cz/>
# Released under the terms of The MIT License
#
#
# Get PAGE_SIZE value from system compile-time variables
#
PAGE_SIZE=`
getconf PAGE_SIZE
`
#
# Get HUGE_PAGE_SIZE value from the running system
#
HUGE_PAGE_SIZE=`
awk '/Hugepagesize/ { print $2 }' /proc/meminfo
`
#
# Get MEM_TOTAL value from the running system
#
MEM_TOTAL=`
awk '/MemTotal/ { print $2 }' /proc/meminfo
`
#
# Round MEM_TOTAL value to the nearest power of 2 (important!)
#
MEM_TOTAL=`
# TODO: find some better (arithmetical) way to do this
power=1
while [ $power -lt $MEM_TOTAL ]
do
power=$(( power * 2 ))
done
echo $power
unset power
`
#
# Get KERNEL_SHM{MAX,MNI,ALL}_ORIG value from the running system
#
KERNEL_SHMMAX_ORIG=`
sysctl -n kernel.shmmax
`
KERNEL_SHMMNI_ORIG=`
sysctl -n kernel.shmmni
`
KERNEL_SHMALL_ORIG=`
sysctl -n kernel.shmall
`
#
# Set KERNEL_SHM{MAX,MNI,ALL} value according to the MEM_TOTAL and/or PAGE_SIZE
#
KERNEL_SHMMAX=$(( MEM_TOTAL * 1024 / 2 ))
KERNEL_SHMMNI=4096
KERNEL_SHMALL=$(( MEM_TOTAL * 1024 / PAGE_SIZE ))
#
# Get VM_HUGEPAGES_ORIG value from the running system
#
VM_NR_HUGEPAGES_ORIG=`
sysctl -n vm.nr_hugepages
`
#
# Branch VM_NR_HUGEPAGES on the boundary of 4 GB of memory
#
if [ $MEM_TOTAL -gt 3145728 ] # > 3 GB (3 * 1024 * 1024)
then
VM_NR_HUGEPAGES=$(( MEM_TOTAL / 2 / HUGE_PAGE_SIZE ))
else
VM_NR_HUGEPAGES=0
fi
#
# Generate /etc/sysctl.conf options
#
cat <<- EOT
#
# <<>> KERNEL SHARED MEMORY <<>>
#
#
# Original runtime value $KERNEL_SHMMAX_ORIG
#
kernel.shmmax = $KERNEL_SHMMAX
#
# Original runtime value $KERNEL_SHMMNI_ORIG
#
kernel.shmmni = $KERNEL_SHMMNI
#
# Original runtime value $KERNEL_SHMALL_ORIG
#
kernel.shmall = $KERNEL_SHMALL
#
# <<>> VIRTUAL MEMORY <<>>
#
#
# Original runtime value $VM_NR_HUGEPAGES_ORIG
#
vm.nr_hugepages = $VM_NR_HUGEPAGES
EOT
#
# Unset all used variables
#
unset PAGE_SIZE
unset HUGE_PAGE_SIZE
unset MEM_TOTAL
unset KERNEL_SHMMAX_ORIG
unset KERNEL_SHMMNI_ORIG
unset KERNEL_SHMALL_ORIG
unset KERNEL_SHMMAX
unset KERNEL_SHMMNI
unset KERNEL_SHMALL
unset VM_NR_HUGEPAGES_ORIG
unset VM_NR_HUGEPAGES
# vi:ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment