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