---
title: "Linux 2.6.31内核优化-1"
author: "loamy"
date: "2010-03-24T14:42:38Z"
tags: []
categories: ["操作系统"]
slug: "linux-2-6-31-1"
draft: false
lastmod: "2010-03-24T14:46:15Z"
id: 623645
url: "https://loamy.iteye.com/blog/623645"
---

**介绍**

本文档是一篇关于Linux Kernel 2.6.31的最简优化指南。作者旨在编译一份性能最佳且适合普通Application/Server开发用的内核。  
本文提及的内核调优，主要是通过选择最佳编译选项，删除无用驱动，减少内核尺寸等方式来进行的。

由于作者水平有限，如果描述错误，还请不吝指教。

 

**运行环境**

虚拟机

Virtualbox 3.1.4

CPU

Core 2 Duo P9500 2.53G 双核 开启 vt-x, APIC

磁盘

虚拟 10G的SATA磁盘

内存

分了1G物理内存

网卡

虚拟 AMD PCNet32 100M

其他

USB，串口，显卡，声卡全关

作者是在虚拟机中编译的内核，如果你与作者的运行环境不同，则本文档中的部分描述可能会与你的实际情况有一定出入。

不过，相信大部分用户在看到选项释义时，能做出适合自己的选择。

 

**说明**

下列选项中，通过 "// " 注释的行

通过红色 "&lt;===" 注释的行，代表内核默认开启，本文档推荐关闭的选项，释义部分会给出关闭的理由。

 

[](http://www.linuxsir.org/bbs/)

 

**General setup  ---&gt;**

\[ ] Prompt for development and/or incomplete code/drivers &lt;=== 显示尚在开发中或尚未完成的代码和驱动. 我不是内核或驱动开发人员。

()  Local version - append to kernel release

\[ ] Automatically append version information to the version string

Kernel compression mode (Bzip2)  ---&gt; &lt;=== 对比默认的Gzip，Bzip2拥有更高的压缩比

\[\*] Support for paging of anonymous memory (swap)

\[\*] System V IPC

\[\*] BSD Process Accounting

\[ ]  BSD Process Accounting version 3 file format

\[ ] Export task/process statistics through netlink (EXPERIMENTAL) &lt;=== 体验类选项一律关

\[ ] Auditing support &lt;=== 给SELinux提供支持。我的使用场景主要是开发应用程序，不需要那么高的安全性，另外安全总是以牺牲性能为代价

RCU Subsystem  ---&gt; // [非对称读写锁系统](http://www.ibm.com/developerworks/cn/linux/l-rcu/) 是一种新的kernel锁机制，适用于读多写少环境

RCU Implementation (Classic RCU)  ---&gt; &lt;=== 选择标准的RCU系统， tree-base是给拥有大规模CPU集群的SMP系统用的

&lt; &gt; Kernel .config support

(18) Kernel log buffer size (16 =&gt; 64KB, 17 =&gt; 128KB)

\[] Control Group support  ---&gt;&lt;=== cgroups支持，[文档资料](http://www.mjmwired.net/kernel/Documentation/cgroups.txt)，cgroups主要作用是给进程分组，并可以动态调控进程组的CPU占用率。比如A进程分到apple组，给予20%CPU占用率，E进程分easy组，给予50%CPU占用率，最高100%。我目前没有此类应用场景，用到时会选择将其编译进去。

\[] Kernel-&gt;user space relay support (formerly relayfs) &lt;=== 在某些文件系统上(比如debugfs)提供从内核空间向用户空间传递大量数据的接口，我目前没有此类应用场景

-\*- Namespaces support

\[]   UTS namespace &lt;=== 我没有已安装的应用程序要用到 UTS ns

\[]   IPC namespace &lt;=== 我没有已安装的应用程序要用到 IPC ns

\[] Initial RAM filesystem and RAM disk (initramfs/initrd) support &lt;=== 用于在真正内核装载前，做一些操作（俗称两阶段启动），比如加载module，mount一些非root分区，提供灾难恢复shell环境等，[资料](http://blog.linux.org.tw/~jserv/archives/001954.html)，我是期望直接从kernel image直接启动，所以没选它

\[\*] Optimize for size

\[ ] Configure standard kernel features (for small systems)  ---&gt;

-\*-   Load all symbols for debugging/ksymoops

\[ ]     Do an extra kallsyms pass &lt;=== 除非你在kallsyms中发现了bug并需要报告这个bug才打开该选项

Performance Counters  ---&gt; 

\[\*] Kernel Performance Counters // CPU 性能监听器，包括 CPU同一时间执行指令数，cache miss数，分支预测失败次数([Branch misprediction](http://en.wikipedia.org/wiki/Branch_misprediction))。调优其他程序时或许会用到，比如JVM

\[ ] Strip assembler-generated symbols during link

\[ ] Disable heap randomization

Choose SLAB allocator (SLUB (Unqueued Allocator))  ---&gt;

\[ ] Profiling support (EXPERIMENTAL) &lt;=== 我不是内核开发人员，不需要profile内核

\[] Activate markers &lt;=== 一种运行时状态分析诊断工具，类似 instrumentation，[资料](http://sourceware.org/systemtap/wiki/UsingMarkers)，我用不到

\[ ] Kprobes &lt;=== 我不是内核开发人员，不需要调试内核

GCOV-based kernel profiling  ---&gt;

\[ ] Enable gcov-based kernel profiling

 

**\[\*] Enable loadable module support  ---&gt; // 我很少使用dynamic module loading，所以，下面的选项只留一个unloading即可（默认）**

--- Enable loadable module support

\[ ]   Forced module loading

\[\*]   Module unloading

\[ ]   Module versioning support

\[ ]   Source checksum for all modules

 

**-\*- Enable the block layer  ---&gt;**

--- Enable the block layer

\[\*]   Support for large (2TB+) block devices and files // 大硬盘支持，SATA设备如果不选，会发生 kernel panic，具体原因未知

\[ ]   Block layer SG support v4 &lt;=== 支持通用scsi块设备第4版，我用的是SATA

\[ ]   Block layer data integrity support

IO Schedulers  ---&gt; // IO调度器支持，不同程序可以会选用不同的调度策略，这里我们不要动他

&lt;\*&gt; Anticipatory I/O scheduler

&lt;\*&gt; Deadline I/O scheduler

&lt;\*&gt; CFQ I/O scheduler

    Default I/O scheduler (CFQ)  ---&gt;

 

**Processor type and features  ---&gt;**

\[\*] Tickless System (Dynamic Ticks) // CPU节能，动态时间片意思是当CPU处于空闲时，降低内核定时器的轮询频率

\[\*] High Resolution Timer Support // 我有的时候要测试 Real Time JVM，希望内核能支持这种高响应速度的内核定时器，一般用户可关闭

\[\*] Symmetric multi-processing support

\[ ] Support sparse irq numbering &lt;=== 如果你想支持大量CPU，又想使用较低的内核内存占用，请启用它。我只有单块CPU

\[\*] Enable MPS table // 让多核/多CPU系统支持ACPI

\[ ] Support for big SMP systems with more than 8 CPUs

\[ ] Support for extended (non-PC) x86 platforms &lt;=== 我是X86平台，不需要再支持其他平台

\[\*] Single-depth WCHAN output // 跟 proc 相关的最好不要关

\[ ] Paravirtualized guest support  ---&gt;

--- Paravirtualized guest support

\[ ] Memtest

Processor family (Core 2/newer Xeon)  ---&gt; &lt;=== 这里请选择对应的CPU，我是 core 2 duo

\[ ] Generic x86 support &lt;=== 通用x86支持, 如果你的CPU能够在上述"Processor family"中找到就别选了

\[\*] HPET Timer Support

(4) Maximum number of CPUs &lt;=== 我的CPU是双核，算上超线程HT最多虚拟4颗逻辑CPU，所以我选4 ，默认是8

\[\*] SMT (Hyperthreading) scheduler support

\[\*] Multi-core scheduler support

Preemption Model (Voluntary Kernel Preemption (Server))  ---&gt; &lt;=== 这里我选择了Server，它最贴近我的使用场景

\[\*] Reroute for broken boot IRQs // 防止同时收到多个boot IRQ(中断)时，系统混乱

\[\*] Machine Check Exception

\[ ]   Use legacy machine check code (will go away)

\[\*]   Intel MCE features

\[ ]   AMD MCE features &lt;=== 我是intel CPU

\[ ] Support for old Pentium 5 / WinChip machine checks

&lt; &gt; Machine check injector support

&lt; &gt; Toshiba Laptop support

&lt; &gt; Dell laptop support

\[ ] Enable X86 board specific fixups for reboot &lt;=== 修正某些旧x86主板的重起bug,这种主板基本绝种了

&lt;\*&gt; /dev/cpu/microcode - microcode support

\[\*]   Intel microcode patch loading support

\[ ]   AMD microcode patch loading support &lt;=== 我是intel CPU

&lt;\*&gt; /dev/cpu/\*/msr - Model-specific register support

&lt;\*&gt; /dev/cpu/\*/cpuid - CPU information support

&lt; &gt; /sys/kernel/debug/x86/cpu/* - CPU Debug support

High Memory Support (4GB)  ---&gt;

(4096) Low address space to protect from user allocation

\[\*] Allocate 3rd-level pagetables from highmem &lt;=== 在内存很多(大于4G)的机器上将用户空间的页表放到高位内存区,以节约宝贵的低端内存 ，我只虚拟了1G

\[ ] Check for low memory corruption &lt;=== 低位内存脏数据检查，默认是每60秒检查一次。一般这种脏数据是因某些Bios处理不当引起的。[资料链接](http://cateee.net/lkddb/web-lkddb/X86_CHECK_BIOS_CORRUPTION.html)。我信任我目前的虚拟bios版本

\[] Reserve low 64K of RAM on AMI/Phoenix BIOSen &lt;=== 我不是 AMI/Phoenix BIOS

\[ ] Math emulation

\[ ] MTRR (Memory Type Range Register) support &lt;=== 我没用 X windows

\[ ]   MTRR cleanup support

\[ ]   x86 PAT support &lt;=== 我没用 X windows

\[ ] EFI runtime service support &lt;=== 我机器是BIOS引导的，用不到EFI(Extensible Firmware Interface)，如果你有兴趣体验EFI，可以在Virtualbox中开启

\[] Enable seccomp to safely compute untrusted bytecode &lt;=== 只有嵌入式系统可以不选

\[ ] Enable -fstack-protector buffer overflow detection (EXPERIMENTAL)

Timer frequency (300 HZ)  ---&gt; &lt;=== Server场景建议设为300hz，减少内核事件响应速度，提高程序吞吐量，桌面场景可以保持不变

\[ ] kexec system call &lt;=== 我没有运行时替换内核的需求

\[ ] kernel crash dumps &lt;=== 内核崩溃时，dump运行时信息。就算crash了，我也不会去调试内核的core dump

\[ ] Build a relocatable kernel &lt;=== [官方说明](http://cateee.net/lkddb/web-lkddb/RELOCATABLE.html) （建立一个移动的内核，并增加10%的内核尺寸，运行时会被丢弃），我认为没实质性的作用

(0x1000000) Physical address where the kernel is loaded

\[] Support for hot-pluggable CPUs &lt;=== 热拔插CPU，我用不到

\[ ] Compat VDSO support

\[ ] Built-in kernel command line

 

**Power management and ACPI options  ---&gt;**

\[\*] Power Management support

\[ ]   Power Management Debug Support &lt;=== 不想调试ACPI

\[ ]     Verbose Power Management debugging

\[ ] Suspend to RAM and standby &lt;=== 我是用来跑server，一般不需要待机

\[ ] Hibernation (aka 'suspend to disk')  &lt;=== 我是用来跑server，一般不需要休眠

\[\*] ACPI (Advanced Configuration and Power Interface) Support  ---&gt;

\[\*]   Deprecated /proc/acpi files

\[\*]   Deprecated power /proc/acpi directories

\[\*]   Future power /sys interface

\[\*]   Deprecated /proc/acpi/event support

&lt; &gt;   AC Adapter &lt;=== 我不需要从AC与Battery之间做切换

&lt; &gt;   Battery &lt;=== 没有电池

&lt; &gt;   Button &lt;=== 没有Fn快捷键

-\*-   Video

&lt;\*&gt;   Fan

&lt;\*&gt;   Processor

&lt;\*&gt;     Thermal Zone

(0)   Disable ACPI for systems before Jan 1st this year

\[ ]   Debug Statements

&lt; &gt;   PCI slot detection driver

&lt; &gt;   Smart Battery System

    CPU Frequency scaling  ---&gt;

\[\*] CPU Frequency scaling

\[ ]   Enable CPUfreq debugging &lt;=== 我不需要调试 CPUfreq

&lt; &gt;   CPU frequency translation statistics

Default CPUFreq governor (performance)  ---&gt; &lt;=== 默认用 performance高性能的CPU调频方式

&lt;\*&gt;   'performance' governor

&lt; &gt;   'powersave' governor

&lt;&gt;   'userspace' governor for userspace frequency scaling &lt;=== 既允许手动调整cpu频率,也允许用户空间的程序动态的调整cpu频率(需要额外的调频软件,比如cpufreqd)  我只用performance

&lt;&gt;   'ondemand' cpufreq policy governor &lt;=== "周期性的考察CPU负载并自动的动态调整cpu频率"，我只用 performance

&lt;&gt;   'conservative' cpufreq governor

\*\** CPUFreq processor drivers \*\**

&lt;\*&gt;   ACPI Processor P-States driver

&lt; &gt;   AMD Mobile K6-2/K6-3 PowerNow!

&lt; &gt;   AMD Mobile Athlon/Duron PowerNow!

&lt; &gt;   AMD Opteron/Athlon64 PowerNow!

&lt; &gt;   Cyrix MediaGX/NatSemi Geode Suspend Modulation

&lt; &gt;   Intel Enhanced SpeedStep (deprecated)

&lt; &gt;   Intel Speedstep on ICH-M chipsets (ioport interface)

&lt; &gt;   Intel Pentium 4 clock modulation

&lt; &gt;   Transmeta LongRun

&lt; &gt;   VIA Cyrix III Longhaul

 

**Bus options (PCI etc.)  ---&gt;**

\[\*] PCI support

PCI access mode (Any)  ---&gt;

\[\*] PCI Express support

\[\*]   Root Port Advanced Error Reporting support // 硬件驱动会负责发送错误信息

\[ ]     PCI Express ECRC settings control

&lt; &gt;     PCIE AER error injector support

\[\*] Message Signaled Interrupts (MSI and MSI-X)

\[ ] Enable deprecated pci\_find\_* API

\[ ] PCI Debugging

&lt; &gt; PCI Stub driver

\[\*] Interrupts on hypertransport devices

\[ ] PCI IOV support

\[ ] ISA support

\[ ] MCA support

&lt; &gt; NatSemi SCx200 support

\[ ] One Laptop Per Child support

&lt; &gt; PCCard (PCMCIA/CardBus) support  ---&gt; &lt;=== 一般笔记本电脑会配备PCCard接口(无线网卡之类的)，看你的硬件和使用场景吧。虽然我也是NB，但我从来不用PCMCIA

&lt; &gt; Support for PCI Hotplug  ---&gt; &lt;=== 我没有要热拔插PCI设备的需求

 

**Executable file formats / Emulations  ---&gt;**

\[\*] Kernel support for ELF binaries

\[\*] Write ELF core dumps with partial segments

&lt; &gt; Kernel support for a.out and ECOFF binaries

&lt;\*&gt; Kernel support for MISC binaries

 

**\[\*] Networking support  ---&gt;**

Networking options  ---&gt;

&lt;\*&gt; Packet socket

\[\*]   Packet socket: mmapped IO

&lt;\*&gt; Unix domain sockets

&lt;\*&gt; Transformation user configuration interface

&lt; &gt; PF\_KEY sockets

\[\*] TCP/IP networking

\[\*]   IP: multicasting

\[\*]   IP: advanced router

Choose IP: FIB lookup algorithm (choose FIB\_HASH if unsure) (FIB\_HASH)  ---&gt;

\[\*]   IP: policy routing

\[\*]   IP: equal cost multipath

\[\*]   IP: verbose route monitoring

\[ ]   IP: kernel level autoconfiguration &lt;=== 我很少用网络引导OS，内核级别的网络配置对我无用。

\[ ]     IP: DHCP support

\[ ]     IP: BOOTP support

\[ ]     IP: RARP support

&lt; &gt;   IP: tunneling

&lt; &gt;   IP: GRE tunnels over IP

\[\*]   IP: multicast routing

\[\*]     IP: PIM-SM version 1 support

\[\*]     IP: PIM-SM version 2 support

\[ ]   IP: ARP daemon support

\[ ]   IP: TCP syncookie support (disabled per default) &lt;=== 抵抗SYN flood攻击，我是开发机，暂不考虑安全特性

&lt; &gt;   IP: AH transformation

&lt; &gt;   IP: ESP transformation

 &lt; &gt;   IP: IPComp transformation

&lt; &gt;   IP: IPsec transport mode

&lt; &gt;   IP: IPsec tunnel mode

&lt; &gt;   IP: IPsec BEET mode

\[\*]   Large Receive Offload (ipv4/tcp)

&lt; &gt;   INET: socket monitoring interface

\[ ]   TCP: advanced congestion control  ---&gt; &lt;=== 高级拥塞控制,如果没有特殊需求(比如无线网络)就别选了

&lt; &gt;   The IPv6 protocol  ---&gt; &lt;=== 我暂时没有要支持IPV6的需求

\[ ]   NetLabel subsystem support &lt;=== NetLabel子系统, 为诸如CIPSO与RIPSO之类能够在分组信息上添加标签的协议提供支持，我用不到

\[ ] Security Marking &lt;=== 对网络包进行安全标记,类似于nfmark,但主要是为安全目的而设计, 安全特性，我暂时不考虑

\[ ] Network packet filtering framework (Netfilter)  ---&gt; &lt;=== 我不打算使用防火墙，要用到时再编译进去

&lt; &gt; Asynchronous Transfer Mode (ATM)

&lt; &gt; 802.1d Ethernet Bridging

&lt; &gt; 802.1Q VLAN Support

&lt; &gt; DECnet Support

&lt; &gt; ANSI/IEEE 802.2 LLC type 2 Support

&lt; &gt; The IPX protocol

&lt; &gt; Appletalk protocol support

&lt; &gt; Phonet protocols family

\[ ] QoS and/or fair queueing  ---&gt;  &lt;=== 通过IPRoute切换网络设备上的Qos策略，我不打算使用IP路由

\[ ] Data Center Bridging support

Network testing  ---&gt;

\[ ]   Amateur Radio support  ---&gt; &lt;=== 我没有无线电

&lt; &gt;   CAN bus subsystem support  ---&gt;

&lt; &gt;   IrDA (infrared) subsystem support  ---&gt;

&lt; &gt;   Bluetooth subsystem support  ---&gt;

\[ ]   Wireless  ---&gt; &lt;=== 我没有使用无线网卡

&lt; &gt;   RF switch subsystem support  ---&gt; &lt;=== 我没有RF切换设备