2015年3月2日 星期一

[Embedded] 在 ARM in Linux 編譯 perf

要改善系統效能,首先必須有個量測系統效能的工具,本篇紀錄在 ARM in Linux 如何手動編譯 perf 。

摘錄網路上對 perf 的說明如下:

perf (sometimes called "Perf Events"or perf tools, originally "Performance Counters for Linux", PCL) is a performance analyzing tool in Linux, available from Linux kernel version 2.6.31.

it can instrument CPU performance counters, tracepoints, kprobes, and uprobes (dynamic tracing). It is capable of lightweight profiling. It is also included in the Linux kernel, under tools/perf, and is frequently updated and enhanced.

perf 的使用範例可以參考 IBM提供的文章



我個人所使用的作業平台是 linux2.6.38,此核心已經支援 perf 工具。系統的CPU為ARM 1136J-S,屬於 ARMv6 架構。由於手邊並沒有 ARMv6 的 perf 執行檔,因此需要自行編譯 perf ,以下對此過程做一記錄。

1. linux 2.6.38 kernel 支援 perf,如何確認現在正使用的 kernel 是否支援此功能呢?
首先需要檢查 kernel 下的 .config,確認下列兩個設定是否為 yes。
CONFIG_PERF_EVENTS=y
CONFIG_HW_PERF_EVENTS=y 
若不支援,則須重新編譯  kernel,設定方式如下
kernel/linux-2.6.38$ make menuconfig
Symbol: HW_PERF_EVENTS [=n]
Location:
-> Kernel Features
Symbol: PERF_EVENTS [=n]
Location:
-> General setup
-> Kernel Performance Events And Counters
2. compile zlib
$ wget http://downloads.sourceforge.net/project/libpng/zlib/1.2.8/zlib-1.2.8.tar.gz
$ tar -zxvf zlib-1.2.8.tar.gz
$ cd  zlib-1.2.8
$ CC=arm-none-linux-gnueabi-gcc ./configure \
    --prefix==/usr/local/arm-2009q3/arm-none-linux-gnueabi/libc/usr/

$ sudo chmod 777 /usr/local/arm-2009q3/arm-none-linux-gnueabi/libc/usr/lib
$ sudo chmod 777 -R /usr/local/arm-2009q3/arm-none-linux-gnueabi/libc/usr/share
$ sudo chmod 777 /usr/local/arm-2009q3/arm-none-linux-gnueabi/libc/usr/include
$ sudo chmod 777 /usr/local/arm-2009q3/arm-none-linux-gnueabi/libc/usr/bin
$ make; make install

3. compile elfutils
$ wget https://fedorahosted.org/releases/e/l/elfutils/0.161/elfutils-0.161.tar.bz2 --no-check-certificate
$ tar -jxvf elfutils-0.161.tar.bz2
$ cd elfutils-0.161
$ ./configure --host=arm-none-linux-gnueabi --prefix=/usr/local/arm-2013.05/arm-none-linux-gnueabi/libc/usr/
$ make
$ make install

4. compile perf
The source code for perf is included in the Linux kernel source tree under tools/perf. For example: /linux-2.6.38/tools/perf 
linux-2.6.38/tools/perf$ make LDFLAGS=-static ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-