1.\" Copyright (c) 2001, Matthew Dillon. Terms and conditions are those of 2.\" the BSD Copyright as specified in the file "/usr/src/COPYRIGHT" in 3.\" the source tree. 4.\" 5.\" $FreeBSD$ 6.\" 7.Dd May 25, 2001 8.Dt TUNING 7 9.Os 10.Sh NAME 11.Nm tuning 12.Nd performance tuning under FreeBSD 13.Sh SYSTEM SETUP - DISKLABEL, NEWFS, TUNEFS, SWAP 14When using 15.Xr disklabel 8 16to lay out your filesystems on a hard disk it is important to remember 17that hard drives can transfer data much more quickly from outer tracks 18than they can from inner tracks. To take advantage of this you should 19try to pack your smaller filesystems and swap closer to the outer tracks, 20follow with the larger filesystems, and end with the largest filesystems. 21It is also important to size system standard filesystems such that you 22will not be forced to resize them later as you scale the machine up. 23I usually create, in order, a 128M root, 1G swap, 128M /var, 128M /var/tmp, 243G /usr, and use any remaining space for /home. 25.Pp 26You should typically size your swap space to approximately 2x main memory. 27If you do not have a lot of ram, though, you will generally want a lot 28more swap. It is not recommended that you configure any less than 29256M of swap on a system and you should keep in mind future memory 30expansion when sizing the swap partition. 31The kernel's VM paging algorithms are tuned to perform best when there is 32at least 2x swap versus main memory. Configuring too little swap can lead 33to inefficiencies in the VM page scanning code as well as create issues 34later on if you add more memory to your machine. Finally, on larger systems 35with multiple SCSI disks (or multiple IDE disks operating on different 36controllers), we strongly recommend that you configure swap on each drive 37(up to four drives). The swap partitions on the drives should be 38approximately the same size. The kernel can handle arbitrary sizes but 39internal data structures scale to 4 times the largest swap partition. Keeping 40the swap partitions near the same size will allow the kernel to optimally 41stripe swap space across the N disks. Don't worry about overdoing it a 42little, swap space is the saving grace of 43.Ux 44and even if you don't normally use much swap, it can give you more time to 45recover from a runaway program before being forced to reboot. 46.Pp 47How you size your 48.Em /var 49partition depends heavily on what you intend to use the machine for. This 50partition is primarily used to hold mailboxes, the print spool, and log 51files. Some people even make 52.Em /var/log 53its own partition (but except for extreme cases it isn't worth the waste 54of a partition id). If your machine is intended to act as a mail 55or print server, 56or you are running a heavily visited web server, you should consider 57creating a much larger partition - perhaps a gig or more. It is very easy 58to underestimate log file storage requirements. 59.Pp 60Sizing 61.Em /var/tmp 62depends on the kind of temporary file usage you think you will need. 128M is 63the minimum we recommend. Also note that sysinstall will create a /tmp 64directory, but it is usually a good idea to make 65.Em /tmp 66a softlink to 67.Em /var/tmp 68after the fact. 69Dedicating a partition for temporary file storage is important for 70two reasons: First, it reduces the possibility of filesystem corruption 71in a crash, and second it reduces the chance of a runaway process that 72fills up [/var]/tmp from blowing up more critical subsystems (mail, 73logging, etc). Filling up [/var]/tmp is a very common problem to have. 74.Pp 75In the old days there were differences between /tmp and /var/tmp, 76but the introduction of /var (and /var/tmp) led to massive confusion 77by program writers so today programs haphazardly use one or the 78other and thus no real distinction can be made between the two. So 79it makes sense to have just one temporary directory. However you handle 80/tmp, the one thing you do not want to do is leave it sitting 81on the root partition where it might cause root to fill up or possibly 82corrupt root in a crash/reboot situation. 83.Pp 84The 85.Em /usr 86partition holds the bulk of the files required to support the system and 87a subdirectory within it called 88.Em /usr/local 89holds the bulk of the files installed from the 90.Xr ports 7 91hierarchy. If you do not use ports all that much and do not intend to keep 92system source (/usr/src) on the machine, you can get away with 93a 1 gigabyte /usr partition. However, if you install a lot of ports 94(especially window managers and linux-emulated binaries), we recommend 95at least a 2 gigabyte /usr and if you also intend to keep system source 96on the machine, we recommend a 3 gigabyte /usr. Do not underestimate the 97amount of space you will need in this partition, it can creep up and 98surprise you! 99.Pp 100The 101.Em /home 102partition is typically used to hold user-specific data. I usually size it 103to the remainder of the disk. 104.Pp 105Why partition at all? Why not create one big 106.Em / 107partition and be done with it? Then I don't have to worry about undersizing 108things! Well, there are several reasons this isn't a good idea. First, 109each partition has different operational characteristics and separating them 110allows the filesystem to tune itself to those characteristics. For example, 111the root and /usr partitions are read-mostly, with very little writing, while 112a lot of reading and writing could occur in /var and /var/tmp. By properly 113partitioning your system fragmentation introduced in the smaller more 114heavily write-loaded partitions will not bleed over into the mostly-read 115partitions. Additionally, keeping the write-loaded partitions closer to 116the edge of the disk (i.e. before the really big partitions instead of after 117in the partition table) will increase I/O performance in the partitions 118where you need it the most. Now it is true that you might also need I/O 119performance in the larger partitions, but they are so large that shifting 120them more towards the edge of the disk will not lead to a significant 121performance improvement whereas moving /var to the edge can have a huge impact. 122Finally, there are safety concerns. Having a small neat root partition that 123is essentially read-only gives it a greater chance of surviving a bad crash 124intact. 125.Pp 126Properly partitioning your system also allows you to tune 127.Xr newfs 8 , 128and 129.Xr tunefs 8 130parameters. Tuning 131.Fn newfs 132requires more experience but can lead to significant improvements in 133performance. There are three parameters that are relatively safe to 134tune: 135.Em blocksize , 136.Em bytes/inode , 137and 138.Em cylinders/group . 139.Pp 140.Fx 141performs best when using 8K or 16K filesystem block sizes. The default 142filesystem block size is 8K. For larger partitions it is usually a good 143idea to use a 16K block size. This also requires you to specify a larger 144fragment size. We recommend always using a fragment size that is 1/8 145the block size (less testing has been done on other fragment size factors). 146The 147.Fn newfs 148options for this would be 149.Em newfs -f 2048 -b 16384 ... 150Using a larger block size can cause fragmentation of the buffer cache and 151lead to lower performance. 152.Pp 153If a large partition is intended to be used to hold fewer, larger files, such 154as a database files, you can increase the 155.Em bytes/inode 156ratio which reduces the number of inodes (maximum number of files and 157directories that can be created) for that partition. Decreasing the number 158of inodes in a filesystem can greatly reduce 159.Xr fsck 8 160recovery times after a crash. Do not use this option 161unless you are actually storing large files on the partition, because if you 162overcompensate you can wind up with a filesystem that has lots of free 163space remaining but cannot accommodate any more files. Using 16432768, 65536, or 262144 bytes/inode is recommended. You can go higher but 165it will have only incremental effects on fsck recovery times. For 166example, 167.Em newfs -i 32768 ... 168.Pp 169Finally, increasing the 170.Em cylinders/group 171ratio has the effect of packing the inodes closer together. This can increase 172directory performance and also decrease fsck times. If you use this option 173at all, we recommend maxing it out. Use 174.Em newfs -c 999 175and newfs will error out and tell you what the maximum is, then use that. 176.Pp 177.Xr tunefs 8 178may be used to further tune a filesystem. This command can be run in 179single-user mode without having to reformat the filesystem. However, this 180is possibly the most abused program in the system. Many people attempt to 181increase available filesystem space by setting the min-free percentage to 0. 182This can lead to severe filesystem fragmentation and we do not recommend 183that you do this. Really the only tunefs option worthwhile here is turning on 184.Em softupdates 185with 186.Em tunefs -n enable /filesystem. 187(Note: In 5.x softupdates can be turned on using the -U option to newfs). 188Softupdates drastically improves meta-data performance, mainly file 189creation and deletion. We recommend enabling softupdates on all of your 190filesystems. There are two downsides to softupdates that you should be 191aware of: First, softupdates guarantees filesystem consistency in the 192case of a crash but could very easily be several seconds (even a minute!) 193behind updating the physical disk. If you crash you may lose more work 194than otherwise. Secondly, softupdates delays the freeing of filesystem 195blocks. If you have a filesystem (such as the root filesystem) which is 196close to full, doing a major update of it, e.g.\& 197.Em make installworld, 198can run it out of space and cause the update to fail. 199.Pp 200A number of run-time mount options exist that can help you tune the system. 201The most obvious and most dangerous one is 202.Em async . 203Don't ever use it, it is far too dangerous. A less dangerous and more 204useful mount option is called 205.Em noatime . 206UNIX filesystems normally update the last-accessed time of a file or 207directory whenever it is accessed. This operation is handled in FreeBSD 208with a delayed write and normally does not create a burden on the system. 209However, if your system is accessing a huge number of files on a continuing 210basis the buffer cache can wind up getting polluted with atime updates, 211creating a burden on the system. For example, if you are running a heavily 212loaded web site, or a news server with lots of readers, you might want to 213consider turning off atime updates on your larger partitions with this 214mount option. Do not gratuitously turn off atime updates everywhere.. for 215example, you might as well leave them turned on for mostly read-only 216partitions such as / and /usr (especially for / since some system utilities 217use the atime field for reporting). 218.Sh STRIPING DISKS 219In larger systems you can stripe partitions from several drives together 220to create a much larger overall partition. Striping can also improve 221the performance of a filesystem by splitting I/O operations across two 222or more disks. The 223.Xr vinum 8 224and 225.Xr ccd 4 226utilities may be used to create simple striped filesystems. Generally 227speaking, striping smaller partitions such as the root and /var/tmp, 228or essentially read-only partitions such as /usr is a complete waste of 229time. You should only stripe partitions that require serious I/O performance, 230typically /var, /home, or custom partitions used to hold databases and web 231pages. Choosing the proper stripe size is also 232important. Filesystems tend to store meta-data on power-of-2 boundaries 233and you usually want to reduce seeking rather than increase seeking. This 234means you want to use a large off-center stripe size such as 1152 sectors 235so sequential I/O does not seek both disks and so meta-data is distributed 236across both disks rather than concentrated on a single disk. If 237you really need to get sophisticated, we recommend using a real hardware 238raid controller from the list of 239.Fx 240supported controllers. 241.Sh SYSCTL TUNING 242There are several hundred 243.Xr sysctl 8 244variables in the system, including many that appear to be candidates for 245tuning but actually aren't. In this document we will only cover the ones 246that have the greatest effect on the system. 247.Pp 248The 249.Em kern.ipc.shm_use_phys 250sysctl defaults to 0 (off) and may be set to 0 (off) or 1 (on). Setting 251this parameter to 1 will cause all SysV shared memory segments to be 252mapped to unpageable physical ram. This feature only has an effect if you 253are either (A) mapping small amounts of shared memory across many (hundreds) 254of processes, or (B) mapping large amounts of shared memory across any 255number of processes. This feature allows the kernel to remove a great deal 256of internal memory management page-tracking overhead at the cost of wiring 257the shared memory into core, making it unswappable. 258.Pp 259The 260.Em vfs.vmiodirenable 261sysctl defaults to 0 (off) (though soon it will default to 1) and may be 262set to 0 (off) or 1 (on). This parameter controls how directories are cached 263by the system. Most directories are small and use but a single fragment 264(typically 1K) in the filesystem and even less (typically 512 bytes) in 265the buffer cache. However, when operating in the default mode the buffer 266cache will only cache a fixed number of directories even if you have a huge 267amount of memory. Turning on this sysctl allows the buffer cache to use 268the VM Page Cache to cache the directories. The advantage is that all of 269memory is now available for caching directories. The disadvantage is that 270the minimum in-core memory used to cache a directory is the physical page 271size (typically 4K) rather than 512 bytes. We recommend turning this option 272on if you are running any services which manipulate large numbers of files. 273Such services can include web caches, large mail systems, and news systems. 274Turning on this option will generally not reduce performance even with the 275wasted memory but you should experiment to find out. 276.Pp 277There are various buffer-cache and VM page cache related sysctls. We do 278not recommend messing around with these at all. As of 279.Fx 4.3 , 280the VM system does an extremely good job tuning itself. 281.Pp 282The 283.Em net.inet.tcp.sendspace 284and 285.Em net.inet.tcp.recvspace 286sysctls are of particular interest if you are running network intensive 287applications. This controls the amount of send and receive buffer space 288allowed for any given TCP connection. The default is 16K. You can often 289improve bandwidth utilization by increasing the default at the cost of 290eating up more kernel memory for each connection. We do not recommend 291increasing the defaults if you are serving hundreds or thousands of 292simultaneous connections because it is possible to quickly run the system 293out of memory due to stalled connections building up. But if you need 294high bandwidth over a fewer number of connections, especially if you have 295gigabit ethernet, increasing these defaults can make a huge difference. 296You can adjust the buffer size for incoming and outgoing data separately. 297For example, if your machine is primarily doing web serving you may want 298to decrease the recvspace in order to be able to increase the sendspace 299without eating too much kernel memory. Note that the route table, see 300.Xr route 8 , 301can be used to introduce route-specific send and receive buffer size 302defaults. As an additional management tool you can use pipes in your 303firewall rules, see 304.Xr ipfw 8 , 305to limit the bandwidth going to or from particular IP blocks or ports. 306For example, if you have a T1 you might want to limit your web traffic 307to 70% of the T1's bandwidth in order to leave the remainder available 308for mail and interactive use. Normally a heavily loaded web server 309will not introduce significant latencies into other services even if 310the network link is maxed out, but enforcing a limit can smooth things 311out and lead to longer term stability. Many people also enforce artificial 312bandwidth limitations in order to ensure that they are not charged for 313using too much bandwidth. 314.Pp 315Setting the send or receive TCP buffer to values larger then 65535 will result 316in a marginal performance improvement unless both hosts support the window 317scaling extension of the TCP protocol, which is controlled by the 318.Em net.inet.tcp.rfc1323 319sysctl. 320These extensions should be enabled and the TCP buffer size should be set 321to a value larger than 65536 in order to obtain good performance out of 322certain types of network links; specifically, gigabit WAN links and 323high-latency satellite links. 324.Pp 325We recommend that you turn on (set to 1) and leave on the 326.Em net.inet.tcp.always_keepalive 327control. The default is usually off. This introduces a small amount of 328additional network bandwidth but guarantees that dead tcp connections 329will eventually be recognized and cleared. Dead tcp connections are a 330particular problem on systems accessed by users operating over dialups, 331because users often disconnect their modems without properly closing active 332connections. 333.Pp 334The 335.Em kern.ipc.somaxconn 336sysctl limits the size of the listen queue for accepting new tcp connections. 337The default value of 128 is typically too low for robust handling of new 338connections in a heavily loaded web server environment. For such environments, 339we recommend increasing this value to 1024 or higher. The service daemon 340may itself limit the listen queue size (e.g. sendmail, apache) but will 341often have a directive in its configuration file to adjust the queue size up. 342Larger listen queues also do a better job of fending off denial of service 343attacks. 344.Pp 345The 346.Em kern.maxfiles 347sysctl determines how many open files the system supports. The default is 348typically a few thousand but you may need to bump this up to ten or twenty 349thousand if you are running databases or large descriptor-heavy daemons. 350.Pp 351The 352.Em vm.swap_idle_enabled 353sysctl is useful in large multi-user systems where you have lots of users 354entering and leaving the system and lots of idle processes. Such systems 355tend to generate a great deal of continuous pressure on free memory reserves. 356Turning this feature on and adjusting the swapout hysteresis (in idle 357seconds) via 358.Em vm.swap_idle_threshold1 359and 360.Em vm.swap_idle_threshold2 361allows you to depress the priority of pages associated with idle processes 362more quickly then the normal pageout algorithm. This gives a helping hand 363to the pageout daemon. Do not turn this option on unless you need it, 364because the tradeoff you are making is to essentially pre-page memory sooner 365rather then later, eating more swap and disk bandwidth. In a small system 366this option will have a detrimental effect but in a large system that is 367already doing moderate paging this option allows the VM system to stage 368whole processes into and out of memory more easily. 369.Sh KERNEL CONFIG TUNING 370There are a number of kernel options that you may have to fiddle with in 371a large scale system. In order to change these options you need to be 372able to compile a new kernel from source. The 373.Xr config 8 374manual page and the handbook are good starting points for learning how to 375do this. Generally the first thing you do when creating your own custom 376kernel is to strip out all the drivers and services you don't use. Removing 377things like 378.Em INET6 379and drivers you don't have will reduce the size of your kernel, sometimes 380by a megabyte or more, leaving more memory available for applications. 381.Pp 382The 383.Em maxusers 384kernel option defaults to an incredibly low value. For most modern machines, 385you probably want to increase this value to 64, 128, or 256. We do not 386recommend going above 256 unless you need a huge number of file descriptors. 387Network buffers are also affected but can be controlled with a separate 388kernel option. Do not increase maxusers just to get more network mbufs. 389.Pp 390.Em NMBCLUSTERS 391may be adjusted to increase the number of network mbufs the system is 392willing to allocate. Each cluster represents approximately 2K of memory, 393so a value of 1024 represents 2M of kernel memory reserved for network 394buffers. You can do a simple calculation to figure out how many you need. 395If you have a web server which maxes out at 1000 simultaneous connections, 396and each connection eats a 16K receive and 16K send buffer, you need 397approximate 32MB worth of network buffers to deal with it. A good rule of 398thumb is to multiply by 2, so 32MBx2 = 64MB/2K = 32768. So for this case 399you would want to se NMBCLUSTERS to 32768. We recommend values between 4001024 and 4096 for machines with moderates amount of memory, and between 4096 401and 32768 for machines with greater amounts of memory. Under no circumstances 402should you specify an arbitrarily high value for this parameter, it could 403lead to a boot-time crash. The -m option to 404.Xr netstat 1 405may be used to observe network cluster use. 406.Pp 407More and more programs are using the 408.Fn sendfile 409system call to transmit files over the network. The 410.Em NSFBUFS 411kernel parameter controls the number of filesystem buffers 412.Fn sendfile 413is allowed to use to perform its work. This parameter nominally scales 414with 415.Em maxusers 416so you should not need to mess with this parameter except under extreme 417circumstances. 418.Pp 419.Em SCSI_DELAY 420and 421.Em IDE_DELAY 422may be used to reduce system boot times. The defaults are fairly high and 423can be responsible for 15+ seconds of delay in the boot process. Reducing 424SCSI_DELAY to 5 seconds usually works (especially with modern drives). 425Reducing IDE_DELAY also works but you have to be a little more careful. 426.Pp 427There are a number of 428.Em XXX_CPU 429options that can be commented out. If you only want the kernel to run 430on a Pentium class cpu, you can easily remove 431.Em I386_CPU 432and 433.Em I486_CPU, 434but only remove 435.Em I586_CPU 436if you are sure your cpu is being recognized as a Pentium II or better. 437Some clones may be recognized as a Pentium or even a 486 and not be able 438to boot without those options. If it works, great! The operating system 439will be able to better-use higher-end cpu features for mmu, task switching, 440timebase, and even device operations. Additionally, higher-end cpus support 4414MB MMU pages which the kernel uses to map the kernel itself into memory, 442which increases its efficiency under heavy syscall loads. 443.Sh IDE WRITE CACHING 444.Fx 4.3 445flirted with turning off IDE write caching. This reduced write bandwidth 446to IDE disks but was considered necessary due to serious data consistency 447issues introduced by hard drive vendors. Basically the problem is that 448IDE drives lie about when a write completes. With IDE write caching turned 449on, IDE hard drives will not only write data to disk out of order, they 450will sometimes delay some of the blocks indefinitely when under heavy disk 451loads. A crash or power failure can result in serious filesystem 452corruption. So our default was changed to be safe. Unfortunately, the 453result was such a huge loss in performance that we caved in and changed the 454default back to on after the release. You should check the default on 455your system by observing the 456.Em hw.ata.wc 457sysctl variable. If IDE write caching is turned off, you can turn it back 458on by setting the 459.Em hw.ata.wc 460kernel variable back to 1. This must be done from the boot loader at boot 461time. Attempting to do it after the kernel boots will have no effect. 462Please see 463.Xr ata 4 , 464and 465.Xr loader 8 . 466.Pp 467There is a new experimental feature for IDE hard drives called hw.ata.tags 468(you also set this in the bootloader) which allows write caching to be safely 469turned on. This brings SCSI tagging features to IDE drives. As of this 470writing only IBM DPTA and DTLA drives support the feature. Warning! These 471drives apparently have quality control problems and I do not recommend 472purchasing them at this time. If you need performance, go with SCSI. 473.Sh CPU, MEMORY, DISK, NETWORK 474The type of tuning you do depends heavily on where your system begins to 475bottleneck as load increases. If your system runs out of cpu (idle times 476are perpetually 0%) then you need to consider upgrading the cpu or moving to 477an SMP motherboard (multiple cpu's), or perhaps you need to revisit the 478programs that are causing the load and try to optimize them. If your system 479is paging to swap a lot you need to consider adding more memory. If your 480system is saturating the disk you typically see high cpu idle times and 481total disk saturation. 482.Xr systat 1 483can be used to monitor this. There are many solutions to saturated disks: 484increasing memory for caching, mirroring disks, distributing operations across 485several machines, and so forth. If disk performance is an issue and you 486are using IDE drives, switching to SCSI can help a great deal. While modern 487IDE drives compare with SCSI in raw sequential bandwidth, the moment you 488start seeking around the disk SCSI drives usually win. 489.Pp 490Finally, you might run out of network suds. The first line of defense for 491improving network performance is to make sure you are using switches instead 492of hubs, especially these days where switches are almost as cheap. Hubs 493have severe problems under heavy loads due to collision backoff and one bad 494host can severely degrade the entire LAN. Second, optimize the network path 495as much as possible. For example, in 496.Xr firewall 7 497we describe a firewall protecting internal hosts with a topology where 498the externally visible hosts are not routed through it. Use 100BaseT rather 499than 10BaseT, or use 1000BaseT rather then 100BaseT, depending on your needs. 500Most bottlenecks occur at the WAN link (e.g. modem, T1, DSL, whatever). 501If expanding the link is not an option it may be possible to use ipfw's 502.Sy DUMMYNET 503feature to implement peak shaving or other forms of traffic shaping to 504prevent the overloaded service (such as web services) from affecting other 505services (such as email), or vice versa. In home installations this could 506be used to give interactive traffic (your browser, ssh logins) priority 507over services you export from your box (web services, email). 508.Sh SEE ALSO 509.Xr netstat 1 , 510.Xr systat 1 , 511.Xr ata 4 , 512.Xr ccd 4 , 513.Xr login.conf 5 , 514.Xr firewall 7 , 515.Xr hier 7 , 516.Xr ports 7 , 517.Xr boot 8 , 518.Xr config 8 , 519.Xr disklabel 8 , 520.Xr fsck 8 , 521.Xr ifconfig 8 , 522.Xr ipfw 8 , 523.Xr loader 8 , 524.Xr newfs 8 , 525.Xr route 8 , 526.Xr sysctl 8 , 527.Xr tunefs 8 , 528.Xr vinum 8 529.Sh HISTORY 530The 531.Nm 532manual page was originally written by 533.An Matthew Dillon 534and first appeared 535in 536.Fx 4.3 , 537May 2001. 538