Linux核心編譯以及perfctr模組安裝
零、需求介紹
需要使用PAPI得到硬體級的資料,即與微指令相關的資料,例如L1/L2 cache miss、TLB miss、Cycle等,而PAPI需要perfctr(Performance-monitoring counters support)模組的支援,而Linux核心當中預設沒有perfctr模組,並且perfctr只在某些核心上被支援,它實際上是核心的一個補丁,需要重新編譯核心,打上補丁才能在核心上運行perfctr模組。更多資訊參考papi和perfctr的INSTALL檔案。
一、環境介紹
Intel(R) Xeon(R) CPU ,X86_64平台;
物理機<建議不要在虛擬機器上試,否則可能出現問題,後面介紹>
CentOS release 5.4 (Final)
核心版本是2.6.18-164.el5-x86_64
查看核心、平台等資訊<br />#uname -a<br />Linux gc04vm13 2.6.18-164.el5<原來核心> #1 SMP Thu Sep 3 03:28:30 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux<br />查看作業系統版本<br />#cat /etc/issue<br />CentOS release 5.4 (Final)<br />查看CPU資訊<br />cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c<br /> 1 Intel(R) Xeon(R) CPU E5520 @ 2.27GHz <只給虛擬機器分配了一個CPU>
二、準備工作
(i)下載papi,可以在http://icl.cs.utk.edu/papi/custom/index.html?lid=51&slid=69上下載各個版本的papi,這裡下載papi-3.6.0。
papi中包含了perfctr,使用者不需要單獨下載perfctr,(當然也可以單獨下載,網址是http://user.it.uu.se/~mikpe/linux/perfctr),這裡選擇2.6版本的perfctr。
解壓papi,閱讀papi-3.6.0/src/perfctr-2.6.x目錄下的INSTALL檔案,裡面講到“該版本的perfctr適用於如下版本的Linux核心:在papi-3.6.0/src/perfctr-2.6.x/patches目錄下,若存在patch-kernel-*補丁,說明適用於*版本的Linux核心,我們選擇2.6.19版本的linux核心。
注意: PERFCTR 的版本並不是和linux核心版本相對應的,比如ompi內建的perfctr-2.6.x可適用於很多版本的linux核心,例如2.4.26版本的核心。
(ii)下載linux-2.6.19.tar.bz2,在網站 http://www.kernel.org/pub/linux/kernel/ 上可以下載任何版本的linux核心。
一般情況下,當前核心版本是x.y.***版本的話,編譯的新核心版本最好也是x.y.***版本,例如不要從2.4.x升級到2.6.x,因為兩者差異比較大,一般[主].[次]版本號碼相同的核心可以順利升級。
(iii) 認真看完perfctr的INSTALL檔案,裡面詳細說明了安裝步驟和測試。
三、安裝步驟
(1)解壓核心
bzip2 -d linux-2.6.19.tar.bz2
tar xvf linux-2.6.19.tar
產生linux-2.6.19目錄
(2)複製核心到/usr/src目錄下
cp -r linux-2.6.19 /usr/src/
(3) 解壓papi到目錄/opt下,
tar zxvf papi-3.6.0.tar.gz
cp papi-3.6.0 /opt/
(4)
cd /usr/src/linux-2.6.19
make mrproper
(5)在核心目錄下執行update-kernel
cd /usr/src/linux-2.6.19
/opt/papi-3.6.0/src/perfctr-2.6.x/update-kernel
<必須在核心目錄下執行,否則在後續選擇核心模組時找不到perctr>
若核心版本有問題則出現如下資訊
Error: no suitable patch found for kernel 2.6.19zkl-perfctr
See perfctr-2.6.x/patches/ for supported kernel versions,
可以查看perfctr-2.6.x/patches/下支援的核心版本。
(6)該步驟可以跳過,也可以這樣做
拷貝原有核心的.config到當前核心目錄下
cd /usr/src/linux-2.6.19
cp /usr/src/kernels/2.6.18-164.el5-x86_64/.config .
(7)配置選擇核心模組
cd /usr/src/linux-2.6.19
make menuconfig
選擇模組:
查看perfctr-2.6.x目錄下的INSTALL檔案,它有如下這些內容,其意思是說必須選擇這樣幾個模組:
(i)Loadable module support 選項下,一般預設即可,這個模組的功能是協助系統可以動態載入模組。
(ii)Processor type and features選項下的Performance-monitoring counters support(即PERFCTR),
這個必須選擇,筆者做實驗覺得應該將PERFCTR選擇為(M),其子項均選擇為*,將它編譯成可動態載入的模組,請按如下方式選擇。
注意:
我在做實驗時,最開始將PERFCTR選擇為*,將其編譯進核心,最後安裝完畢,始終不能執行perfectr的執行個體,查看系統目錄/lib/modules/'uname -r'/drivers下也沒有perfectr模組,使用lsmod不能查看到perfctr模組,既然編譯進了核心,應該能看到的,不知啥原因?更不能使用命令modprobe perfctr載入模組(因為不是以動態載入模組的形式載入的),可能與沒有將PERFCTR模組設定為M有關。
核心模組選擇操作方式:
選擇模組是按Y會出現*,按M會出現M,按N可以取消選擇。也可以按空格鍵從*切換到無,為大寫M(鍵下大寫M)表示編譯成可動態載入的模組,*表示編譯進入核心。
(iii)核心支援APIC
此外必須保證核心支援APIC(Advanced Programmable Interrupt Controller進階可程式化插斷控制器),這是因為後面papi要使用,所以請保證選中了APIC相關項目,可以按"/",然後鍵入APIC,搜尋以下與之相關的模組是否選擇了,這個模組實際上在在Processor type and features選項下,模組選擇完畢,可以使用命令grep PIC /usr/src/linux-2.6.19/.config查看apic是否選擇,選擇了的話應該顯示CONFIG_X86_GOOD_APIC=y等字樣,papi的INSTALL檔案中說到“SMP(多核,對稱式多處理器)上預設選擇了該項,但是UP(單一處理器)上預設沒選擇”。
----------------------------------------------------------------------------------------
以下摘自perfctr的INSTALL檔案
You should either configure with modules (CONFIG_MODULES=y) AND module
versions (CONFIG_MODVERSIONS=y), or completely without modules
(CONFIG_MODULES=n). Modules without module versions is an inherently
unsafe configuration which is not recommended.
Loadable module support選項
You should also enable at least CONFIG_PERFCTR, CONFIG_PERFCTR_VIRTUAL,
and CONFIG_PERFCTR_GLOBAL.
Processor type and features選項下的Performance-monitoring counters support(即PERFCTR)選項下的所有選項都選中
You may also select CONFIG_PERFCTR=m to build the bulk of the driver (或者設為M,編譯成動態載入的模組)
as a loadable kernel module; the module will be named `perfctr'.
Note: The perfctr kernel patch adds one word to the `thread_struct'
type, which makes the patched kernel binary incompatible with an
unpatched one. This is why it is important to distinguish the patched
kernel via EXTRAVERSION and CONFIG_MODVERSIONS.
----------------------------------------------------------------------------------------
(7)修改/usr/src/linux-2.6.19/Makefile,加上擴充版本號碼,對該核心給一個標記,
EXTRAVERSION =-zkl-perfctr
這樣做之後,您編譯核心完成後,你的核心版本就是linux-2.6.19-zkl-perfctr
(8)編譯核心
第一種做法=================================================================================
# make clean
# make vmlinux modules <使用此命令編譯核心和模組> 動作完成後在/usr/src/linux-2.6.19/下產生了模組相關檔案,例如在drivers中有很多相關檔案
# make modules_install 安裝模組,此命令會利用上面在/usr/src/linux-2.6.19/下產生的模組相關檔案,對它們進行編譯,在系統目錄/lib/modules/2.6.19-zkl-perfctr下產生模組,例如可以在drivers中看到perfctr模組
# make bzImage 編譯產生核心檔案,/usr/src/linux-2.6.19/arch/x86_64/boot/目錄下產生了核心檔案bzImage
# make install 執行此命令必須保證之前產生了核心檔案bzImage
sh /usr/src/linux-2.6.19/arch/x86_64/boot/install.sh 2.6.19-zkl-perfctr arch/x86_64/boot/bzImage System.map "/boot"
此命令等同於第二種方法中的(e),只不過使用make install命令會自動完成e中的幾種動作,實現核心多重啟動
=====================================================================================
或者
第二種做法
<建議使用第一種>
===============================================================================
(a) make clean 將之前產生的.o檔案刪除掉,這樣不會產生新舊版本的誤差
(b) make bzImage
命令執行成功將提示如下資訊,在/usr/src/linux-2.6.19/arch/x86_64/boot/目錄下產生了核心檔案bzImage
Root device is (8, 3)
Boot sector 512 bytes.
Setup is 7252 bytes.
System is 1794 kB
Kernel: arch/x86_64/boot/bzImage is ready (#1)
(c) make modules 製作出模組相關的檔案
(d) make modules_install 安裝模組
(e)實現核心多重啟動
cp /usr/src/linux-2.6.19/arch/x86_64/boot/bzImage / (/表示一條命令太長了,換行)
> /boot/vmlinux-2.6.19-zkl-perfctr
cp /usr/src/linux-2.6.19/System.map /
> /boot/System.map-2.6.19-zkl-perfctr
手動產生initrd-2.6.19-zkl-perfctr.img檔案到/boot目錄下:
cd /usr/src/linux-2.6.19
[root@gc04vm13 linux-2.6.19]# mkinitrd /boot/initrd-2.6.19zkl-perfctr.img 2.6.19-zkl-perfctr (注意核心是2.6.19-zkl-perfctr,這是因為前面修改了Makefile中的版本擴充號,該命令將在/boot目錄下產生initrd-2.6.19zkl-perfctr.img檔案)
編輯系統核心開機檔案
vim /boot/grub/menu.lst
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/VolGroup00/LogVol00
# initrd /initrd-version.img
#boot=/dev/sda
default=0 #對應到第一個title
timeout=10 #系統核心選擇時的延遲
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-164.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-164.el5 ro root=/dev/VolGroup00/LogVol00
initrd /initrd-2.6.18-164.el5.img
##加上以下啟動新核心的語句
title CentOS (2.6.19-zkl-perfctr)
root (hd0,0)
kernel /vmlinuz-2.6.19-zkl-perfctr ro root=/dev/VolGroup00/LogVol00 # 這是表示核心檔案是/boot/vmlinux-2.6.19-zkl-perfctr
initrd /initrd-2.6.19-zkl-perfctr.img # 這是前面產生的檔案/boot/initrd-2.6.19zkl-perfctr.img
=================================================================================
(10)重新啟動系統,
在啟動菜單中將會出現兩個核心選項,使用上下按鈕選擇新核心啟動
或者
將新核心設定為預設啟動核心,即修改上面檔案中default=1
(11)系統重啟後,可以使用命令查看perfctr模組是否安裝上,
# modprobe perfctr 載入模組
lsmod 顯示模組
rmmod perfctr移除模組
若perfctr模組安裝成功後,你將看到perfctr模組,並且你可以在 /lib/modules/2.6.19-zkl-perfctr/kernel/drivers/perfctr中看到perfctr模組perfctr.ko
(12)編譯安裝perfctr,
#cd /opt/papi-3.6.0/src/perfctr-2.6.x
#make
#mknod /dev/perfctr c 10 108 <可能提示已存在檔案,刪除再建即可>
#chmod 644 /dev/perfctr
#make PREFIX=/usr/local install
至此,安裝全部完成,perfctr相關檔案安裝再/usr/local下的bin、include、lib下
(13)測試
測試perfctr-2.6.x下面的例子,出現結果,證明安裝perfctr安裝成功
[root@gc04vm13 perfctr-2.6.x]# examples/global/global
PerfCtr Info:
abi_version 0x05010501
driver_version 2.6.32 DEBUG
cpu_type 5 (?)
cpu_features 0x7 (rdpmc,rdtsc,pcint)
cpu_khz 2259879
tsc_to_cpu_mult 1
cpu_nrctrs 0
cpus [0], total: 1
cpus_forbidden [], total: 0
cpu type 5 (?) not supported
不支援,這是因為以上步驟我是在VMware虛擬機器中完成的,換成物理機就沒問題,正常情況下是運行後每隔5秒刷現一次
涉及的命令:</p><p> cp -r papi-3.6.0 /opt/<br /> cp linux-2.6.20.tar.gz /usr/src/kernels<br /> cd /usr/src/kernels<br /> tar zxvf linux-2.6.20.tar.gz<br /> cd linux-2.6.20<br /> #make clean <當前核心未編譯過時不需要執行該命令><br /> #make mrproper <當前核心未編譯時間不需要執行該命令><br /> /opt/papi-3.6.0/src/perfctr-2.6.x/update-kernel<br /> cp 當前核心目錄/.config . <複製當前核心下的.config檔案到目前的目錄><br /> make menuconfig<br /> 做如下選擇:<br /> (1)Processor type and features選項下的Performance-monitoring counters support,即要支援硬體計數器<br /> (2)搜尋 APIC 選項,查看是否已經選上y,沒有則選上<br /> (也可以在退出 menuconfig 後使用 grep APIC .config 查詢APIC相關選項是否是y)<br /> 退出 menuconfig,儲存。</p><p> make modules<br /> make modules_install<br /> make bzImage<br /> make install</p><p> 完畢後選擇新核心重新啟動<br /> 新核心啟動後<br /> modprobe perfctr<br /> lsmod | grep perfctr<br /> cd /opt/papi-3.6.0/src/perfctr-2.6.x/<br /> make<br /> examples/perfex -i<br /> examples/global/global 檢測perfctr是否能正常運行,若出現錯誤,重啟系統<br /> mknod /dev/perfctr c 10 108<br /> chmod 644 /dev/perfctr<br /> make PREFIX=/usr/local install 將perfctr的標頭檔、庫、可執行命令安裝到/usr/local下<br />