xref: /freebsd/share/man/man7/tuning.7 (revision 35a04710d7286aa9538917fd7f8e417dbee95b82)
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 November 19, 2007
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 .
315Only use this option in conjunction with
316.Xr gjournal 8 ,
317as it is far too dangerous on a normal file system.
318A less dangerous and more
319useful
320.Xr mount 8
321option is called
322.Cm noatime .
323.Ux
324file systems normally update the last-accessed time of a file or
325directory whenever it is accessed.
326This operation is handled in
327.Fx
328with a delayed write and normally does not create a burden on the system.
329However, if your system is accessing a huge number of files on a continuing
330basis the buffer cache can wind up getting polluted with atime updates,
331creating a burden on the system.
332For example, if you are running a heavily
333loaded web site, or a news server with lots of readers, you might want to
334consider turning off atime updates on your larger partitions with this
335.Xr mount 8
336option.
337However, you should not gratuitously turn off atime
338updates everywhere.
339For example, the
340.Pa /var
341file system customarily
342holds mailboxes, and atime (in combination with mtime) is used to
343determine whether a mailbox has new mail.
344You might as well leave
345atime turned on for mostly read-only partitions such as
346.Pa /
347and
348.Pa /usr
349as well.
350This is especially useful for
351.Pa /
352since some system utilities
353use the atime field for reporting.
354.Sh STRIPING DISKS
355In larger systems you can stripe partitions from several drives together
356to create a much larger overall partition.
357Striping can also improve
358the performance of a file system by splitting I/O operations across two
359or more disks.
360The
361.Xr gstripe 8 ,
362.Xr gvinum 8 ,
363and
364.Xr ccdconfig 8
365utilities may be used to create simple striped file systems.
366Generally
367speaking, striping smaller partitions such as the root and
368.Pa /var/tmp ,
369or essentially read-only partitions such as
370.Pa /usr
371is a complete waste of time.
372You should only stripe partitions that require serious I/O performance,
373typically
374.Pa /var , /home ,
375or custom partitions used to hold databases and web pages.
376Choosing the proper stripe size is also
377important.
378File systems tend to store meta-data on power-of-2 boundaries
379and you usually want to reduce seeking rather than increase seeking.
380This
381means you want to use a large off-center stripe size such as 1152 sectors
382so sequential I/O does not seek both disks and so meta-data is distributed
383across both disks rather than concentrated on a single disk.
384If
385you really need to get sophisticated, we recommend using a real hardware
386RAID controller from the list of
387.Fx
388supported controllers.
389.Sh SYSCTL TUNING
390.Xr sysctl 8
391variables permit system behavior to be monitored and controlled at
392run-time.
393Some sysctls simply report on the behavior of the system; others allow
394the system behavior to be modified;
395some may be set at boot time using
396.Xr rc.conf 5 ,
397but most will be set via
398.Xr sysctl.conf 5 .
399There are several hundred sysctls in the system, including many that appear
400to be candidates for tuning but actually are not.
401In this document we will only cover the ones that have the greatest effect
402on the system.
403.Pp
404The
405.Va kern.ipc.shm_use_phys
406sysctl defaults to 0 (off) and may be set to 0 (off) or 1 (on).
407Setting
408this parameter to 1 will cause all System V shared memory segments to be
409mapped to unpageable physical RAM.
410This feature only has an effect if you
411are either (A) mapping small amounts of shared memory across many (hundreds)
412of processes, or (B) mapping large amounts of shared memory across any
413number of processes.
414This feature allows the kernel to remove a great deal
415of internal memory management page-tracking overhead at the cost of wiring
416the shared memory into core, making it unswappable.
417.Pp
418The
419.Va vfs.vmiodirenable
420sysctl defaults to 1 (on).
421This parameter controls how directories are cached
422by the system.
423Most directories are small and use but a single fragment
424(typically 1K) in the file system and even less (typically 512 bytes) in
425the buffer cache.
426However, when operating in the default mode the buffer
427cache will only cache a fixed number of directories even if you have a huge
428amount of memory.
429Turning on this sysctl allows the buffer cache to use
430the VM Page Cache to cache the directories.
431The advantage is that all of
432memory is now available for caching directories.
433The disadvantage is that
434the minimum in-core memory used to cache a directory is the physical page
435size (typically 4K) rather than 512 bytes.
436We recommend turning this option off in memory-constrained environments;
437however, when on, it will substantially improve the performance of services
438that manipulate a large number of files.
439Such services can include web caches, large mail systems, and news systems.
440Turning on this option will generally not reduce performance even with the
441wasted memory but you should experiment to find out.
442.Pp
443The
444.Va vfs.write_behind
445sysctl defaults to 1 (on).
446This tells the file system to issue media
447writes as full clusters are collected, which typically occurs when writing
448large sequential files.
449The idea is to avoid saturating the buffer
450cache with dirty buffers when it would not benefit I/O performance.
451However,
452this may stall processes and under certain circumstances you may wish to turn
453it off.
454.Pp
455The
456.Va vfs.hirunningspace
457sysctl determines how much outstanding write I/O may be queued to
458disk controllers system-wide at any given instance.
459The default is
460usually sufficient but on machines with lots of disks you may want to bump
461it up to four or five megabytes.
462Note that setting too high a value
463(exceeding the buffer cache's write threshold) can lead to extremely
464bad clustering performance.
465Do not set this value arbitrarily high!
466Also,
467higher write queueing values may add latency to reads occurring at the same
468time.
469.Pp
470There are various other buffer-cache and VM page cache related sysctls.
471We do not recommend modifying these values.
472As of
473.Fx 4.3 ,
474the VM system does an extremely good job tuning itself.
475.Pp
476The
477.Va net.inet.tcp.sendspace
478and
479.Va net.inet.tcp.recvspace
480sysctls are of particular interest if you are running network intensive
481applications.
482They control the amount of send and receive buffer space
483allowed for any given TCP connection.
484The default sending buffer is 32K; the default receiving buffer
485is 64K.
486You can often
487improve bandwidth utilization by increasing the default at the cost of
488eating up more kernel memory for each connection.
489We do not recommend
490increasing the defaults if you are serving hundreds or thousands of
491simultaneous connections because it is possible to quickly run the system
492out of memory due to stalled connections building up.
493But if you need
494high bandwidth over a fewer number of connections, especially if you have
495gigabit Ethernet, increasing these defaults can make a huge difference.
496You can adjust the buffer size for incoming and outgoing data separately.
497For example, if your machine is primarily doing web serving you may want
498to decrease the recvspace in order to be able to increase the
499sendspace without eating too much kernel memory.
500Note that the routing table (see
501.Xr route 8 )
502can be used to introduce route-specific send and receive buffer size
503defaults.
504.Pp
505As an additional management tool you can use pipes in your
506firewall rules (see
507.Xr ipfw 8 )
508to limit the bandwidth going to or from particular IP blocks or ports.
509For example, if you have a T1 you might want to limit your web traffic
510to 70% of the T1's bandwidth in order to leave the remainder available
511for mail and interactive use.
512Normally a heavily loaded web server
513will not introduce significant latencies into other services even if
514the network link is maxed out, but enforcing a limit can smooth things
515out and lead to longer term stability.
516Many people also enforce artificial
517bandwidth limitations in order to ensure that they are not charged for
518using too much bandwidth.
519.Pp
520Setting the send or receive TCP buffer to values larger than 65535 will result
521in a marginal performance improvement unless both hosts support the window
522scaling extension of the TCP protocol, which is controlled by the
523.Va net.inet.tcp.rfc1323
524sysctl.
525These extensions should be enabled and the TCP buffer size should be set
526to a value larger than 65536 in order to obtain good performance from
527certain types of network links; specifically, gigabit WAN links and
528high-latency satellite links.
529RFC1323 support is enabled by default.
530.Pp
531The
532.Va net.inet.tcp.always_keepalive
533sysctl determines whether or not the TCP implementation should attempt
534to detect dead TCP connections by intermittently delivering
535.Dq keepalives
536on the connection.
537By default, this is enabled for all applications; by setting this
538sysctl to 0, only applications that specifically request keepalives
539will use them.
540In most environments, TCP keepalives will improve the management of
541system state by expiring dead TCP connections, particularly for
542systems serving dialup users who may not always terminate individual
543TCP connections before disconnecting from the network.
544However, in some environments, temporary network outages may be
545incorrectly identified as dead sessions, resulting in unexpectedly
546terminated TCP connections.
547In such environments, setting the sysctl to 0 may reduce the occurrence of
548TCP session disconnections.
549.Pp
550The
551.Va net.inet.tcp.delayed_ack
552TCP feature is largely misunderstood.
553Historically speaking, this feature
554was designed to allow the acknowledgement to transmitted data to be returned
555along with the response.
556For example, when you type over a remote shell,
557the acknowledgement to the character you send can be returned along with the
558data representing the echo of the character.
559With delayed acks turned off,
560the acknowledgement may be sent in its own packet, before the remote service
561has a chance to echo the data it just received.
562This same concept also
563applies to any interactive protocol (e.g.\& SMTP, WWW, POP3), and can cut the
564number of tiny packets flowing across the network in half.
565The
566.Fx
567delayed ACK implementation also follows the TCP protocol rule that
568at least every other packet be acknowledged even if the standard 100ms
569timeout has not yet passed.
570Normally the worst a delayed ACK can do is
571slightly delay the teardown of a connection, or slightly delay the ramp-up
572of a slow-start TCP connection.
573While we are not sure we believe that
574the several FAQs related to packages such as SAMBA and SQUID which advise
575turning off delayed acks may be referring to the slow-start issue.
576In
577.Fx ,
578it would be more beneficial to increase the slow-start flightsize via
579the
580.Va net.inet.tcp.slowstart_flightsize
581sysctl rather than disable delayed acks.
582.Pp
583The
584.Va net.inet.tcp.inflight.enable
585sysctl turns on bandwidth delay product limiting for all TCP connections.
586The system will attempt to calculate the bandwidth delay product for each
587connection and limit the amount of data queued to the network to just the
588amount required to maintain optimum throughput.
589This feature is useful
590if you are serving data over modems, GigE, or high speed WAN links (or
591any other link with a high bandwidth*delay product), especially if you are
592also using window scaling or have configured a large send window.
593If you enable this option, you should also be sure to set
594.Va net.inet.tcp.inflight.debug
595to 0 (disable debugging), and for production use setting
596.Va net.inet.tcp.inflight.min
597to at least 6144 may be beneficial.
598Note however, that setting high
599minimums may effectively disable bandwidth limiting depending on the link.
600The limiting feature reduces the amount of data built up in intermediate
601router and switch packet queues as well as reduces the amount of data built
602up in the local host's interface queue.
603With fewer packets queued up,
604interactive connections, especially over slow modems, will also be able
605to operate with lower round trip times.
606However, note that this feature
607only affects data transmission (uploading / server-side).
608It does not
609affect data reception (downloading).
610.Pp
611Adjusting
612.Va net.inet.tcp.inflight.stab
613is not recommended.
614This parameter defaults to 20, representing 2 maximal packets added
615to the bandwidth delay product window calculation.
616The additional
617window is required to stabilize the algorithm and improve responsiveness
618to changing conditions, but it can also result in higher ping times
619over slow links (though still much lower than you would get without
620the inflight algorithm).
621In such cases you may
622wish to try reducing this parameter to 15, 10, or 5, and you may also
623have to reduce
624.Va net.inet.tcp.inflight.min
625(for example, to 3500) to get the desired effect.
626Reducing these parameters
627should be done as a last resort only.
628.Pp
629The
630.Va net.inet.ip.portrange.*
631sysctls control the port number ranges automatically bound to TCP and UDP
632sockets.
633There are three ranges: a low range, a default range, and a
634high range, selectable via the
635.Dv IP_PORTRANGE
636.Xr setsockopt 2
637call.
638Most
639network programs use the default range which is controlled by
640.Va net.inet.ip.portrange.first
641and
642.Va net.inet.ip.portrange.last ,
643which default to 49152 and 65535, respectively.
644Bound port ranges are
645used for outgoing connections, and it is possible to run the system out
646of ports under certain circumstances.
647This most commonly occurs when you are
648running a heavily loaded web proxy.
649The port range is not an issue
650when running a server which handles mainly incoming connections, such as a
651normal web server, or has a limited number of outgoing connections, such
652as a mail relay.
653For situations where you may run out of ports,
654we recommend decreasing
655.Va net.inet.ip.portrange.first
656modestly.
657A range of 10000 to 30000 ports may be reasonable.
658You should also consider firewall effects when changing the port range.
659Some firewalls
660may block large ranges of ports (usually low-numbered ports) and expect systems
661to use higher ranges of ports for outgoing connections.
662By default
663.Va net.inet.ip.portrange.last
664is set at the maximum allowable port number.
665.Pp
666The
667.Va kern.ipc.somaxconn
668sysctl limits the size of the listen queue for accepting new TCP connections.
669The default value of 128 is typically too low for robust handling of new
670connections in a heavily loaded web server environment.
671For such environments,
672we recommend increasing this value to 1024 or higher.
673The service daemon
674may itself limit the listen queue size (e.g.\&
675.Xr sendmail 8 ,
676apache) but will
677often have a directive in its configuration file to adjust the queue size up.
678Larger listen queues also do a better job of fending off denial of service
679attacks.
680.Pp
681The
682.Va kern.maxfiles
683sysctl determines how many open files the system supports.
684The default is
685typically a few thousand but you may need to bump this up to ten or twenty
686thousand if you are running databases or large descriptor-heavy daemons.
687The read-only
688.Va kern.openfiles
689sysctl may be interrogated to determine the current number of open files
690on the system.
691.Pp
692The
693.Va vm.swap_idle_enabled
694sysctl is useful in large multi-user systems where you have lots of users
695entering and leaving the system and lots of idle processes.
696Such systems
697tend to generate a great deal of continuous pressure on free memory reserves.
698Turning this feature on and adjusting the swapout hysteresis (in idle
699seconds) via
700.Va vm.swap_idle_threshold1
701and
702.Va vm.swap_idle_threshold2
703allows you to depress the priority of pages associated with idle processes
704more quickly then the normal pageout algorithm.
705This gives a helping hand
706to the pageout daemon.
707Do not turn this option on unless you need it,
708because the tradeoff you are making is to essentially pre-page memory sooner
709rather than later, eating more swap and disk bandwidth.
710In a small system
711this option will have a detrimental effect but in a large system that is
712already doing moderate paging this option allows the VM system to stage
713whole processes into and out of memory more easily.
714.Sh LOADER TUNABLES
715Some aspects of the system behavior may not be tunable at runtime because
716memory allocations they perform must occur early in the boot process.
717To change loader tunables, you must set their values in
718.Xr loader.conf 5
719and reboot the system.
720.Pp
721.Va kern.maxusers
722controls the scaling of a number of static system tables, including defaults
723for the maximum number of open files, sizing of network memory resources, etc.
724As of
725.Fx 4.5 ,
726.Va kern.maxusers
727is automatically sized at boot based on the amount of memory available in
728the system, and may be determined at run-time by inspecting the value of the
729read-only
730.Va kern.maxusers
731sysctl.
732Some sites will require larger or smaller values of
733.Va kern.maxusers
734and may set it as a loader tunable; values of 64, 128, and 256 are not
735uncommon.
736We do not recommend going above 256 unless you need a huge number
737of file descriptors; many of the tunable values set to their defaults by
738.Va kern.maxusers
739may be individually overridden at boot-time or run-time as described
740elsewhere in this document.
741Systems older than
742.Fx 4.4
743must set this value via the kernel
744.Xr config 8
745option
746.Cd maxusers
747instead.
748.Pp
749The
750.Va kern.dfldsiz
751and
752.Va kern.dflssiz
753tunables set the default soft limits for process data and stack size
754respectively.
755Processes may increase these up to the hard limits by calling
756.Xr setrlimit 2 .
757The
758.Va kern.maxdsiz ,
759.Va kern.maxssiz ,
760and
761.Va kern.maxtsiz
762tunables set the hard limits for process data, stack, and text size
763respectively; processes may not exceed these limits.
764The
765.Va kern.sgrowsiz
766tunable controls how much the stack segment will grow when a process
767needs to allocate more stack.
768.Pp
769.Va kern.ipc.nmbclusters
770may be adjusted to increase the number of network mbufs the system is
771willing to allocate.
772Each cluster represents approximately 2K of memory,
773so a value of 1024 represents 2M of kernel memory reserved for network
774buffers.
775You can do a simple calculation to figure out how many you need.
776If you have a web server which maxes out at 1000 simultaneous connections,
777and each connection eats a 16K receive and 16K send buffer, you need
778approximately 32MB worth of network buffers to deal with it.
779A good rule of
780thumb is to multiply by 2, so 32MBx2 = 64MB/2K = 32768.
781So for this case
782you would want to set
783.Va kern.ipc.nmbclusters
784to 32768.
785We recommend values between
7861024 and 4096 for machines with moderates amount of memory, and between 4096
787and 32768 for machines with greater amounts of memory.
788Under no circumstances
789should you specify an arbitrarily high value for this parameter, it could
790lead to a boot-time crash.
791The
792.Fl m
793option to
794.Xr netstat 1
795may be used to observe network cluster use.
796Older versions of
797.Fx
798do not have this tunable and require that the
799kernel
800.Xr config 8
801option
802.Dv NMBCLUSTERS
803be set instead.
804.Pp
805More and more programs are using the
806.Xr sendfile 2
807system call to transmit files over the network.
808The
809.Va kern.ipc.nsfbufs
810sysctl controls the number of file system buffers
811.Xr sendfile 2
812is allowed to use to perform its work.
813This parameter nominally scales
814with
815.Va kern.maxusers
816so you should not need to modify this parameter except under extreme
817circumstances.
818See the
819.Sx TUNING
820section in the
821.Xr sendfile 2
822manual page for details.
823.Sh KERNEL CONFIG TUNING
824There are a number of kernel options that you may have to fiddle with in
825a large-scale system.
826In order to change these options you need to be
827able to compile a new kernel from source.
828The
829.Xr config 8
830manual page and the handbook are good starting points for learning how to
831do this.
832Generally the first thing you do when creating your own custom
833kernel is to strip out all the drivers and services you do not use.
834Removing things like
835.Dv INET6
836and drivers you do not have will reduce the size of your kernel, sometimes
837by a megabyte or more, leaving more memory available for applications.
838.Pp
839.Dv SCSI_DELAY
840may be used to reduce system boot times.
841The defaults are fairly high and
842can be responsible for 5+ seconds of delay in the boot process.
843Reducing
844.Dv SCSI_DELAY
845to something below 5 seconds could work (especially with modern drives).
846.Pp
847There are a number of
848.Dv *_CPU
849options that can be commented out.
850If you only want the kernel to run
851on a Pentium class CPU, you can easily remove
852.Dv I486_CPU ,
853but only remove
854.Dv I586_CPU
855if you are sure your CPU is being recognized as a Pentium II or better.
856Some clones may be recognized as a Pentium or even a 486 and not be able
857to boot without those options.
858If it works, great!
859The operating system
860will be able to better use higher-end CPU features for MMU, task switching,
861timebase, and even device operations.
862Additionally, higher-end CPUs support
8634MB MMU pages, which the kernel uses to map the kernel itself into memory,
864increasing its efficiency under heavy syscall loads.
865.Sh IDE WRITE CACHING
866.Fx 4.3
867flirted with turning off IDE write caching.
868This reduced write bandwidth
869to IDE disks but was considered necessary due to serious data consistency
870issues introduced by hard drive vendors.
871Basically the problem is that
872IDE drives lie about when a write completes.
873With IDE write caching turned
874on, IDE hard drives will not only write data to disk out of order, they
875will sometimes delay some of the blocks indefinitely under heavy disk
876load.
877A crash or power failure can result in serious file system
878corruption.
879So our default was changed to be safe.
880Unfortunately, the
881result was such a huge loss in performance that we caved in and changed the
882default back to on after the release.
883You should check the default on
884your system by observing the
885.Va hw.ata.wc
886sysctl variable.
887If IDE write caching is turned off, you can turn it back
888on by setting the
889.Va hw.ata.wc
890loader tunable to 1.
891More information on tuning the ATA driver system may be found in the
892.Xr ata 4
893manual page.
894If you need performance, go with SCSI.
895.Sh CPU, MEMORY, DISK, NETWORK
896The type of tuning you do depends heavily on where your system begins to
897bottleneck as load increases.
898If your system runs out of CPU (idle times
899are perpetually 0%) then you need to consider upgrading the CPU or moving to
900an SMP motherboard (multiple CPU's), or perhaps you need to revisit the
901programs that are causing the load and try to optimize them.
902If your system
903is paging to swap a lot you need to consider adding more memory.
904If your
905system is saturating the disk you typically see high CPU idle times and
906total disk saturation.
907.Xr systat 1
908can be used to monitor this.
909There are many solutions to saturated disks:
910increasing memory for caching, mirroring disks, distributing operations across
911several machines, and so forth.
912If disk performance is an issue and you
913are using IDE drives, switching to SCSI can help a great deal.
914While modern
915IDE drives compare with SCSI in raw sequential bandwidth, the moment you
916start seeking around the disk SCSI drives usually win.
917.Pp
918Finally, you might run out of network suds.
919The first line of defense for
920improving network performance is to make sure you are using switches instead
921of hubs, especially these days where switches are almost as cheap.
922Hubs
923have severe problems under heavy loads due to collision back-off and one bad
924host can severely degrade the entire LAN.
925Second, optimize the network path
926as much as possible.
927For example, in
928.Xr firewall 7
929we describe a firewall protecting internal hosts with a topology where
930the externally visible hosts are not routed through it.
931Use 100BaseT rather
932than 10BaseT, or use 1000BaseT rather than 100BaseT, depending on your needs.
933Most bottlenecks occur at the WAN link (e.g.\&
934modem, T1, DSL, whatever).
935If expanding the link is not an option it may be possible to use the
936.Xr dummynet 4
937feature to implement peak shaving or other forms of traffic shaping to
938prevent the overloaded service (such as web services) from affecting other
939services (such as email), or vice versa.
940In home installations this could
941be used to give interactive traffic (your browser,
942.Xr ssh 1
943logins) priority
944over services you export from your box (web services, email).
945.Sh SEE ALSO
946.Xr netstat 1 ,
947.Xr systat 1 ,
948.Xr sendfile 2 ,
949.Xr ata 4 ,
950.Xr dummynet 4 ,
951.Xr login.conf 5 ,
952.Xr rc.conf 5 ,
953.Xr sysctl.conf 5 ,
954.Xr firewall 7 ,
955.Xr hier 7 ,
956.Xr ports 7 ,
957.Xr boot 8 ,
958.Xr bsdlabel 8 ,
959.Xr ccdconfig 8 ,
960.Xr config 8 ,
961.Xr fsck 8 ,
962.Xr gjournal 8 ,
963.Xr gstripe 8 ,
964.Xr gvinum 8 ,
965.Xr ifconfig 8 ,
966.Xr ipfw 8 ,
967.Xr loader 8 ,
968.Xr mount 8 ,
969.Xr newfs 8 ,
970.Xr route 8 ,
971.Xr sysctl 8 ,
972.Xr sysinstall 8 ,
973.Xr tunefs 8
974.Sh HISTORY
975The
976.Nm
977manual page was originally written by
978.An Matthew Dillon
979and first appeared
980in
981.Fx 4.3 ,
982May 2001.
983