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