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. However, you should not gratuitously turn off atime 215updates everywhere. For example, the /var filesystem customarily 216holds mailboxes, and atime (in combination with mtime) is used to 217determine whether a mailbox has new mail. You might as well leave 218atime turned on for mostly read-only partitions such as / and /usr 219as well. This is especially useful for / since some system utilities 220use the atime field for reporting. 221.Sh STRIPING DISKS 222In larger systems you can stripe partitions from several drives together 223to create a much larger overall partition. Striping can also improve 224the performance of a filesystem by splitting I/O operations across two 225or more disks. The 226.Xr vinum 8 227and 228.Xr ccd 4 229utilities may be used to create simple striped filesystems. Generally 230speaking, striping smaller partitions such as the root and /var/tmp, 231or essentially read-only partitions such as /usr is a complete waste of 232time. You should only stripe partitions that require serious I/O performance, 233typically /var, /home, or custom partitions used to hold databases and web 234pages. Choosing the proper stripe size is also 235important. Filesystems tend to store meta-data on power-of-2 boundaries 236and you usually want to reduce seeking rather than increase seeking. This 237means you want to use a large off-center stripe size such as 1152 sectors 238so sequential I/O does not seek both disks and so meta-data is distributed 239across both disks rather than concentrated on a single disk. If 240you really need to get sophisticated, we recommend using a real hardware 241raid controller from the list of 242.Fx 243supported controllers. 244.Sh SYSCTL TUNING 245There are several hundred 246.Xr sysctl 8 247variables in the system, including many that appear to be candidates for 248tuning but actually aren't. In this document we will only cover the ones 249that have the greatest effect on the system. 250.Pp 251The 252.Em kern.ipc.shm_use_phys 253sysctl defaults to 0 (off) and may be set to 0 (off) or 1 (on). Setting 254this parameter to 1 will cause all SysV shared memory segments to be 255mapped to unpageable physical ram. This feature only has an effect if you 256are either (A) mapping small amounts of shared memory across many (hundreds) 257of processes, or (B) mapping large amounts of shared memory across any 258number of processes. This feature allows the kernel to remove a great deal 259of internal memory management page-tracking overhead at the cost of wiring 260the shared memory into core, making it unswappable. 261.Pp 262The 263.Em vfs.vmiodirenable 264sysctl defaults to 0 (off) (though soon it will default to 1) and may be 265set to 0 (off) or 1 (on). This parameter controls how directories are cached 266by the system. Most directories are small and use but a single fragment 267(typically 1K) in the filesystem and even less (typically 512 bytes) in 268the buffer cache. However, when operating in the default mode the buffer 269cache will only cache a fixed number of directories even if you have a huge 270amount of memory. Turning on this sysctl allows the buffer cache to use 271the VM Page Cache to cache the directories. The advantage is that all of 272memory is now available for caching directories. The disadvantage is that 273the minimum in-core memory used to cache a directory is the physical page 274size (typically 4K) rather than 512 bytes. We recommend turning this option 275on if you are running any services which manipulate large numbers of files. 276Such services can include web caches, large mail systems, and news systems. 277Turning on this option will generally not reduce performance even with the 278wasted memory but you should experiment to find out. 279.Pp 280There are various buffer-cache and VM page cache related sysctls. We do 281not recommend messing around with these at all. As of 282.Fx 4.3 , 283the VM system does an extremely good job tuning itself. 284.Pp 285The 286.Em net.inet.tcp.sendspace 287and 288.Em net.inet.tcp.recvspace 289sysctls are of particular interest if you are running network intensive 290applications. This controls the amount of send and receive buffer space 291allowed for any given TCP connection. The default is 16K. You can often 292improve bandwidth utilization by increasing the default at the cost of 293eating up more kernel memory for each connection. We do not recommend 294increasing the defaults if you are serving hundreds or thousands of 295simultaneous connections because it is possible to quickly run the system 296out of memory due to stalled connections building up. But if you need 297high bandwidth over a fewer number of connections, especially if you have 298gigabit ethernet, increasing these defaults can make a huge difference. 299You can adjust the buffer size for incoming and outgoing data separately. 300For example, if your machine is primarily doing web serving you may want 301to decrease the recvspace in order to be able to increase the sendspace 302without eating too much kernel memory. Note that the route table, see 303.Xr route 8 , 304can be used to introduce route-specific send and receive buffer size 305defaults. As an additional management tool you can use pipes in your 306firewall rules, see 307.Xr ipfw 8 , 308to limit the bandwidth going to or from particular IP blocks or ports. 309For example, if you have a T1 you might want to limit your web traffic 310to 70% of the T1's bandwidth in order to leave the remainder available 311for mail and interactive use. Normally a heavily loaded web server 312will not introduce significant latencies into other services even if 313the network link is maxed out, but enforcing a limit can smooth things 314out and lead to longer term stability. Many people also enforce artificial 315bandwidth limitations in order to ensure that they are not charged for 316using too much bandwidth. 317.Pp 318Setting the send or receive TCP buffer to values larger then 65535 will result 319in a marginal performance improvement unless both hosts support the window 320scaling extension of the TCP protocol, which is controlled by the 321.Em net.inet.tcp.rfc1323 322sysctl. 323These extensions should be enabled and the TCP buffer size should be set 324to a value larger than 65536 in order to obtain good performance out of 325certain types of network links; specifically, gigabit WAN links and 326high-latency satellite links. 327.Pp 328We recommend that you turn on (set to 1) and leave on the 329.Em net.inet.tcp.always_keepalive 330control. The default is usually off. This introduces a small amount of 331additional network bandwidth but guarantees that dead tcp connections 332will eventually be recognized and cleared. Dead tcp connections are a 333particular problem on systems accessed by users operating over dialups, 334because users often disconnect their modems without properly closing active 335connections. 336.Pp 337The 338.Em kern.ipc.somaxconn 339sysctl limits the size of the listen queue for accepting new tcp connections. 340The default value of 128 is typically too low for robust handling of new 341connections in a heavily loaded web server environment. For such environments, 342we recommend increasing this value to 1024 or higher. The service daemon 343may itself limit the listen queue size (e.g. sendmail, apache) but will 344often have a directive in its configuration file to adjust the queue size up. 345Larger listen queues also do a better job of fending off denial of service 346attacks. 347.Pp 348The 349.Em kern.maxfiles 350sysctl determines how many open files the system supports. The default is 351typically a few thousand but you may need to bump this up to ten or twenty 352thousand if you are running databases or large descriptor-heavy daemons. 353.Pp 354The 355.Em vm.swap_idle_enabled 356sysctl is useful in large multi-user systems where you have lots of users 357entering and leaving the system and lots of idle processes. Such systems 358tend to generate a great deal of continuous pressure on free memory reserves. 359Turning this feature on and adjusting the swapout hysteresis (in idle 360seconds) via 361.Em vm.swap_idle_threshold1 362and 363.Em vm.swap_idle_threshold2 364allows you to depress the priority of pages associated with idle processes 365more quickly then the normal pageout algorithm. This gives a helping hand 366to the pageout daemon. Do not turn this option on unless you need it, 367because the tradeoff you are making is to essentially pre-page memory sooner 368rather then later, eating more swap and disk bandwidth. In a small system 369this option will have a detrimental effect but in a large system that is 370already doing moderate paging this option allows the VM system to stage 371whole processes into and out of memory more easily. 372.Sh BOOT-TIME SYSCTL TUNING 373Some sysctls may not be tunable at runtime because the memory allocations 374they perform must occur early in the boot process. To change these sysctls, 375you must set their value in 376.Xr loader.conf 5 377and reboot the system. 378.Pp 379The 380.Em kern.maxusers 381sysctl defaults to an incredibly low value. For most modern machines, 382you probably want to increase this value to 64, 128, or 256. We do not 383recommend going above 256 unless you need a huge number of file descriptors. 384Network buffers are also affected but can be controlled with a separate 385kernel option. Do not increase maxusers just to get more network mbufs. 386Systems older than FreeBSD 4.4 do not have this sysctl and require that 387the kernel config option maxusers be set instead. 388.Pp 389.Em kern.ipc.nmbclusters 390may be adjusted to increase the number of network mbufs the system is 391willing to allocate. Each cluster represents approximately 2K of memory, 392so a value of 1024 represents 2M of kernel memory reserved for network 393buffers. You can do a simple calculation to figure out how many you need. 394If you have a web server which maxes out at 1000 simultaneous connections, 395and each connection eats a 16K receive and 16K send buffer, you need 396approximate 32MB worth of network buffers to deal with it. A good rule of 397thumb is to multiply by 2, so 32MBx2 = 64MB/2K = 32768. So for this case 398you would want to set nmbclusters to 32768. We recommend values between 3991024 and 4096 for machines with moderates amount of memory, and between 4096 400and 32768 for machines with greater amounts of memory. Under no circumstances 401should you specify an arbitrarily high value for this parameter, it could 402lead to a boot-time crash. The -m option to 403.Xr netstat 1 404may be used to observe network cluster use. 405Older versions of FreeBSD do not have this sysctl and require that the 406kernel config option NMBCLUSTERS be set instead. 407.Pp 408More and more programs are using the 409.Fn sendfile 410system call to transmit files over the network. The 411.Em kern.ipc.nsfbufs 412sysctl controls the number of filesystem buffers 413.Fn sendfile 414is allowed to use to perform its work. This parameter nominally scales 415with 416.Em maxusers 417so you should not need to mess with this parameter except under extreme 418circumstances. 419.Pp 420.Sh KERNEL CONFIG TUNING 421There are a number of kernel options that you may have to fiddle with in 422a large scale system. In order to change these options you need to be 423able to compile a new kernel from source. The 424.Xr config 8 425manual page and the handbook are good starting points for learning how to 426do this. Generally the first thing you do when creating your own custom 427kernel is to strip out all the drivers and services you don't use. Removing 428things like 429.Em INET6 430and drivers you don't have will reduce the size of your kernel, sometimes 431by a megabyte or more, leaving more memory available for applications. 432.Pp 433.Em SCSI_DELAY 434and 435.Em IDE_DELAY 436may be used to reduce system boot times. The defaults are fairly high and 437can be responsible for 15+ seconds of delay in the boot process. Reducing 438SCSI_DELAY to 5 seconds usually works (especially with modern drives). 439Reducing IDE_DELAY also works but you have to be a little more careful. 440.Pp 441There are a number of 442.Em XXX_CPU 443options that can be commented out. If you only want the kernel to run 444on a Pentium class cpu, you can easily remove 445.Em I386_CPU 446and 447.Em I486_CPU, 448but only remove 449.Em I586_CPU 450if you are sure your cpu is being recognized as a Pentium II or better. 451Some clones may be recognized as a Pentium or even a 486 and not be able 452to boot without those options. If it works, great! The operating system 453will be able to better-use higher-end cpu features for mmu, task switching, 454timebase, and even device operations. Additionally, higher-end cpus support 4554MB MMU pages which the kernel uses to map the kernel itself into memory, 456which increases its efficiency under heavy syscall loads. 457.Sh IDE WRITE CACHING 458.Fx 4.3 459flirted with turning off IDE write caching. This reduced write bandwidth 460to IDE disks but was considered necessary due to serious data consistency 461issues introduced by hard drive vendors. Basically the problem is that 462IDE drives lie about when a write completes. With IDE write caching turned 463on, IDE hard drives will not only write data to disk out of order, they 464will sometimes delay some of the blocks indefinitely when under heavy disk 465loads. A crash or power failure can result in serious filesystem 466corruption. So our default was changed to be safe. Unfortunately, the 467result was such a huge loss in performance that we caved in and changed the 468default back to on after the release. You should check the default on 469your system by observing the 470.Em hw.ata.wc 471sysctl variable. If IDE write caching is turned off, you can turn it back 472on by setting the 473.Em hw.ata.wc 474kernel variable back to 1. This must be done from the boot loader at boot 475time. Attempting to do it after the kernel boots will have no effect. 476Please see 477.Xr ata 4 , 478and 479.Xr loader 8 . 480.Pp 481There is a new experimental feature for IDE hard drives called hw.ata.tags 482(you also set this in the bootloader) which allows write caching to be safely 483turned on. This brings SCSI tagging features to IDE drives. As of this 484writing only IBM DPTA and DTLA drives support the feature. Warning! These 485drives apparently have quality control problems and I do not recommend 486purchasing them at this time. If you need performance, go with SCSI. 487.Sh CPU, MEMORY, DISK, NETWORK 488The type of tuning you do depends heavily on where your system begins to 489bottleneck as load increases. If your system runs out of cpu (idle times 490are perpetually 0%) then you need to consider upgrading the cpu or moving to 491an SMP motherboard (multiple cpu's), or perhaps you need to revisit the 492programs that are causing the load and try to optimize them. If your system 493is paging to swap a lot you need to consider adding more memory. If your 494system is saturating the disk you typically see high cpu idle times and 495total disk saturation. 496.Xr systat 1 497can be used to monitor this. There are many solutions to saturated disks: 498increasing memory for caching, mirroring disks, distributing operations across 499several machines, and so forth. If disk performance is an issue and you 500are using IDE drives, switching to SCSI can help a great deal. While modern 501IDE drives compare with SCSI in raw sequential bandwidth, the moment you 502start seeking around the disk SCSI drives usually win. 503.Pp 504Finally, you might run out of network suds. The first line of defense for 505improving network performance is to make sure you are using switches instead 506of hubs, especially these days where switches are almost as cheap. Hubs 507have severe problems under heavy loads due to collision backoff and one bad 508host can severely degrade the entire LAN. Second, optimize the network path 509as much as possible. For example, in 510.Xr firewall 7 511we describe a firewall protecting internal hosts with a topology where 512the externally visible hosts are not routed through it. Use 100BaseT rather 513than 10BaseT, or use 1000BaseT rather then 100BaseT, depending on your needs. 514Most bottlenecks occur at the WAN link (e.g. modem, T1, DSL, whatever). 515If expanding the link is not an option it may be possible to use ipfw's 516.Sy DUMMYNET 517feature to implement peak shaving or other forms of traffic shaping to 518prevent the overloaded service (such as web services) from affecting other 519services (such as email), or vice versa. In home installations this could 520be used to give interactive traffic (your browser, ssh logins) priority 521over services you export from your box (web services, email). 522.Sh SEE ALSO 523.Xr netstat 1 , 524.Xr systat 1 , 525.Xr ata 4 , 526.Xr ccd 4 , 527.Xr login.conf 5 , 528.Xr firewall 7 , 529.Xr hier 7 , 530.Xr ports 7 , 531.Xr boot 8 , 532.Xr config 8 , 533.Xr disklabel 8 , 534.Xr fsck 8 , 535.Xr ifconfig 8 , 536.Xr ipfw 8 , 537.Xr loader 8 , 538.Xr newfs 8 , 539.Xr route 8 , 540.Xr sysctl 8 , 541.Xr tunefs 8 , 542.Xr vinum 8 543.Sh HISTORY 544The 545.Nm 546manual page was originally written by 547.An Matthew Dillon 548and first appeared 549in 550.Fx 4.3 , 551May 2001. 552