1.\" Copyright (C) 2001 Matthew Dillon. All rights reserved. 2.\" 3.\" Redistribution and use in source and binary forms, with or without 4.\" modification, are permitted provided that the following conditions 5.\" are met: 6.\" 1. Redistributions of source code must retain the above copyright 7.\" notice, this list of conditions and the following disclaimer. 8.\" 2. Redistributions in binary form must reproduce the above copyright 9.\" notice, this list of conditions and the following disclaimer in the 10.\" documentation and/or other materials provided with the distribution. 11.\" 12.\" THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 13.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 15.\" ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 16.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 17.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 18.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 19.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 20.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 21.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 22.\" SUCH DAMAGE. 23.\" 24.\" $FreeBSD$ 25.\" 26.Dd January 23, 2009 27.Dt TUNING 7 28.Os 29.Sh NAME 30.Nm tuning 31.Nd performance tuning under FreeBSD 32.Sh SYSTEM SETUP - DISKLABEL, NEWFS, TUNEFS, SWAP 33When using 34.Xr bsdlabel 8 35or 36.Xr sysinstall 8 37to lay out your file systems on a hard disk it is important to remember 38that hard drives can transfer data much more quickly from outer tracks 39than they can from inner tracks. 40To take advantage of this you should 41try to pack your smaller file systems and swap closer to the outer tracks, 42follow with the larger file systems, and end with the largest file systems. 43It is also important to size system standard file systems such that you 44will not be forced to resize them later as you scale the machine up. 45I usually create, in order, a 128M root, 1G swap, 128M 46.Pa /var , 47128M 48.Pa /var/tmp , 493G 50.Pa /usr , 51and use any remaining space for 52.Pa /home . 53.Pp 54You should typically size your swap space to approximately 2x main memory 55for systems with less than 2GB of RAM, or approximately 1x main memory 56if you have more. 57If you do not have a lot of RAM, though, you will generally want a lot 58more swap. 59It is not recommended that you configure any less than 60256M of swap on a system and you should keep in mind future memory 61expansion when sizing the swap partition. 62The kernel's VM paging algorithms are tuned to perform best when there is 63at least 2x swap versus main memory. 64Configuring too little swap can lead 65to inefficiencies in the VM page scanning code as well as create issues 66later on if you add more memory to your machine. 67Finally, on larger systems 68with multiple SCSI disks (or multiple IDE disks operating on different 69controllers), we strongly recommend that you configure swap on each drive. 70The swap partitions on the drives should be approximately the same size. 71The kernel can handle arbitrary sizes but 72internal data structures scale to 4 times the largest swap partition. 73Keeping 74the swap partitions near the same size will allow the kernel to optimally 75stripe swap space across the N disks. 76Do not worry about overdoing it a 77little, swap space is the saving grace of 78.Ux 79and even if you do not normally use much swap, it can give you more time to 80recover from a runaway program before being forced to reboot. 81.Pp 82How you size your 83.Pa /var 84partition depends heavily on what you intend to use the machine for. 85This 86partition is primarily used to hold mailboxes, the print spool, and log 87files. 88Some people even make 89.Pa /var/log 90its own partition (but except for extreme cases it is not worth the waste 91of a partition ID). 92If your machine is intended to act as a mail 93or print server, 94or you are running a heavily visited web server, you should consider 95creating a much larger partition \(en perhaps a gig or more. 96It is very easy 97to underestimate log file storage requirements. 98.Pp 99Sizing 100.Pa /var/tmp 101depends on the kind of temporary file usage you think you will need. 102128M is 103the minimum we recommend. 104Also note that sysinstall will create a 105.Pa /tmp 106directory. 107Dedicating a partition for temporary file storage is important for 108two reasons: first, it reduces the possibility of file system corruption 109in a crash, and second it reduces the chance of a runaway process that 110fills up 111.Oo Pa /var Oc Ns Pa /tmp 112from blowing up more critical subsystems (mail, 113logging, etc). 114Filling up 115.Oo Pa /var Oc Ns Pa /tmp 116is a very common problem to have. 117.Pp 118In the old days there were differences between 119.Pa /tmp 120and 121.Pa /var/tmp , 122but the introduction of 123.Pa /var 124(and 125.Pa /var/tmp ) 126led to massive confusion 127by program writers so today programs haphazardly use one or the 128other and thus no real distinction can be made between the two. 129So it makes sense to have just one temporary directory and 130softlink to it from the other 131.Pa tmp 132directory locations. 133However you handle 134.Pa /tmp , 135the one thing you do not want to do is leave it sitting 136on the root partition where it might cause root to fill up or possibly 137corrupt root in a crash/reboot situation. 138.Pp 139The 140.Pa /usr 141partition holds the bulk of the files required to support the system and 142a subdirectory within it called 143.Pa /usr/local 144holds the bulk of the files installed from the 145.Xr ports 7 146hierarchy. 147If you do not use ports all that much and do not intend to keep 148system source 149.Pq Pa /usr/src 150on the machine, you can get away with 151a 1 gigabyte 152.Pa /usr 153partition. 154However, if you install a lot of ports 155(especially window managers and Linux-emulated binaries), we recommend 156at least a 2 gigabyte 157.Pa /usr 158and if you also intend to keep system source 159on the machine, we recommend a 3 gigabyte 160.Pa /usr . 161Do not underestimate the 162amount of space you will need in this partition, it can creep up and 163surprise you! 164.Pp 165The 166.Pa /home 167partition is typically used to hold user-specific data. 168I usually size it to the remainder of the disk. 169.Pp 170Why partition at all? 171Why not create one big 172.Pa / 173partition and be done with it? 174Then I do not have to worry about undersizing things! 175Well, there are several reasons this is not a good idea. 176First, 177each partition has different operational characteristics and separating them 178allows the file system to tune itself to those characteristics. 179For example, 180the root and 181.Pa /usr 182partitions are read-mostly, with very little writing, while 183a lot of reading and writing could occur in 184.Pa /var 185and 186.Pa /var/tmp . 187By properly 188partitioning your system fragmentation introduced in the smaller more 189heavily write-loaded partitions will not bleed over into the mostly-read 190partitions. 191Additionally, keeping the write-loaded partitions closer to 192the edge of the disk (i.e., before the really big partitions instead of after 193in the partition table) will increase I/O performance in the partitions 194where you need it the most. 195Now it is true that you might also need I/O 196performance in the larger partitions, but they are so large that shifting 197them more towards the edge of the disk will not lead to a significant 198performance improvement whereas moving 199.Pa /var 200to the edge can have a huge impact. 201Finally, there are safety concerns. 202Having a small neat root partition that 203is essentially read-only gives it a greater chance of surviving a bad crash 204intact. 205.Pp 206Properly partitioning your system also allows you to tune 207.Xr newfs 8 , 208and 209.Xr tunefs 8 210parameters. 211Tuning 212.Xr newfs 8 213requires more experience but can lead to significant improvements in 214performance. 215There are three parameters that are relatively safe to tune: 216.Em blocksize , bytes/i-node , 217and 218.Em cylinders/group . 219.Pp 220.Fx 221performs best when using 8K or 16K file system block sizes. 222The default file system block size is 16K, 223which provides best performance for most applications, 224with the exception of those that perform random access on large files 225(such as database server software). 226Such applications tend to perform better with a smaller block size, 227although modern disk characteristics are such that the performance 228gain from using a smaller block size may not be worth consideration. 229Using a block size larger than 16K 230can cause fragmentation of the buffer cache and 231lead to lower performance. 232.Pp 233The defaults may be unsuitable 234for a file system that requires a very large number of i-nodes 235or is intended to hold a large number of very small files. 236Such a file system should be created with an 8K or 4K block size. 237This also requires you to specify a smaller 238fragment size. 239We recommend always using a fragment size that is 1/8 240the block size (less testing has been done on other fragment size factors). 241The 242.Xr newfs 8 243options for this would be 244.Dq Li "newfs -f 1024 -b 8192 ..." . 245.Pp 246If a large partition is intended to be used to hold fewer, larger files, such 247as database files, you can increase the 248.Em bytes/i-node 249ratio which reduces the number of i-nodes (maximum number of files and 250directories that can be created) for that partition. 251Decreasing the number 252of i-nodes in a file system can greatly reduce 253.Xr fsck 8 254recovery times after a crash. 255Do not use this option 256unless you are actually storing large files on the partition, because if you 257overcompensate you can wind up with a file system that has lots of free 258space remaining but cannot accommodate any more files. 259Using 32768, 65536, or 262144 bytes/i-node is recommended. 260You can go higher but 261it will have only incremental effects on 262.Xr fsck 8 263recovery times. 264For example, 265.Dq Li "newfs -i 32768 ..." . 266.Pp 267.Xr tunefs 8 268may be used to further tune a file system. 269This command can be run in 270single-user mode without having to reformat the file system. 271However, this is possibly the most abused program in the system. 272Many people attempt to 273increase available file system space by setting the min-free percentage to 0. 274This can lead to severe file system fragmentation and we do not recommend 275that you do this. 276Really the only 277.Xr tunefs 8 278option worthwhile here is turning on 279.Em softupdates 280with 281.Dq Li "tunefs -n enable /filesystem" . 282(Note: in 283.Fx 4.5 284and later, softupdates can be turned on using the 285.Fl U 286option to 287.Xr newfs 8 , 288and 289.Xr sysinstall 8 290will typically enable softupdates automatically for non-root file systems). 291Softupdates drastically improves meta-data performance, mainly file 292creation and deletion. 293We recommend enabling softupdates on most file systems; however, there 294are two limitations to softupdates that you should be aware of when 295determining whether to use it on a file system. 296First, softupdates guarantees file system consistency in the 297case of a crash but could very easily be several seconds (even a minute!\&) 298behind on pending write to the physical disk. 299If you crash you may lose more work 300than otherwise. 301Secondly, softupdates delays the freeing of file system 302blocks. 303If you have a file system (such as the root file system) which is 304close to full, doing a major update of it, e.g.\& 305.Dq Li "make installworld" , 306can run it out of space and cause the update to fail. 307For this reason, softupdates will not be enabled on the root file system 308during a typical install. 309There is no loss of performance since the root 310file system is rarely written to. 311.Pp 312A number of run-time 313.Xr mount 8 314options exist that can help you tune the system. 315The most obvious and most dangerous one is 316.Cm async . 317Only use this option in conjunction with 318.Xr gjournal 8 , 319as it is far too dangerous on a normal file system. 320A less dangerous and more 321useful 322.Xr mount 8 323option is called 324.Cm noatime . 325.Ux 326file systems normally update the last-accessed time of a file or 327directory whenever it is accessed. 328This operation is handled in 329.Fx 330with a delayed write and normally does not create a burden on the system. 331However, if your system is accessing a huge number of files on a continuing 332basis the buffer cache can wind up getting polluted with atime updates, 333creating a burden on the system. 334For example, if you are running a heavily 335loaded web site, or a news server with lots of readers, you might want to 336consider turning off atime updates on your larger partitions with this 337.Xr mount 8 338option. 339However, you should not gratuitously turn off atime 340updates everywhere. 341For example, the 342.Pa /var 343file system customarily 344holds mailboxes, and atime (in combination with mtime) is used to 345determine whether a mailbox has new mail. 346You might as well leave 347atime turned on for mostly read-only partitions such as 348.Pa / 349and 350.Pa /usr 351as well. 352This is especially useful for 353.Pa / 354since some system utilities 355use the atime field for reporting. 356.Sh STRIPING DISKS 357In larger systems you can stripe partitions from several drives together 358to create a much larger overall partition. 359Striping can also improve 360the performance of a file system by splitting I/O operations across two 361or more disks. 362The 363.Xr gstripe 8 , 364.Xr gvinum 8 , 365and 366.Xr ccdconfig 8 367utilities may be used to create simple striped file systems. 368Generally 369speaking, striping smaller partitions such as the root and 370.Pa /var/tmp , 371or essentially read-only partitions such as 372.Pa /usr 373is a complete waste of time. 374You should only stripe partitions that require serious I/O performance, 375typically 376.Pa /var , /home , 377or custom partitions used to hold databases and web pages. 378Choosing the proper stripe size is also 379important. 380File systems tend to store meta-data on power-of-2 boundaries 381and you usually want to reduce seeking rather than increase seeking. 382This 383means you want to use a large off-center stripe size such as 1152 sectors 384so sequential I/O does not seek both disks and so meta-data is distributed 385across both disks rather than concentrated on a single disk. 386If 387you really need to get sophisticated, we recommend using a real hardware 388RAID controller from the list of 389.Fx 390supported controllers. 391.Sh SYSCTL TUNING 392.Xr sysctl 8 393variables permit system behavior to be monitored and controlled at 394run-time. 395Some sysctls simply report on the behavior of the system; others allow 396the system behavior to be modified; 397some may be set at boot time using 398.Xr rc.conf 5 , 399but most will be set via 400.Xr sysctl.conf 5 . 401There are several hundred sysctls in the system, including many that appear 402to be candidates for tuning but actually are not. 403In this document we will only cover the ones that have the greatest effect 404on the system. 405.Pp 406The 407.Va vm.overcommit 408sysctl defines the overcommit behaviour of the vm subsystem. 409The virtual memory system always does accounting of the swap space 410reservation, both total for system and per-user. Corresponding values 411are available through sysctl 412.Va vm.swap_total, 413that gives the total bytes available for swapping, and 414.Va vm.swap_reserved, 415that gives number of bytes that may be needed to back all currently 416allocated anonymous memory. 417.Pp 418Setting bit 0 of the 419.Va vm.overcommit 420sysctl causes the virtual memory system to return failure 421to the process when allocation of memory causes vm.swap_reserved 422to exceed vm.swap_total. 423Bit 1 of the sysctl enforces RLIMIT_SWAP limit 424(see 425.Xr getrlimit 2 ). 426Root is exempt from this limit. 427Bit 2 allows to count most of the physical 428memory as allocatable, except wired and free reserved pages 429(accounted by 430.Va vm.stats.vm.v_free_target 431and 432.Va vm.stats.vm.v_wire_count 433sysctls, respectively). 434.Pp 435The 436.Va kern.ipc.maxpipekva 437loader tunable is used to set a hard limit on the 438amount of kernel address space allocated to mapping of pipe buffers. 439Use of the mapping allows the kernel to eliminate a copy of the 440data from writer address space into the kernel, directly copying 441the content of mapped buffer to the reader. 442Increasing this value to a higher setting, such as `25165824' might 443improve performance on systems where space for mapping pipe buffers 444is quickly exhausted. 445This exhaustion is not fatal; however, and it will only cause pipes to 446to fall back to using double-copy. 447.Pp 448The 449.Va kern.ipc.shm_use_phys 450sysctl defaults to 0 (off) and may be set to 0 (off) or 1 (on). 451Setting 452this parameter to 1 will cause all System V shared memory segments to be 453mapped to unpageable physical RAM. 454This feature only has an effect if you 455are either (A) mapping small amounts of shared memory across many (hundreds) 456of processes, or (B) mapping large amounts of shared memory across any 457number of processes. 458This feature allows the kernel to remove a great deal 459of internal memory management page-tracking overhead at the cost of wiring 460the shared memory into core, making it unswappable. 461.Pp 462The 463.Va vfs.vmiodirenable 464sysctl defaults to 1 (on). 465This parameter controls how directories are cached 466by the system. 467Most directories are small and use but a single fragment 468(typically 1K) in the file system and even less (typically 512 bytes) in 469the buffer cache. 470However, when operating in the default mode the buffer 471cache will only cache a fixed number of directories even if you have a huge 472amount of memory. 473Turning on this sysctl allows the buffer cache to use 474the VM Page Cache to cache the directories. 475The advantage is that all of 476memory is now available for caching directories. 477The disadvantage is that 478the minimum in-core memory used to cache a directory is the physical page 479size (typically 4K) rather than 512 bytes. 480We recommend turning this option off in memory-constrained environments; 481however, when on, it will substantially improve the performance of services 482that manipulate a large number of files. 483Such services can include web caches, large mail systems, and news systems. 484Turning on this option will generally not reduce performance even with the 485wasted memory but you should experiment to find out. 486.Pp 487The 488.Va vfs.write_behind 489sysctl defaults to 1 (on). 490This tells the file system to issue media 491writes as full clusters are collected, which typically occurs when writing 492large sequential files. 493The idea is to avoid saturating the buffer 494cache with dirty buffers when it would not benefit I/O performance. 495However, 496this may stall processes and under certain circumstances you may wish to turn 497it off. 498.Pp 499The 500.Va vfs.hirunningspace 501sysctl determines how much outstanding write I/O may be queued to 502disk controllers system-wide at any given instance. 503The default is 504usually sufficient but on machines with lots of disks you may want to bump 505it up to four or five megabytes. 506Note that setting too high a value 507(exceeding the buffer cache's write threshold) can lead to extremely 508bad clustering performance. 509Do not set this value arbitrarily high! 510Also, 511higher write queueing values may add latency to reads occurring at the same 512time. 513.Pp 514There are various other buffer-cache and VM page cache related sysctls. 515We do not recommend modifying these values. 516As of 517.Fx 4.3 , 518the VM system does an extremely good job tuning itself. 519.Pp 520The 521.Va net.inet.tcp.sendspace 522and 523.Va net.inet.tcp.recvspace 524sysctls are of particular interest if you are running network intensive 525applications. 526They control the amount of send and receive buffer space 527allowed for any given TCP connection. 528The default sending buffer is 32K; the default receiving buffer 529is 64K. 530You can often 531improve bandwidth utilization by increasing the default at the cost of 532eating up more kernel memory for each connection. 533We do not recommend 534increasing the defaults if you are serving hundreds or thousands of 535simultaneous connections because it is possible to quickly run the system 536out of memory due to stalled connections building up. 537But if you need 538high bandwidth over a fewer number of connections, especially if you have 539gigabit Ethernet, increasing these defaults can make a huge difference. 540You can adjust the buffer size for incoming and outgoing data separately. 541For example, if your machine is primarily doing web serving you may want 542to decrease the recvspace in order to be able to increase the 543sendspace without eating too much kernel memory. 544Note that the routing table (see 545.Xr route 8 ) 546can be used to introduce route-specific send and receive buffer size 547defaults. 548.Pp 549As an additional management tool you can use pipes in your 550firewall rules (see 551.Xr ipfw 8 ) 552to limit the bandwidth going to or from particular IP blocks or ports. 553For example, if you have a T1 you might want to limit your web traffic 554to 70% of the T1's bandwidth in order to leave the remainder available 555for mail and interactive use. 556Normally a heavily loaded web server 557will not introduce significant latencies into other services even if 558the network link is maxed out, but enforcing a limit can smooth things 559out and lead to longer term stability. 560Many people also enforce artificial 561bandwidth limitations in order to ensure that they are not charged for 562using too much bandwidth. 563.Pp 564Setting the send or receive TCP buffer to values larger than 65535 will result 565in a marginal performance improvement unless both hosts support the window 566scaling extension of the TCP protocol, which is controlled by the 567.Va net.inet.tcp.rfc1323 568sysctl. 569These extensions should be enabled and the TCP buffer size should be set 570to a value larger than 65536 in order to obtain good performance from 571certain types of network links; specifically, gigabit WAN links and 572high-latency satellite links. 573RFC1323 support is enabled by default. 574.Pp 575The 576.Va net.inet.tcp.always_keepalive 577sysctl determines whether or not the TCP implementation should attempt 578to detect dead TCP connections by intermittently delivering 579.Dq keepalives 580on the connection. 581By default, this is enabled for all applications; by setting this 582sysctl to 0, only applications that specifically request keepalives 583will use them. 584In most environments, TCP keepalives will improve the management of 585system state by expiring dead TCP connections, particularly for 586systems serving dialup users who may not always terminate individual 587TCP connections before disconnecting from the network. 588However, in some environments, temporary network outages may be 589incorrectly identified as dead sessions, resulting in unexpectedly 590terminated TCP connections. 591In such environments, setting the sysctl to 0 may reduce the occurrence of 592TCP session disconnections. 593.Pp 594The 595.Va net.inet.tcp.delayed_ack 596TCP feature is largely misunderstood. 597Historically speaking, this feature 598was designed to allow the acknowledgement to transmitted data to be returned 599along with the response. 600For example, when you type over a remote shell, 601the acknowledgement to the character you send can be returned along with the 602data representing the echo of the character. 603With delayed acks turned off, 604the acknowledgement may be sent in its own packet, before the remote service 605has a chance to echo the data it just received. 606This same concept also 607applies to any interactive protocol (e.g.\& SMTP, WWW, POP3), and can cut the 608number of tiny packets flowing across the network in half. 609The 610.Fx 611delayed ACK implementation also follows the TCP protocol rule that 612at least every other packet be acknowledged even if the standard 100ms 613timeout has not yet passed. 614Normally the worst a delayed ACK can do is 615slightly delay the teardown of a connection, or slightly delay the ramp-up 616of a slow-start TCP connection. 617While we are not sure we believe that 618the several FAQs related to packages such as SAMBA and SQUID which advise 619turning off delayed acks may be referring to the slow-start issue. 620In 621.Fx , 622it would be more beneficial to increase the slow-start flightsize via 623the 624.Va net.inet.tcp.slowstart_flightsize 625sysctl rather than disable delayed acks. 626.Pp 627The 628.Va net.inet.tcp.inflight.enable 629sysctl turns on bandwidth delay product limiting for all TCP connections. 630The system will attempt to calculate the bandwidth delay product for each 631connection and limit the amount of data queued to the network to just the 632amount required to maintain optimum throughput. 633This feature is useful 634if you are serving data over modems, GigE, or high speed WAN links (or 635any other link with a high bandwidth*delay product), especially if you are 636also using window scaling or have configured a large send window. 637If you enable this option, you should also be sure to set 638.Va net.inet.tcp.inflight.debug 639to 0 (disable debugging), and for production use setting 640.Va net.inet.tcp.inflight.min 641to at least 6144 may be beneficial. 642Note however, that setting high 643minimums may effectively disable bandwidth limiting depending on the link. 644The limiting feature reduces the amount of data built up in intermediate 645router and switch packet queues as well as reduces the amount of data built 646up in the local host's interface queue. 647With fewer packets queued up, 648interactive connections, especially over slow modems, will also be able 649to operate with lower round trip times. 650However, note that this feature 651only affects data transmission (uploading / server-side). 652It does not 653affect data reception (downloading). 654.Pp 655Adjusting 656.Va net.inet.tcp.inflight.stab 657is not recommended. 658This parameter defaults to 20, representing 2 maximal packets added 659to the bandwidth delay product window calculation. 660The additional 661window is required to stabilize the algorithm and improve responsiveness 662to changing conditions, but it can also result in higher ping times 663over slow links (though still much lower than you would get without 664the inflight algorithm). 665In such cases you may 666wish to try reducing this parameter to 15, 10, or 5, and you may also 667have to reduce 668.Va net.inet.tcp.inflight.min 669(for example, to 3500) to get the desired effect. 670Reducing these parameters 671should be done as a last resort only. 672.Pp 673The 674.Va net.inet.ip.portrange.* 675sysctls control the port number ranges automatically bound to TCP and UDP 676sockets. 677There are three ranges: a low range, a default range, and a 678high range, selectable via the 679.Dv IP_PORTRANGE 680.Xr setsockopt 2 681call. 682Most 683network programs use the default range which is controlled by 684.Va net.inet.ip.portrange.first 685and 686.Va net.inet.ip.portrange.last , 687which default to 49152 and 65535, respectively. 688Bound port ranges are 689used for outgoing connections, and it is possible to run the system out 690of ports under certain circumstances. 691This most commonly occurs when you are 692running a heavily loaded web proxy. 693The port range is not an issue 694when running a server which handles mainly incoming connections, such as a 695normal web server, or has a limited number of outgoing connections, such 696as a mail relay. 697For situations where you may run out of ports, 698we recommend decreasing 699.Va net.inet.ip.portrange.first 700modestly. 701A range of 10000 to 30000 ports may be reasonable. 702You should also consider firewall effects when changing the port range. 703Some firewalls 704may block large ranges of ports (usually low-numbered ports) and expect systems 705to use higher ranges of ports for outgoing connections. 706By default 707.Va net.inet.ip.portrange.last 708is set at the maximum allowable port number. 709.Pp 710The 711.Va kern.ipc.somaxconn 712sysctl limits the size of the listen queue for accepting new TCP connections. 713The default value of 128 is typically too low for robust handling of new 714connections in a heavily loaded web server environment. 715For such environments, 716we recommend increasing this value to 1024 or higher. 717The service daemon 718may itself limit the listen queue size (e.g.\& 719.Xr sendmail 8 , 720apache) but will 721often have a directive in its configuration file to adjust the queue size up. 722Larger listen queues also do a better job of fending off denial of service 723attacks. 724.Pp 725The 726.Va kern.maxfiles 727sysctl determines how many open files the system supports. 728The default is 729typically a few thousand but you may need to bump this up to ten or twenty 730thousand if you are running databases or large descriptor-heavy daemons. 731The read-only 732.Va kern.openfiles 733sysctl may be interrogated to determine the current number of open files 734on the system. 735.Pp 736The 737.Va vm.swap_idle_enabled 738sysctl is useful in large multi-user systems where you have lots of users 739entering and leaving the system and lots of idle processes. 740Such systems 741tend to generate a great deal of continuous pressure on free memory reserves. 742Turning this feature on and adjusting the swapout hysteresis (in idle 743seconds) via 744.Va vm.swap_idle_threshold1 745and 746.Va vm.swap_idle_threshold2 747allows you to depress the priority of pages associated with idle processes 748more quickly then the normal pageout algorithm. 749This gives a helping hand 750to the pageout daemon. 751Do not turn this option on unless you need it, 752because the tradeoff you are making is to essentially pre-page memory sooner 753rather than later, eating more swap and disk bandwidth. 754In a small system 755this option will have a detrimental effect but in a large system that is 756already doing moderate paging this option allows the VM system to stage 757whole processes into and out of memory more easily. 758.Sh LOADER TUNABLES 759Some aspects of the system behavior may not be tunable at runtime because 760memory allocations they perform must occur early in the boot process. 761To change loader tunables, you must set their values in 762.Xr loader.conf 5 763and reboot the system. 764.Pp 765.Va kern.maxusers 766controls the scaling of a number of static system tables, including defaults 767for the maximum number of open files, sizing of network memory resources, etc. 768As of 769.Fx 4.5 , 770.Va kern.maxusers 771is automatically sized at boot based on the amount of memory available in 772the system, and may be determined at run-time by inspecting the value of the 773read-only 774.Va kern.maxusers 775sysctl. 776Some sites will require larger or smaller values of 777.Va kern.maxusers 778and may set it as a loader tunable; values of 64, 128, and 256 are not 779uncommon. 780We do not recommend going above 256 unless you need a huge number 781of file descriptors; many of the tunable values set to their defaults by 782.Va kern.maxusers 783may be individually overridden at boot-time or run-time as described 784elsewhere in this document. 785Systems older than 786.Fx 4.4 787must set this value via the kernel 788.Xr config 8 789option 790.Cd maxusers 791instead. 792.Pp 793The 794.Va kern.dfldsiz 795and 796.Va kern.dflssiz 797tunables set the default soft limits for process data and stack size 798respectively. 799Processes may increase these up to the hard limits by calling 800.Xr setrlimit 2 . 801The 802.Va kern.maxdsiz , 803.Va kern.maxssiz , 804and 805.Va kern.maxtsiz 806tunables set the hard limits for process data, stack, and text size 807respectively; processes may not exceed these limits. 808The 809.Va kern.sgrowsiz 810tunable controls how much the stack segment will grow when a process 811needs to allocate more stack. 812.Pp 813.Va kern.ipc.nmbclusters 814may be adjusted to increase the number of network mbufs the system is 815willing to allocate. 816Each cluster represents approximately 2K of memory, 817so a value of 1024 represents 2M of kernel memory reserved for network 818buffers. 819You can do a simple calculation to figure out how many you need. 820If you have a web server which maxes out at 1000 simultaneous connections, 821and each connection eats a 16K receive and 16K send buffer, you need 822approximately 32MB worth of network buffers to deal with it. 823A good rule of 824thumb is to multiply by 2, so 32MBx2 = 64MB/2K = 32768. 825So for this case 826you would want to set 827.Va kern.ipc.nmbclusters 828to 32768. 829We recommend values between 8301024 and 4096 for machines with moderates amount of memory, and between 4096 831and 32768 for machines with greater amounts of memory. 832Under no circumstances 833should you specify an arbitrarily high value for this parameter, it could 834lead to a boot-time crash. 835The 836.Fl m 837option to 838.Xr netstat 1 839may be used to observe network cluster use. 840Older versions of 841.Fx 842do not have this tunable and require that the 843kernel 844.Xr config 8 845option 846.Dv NMBCLUSTERS 847be set instead. 848.Pp 849More and more programs are using the 850.Xr sendfile 2 851system call to transmit files over the network. 852The 853.Va kern.ipc.nsfbufs 854sysctl controls the number of file system buffers 855.Xr sendfile 2 856is allowed to use to perform its work. 857This parameter nominally scales 858with 859.Va kern.maxusers 860so you should not need to modify this parameter except under extreme 861circumstances. 862See the 863.Sx TUNING 864section in the 865.Xr sendfile 2 866manual page for details. 867.Sh KERNEL CONFIG TUNING 868There are a number of kernel options that you may have to fiddle with in 869a large-scale system. 870In order to change these options you need to be 871able to compile a new kernel from source. 872The 873.Xr config 8 874manual page and the handbook are good starting points for learning how to 875do this. 876Generally the first thing you do when creating your own custom 877kernel is to strip out all the drivers and services you do not use. 878Removing things like 879.Dv INET6 880and drivers you do not have will reduce the size of your kernel, sometimes 881by a megabyte or more, leaving more memory available for applications. 882.Pp 883.Dv SCSI_DELAY 884may be used to reduce system boot times. 885The defaults are fairly high and 886can be responsible for 5+ seconds of delay in the boot process. 887Reducing 888.Dv SCSI_DELAY 889to something below 5 seconds could work (especially with modern drives). 890.Pp 891There are a number of 892.Dv *_CPU 893options that can be commented out. 894If you only want the kernel to run 895on a Pentium class CPU, you can easily remove 896.Dv I486_CPU , 897but only remove 898.Dv I586_CPU 899if you are sure your CPU is being recognized as a Pentium II or better. 900Some clones may be recognized as a Pentium or even a 486 and not be able 901to boot without those options. 902If it works, great! 903The operating system 904will be able to better use higher-end CPU features for MMU, task switching, 905timebase, and even device operations. 906Additionally, higher-end CPUs support 9074MB MMU pages, which the kernel uses to map the kernel itself into memory, 908increasing its efficiency under heavy syscall loads. 909.Sh IDE WRITE CACHING 910.Fx 4.3 911flirted with turning off IDE write caching. 912This reduced write bandwidth 913to IDE disks but was considered necessary due to serious data consistency 914issues introduced by hard drive vendors. 915Basically the problem is that 916IDE drives lie about when a write completes. 917With IDE write caching turned 918on, IDE hard drives will not only write data to disk out of order, they 919will sometimes delay some of the blocks indefinitely under heavy disk 920load. 921A crash or power failure can result in serious file system 922corruption. 923So our default was changed to be safe. 924Unfortunately, the 925result was such a huge loss in performance that we caved in and changed the 926default back to on after the release. 927You should check the default on 928your system by observing the 929.Va hw.ata.wc 930sysctl variable. 931If IDE write caching is turned off, you can turn it back 932on by setting the 933.Va hw.ata.wc 934loader tunable to 1. 935More information on tuning the ATA driver system may be found in the 936.Xr ata 4 937manual page. 938If you need performance, go with SCSI. 939.Sh CPU, MEMORY, DISK, NETWORK 940The type of tuning you do depends heavily on where your system begins to 941bottleneck as load increases. 942If your system runs out of CPU (idle times 943are perpetually 0%) then you need to consider upgrading the CPU or moving to 944an SMP motherboard (multiple CPU's), or perhaps you need to revisit the 945programs that are causing the load and try to optimize them. 946If your system 947is paging to swap a lot you need to consider adding more memory. 948If your 949system is saturating the disk you typically see high CPU idle times and 950total disk saturation. 951.Xr systat 1 952can be used to monitor this. 953There are many solutions to saturated disks: 954increasing memory for caching, mirroring disks, distributing operations across 955several machines, and so forth. 956If disk performance is an issue and you 957are using IDE drives, switching to SCSI can help a great deal. 958While modern 959IDE drives compare with SCSI in raw sequential bandwidth, the moment you 960start seeking around the disk SCSI drives usually win. 961.Pp 962Finally, you might run out of network suds. 963The first line of defense for 964improving network performance is to make sure you are using switches instead 965of hubs, especially these days where switches are almost as cheap. 966Hubs 967have severe problems under heavy loads due to collision back-off and one bad 968host can severely degrade the entire LAN. 969Second, optimize the network path 970as much as possible. 971For example, in 972.Xr firewall 7 973we describe a firewall protecting internal hosts with a topology where 974the externally visible hosts are not routed through it. 975Use 100BaseT rather 976than 10BaseT, or use 1000BaseT rather than 100BaseT, depending on your needs. 977Most bottlenecks occur at the WAN link (e.g.\& 978modem, T1, DSL, whatever). 979If expanding the link is not an option it may be possible to use the 980.Xr dummynet 4 981feature to implement peak shaving or other forms of traffic shaping to 982prevent the overloaded service (such as web services) from affecting other 983services (such as email), or vice versa. 984In home installations this could 985be used to give interactive traffic (your browser, 986.Xr ssh 1 987logins) priority 988over services you export from your box (web services, email). 989.Sh SEE ALSO 990.Xr netstat 1 , 991.Xr systat 1 , 992.Xr sendfile 2 , 993.Xr ata 4 , 994.Xr dummynet 4 , 995.Xr login.conf 5 , 996.Xr rc.conf 5 , 997.Xr sysctl.conf 5 , 998.Xr firewall 7 , 999.Xr hier 7 , 1000.Xr ports 7 , 1001.Xr boot 8 , 1002.Xr bsdlabel 8 , 1003.Xr ccdconfig 8 , 1004.Xr config 8 , 1005.Xr fsck 8 , 1006.Xr gjournal 8 , 1007.Xr gstripe 8 , 1008.Xr gvinum 8 , 1009.Xr ifconfig 8 , 1010.Xr ipfw 8 , 1011.Xr loader 8 , 1012.Xr mount 8 , 1013.Xr newfs 8 , 1014.Xr route 8 , 1015.Xr sysctl 8 , 1016.Xr sysinstall 8 , 1017.Xr tunefs 8 1018.Sh HISTORY 1019The 1020.Nm 1021manual page was originally written by 1022.An Matthew Dillon 1023and first appeared 1024in 1025.Fx 4.3 , 1026May 2001. 1027