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