xref: /freebsd/share/man/man7/tuning.7 (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1.\" Copyright (c) 2001, Matthew Dillon.  Terms and conditions are those of
2.\" the BSD Copyright as specified in the file "/usr/src/COPYRIGHT" in
3.\" the source tree.
4.\"
5.\" $FreeBSD$
6.\"
7.Dd May 25, 2001
8.Dt TUNING 7
9.Os FreeBSD
10.Sh NAME
11.Nm tuning
12.Nd performance tuning under FreeBSD
13.Sh SYSTEM SETUP - DISKLABEL, NEWFS, TUNEFS, SWAP
14.Pp
15When using
16.Xr disklabel 8
17to lay out your filesystems on a hard disk it is important to remember
18that hard drives can transfer data much more quickly from outer tracks
19than they can from inner tracks.  To take advantage of this you should
20try to pack your smaller filesystems and swap closer to the outer tracks,
21follow with the larger filesystems, and end with the largest filesystems.
22It is also important to size system standard filesystems such that you
23will not be forced to resize them later as you scale the machine up.
24I usually create, in order, a 128M root, 1G swap, 128M /var, 128M /var/tmp,
253G /usr, and use any remaining space for /home.
26.Pp
27You should typically size your swap space to approximately 2x main memory.
28If you do not have a lot of ram, though, you will generally want a lot
29more swap.  It is not recommended that you configure any less than
30256M of swap on a system and you should keep in mind future memory
31expansion when sizing the swap partition.
32The kernel's VM paging algorithms are tuned to perform best when there is
33at least 2x swap versus main memory.  Configuring too little swap can lead
34to inefficiencies in the VM page scanning code as well as create issues
35later on if you add more memory to your machine.  Finally, on larger systems
36with multiple SCSI disks (or multiple IDE disks operating on different
37controllers), we strongly recommend that you configure swap on each drive
38(up to four drives).  The swap partitions on the drives should be
39approximately the same size.  The kernel can handle arbitrary sizes but
40internal data structures scale to 4 times the largest swap partition.  Keeping
41the swap partitions near the same size will allow the kernel to optimally
42stripe swap space across the N disks.  Don't worry about overdoing it a
43little, swap space is the saving grace of
44.Ux
45and even if you don't normally use much swap, it can give you more time to
46recover from a runaway program before being forced to reboot.
47.Pp
48How you size your
49.Em /var
50partition depends heavily on what you intend to use the machine for.  This
51partition is primarily used to hold mailboxes, the print spool, and log
52files.  Some people even make
53.Em /var/log
54its own partition (but except for extreme cases it isn't worth the waste
55of a partition id).  If your machine is intended to act as a mail
56or print server,
57or you are running a heavily visited web server, you should consider
58creating a much larger partition - perhaps a gig or more.  It is very easy
59to underestimate log file storage requirements.
60.Pp
61Sizing
62.Em /var/tmp
63depends on the kind of temporary file usage you think you will need.  128M is
64the minimum we recommend.  Also note that you usually want to make
65.Em /tmp
66a softlink to
67.Em /var/tmp .
68Dedicating a partition for temporary file storage is important for
69two reasons:  First, it reduces the possibility of filesystem corruption
70in a crash, and second it reduces the chance of a runaway process that
71fills up [/var]/tmp from blowing up more critical subsystems (mail,
72logging, etc).  Filling up [/var]/tmp is a very common problem to have.
73.Pp
74In the old days there were differences between /tmp and /var/tmp,
75but the introduction of /var (and /var/tmp) led to massive confusion
76by program writers so today programs halfhazardly use one or the
77other and thus no real distinction can be made between the two.  So
78it makes sense to have just one temporary directory.  You can do the
79softlink either way.  The one thing you do not want to do is leave /tmp
80on the root partition where it might cause root to fill up or possibly
81corrupt root in a crash/reboot situation.
82.Pp
83The
84.Em /usr
85partition holds the bulk of the files required to support the system and
86a subdirectory within it called
87.Em /usr/local
88holds the bulk of the files installed from the
89.Xr ports 7
90hierarchy.  If you do not use ports all that much and do not intend to keep
91system source (/usr/src) on the machine, you can get away with
92a 1 gigabyte /usr partition.  However, if you install a lot of ports
93(especially window managers and linux-emulated binaries), we recommend
94at least a 2 gigabyte /usr and if you also intend to keep system source
95on the machine, we recommend a 3 gigabyte /usr.  Do not underestimate the
96amount of space you will need in this partition, it can creep up and
97surprise you!
98.Pp
99The
100.Em /home
101partition is typically used to hold user-specific data.  I usually size it
102to the remainder of the disk.
103.Pp
104Why partition at all?  Why not create one big
105.Em /
106partition and be done with it?  Then I don't have to worry about undersizing
107things!  Well, there are several reasons this isn't a good idea.  First,
108each partition has different operational characteristics and separating them
109allows the filesystem to tune itself to those characteristics.  For example,
110the root and /usr partitions are read-mostly, with very little writing, while
111a lot of reading and writing could occur in /var and /var/tmp.  By properly
112partitioning your system, fragmentation introduced in the smaller more
113heavily write-loaded partitions will not bleed over into the mostly-read
114partitions.  Additionally, keeping the write-loaded partitions closer to
115the edge of the disk (i.e. before the really big partitions instead of after
116in the partition table) will increase I/O performance in the partitions
117where you need it the most.  Now it is true that you might also need I/O
118performance in the larger partitions, but they are so large that shifting
119them more towards the edge of the disk will not lead to a significant
120performance improvement whereas moving /var to the edge can have a huge impact.
121Finally, there are safety concerns.  Having a small neat root partition that
122is essentially read-only gives it a greater chance of surviving a bad crash
123intact.
124.Pp
125Properly partitioning your system also allows you to tune
126.Xr newfs 8 ,
127and
128.Xr tunefs 8
129parameters.  Tuning
130.Fn newfs
131requires more experience but can lead to significant improvements in
132performance.  There are three parameters that are relatively safe to
133tune:
134.Em blocksize ,
135.Em bytes/inode ,
136and
137.Em cylinders/group .
138.Pp
139.Fx
140performs best when using 8K or 16K filesystem block sizes.  The default
141filesystem  block size is 8K.  For larger partitions it is usually a good
142idea to use a 16K block size.  This also requires you to specify a larger
143fragment size.  We recommend always using a fragment size that is 1/8
144the block size (less testing has been done on other fragment size factors).
145The
146.Fn newfs
147options for this would be
148.Em newfs -f 2048 -b 16384 ...
149Using a larger block size can cause fragmentation of the buffer cache and
150lead to lower performance.
151.Pp
152If a large partition is intended to be used to hold fewer, larger files, such
153as a database files, you can increase the
154.Em bytes/inode
155ratio which reduces the number if inodes (maximum number of files and
156directories that can be created) for that partition.  Decreasing the number
157of inodes in a filesystem can greatly reduce
158.Xr fsck 8
159recovery times after a crash.  Do not use this option
160unless you are actually storing large files on the partition, because if you
161overcompensate you can wind up with a filesystem that has lots of free
162space remaining but cannot accommodate any more files.  Using
16332768, 65536, or 262144 bytes/inode is recommended.  You can go higher but
164it will have only incremental effects on fsck recovery times.  For
165example,
166.Em newfs -i 32768 ...
167.Pp
168Finally, increasing the
169.Em cylinders/group
170ratio has the effect of packing the inodes closer together.  This can increase
171directory performance and also decrease fsck times.  If you use this option
172at all, we recommend maxing it out.  Use
173.Em newfs -c 999
174and newfs will error out and tell you what the maximum is, then use that.
175.Pp
176.Xr tunefs 8
177may be used to further tune a filesystem.  This command can be run in
178single-user mode without having to reformat the filesystem.  However, this
179is possibly the most abused program in the system.  Many people attempt to
180increase available filesystem space by setting the min-free percentage to 0.
181This can lead to severe filesystem fragmentation and we do not recommend
182that you do this.  Really the only tunefs option worthwhile here is turning on
183.Em softupdates
184with
185.Em tunefs -n enable /filesystem.
186(Note: In 5.x softupdates can be turned on using the -U option to newfs).
187Softupdates drastically improves meta-data performance, mainly file
188creation and deletion.  We recommend enabling softupdates on all of your
189filesystems.  There are two downsides to softupdates that you should be
190aware of:  First, softupdates guarantees filesystem consistency in the
191case of a crash but could very easily be several seconds (even a minute!)
192behind updating the physical disk.  If you crash you may lose more work
193than otherwise.  Secondly, softupdates delays the freeing of filesystem
194blocks.  If you have a filesystem (such as the root filesystem) which is
195close to full, doing a major update of it, e.g.
196.Em make installworld,
197can run it out of space and cause the update to fail.
198.Sh STRIPING DISKS
199In larger systems you can stripe partitions from several drives together
200to create a much larger overall partition.  Striping can also improve
201the performance of a filesystem by splitting I/O operations across two
202or more disks.  The
203.Xr vinum 8
204and
205.Xr ccd 4
206utilities may be used to create simple striped filesystems.  Generally
207speaking, striping smaller partitions such as the root and /var/tmp,
208or essentially read-only partitions such as /usr is a complete waste of
209time.  You should only stripe partitions that require serious I/O performance...
210typically /var, /home, or custom partitions used to hold databases and web
211pages.  Choosing the proper stripe size is also
212important.  Filesystems tend to store meta-data on power-of-2 boundaries
213and you usually want to reduce seeking rather than increase seeking.  This
214means you want to use a large off-center stripe size such as 1152 sectors
215so sequential I/O does not seek both disks and so meta-data is distributed
216across both disks rather than concentrated on a single disk.  If
217you really need to get sophisticated, we recommend using a real hardware
218raid controller from the list of
219.Fx
220supported controllers.
221.Sh SYSCTL TUNING
222.Pp
223There are several hundred
224.Xr sysctl 8
225variables in the system, including many that appear to be candidates for
226tuning but actually aren't.  In this document we will only cover the ones
227that have the greatest effect on the system.
228.Pp
229The
230.Em kern.ipc.shm_use_phys
231sysctl defaults to 0 (off) and may be set to 0 (off) or 1 (on).  Setting
232this parameter to 1 will cause all SysV shared memory segments to be
233mapped to unpageable physical ram.  This feature only has an effect if you
234are either (A) mapping small amounts of shared memory across many (hundreds)
235of processes, or (B) mapping large amounts of shared memory across any
236number of processes.  This feature allows the kernel to remove a great deal
237of internal memory management page-tracking overhead at the cost of wiring
238the shared memory into core, making it unswappable.
239.Pp
240The
241.Em vfs.vmiodirenable
242sysctl defaults to 0 (off) (though soon it will default to 1) and may be
243set to 0 (off) or 1 (on).  This parameter controls how directories are cached
244by the system.  Most directories are small and use but a single fragment
245(typically 1K) in the filesystem and even less (typically 512 bytes) in
246the buffer cache.  However, when operating in the default mode the buffer
247cache will only cache a fixed number of directories even if you have a huge
248amount of memory.  Turning on this sysctl allows the buffer cache to use
249the VM Page Cache to cache the directories.  The advantage is that all of
250memory is now available for caching directories.  The disadvantage is that
251the minimum in-core memory used to cache a directory is the physical page
252size (typically 4K) rather than 512 bytes.  We recommend turning this option
253on if you are running any services which manipulate large numbers of files.
254Such services can include web caches, large mail systems, and news systems.
255Turning on this option will generally not reduce performance even with the
256wasted memory but you should experiment to find out.
257.Pp
258There are various buffer-cache and VM page cache related sysctls.  We do
259not recommend messing around with these at all.  As of
260.Fx 4.3 ,
261the VM system does an extremely good job tuning itself.
262.Pp
263The
264.Em net.inet.tcp.sendspace
265and
266.Em net.inet.tcp.recvspace
267sysctls are of particular interest if you are running network intensive
268applications.  This controls the amount of send and receive buffer space
269allowed for any given TCP connection.  The default is 16K.  You can often
270improve bandwidth utilization by increasing the default at the cost of
271eating up more kernel memory for each connection.  We do not recommend
272increasing the defaults if you are serving hundreds or thousands of
273simultaneous connections because it is possible to quickly run the system
274out of memory due to stalled connections building up.  But if you need
275high bandwidth over a fewer number of connections, especially if you have
276gigabit ethernet, increasing these defaults can make a huge difference.
277You can adjust the buffer size for incoming and outgoing data separately.
278For example, if your machine is primarily doing web serving you may want
279to decrease the recvspace in order to be able to increase the sendspace
280without eating too much kernel memory.  Note that the route table, see
281.Xr route 8 ,
282can be used to introduce route-specific send and receive buffer size
283defaults.  As an additional management tool you can use pipes in your
284firewall rules, see
285.Xr ipfw 8 ,
286to limit the bandwidth going to or from particular IP blocks or ports.
287For example, if you have a T1 you might want to limit your web traffic
288to 70% of the T1's bandwidth in order to leave the remainder available
289for mail and interactive use.   Normally a heavily loaded web server
290will not introduce significant latencies into other services even if
291the network link is maxed out, but enforcing a limit can smooth things
292out and lead to longer term stability.  Many people also enforce artificial
293bandwidth limitations in order to ensure that they are not charged for
294using too much bandwidth.
295.Pp
296Setting the send or receive TCP buffer to values larger then 65535 will result
297in a marginal performance improvement at best due to limitations within
298the TCP protocol itself.
299These limitations can prevent certain types of network links (specifically,
300gigabit WAN links and high-latency satellite links) from reaching
301their maximum level of performance.  For such cases we first recommend that
302you simply set the TCP buffer size to 65535 and stick with that if the
303performance is acceptable.  In extreme cases you may have to turn on the
304.Em net.inet.tcp.rfc1323
305sysctl and increase the buffer size to values greater then 65535.  This option
306turns on the window sizing extension to the TCP protocol.  We do not recommend
307that you use this option unless you absolutely have to because many hosts on
308the internet can't handle the feature and may cause connections to freeze up.
309.Pp
310We recommend that you turn on (set to 1) and leave on the
311.Em net.inet.tcp.always_keepalive
312control.  The default is usually off.  This introduces a small amount of
313additional network bandwidth but guarantees that dead tcp connections
314will eventually be recognized and cleared.  Dead tcp connections are a
315particular problem on systems accessed by users operating over dialups,
316because users often disconnect their modems without properly closing active
317connections.
318.Pp
319The
320.Em kern.ipc.somaxconn
321sysctl limits the size of the listen queue for accepting new tcp connections.
322The default value of 128 is typically too low for robust handling of new
323connections in a heavily loaded web server environment.  For such environments,
324we recommend increasing this value to 1024 or higher.  The service daemon
325may itself limit the listen queue size (e.g. sendmail, apache) but will
326often have a directive in its configuration file to adjust the queue size up.
327Larger listen queue also do a better job of fending of denial of service
328attacks.
329.Sh KERNEL CONFIG TUNING
330.Pp
331There are a number of kernel options that you may have to fiddle with in
332a large scale system.  In order to change these options you need to be
333able to compile a new kernel from source.  The
334.Xr config 8
335manual page and the handbook are good starting points for learning how to
336do this.  Generally the first thing you do when creating your own custom
337kernel is to strip out all the drivers and services you don't use.  Removing
338things like
339.Em INET6
340and drivers you don't have will reduce the size of your kernel, sometimes
341by a megabyte or more, leaving more memory available for applications.
342.Pp
343The
344.Em maxusers
345kernel option defaults to an incredibly low value.  For most modern machines,
346you probably want to increase this value to 64, 128, or 256.  We do not
347recommend going above 256 unless you need a huge number of file descriptors.
348Network buffers are also affected but can be controlled with a separate
349kernel option.  Do not increase maxusers just to get more network mbufs.
350.Pp
351.Em NMBCLUSTERS
352may be adjusted to increase the number of network mbufs the system is
353willing to allocate.  Each cluster represents approximately 2K of memory,
354so a value of 1024 represents 2M of kernel memory reserved for network
355buffers.  You can do a simple calculation to figure out how many you need.
356If you have a web server which maxes out at 1000 simultaneous connections,
357and each connection eats a 16K receive and 16K send buffer, you need
358approximate 32MB worth of network buffers to deal with it.  A good rule of
359thumb is to multiply by 2, so 32MBx2 = 64MB/2K = 32768.  So for this case
360you would want to se NMBCLUSTERS to 32768.  We recommend values between
3611024 and 4096 for machines with moderates amount of memory, and between 4096
362and 32768 for machines with greater amounts of memory.  Under no circumstances
363should you specify an arbitrarily high value for this parameter, it could
364lead to a boot-time crash.  The -m option to
365.Xr netstat 1
366may be used to observe network cluster use.
367.Pp
368More and more programs are using the
369.Fn sendfile
370system call to transmit files over the network.  The
371.Em NSFBUFS
372kernel parameter controls the number of filesystem buffers
373.Fn sendfile
374is allowed to use to perform its work.  This parameter nominally scales
375with
376.Em maxusers
377so you should not need to mess with this parameter except under extreme
378circumstances.
379.Pp
380.Em SCSI_DELAY
381and
382.Em IDE_DELAY
383may be used to reduce system boot times.  The defaults are fairly high and
384can be responsible for 15+ seconds of delay in the boot process.  Reducing
385SCSI_DELAY to 5 seconds usually works (especially with modern drives).
386Reducing IDE_DELAY also works but you have to be a little more careful.
387.Pp
388There are a number of
389.Em XXX_CPU
390options that can be commented out.  If you only want the kernel to run
391on a Pentium class cpu, you can easily remove
392.Em I386_CPU
393and
394.Em I486_CPU,
395but only remove
396.Em I586_CPU
397if you are sure your cpu is being recognized as a Pentium II or better.
398Some clones may be recognized as a pentium or even a 486 and not be able
399to boot without those options.  If it works, great!  The operating system
400will be able to better-use higher-end cpu features for mmu, task switching,
401timebase, and even device operations.  Additionally, higher-end cpus support
4024MB MMU pages which the kernel uses to map the kernel itself into memory,
403which increases its efficiency under heavy syscall loads.
404.Sh IDE WRITE CACHING
405.Fx 4.3
406flirted with turning off IDE write caching.  This reduced write bandwidth
407to IDE disks but was considered necessary due to serious data consistency
408issues introduced by hard drive vendors.  Basically the problem is that
409IDE drives lie about when a write completes.  With IDE write caching turned
410on, IDE hard drives will not only write data to disk out of order, they
411will sometimes delay some of the blocks indefinitely when under heavy disk
412loads.  A crash or power failure can result in serious filesystem
413corruption.  So our default was changed to be safe.  Unfortunately, the
414result was such a huge loss in performance that we caved in and changed the
415default back to on after the release.  You should check the default on
416your system by observing the
417.Em hw.ata.wc
418sysctl variable.  If IDE write caching is turned off, you can turn it back
419on by setting the
420.Eme hw.ata.wc
421kernel variable back to 1.  This must be done from the boot loader at boot
422time.  Attempting to do it after the kernel boots will have no effect.
423Please see
424.Xr ata 4 ,
425and
426.Xr loader 8 .
427.Pp
428There is a new experimental feature for IDE hard drives called hw.ata.tags
429(you also set this in the bootloader) which allows write caching to be safely
430turned on.  This brings SCSI tagging features to IDE drives.  As of this
431writing only IBM DPTA and DTLA drives support the feature.  Warning!  These
432drives apparently have quality control problems and I do not recommend
433purchasing them at this time.  If you need performance, go with SCSI.
434.Sh CPU, MEMORY, DISK, NETWORK
435The type of tuning you do depends heavily on where your system begins to
436bottleneck as load increases.  If your system runs out of cpu (idle times
437are perpetually 0%) then you need to consider upgrading the cpu or moving to
438an SMP motherboard (multiple cpu's), or perhaps you need to revisit the
439programs that are causing the load and try to optimize them.  If your system
440is paging to swap a lot you need to consider adding more memory.  If your
441system is saturating the disk you typically see high cpu idle times and
442total disk saturation.
443.Xr systat 1
444can be used to monitor this.  There are many solutions to saturated disks:
445increasing memory for caching, mirroring disks, distributing operations across
446several machines, and so forth.  If disk performance is an issue and you
447are using IDE drives, switching to SCSI can help a great deal.  While modern
448IDE drives compare with SCSI in raw sequential bandwidth, the moment you
449start seeking around the disk SCSI drives usually win.
450.Pp
451Finally, you might run out of network suds.  The first line of defense for
452improving network performance is to make sure you are using switches instead
453of hubs, especially these days where switches are almost as cheap.  Hubs
454have severe problems under heavy loads due to collision backoff and one bad
455host can severely degrade the entire LAN.  Second, optimize the network path
456as much as possible.  For example, in
457.Xr firewall 7
458we describe a firewall protecting internal hosts with a topology where
459the externally visible hosts are not routed through it.  Use 100BaseT rather
460than 10BaseT, or use 1000BaseT rather then 100BaseT, depending on your needs.
461Most bottlenecks occur at the WAN link (e.g. modem, T1, DSL, whatever).
462If expanding the link is not an option it may be possible to use ipfw's
463.Sy DUMMYNET
464feature to implement peak shaving or other forms of traffic shaping to
465prevent the overloaded service (such as web services) from effecting other
466services (such as email), or vise versa.  In home installations this could
467be used to give interactive traffic (your browser, ssh logins) priority
468over services you export from your box (web services, email).
469.Sh SEE ALSO
470.Pp
471.Xr netstat 1 ,
472.Xr systat 1 ,
473.Xr ata 4 ,
474.Xr ccd 4 ,
475.Xr login.conf 5 ,
476.Xr hier 7 ,
477.Xr firewall 7 ,
478.Xr ports 7 ,
479.Xr boot 8 ,
480.Xr config 8 ,
481.Xr disklabel 8 ,
482.Xr fsck 8 ,
483.Xr ifconfig 8 ,
484.Xr ipfw 8 ,
485.Xr loader 8 ,
486.Xr newfs 8 ,
487.Xr route 8 ,
488.Xr sysctl 8 ,
489.Xr tunefs 8 ,
490.Xr vinum 8
491.Sh HISTORY
492The
493.Nm
494manual page was originally written by
495.An Matthew Dillon
496and first appeared
497in
498.Fx 4.3 ,
499May 2001.
500