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