Saturday, July 17, 2010

System and Kernel Settings in Mac OS X

After scouring the Internet looking for a way to cleanly increase the number of open files, (ulimit -n), I've finally discovered a nice, clean, and easy way to update the system and kernel settings. To start, I'll explain the problem. I'm trying to set up MySQL on a Mac Pro running OS X 10.5.8 and having 4 cores plus 8GB of RAM. After tuning the MySQL configuration file (/etc/my.cnf), MySQL eventually has a problem with the number of files it can have open, or runs out of locked memory, etc. To fix these issues, some default settings need to be changed in the system:

To modify system limits (ulimit settings), update /etc/launchd.conf with the following:
limit memlock unlimited
limit maxproc 1024 2048
limit maxfiles 32768
Make sure that my.cnf contains the open-files-limit setting under the [mysqld_safe] section to match the maxfiles limit set in launchd.conf:
open-files-limit = 32768

To modify kernel parameters (sysctl settings), update /etc/sysctl.conf with the following:
kernel.sysv.shmmax=402653184
kernel.sysv.shmall=393216
kernel.sysv.shmmni=4096

kern.maxfiles=32768
kern.maxfilesperproc=32768

kern.maxproc=2048
kern.maxprocperuid=1024
NOTE: make sure you use your own values specific to your environment. Here are my notes on setting shmmax and shmall:
# Set the maximum size of shared memory segment to 75% of
# total memory installed:
#
# kernel.shmmax = 402653184 <-- 512MB (536870912)
# kernel.shmmax = 1610612736 <-- 2GB (2147483648)
# kernel.shmmax = 4831838208 <-- 6GB (6442450944)
#
# Set the total amount of shared memory available using the
# following formula:
#
# shmall = AmountOfFreeMemBytes / PAGESIZE
#
# AmountOfFreeMemBytes is the amount of memory to be
# allocated to MySQL (typically use shmmax) divided by the
# PAGESIZE of the memory, 4096 on most systems.
# To get the page size on a host, run the following command:
#
# linux:
# getconf -a | grep -i PAGESIZE
#
# OS X:
# getconf PAGESIZE
#
# kernel.shmall = 98304 <-- calculated for 512MB
# kernel.shmall = 393216 <-- calculated for 2GB
# kernel.shmall = 1179648 <-- calculated for 6GB
In order for these settings to take effect, OS X needs to be rebooted. Once the system comes back up, check the system settings and kernel parameters to make sure the settings are active. MySQL (or whatever service you're using) can be started now.

I hope you found this helpful. Good luck!

No comments:

Post a Comment