xref: /freebsd/share/man/man7/tuning.7 (revision 99e8005137088aafb1350e23b113d69b01b0820f)
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
19then 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 significnat
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 accomodate 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 turning softupdates on on all of your
189filesystems.  There are two downsides to softupdates that you should be
190aware of:  First, softupdates guarentees 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
193then 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 boundries
213and you usually want to reduce seeking rather then 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 then 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 then 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
273simultanious 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 mangagement 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
296We recommend that you turn on (set to 1) and leave on the
297.Em net.inet.tcp.always_keepalive
298control.  The default is usually off.  This introduces a small amount of
299additional network bandwidth but guarentees that dead tcp connections
300will eventually be recognized and cleared.  Dead tcp connections are a
301particular problem on systems accesed by users operating over dialups,
302because users often disconnect their modems without properly closing active
303connections.
304.Pp
305The
306.Em kern.ipc.somaxconn
307sysctl limits the size of the listen queue for accepting new tcp connections.
308The default value of 128 is typically too low for robust handling of new
309connections in a heavily loaded web server environment.  For such environments,
310we recommend increasing this value to 1024 or higher.  The service daemon
311may itself limit the listen queue size (e.g. sendmail, apache) but will
312often have a directive in its configuration file to adjust the queue size up.
313Larger listen queue also do a better job of fending of denial of service
314attacks.
315.Sh KERNEL CONFIG TUNING
316.Pp
317There are a number of kernel options that you may have to fiddle with in
318a large scale system.  In order to change these options you need to be
319able to compile a new kernel from source.  The
320.Xr config 8
321manual page and the handbook are good starting points for learning how to
322do this.  Generally the first thing you do when creating your own custom
323kernel is to strip out all the drivers and services you don't use.  Removing
324things like
325.Em INET6
326and drivers you don't have will reduce the size of your kernel, sometimes
327by a megabyte or more, leaving more memory available for applications.
328.Pp
329The
330.Em maxusers
331kernel option defaults to an incredibly low value.  For most modern machines,
332you probably want to increase this value to 64, 128, or 256.  We do not
333recommend going above 256 unless you need a huge number of file descriptors.
334Network buffers are also affected but can be controlled with a separate
335kernel option.  Do not increase maxusers just to get more network mbufs.
336.Pp
337.Em NMBCLUSTERS
338may be adjusted to increase the number of network mbufs the system is
339willing to allocate.  Each cluster represents approximately 2K of memory,
340so a value of 1024 represents 2M of kernel memory reserved for network
341buffers.  You can do a simple calculation to figure out how many you need.
342If you have a web server which maxes out at 1000 simultanious connections,
343and each connection eats a 16K receive and 16K send buffer, you need
344approximate 32MB worth of network buffers to deal with it.  A good rule of
345thumb is to multiply by 2, so 32MBx2 = 64MB/2K = 32768.  So for this case
346you would want to se NMBCLUSTERS to 32768.  We recommend values between
3471024 and 4096 for machines with moderates amount of memory, and between 4096
348and 32768 for machines with greater amounts of memory.  Under no circumstances
349should you specify an arbitrarily high value for this parameter, it could
350lead to a boot-time crash.  The -m option to
351.Xr netstat 1
352may be used to observe network cluster use.
353.Pp
354More and more programs are using the
355.Fn sendfile
356system call to transmit files over the network.  The
357.Em NSFBUFS
358kernel parameter controls the number of filesystem buffers
359.Fn sendfile
360is allowed to use to perform its work.  This parameter nominally scales
361with
362.Em maxusers
363so you should not need to mess with this parameter except under extreme
364circumstances.
365.Pp
366.Em SCSI_DELAY
367and
368.Em IDE_DELAY
369may be used to reduce system boot times.  The defaults are fairly high and
370can be responsible for 15+ seconds of delay in the boot process.  Reducing
371SCSI_DELAY to 5 seconds usually works (especially with modern drives).
372Reducing IDE_DELAY also works but you have to be a little more careful.
373.Pp
374There are a number of
375.Em XXX_CPU
376options that can be commented out.  If you only want the kernel to run
377on a Pentium class cpu, you can easily remove
378.Em I386_CPU
379and
380.Em I486_CPU,
381but only remove
382.Em I586_CPU
383if you are sure your cpu is being recognized as a Pentium II or better.
384Some clones may be recognized as a pentium or even a 486 and not be able
385to boot without those options.  If it works, great!  The operating system
386will be able to better-use higher-end cpu features for mmu, task switching,
387timebase, and even device operations.  Additionally, higher-end cpus support
3884MB MMU pages which the kernel uses to map the kernel itself into memory,
389which increases its efficiency under heavy syscall loads.
390.Sh IDE WRITE CACHING
391As of
392.Fx 4.3 ,
393IDE write caching is turned off by default.  This will reduce write bandwidth
394to IDE disks but is considered necessary due to serious data consistency
395issues introduced by hard drive vendors.  Basically the problem is that
396IDE drives lie about when a write completes.  With IDE write caching turned
397on, IDE hard drives will not only write data to disk out of order, they
398will sometimes delay some of the blocks indefinitely when under heavy disk
399loads.  A crash or power failure can result in serious filesystem
400corruption.  So our default is to be safe.  If you are willing to risk
401filesystem corruption, you can return to the old behavior by setting the
402hw.ata.wc
403kernel variable back to 1.  This must be done from the boot loader at boot
404time.  Please see
405.Xr ata 4 ,
406and
407.Xr loader 8 .
408.Pp
409There is a new experimental feature for IDE hard drives called hw.ata.tags
410(you also set this in the bootloader) which allows write caching to be safely
411turned on.  This brings SCSI tagging features to IDE drives.  As of this
412writing only IBM DPTA and DTLA drives support the feature.
413.Sh CPU, MEMORY, DISK, NETWORK
414The type of tuning you do depends heavily on where your system begins to
415bottleneck as load increases.  If your system runs out of cpu (idle times
416are pepetually 0%) then you need to consider upgrading the cpu or moving to
417an SMP motherboard (multiple cpu's), or perhaps you need to revisit the
418programs that are causing the load and try to optimize them.  If your system
419is paging to swap a lot you need to consider adding more memory.  If your
420system is saturating the disk you typically see high cpu idle times and
421total disk saturation.
422.Xr systat 1
423can be used to monitor this.  There are many solutions to saturated disks:
424increasing memory for caching, mirroring disks, distributing operations across
425several machines, and so forth.  If disk performance is an issue and you
426are using IDE drives, switching to SCSI can help a great deal.  While modern
427IDE drives compare with SCSI in raw sequential bandwidth, the moment you
428start seeking around the disk SCSI drives usually win.
429.Pp
430Finally, you might run out of network suds.  The first line of defense for
431improving network performance is to make sure you are using switches instead
432of hubs, especially these days where switches are almost as cheap.  Hubs
433have severe problems under heavy loads due to collision backoff and one bad
434host can severely degrade the entire LAN.  Second, optimize the network path
435as much as possible.  For example, in
436.Xr firewall 7
437we describe a firewall protecting internal hosts with a topology where
438the externally visible hosts are not routed through it.  Use 100BaseT rather
439then 10BaseT, or use 1000BaseT rather then 100BaseT, depending on your needs.
440Most bottlenecks occur at the WAN link (e.g. modem, T1, DSL, whatever).
441If expanding the link is not an option it may be possible to use ipfw's
442.Sy DUMMYNET
443feature to implement peak shaving or other forms of traffic shaping to
444prevent the overloaded service (such as web services) from effecting other
445services (such as email), or vise versa.  In home installations this could
446be used to give interactive traffic (your browser, ssh logins) priority
447over services you export from your box (web services, email).
448.Sh SEE ALSO
449.Pp
450.Xr ata 4 ,
451.Xr boot 8 ,
452.Xr ccd 4 ,
453.Xr config 8 ,
454.Xr disklabel 8 ,
455.Xr firewall 7 ,
456.Xr fsck 8 ,
457.Xr hier 7 ,
458.Xr ifconfig 8 ,
459.Xr ipfw 8 ,
460.Xr loader 8 ,
461.Xr login.conf 5 ,
462.Xr netstat 1 ,
463.Xr newfs 8 ,
464.Xr ports 7 ,
465.Xr route 8 ,
466.Xr sysctl 8 ,
467.Xr systat 1 ,
468.Xr tunefs 8 ,
469.Xr vinum 8
470.Sh HISTORY
471The
472.Nm
473manual page was originally written by
474.An Matthew Dillon
475and first appeared
476in
477.Fx 4.3 ,
478May 2001.
479