Linux System Performance Tuning

Source: Internet
Author: User
Tags website server

Level: elementary

Deng yanjun (, graduate student, Institute of software engineering, Xi'an University of Electronic Science and Technology

September 09, 2002

This article describes in detail how to tune Linux system performance in terms of disks, file and file systems, memory and compilation. Whether it is a Linux server, a client, or a single user, the tuning method provided in this Article can improve the performance of the Linux system to varying degrees.

1. Introduction

Practice has proved that Linux is a high-performance, stable, reliable, and flexible operating system. In recent years, many major software companies around the world have launched various Linux server systems and application software under Linux. At present, Linux has been able to stand out from various traditional commercial operating systems, occupying a considerable share in the server market. Linux server systems are diverse and can be used as Web servers, email servers, FTP servers, file servers, and database servers. Linux performance can be tuned to different systems and specific application environments. The following describes the tuning policies for Linux systems from the aspects of disk tuning, file system, memory management, and compilation optimization.

Back to Top

2. Disk Tuning

2.1 optimized partitions

When installing the Linux system, you should consider how to make the Linux system get the best performance. In Linux, We can freely organize disk partitions. An Optimized Partitioning policy can improve the Linux system performance, reduce disk fragments, and improve disk I/O capabilities.

Based on the characteristics of the disk, we know that the more the external cylinder of the disk, the faster the rotation, and each rotation, the disk read/write header can cover a large number of areas, this means that the external cylinder can achieve better performance. Therefore, when partitioning, we should consider placing partitions with a High Access frequency that has a relatively large impact on system performance on the disk's off-site. At the same time, in order to reduce disk fragments, the directories frequently changed in content should be placed in separate partitions. From the perspective of convenient data backup, because many backup tools have a high efficiency in backing up the entire partition, we should use several main directories of the Linux system as separate file systems, allocate a zone for each of them. Recommended partition policy 1:

There is still a portion of unpartitioned space on the disk. Why is the entire disk partition not installed? Currently, the hard disk space is relatively large. When installing the system, you may not be able to use the entire disk storage space when the files you install are added with the space needed for further growth. Reserve a portion of unpartitioned space. You can use fdisk to re-partition it if you need it later. When partitioning, you must estimate the size of each partition based on the system's future operation needs or past experience to avoid insufficient space in the future.

If your system has multiple hard disks, consider using multiple swap partitions. Each disk is divided into one swap partition. By setting the PRI option in the/etc/fstab file, multiple swap partitions can have the same priority. Linux can be used in parallel. This improves the performance of swap partitions.

/dev/hda5               swap             swap    defaults,pri=5  0 0/dev/hdc5               swap             swap    defaults,pri=5  0 0

Figure (2) setting the same priority for multiple swap partitions

Of course, if your system memory is large enough, and your system is far from using that much memory, there will be no virtual memory. When partitioning, you can consider removing swap partitions. However, as a Linux server, you should set swap partitions even if your memory is large enough.

2.2 Use hdparm to improve Linux system performance

If your Linux system runs on an IDE hard disk, you can use hdparm to improve disk I/O performance. However, be careful when using hdparm because data on the hard disk may be damaged. So read your hard disk manual carefully before using hdparm. Use the corresponding hdparm switch parameters according to your specific hard disk specifications. For an ultraata/66 Eide hard disk, its control chip supports multiple Pio modes and DMA. We use the following command to tune the disk performance:

                # /sbin/hdparm -x66 -d1 -u1 -m16 -c3  /dev/hda        

Option description:

① C3: Convert the 16-bit format of the hard disk to 32-Bit mode (32-bit Mode w/Sync ). Controls how data is transmitted from the PCI bus to the Controller.
② M16: changed the multi-sector reading function of the hard disk.-M16 allows the hard disk to read data in 16 sectors during an I/O interruption (depending on the specific hard disk ).
③ D1: Enable the DMA mode.
④ X66: In a hard disk that supports udma-capable, this parameter can support dual-DMA channel data transmission mode.
⑤ U1: When Linux is dealing with disk interruptions, it can unmask other interruptions or respond to other interruption-related tasks.


To view the above changes, run the following command:

          # /sbin/hdparm   /dev/hda     

To test disk I/O performance, run the following command:

          # /sbin/hdparm  -tT  /dev/hda     

If the disk performance is improved, run the following command to save the settings:

          # /sbin/hdparm  -k1  /dev/hda     

2.3 use soft RAID in Linux

RAID (redundant disk array) is a technology that distributes data across multiple disks to enhance disk performance and reliability. If your system does not have a hardware RAID Controller, You can implement raid through software in Linux. Raid has many levels. Different levels have different requirements on hardware, and different performance and reliability are also achieved. Raid0 is divided by data, and data blocks are repeatedly written to the disk, which provides the best read/write performance, but does not provide data redundancy. Raid1 is a disk image. Everything written to disk 1 is also written to Disk 2 and can be read from any disk. In addition to data splitting, raid3 also specifies a disk drive to store parity information.

In Linux, raid is implemented through software. Linux Kernel support is required. You can add raid support by compiling a new kernel. Then you need to compile and install the raidtools software package. Raidtools is a user-level tool that can initialize, start, stop, and control raid arrays. The following describes how to implement raid0 on Linux kernel 2.4 using two IDE hard disks.

Setting partitions to use raid0 requires at least two partitions, which are located on different disks. It is recommended that the two shards of raid0 have the same size. When creating a partition, specify the partition type as "FD ". To enable nuclear energy in Linux to recognize them as raid partitions so that these partitions can be automatically detected and started at each boot. If you do not mark a raid partition in this way, you must first enter "Raid start -- all" after each boot to mount the raid array. We create two shards of raid0: hda5 and hdc5.

Edit the/etc/raidtab file and create the/etc/raidtab file to describe the configuration information of the raid array. The Linux kernel uses this information to automatically detect and start raid arrays during boot. Therefore, this configuration must be performed for each created raid array. The configuration file that combines the partition hda5 and hdc5 into the md0 raid0 array is as follows:

        raiddev /dev/md0        raid-level0        nr-raid-disks2        persistent-superblock   1        chunk-size32        device/dev/hda5        raid-disk0        device/dev/hdc5        raid-disk1         

Figure (3) raid tab File

In the raidtab file, the "raiddev" entry indicates the raid array to be created; "nR-raid-Disks" indicates the number of disks in the array; "persistent-superblock" is set to 1, tells the raid tool when to create the volume. "chunk-size" specifies the block size used by RAID 0 in K Bytes. Finally, you must specify the devices that make up the array as partitions.

Run the "# mkraid/dev/md0" command to initialize the/dev/md0 partition for mkraid and create a file system. The md0 raid0 array is also started. Next, you can create the desired file system on md0. We use the reiserfs Log File System on the Linux server. The created command is "# mkreiserfs/dev/md0 ".

In this way, you can load the newly created raid0-based file system like other file systems.

2.4 use elvtune to tune disk I/O

In Linux kernel Versions later than 2.4, disk I/O Scheduling can be used to control the disk I/O response time and throughput. By adjusting the maximum waiting time of I/O requests in the queue, you can tune between the response time and throughput. If less response time is required, the throughput will be reduced. Otherwise, a longer response time will produce a larger throughput. You can use the tool "/sbin/elvtune" to change the maximum response time value. The usage is as follows:

View Current Settings

    # /sbin/elvtune  /dev/hda1  

Modify current configuration

   # /sbin/elvtune  -r 2000 -w 4000 /dev/hda1           

Here, the-R parameter is for read operations, and the-W parameter is for write operations.

You can use the "iostat-D-x/dev/hda1" command to obtain the average information (including the average request size and average queue length) to monitor the above I/O configuration results, and adjust the configuration to get the best performance. Generally, for Linux servers with frequent reads and writes but a small amount of data to be operated, and with high real-time requirements, you can reduce the parameter size. If a Linux server that does not frequently read/write but requires a large throughput, you can increase the parameter value to get a large throughput.

Back to Top

3. File and file system tuning

Size of 3.1 Blocks

You can specify the block size when creating a file system. In the future, if some large files are used in your file system, using a large block size will provide better performance. Adjust the block size of the ext2 File System to 4096byte instead of the default 1024 byte, which can reduce file fragmentation and speed up fsck scanning and file deletion and read operations. In addition, the ext2 file system retains 5% of the space for the root directory. For a large file system, unless used as a log file, the proportion of 5% is too large. You can use the command "# mke2fs-B 4096-M 1/dev/hda6" to change it to 1% and create a file system with a block size of 4096byte.

The size of the chunks to be used depends on the overall consideration of your system. If the system is used as a mail or news server, a large chunk size is used, although the performance is improved, however, this will cause a large waste of disk space. For example, the average file size in the file system is 2145 bytes. If the block size of 4096byte is used, every file will waste bytes of space on average. If the block size of bytes is used, each file will waste bytes of space on average. How to balance the cost of performance and disk depends on the needs of specific applications.

3.2 do not use the atime attribute.

When a file is created, modified, and accessed, the Linux system records the time information. Record the last time the file was read. When the system reads files frequently, it will be a lot of overhead. Therefore, to improve the system performance, we can read the file without modifying the atime attribute of the file. You can do this by using the notime option when loading the file system. When the noatime option is used to load (Mount) the file system, reading the file will not update the atime information in the file attributes. The importance of setting noatime is to eliminate the file system's write operations on the file. The file is simply read by the system. Since write operations consume more system resources than read operations, such settings can significantly improve server performance. Note that the wtime information is still valid and is updated when the file is written.

For example, if you want to set the notime option for the/Home file system in your system, you can modify the corresponding lines of the/etc/fstab file as follows:

  LABEL=/home         /home               ext2    noatime        1 2 

To make the setting take effect immediately, run the command "# Mount-oremount/home ". In this way, the atime attribute will not be modified when the system reads files in/home.

3.3 adjust the buffer refresh Parameters

The Linux kernel contains configurable parameters for the system running state. You can adjust the buffer refresh parameters by adjusting the/proc/sys/Vm/bdflush file. The format of this file is as follows:

            # cat /proc/sys/vm/bdflush       30      64      64      256     500     3000    60      0       0        

Each column is a parameter, the most important of which is the previous parameters. The first number is to force the bdflush process to refresh the hard disk when the "dirty" buffer reaches, and the second number is the number of dirty blocks each time the bdflush process refreshes. The dirty block must be written to the cache block on the disk. The following parameter lists the number of memory blocks allowed to be flushed into idle buffer blocks each time. The above values are the default values in RedHat Linux 7.1. How to modify them? There are two methods for different systems:

(1) # echo "100 128 128 512 5000 3000 60 0 0">/proc/sys/Vm/bdflush and add this command to/etc/rc. d/RC. in the local file.
(2) Add the following lines to the/etc/sysctl. conf file: VM. bdflush = 100 128 128 512 5000 3000 60 0 0

The above settings increase the buffer size, reduce the frequency of bdflush being started, and increase the risk of data loss in case of system crash. Caching and refresh of VFS is one of the important reasons for Linux File System Efficiency. If performance is really important to you, you should consider adjusting this parameter.

3.4 adjust the number of file handles and I-nodes

In a large website server, the maximum number of files that can be opened by default in Linux cannot meet the system requirements, we can adjust the number of file handles and the number of I-nodes to increase the system's default limit. Different Linux kernel versions have different adjustment methods.

Run the following command in Linux kernel 2.2.x:

  # echo '8192' > /proc/sys/fs/file-max  # echo '32768' > /proc/sys/fs/inode-max

Add the preceding commands to the/etc/rc. c/rc. Local file so that the above values are configured each time the system restarts.

In Linux kernel 2.4.x, You need to modify the source code and then re-compile the kernel. Edit the include/Linux/fs. h file in the Linux kernel source code, change nr_file from 8192 to 65536, and change nr_reserved_files from 10 to 128. Edit the FS/inode. c file and change max_inode from 16384 to 262144.

Generally, the maximum number of opened files is reasonably set to 256 per 4 m physical memory. For example, the 16384 M memory can be set, the maximum number of I nodes used should be three to four times the maximum number of opened files.

3.5 use the memory File System

In Linux, part of the memory can be used as a partition, which is called ramdisk. For frequently accessed files that are not changed, you can place them in the memory through ramdisk to significantly improve the system performance. Of course, your memory is large enough. There are two types of ramdisk, one of which can be formatted and loaded. It is supported in Linux kernel 2.0/2.2. Its disadvantage is that the size is fixed. The other is supported by kernel 2.4, which can be implemented through ramfs or tmpfs. They cannot be formatted, but used flexibly, and their size increases or decreases as needed. Here we will mainly introduce ramfs and tmpfs.

Ramfs, as its name implies, is a memory file system that works on the VFS layer. It cannot be formatted. You can create multiple instances. You can specify the maximum memory size that can be used during creation. If your Linux system has compiled ramfs into the kernel, you can easily use ramfs. Create a directory and load ramfs to the directory.

# mkdir -p /RAM1# mount -t ramfs none /RAM1

By default, ramfs is limited to up to half of the memory size. You can use the maxsize (in Kbyte) option to change the value.

      # mkdir -p /RAM1      # mount -t ramfs none /RAM1 -o maxsize=10000      

A ramdisk with a maximum memory usage of 10 MB is created.

Tmpfs is a virtual memory file system, which is different from the traditional ramdisk implemented in the form of Block devices, but also from the ramfs for physical memory. Tmpfs can use physical memory or swap partitions. In the Linux kernel, virtual memory resources are composed of physical memory (RAM) and swap partitions. These resources are allocated and managed by the virtual memory subsystem in the kernel. Tmpfs is used to "deal" with the virtual memory subsystem. It requests pages from the virtual memory subsystem to store files. It is the same as other request pages in Linux, I don't know whether the page allocated to myself is in memory or in SWAp partition. Like ramfs, tmpfs is not fixed in size, but dynamically increases or decreases with the required space. When using tmpfs, you must first select "virtual memory filesystem support" when compiling the kernel, and then load the tmpfs file system.

    # mkdir -p /mnt/tmpfs# mount tmpfs /mnt/tmpfs -t tmpfs

To prevent tmpfs from using too many memory resources and causing system performance degradation or crashes, you can specify the maximum size limit of the tmpfs file system during loading.

# mount tmpfs /mnt/tmpfs -t tmpfs -o size=32m

The tmpfs file system created above sets its maximum size to 32 MB. Whether using ramfs or tmpfs, you must understand that once the system is restarted, the content in them will be lost. So those things can be stored in the memory file system according to the specific situation of the system.

3.6 use the Log File System

If the Linux system does not shut down normally due to unexpected circumstances, it may cause metadata of some files in the file system (meta-data is file-related information, for example: permission, owner, and creation and access time are damaged. The file system must maintain the metadata of the file to ensure that the file is organized and accessible. If the metadata is in an unreasonable or inconsistent state, the file cannot be accessed or accessed. When the system restarts, fsck scans all file systems listed in the/etc/fstab file to ensure that their metadata is available. If metadata inconsistency is found, fsck scans and detects metadata and corrects errors. If the file system is large, this process takes a long time. To solve this problem, you can use the log file system. The log file system uses independent log files to track disk Content Changes and Write File metadata while writing file content. Each time you modify the metadata of a file, you must first register the corresponding entries with the data structure called "log. In this way, the log file system maintains records of recently changed metadata. When loading the log file system, if an error is found, the metadata of the entire file system is not scanned. Instead, the latest modified metadata is checked based on the log. Therefore, compared with traditional file systems (such as ext2), log file systems greatly speed up scanning and detection.

There are many log file systems available in Linux, such as XFS, JFS, reiserfs, and ext3. The log file system is designed to provide excellent performance and high availability for the server environment. Of course, Linux workstations and home machines can also benefit from high-performance and reliable log file systems. To install the log file system, you generally need to download the corresponding compressed package, patch the kernel, reconfigure and re-compile the kernel. For detailed installation procedures, visit the official website of the corresponding file system.

Back to Top

4. Other Tuning

4.1 tuned buffermem

The file buffermen is closely related to the kernel virtual memory subsystem. The file/proc/sys/Vm/buffermem controls how much memory is used in the buffer (expressed as a percentage ). Default Value of kernel 2.4: "2 10 60 ". You can modify it as follows:

         # echo "70 10 60" >/proc/sys/vm/buffermem     

Add it to the script file/etc/rc. d/rc. Local. Or add it to the/etc/sysctl. conf file.

         vm.buffermem = 70 10 60     

The first parameter 70 means at least 70% of memory is allocated as a buffer. The last two parameters retain the default values of the system. The first parameter is set to a high value, depending on the system memory size and memory usage during high system load (free monitoring is available ).

4.2 process restrictions

Linux limits the maximum number of processes for each user. To improve performance, you can set the maximum number of root processes to an unlimited number. Edit the. bashrc file (VI/root/. bashrc) and add the line "ulimit-u unlimited" to remove the superuser process restrictions.

The core and system can also use the ulimit command to view and modify other restrictions on user processes. "Ulimit-a" is used to display various current user process restrictions. Some examples of changing user restrictions are as follows:

Ulimit-N 4096 increases the number of files that each process can open to 4096. The default value is 1024.
Ulimit-M 4096 limits the amount of memory used by each process.

4.3 optimizing GCC Compilation

Put the optimization flag in the/etc/profile file. Use the following optimization flag on a Pentium III processor to get the best application:

      CFLAGS=-O9 -funroll-loops -ffast-math -malign-double -mcpu=pentiumpro       -march=pentiumpro -fomit-frame-pointer -fno-exceptions

Then add the following lines to the back position of/etc/profile:


With the above optimization, programs compiled by GCC or egcs will get the best performance.

4.4 kernel compilation Optimization

Edit the/usr/src/Linux/MAKEFILE file and compile the kernel based on the CPU. The following parameter settings optimize the kernel performance.

① Vi + 18/usr/src/Linux/makefile, change hostcc = GCC to hostcc = egcs.
② Vi + 25/usr/src/Linux/makefile
Convert CC =$ (cross_compile) GCC d1_kernel _-I $ (hpath)
Change to CC =$ (cross_compile) egcs d1_kernel _-I $ (hpath ).
③ Vi + 90/usr/src/Linux/makefile
Set cflags =-wall-wstrict-prototypes-O2-fomit-frame-pointer
Change to cflags =-wall-wstrict-prototypes-O9-funroll-loops-ffast-math-malign-double-mcpu = pentiumpro-March = pentiumpro-fomit-frame-pointer-fno- exceptions
④ Vi + 19/usr/src/Linux/makefile
Set hostcflags =-wall-wstrict-prototypes-O2-fomit-frame-pointer
Change to hostcflags =-wall-wstrict-prototypes-O9-funroll-loops-ffast-math-malign-double-mcpu = pentiumpro-March = pentiumpro-fomit-frame-pointer-fno- exceptions

Recompile the kernel based on the modified MAKEFILE file above to achieve better performance.

Back to Top

5. Conclusion

Linux is a flexible and open system. You can tune the application environment from the peripheral system to the kernel of the system. Peripheral system tuning includes system hardware configuration, system installation, and System Service optimization. System Kernel Tuning includes parameter modification and source code improvement. In the tuning of the Linux System Used as the DB2 database server, and in view of the characteristics of the DB2 database, according to various tuning aspects in this article, we also include network tuning, the system performance is comprehensively tuned. According to the comprehensive test of the tuned system, the system performance has been greatly improved.


  • Gerhard mourani. Securing and optimizing for Linux [m], openna Inc, May 2001

  • Daniel Robbins, advanced filesystem implementor's Guide [J/Ol],
  • Fergus Donohue. system tuning info for Linux servers [R],
  • Moshe bar, Linux technology insider [m], Tsinghua University Press, 2001

About the author

Deng yanjun, male, Yongzhou, Hunan province, master of Software Engineering Research Institute, Xi'an University of electronic science and technology, focusing on multicast protocol and system tuning. You can contact him by email

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.