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