#!/bin/sh # Copyright (c) 2011 Sean Chittenden . Public domain. # # GOAL: Provide sane kernel tuning parameters for dedicated PostgreSQL # servers running on FreeBSD, Darwin or Leenox. Tune the shared memory # settings to 50% of the available pages # # If this doesn't work or you have an adjustment you'd like to see made, # please send it along to sean@chittenden.org. # # TODO: Can we auto-tune kern.ipc.semmap (aka $semmap)? # Get the size of a page page_size=`getconf PAGE_SIZE` OSTYPE=`uname -s` # Deal with GNUtards if [ "${OSTYPE}" = 'linux' ]; then OSTYPE="Linux" fi # Get the number of pages available on this host if [ "${OSTYPE}" = "Linux" ]; then phys_pages=`getconf _PHYS_PAGES` elif [ "${OSTYPE}" = "FreeBSD" ]; then phys_pages=`sysctl -n hw.availpages` elif [ "${OSTYPE}" = "Darwin" ]; then phys_pages=$((`sysctl -n hw.memsize` / ${page_size})) fi if [ -z "$page_size" -o -z "$phys_pages" ]; then printf "ERROR: Unable to determine the page_size or number of pages available on this system\n" printf "HINT: Run this script with -x and figure out which one didn't work\n" printf "BONUS POINTS: Submit a correction to sean@chittenden.org\n" exit 1 fi # Calcs ala sh(1). Use 50% of available pages for shm. shmall=`expr $phys_pages / 2` # Max shared segment size (bytes) shmmax=`expr $shmall \* $page_size` # Max num of shared mem segments (pages) semmap=256 if [ "${OSTYPE}" = "Linux" ]; then printf "# Append the following to /etc/sysctl.conf and then run: sysctl -p\n" printf "kernel.shmmax = %d\n" $shmmax printf "kernel.shmall = %d\n" $shmall elif [ "${OSTYPE}" = "FreeBSD" ]; then printf "# Append the following to /etc/sysctl.conf and then run\n" printf "# '/etc/rc.d/sysctl start'\n" printf "kern.ipc.shmmax=%d\n" $shmmax printf "kern.ipc.shmall=%d\n" $shmall printf "kern.ipc.semmap=%d\n" $semmap printf "\n" printf "One time use: \n" printf "sudo sysctl -w kern.ipc.shmmax=%d\n" $shmmax printf "sudo sysctl -w kern.ipc.shmall=%d\n" $shmall printf "sudo sysctl -w kern.ipc.semmap=%d\n" $semmap printf "\n" printf "Add the following to /boot/loader.conf\n" printf "These changes require a restart to go in to effect:\n" printf "kern.ipc.semmni=256\n" printf "kern.ipc.semmns=512\n" printf "kern.ipc.semmnu=256\n" elif [ "${OSTYPE}" = 'Darwin' ]; then printf "# Append the following to /etc/sysctl.conf and either restart or\n" printf "# run the following sysctl commands by hand\n" printf "kern.ipc.shmmax=%d\n" $shmmax printf "kern.ipc.shmall=%d\n" $shmall printf "\n" printf "One time use: \n" printf "sudo sysctl -w kern.ipc.shmmax=%d\n" $shmmax printf "sudo sysctl -w kern.ipc.shmall=%d\n" $shmall fi