Different methods should be adopted for system optimization based on different systems. This article aims to describe some general methods to optimize Linux.
I. Compilation options
The "etc/profile" file contains a large amount of environment and Startup Program configuration information. You can apply for global environment variables for configuration in this file. Therefore, setting the optimization flag in this file is a wise choice. For the x86 program to achieve the best performance, you can use the best optimization option-O9 during compilation. Many program "Makefile" files already contain the-O2 option, while-O9 enables the compiler to adopt the highest level of optimization. Although it will increase the size of the final program, it can achieve a higher running speed. During compilation, the "-fomit-frame-pointer" option is used. When the program is running, the stack is used to access the variable. However, with this option, the Production Code cannot be debugged. With the "-mcpu = cpu_type" and "-march = cpu_type" options, Gcc will perform special optimization for this type of CPU, however, the generated code can only run on the specified CPU or higher series CPU. When we use-O9 to compile programs, the number of file bytes may be the largest, but the speed is usually the fastest. Depending on the machine architecture, you can add the following statement to/etc/profile.
For CPU i686 or PentiumPro, Pentium II, Pentium III, etc:
CFLAGS =-O9-funroll-loops-ffast-math-malign-double-mcpu = pentiumpro-march = pentiumpro-fomit-frame-pointer-fno-exceptions
For CPU i586 or Pentium:
CFLAGS =-O3-march = pentium-mcpu = pentium-ffast-math-funroll-loops-fomit-frame-pointer-fforce-mem-fforce-addr-malign-double-fno -exceptions
For CPU i486:
FLAGS =-O3-funroll-all-loops-malign-double-mcpu = iworkflow-march = iworkflow-fomit-frame-pointer-fno-limits tions
After completing the preceding settings, add "cflags lang lesscharset" to the "export" row in the "etc/profile" file:
Export PATH PS1 HOSTNAME HISTSIZE HISTFILESIZE USER LOGNAME MAIL INPUTRC CFLAGS LANG LESSCHARSET
Then, log on again. At this time, the environment variable CFLAGS has been assigned a value and can be used by the compiler and other configuration tools. Optimization of Pentium (Pro/II/III) must use the egcs or pgcc compiler. Egcs has been installed in Linux by default, so don't worry.
In addition, when installing programs, the tarball format is always better than the RPM format, because the tarball needs to be re-compiled to optimize your machine architecture for the best performance.
Ii. recompile the kernel
For Linux versions released by Linux publishers, the kernel always includes something that is not very practical for you, such as support for HAM Radio and Token Ring. You can view the information of the following two websites:
Linux Kernel Howto: www.linuxdoc.org/HOWTO/Kernel-HOWTO.html
Linux Kernel: www.kernel.org
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.
3. Stop unused services
Sendmail, httpd, crond, kudzu, mysql, portmap, postgresql, smb, snortd, webmin, inetd (or xinetd) are all great programs, but if you don't use them, why do you want them to take up your valuable resources? Moreover, these programs pose security risks. Generally, for a "clean" Linux system, only gpm, network, syslog, ssh, and xfs are required. If you need it temporarily, start it manually. These startup scripts are generally in the/etc/rc. d/init. d directory.
Check the services used on your machine. You can use the nmap tool for scanning.
[Xyj@freelamp.com] $ nmap-p1-65535 211.94.129.28
Starting nmap V. 2.54BETA30 (www.insecure.org/nmap /)
Interesting ports on freelamp.com (211.94.129.28 ):
(The 65492 ports scanned but not shown below are in state: closed)
--- Edited version of the output due to space considerations --
Port State Service
...
514/tcp open shell
1025/tcp open listen
1099/tcp open unknown
1214/tcp open unknown
...
Check the name of the program running on a specific port. You can use:
[Root@freelamp.com/root] # netstat-anp | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
....
Tcp 0 0 203.199.66.200: 8007 203.199.66.197: 1060 CLOSE 1005/java
Tcp 0 0 203.199.66.200: 22 203.199.66.197: 3768 establihed 1381/sshd
Tcp 0 0 203.199.66.200: 23 192.168.57.46: 1546 ESTABLISHED 1229/in. telnetd: 19
....
The last column shows the running program and its PID.
We can also use the ps command to check the running daemon on the machine:
[Root@freelamp.com root] # ps xc-u 0
PID TTY STAT TIME COMMAND
...
1475? S 0: 02 sendmail
1498? S 0: 04 gpm
16539? S 0: 01 httpd
...
4. Disk Optimization
1. Optimize partitions
Before installing the system, you must plan the hard disk properly. Dividing a certain file system is not only the need of the system itself, but also makes sense at the security level. 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.
/Dev/hda5 swap defaults, pri = 5 0 0
/Dev/hdc5 swap defaults, pri = 5 0 0
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. If there are multiple hard disks, you can enable swap partitions on each hard disk. In addition, we recommend that the size of the swap partition be twice the size of the physical memory.
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
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 can be initialized, started, and stopped.