1. 程式人生 > >對比直接編譯進核心和模組形式

對比直接編譯進核心和模組形式

Compiling directly into a kernel vs. modules

You have two options for adding functionality to the kernel: building functions into the kernel (making a monolithic kernel) or adding them as modules. 



Monolithic kernels: 
Building a function into the kernel directly ensures that that function will be available at all times. The downside is that it makes your kernel bigger, increasing boot time, and ultimately using that much more memory to hold the kernel. If you are compiling a kernel to fit on a floppy so that you can boot Linux off a rescue floppy, space will become an issue. 


Modules: 
Building a function as a module allows that function to be loaded into memory as needed and unloaded when it is no longer needed. This helps keep your kernel small. It is very useful if, say, you are swapping hardware in and out of your system frequently. You could compile support for a variety of, say, sound cards as modules, and your Linux system will theoretically only load the driver that is appropriate for the hardware setup at the time. 


Another benefit of building functions as modules is that parameters can be passed to the modules which can be useful in debugging your system if problems occur. 


There are some considerations to be made when deciding if a kernel function should be modularized. A small performance penalty is paid as it takes a little time to get the module loaded and unloaded. There are some functions that are needed at boot time and these cannot be compiled as modules -- they need to be present in the kernel so your system can be loaded. For example, ext2/ext3/reiserfs file system support needs to be built into the kernel so that your partitions can be read, as you need to be able to read the filesystem to load modules. In my case, if I have PCMCIA support built into the kernel, then metworking works. If PCMCIA support is modularized, networking fails to start, probably because the PCMCIA support needs to be available very early in the boot process to set up networking. 


General approach 

The way that I approached kernel building was to see which functions I would need all the time versus the ones that I would only use infrequently. If it was a function that I would be using a lot, I built it directly into the kernel. Otherwise, I compiled it as a module. As I got more comfortable with recompiling the kernel, I started making more functions as modules, and honestly, have not seen that much change in system performance. On the other hand, my hardware setup is pretty static -- my machine is a laptop, and the only hardware option that I have is whether or not I have a USB mouse plugged into it. 

你有兩個方法將功能載入到核心:直接將功能內建在核心使他變成核心的一部分或者以模組形式加入。


內建形式:


將功能內建在核心裡可以確保此功能永遠都開啟。但缺點就是增加你核心的“體積”從而拖慢了你開啟引導的時間, 並且這個“變大”的核心需要更多的記憶體。如果你是打算將核心編譯到軟盤, 從急救軟盤開啟引導的話, 空間將成為一個不容忽視的問題。


模組形式:


將功能編成模組可以讓此功能在需要的時候載入在記憶體裡而不需要的時候可以解除安裝,這樣, 你的核心可以保持一個比較小的“體積”。 如果你是經常將硬體“交換”(swap in and out)進出的話, 這個方式是很有用的。你可以將很多支援的硬體編成模組形式比如你的音效卡, 這樣理論上系統會將此驅動正常的在硬體安裝的時候掛載上去。


另一個編成模組形式的優點就是當你要在系統出錯的時候, 模組形式可以允許你加入你自己的引數, 從而使得出錯工作簡單化。


當你要編成模組形式的時候幾個問題要考慮進去。 首先是考慮到載入和解除安裝模組的時間, 當然這個問題並不是很嚴重。有一些功能是需要在開機引導的時候就要的而他們是不可以編成模組形式。當系統被加入在記憶體後, 這些功能就必須要載入了。比如支援ext2/ext3/reiserfs的功能是需要內建在核心裡的這樣你的分割槽才能被系統讀到。另外我還有一個例子, 我有一個PCMCIA的網絡卡, 如果我將PCMCIA編入到核心使他可以在開機引導的時候就正確掛載的話, 我的網路可以啟動運作, 但相反如果我用模組形式的話, 網路無法正確啟動, 這是因為PCMCIA是需要在剛啟動系統的時候就要掛載上去的原因。


一般編譯方案:
我判斷是否要將功能編成內建形式是判斷此功能是否是我一直都需要並且是否我經常在用。如果這個功能我平時用的很多的話, 我會直接講它編到核心裡, 不然就變成模組。再我不斷的重新編譯核心的時候, 我開始將許多功能轉成模組, 老實說, 我沒有覺得這樣會讓系統效率下降。另一方面, 我的硬體功能也不多, 就是一個膝上型電腦, 唯一的硬體選擇專案就是我要不要將USB的滑鼠插到系統上。