1. 简介

本文档主要记录Linux内核perf工具的使用方法以及遇到的问题。

2. 前期准备

2.1. 内核配置

打开以下配置,编译得到uImage及驱动.ko文件

CONFIG_KALLSYMS=y
CONFIG_KALLSYMS_ALL=y
CONFIG_PERF_EVENTS=y
CONFIG_DEBUG_PERF_USE_VMALLOC=y
CONFIG_KUSER_HELPERS=y
CONFIG_DEBUG_INFO=y

2.2. 交叉编译perf可执行程序

perf代码在kernel的src/tools/perf/目录下,执行步骤如下:

   [Code]$ cd ./src/tools/perf/
   
   [perf]$ make ARCH=arm CROSS_COMPILE=arm-linux-uclibcgnueabihf-
   [perf]$ file perf
   perf:   ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses   shared libs), not stripped

其中make参数ARCH和CROSS_COMPILE在kernel根目录下的src/build/Makefile可以找到。嵌入式设备由于flash空间不足,最好strip剥离符号后合入到程序中。

2.3. 外设驱动及厂商SDK驱动

需要同步2.1小节中的kernel配置后,再编译其他驱动。所有的驱动都需要重新编译,否则设备可能无法正常启动,切记!!!

3. 使用方法

将上面生成的perf可执行文件放到打包环境中,替换重新编译的内核和驱动,打包升级,进入到shell下执行perf –help,可以看到如下内容,表明perf工具正常使用:

~ #   perf
   
    usage: perf [--version] [--help] COMMAND   [ARGS]
   
    The most commonly used perf commands are:
      annotate        Read perf.data (created by perf   record) and display annotated code
      archive         Create archive with object files   with build-ids found in perf.data file
      bench           General framework for benchmark   suites
      buildid-cache   Manage build-id cache.
      buildid-list    List the buildids in a perf.data file
      diff            Read two perf.data files and   display the differential profile
      evlist          List the event names in a perf.data   file
      inject          Filter to augment the events stream   with additional information
      kmem            Tool to trace/measure kernel   memory(slab) properties
      kvm             Tool to trace/measure kvm guest   os
      list            List all symbolic event types
      lock            Analyze lock events
      mem             Profile memory accesses
      record          Run a command and record its   profile into perf.data
      report          Read perf.data (created by perf   record) and display the profile
      sched           Tool to trace/measure scheduler properties   (latencies)
      script          Read perf.data (created by perf   record) and display trace output
      stat            Run a command and gather   performance counter statistics
      test            Runs sanity tests.
      timechart       Tool to visualize total system   behavior during a workload
      top             System profiling tool.
      trace           strace inspired tool
   
    See 'perf help COMMAND' for more information   on a specific command.

3.1. 生成火焰图

3.1.1. 下载FlameGraph工具

git clone https://github.com/brendangregg/FlameGraph.git

3.1.2. 具体步骤

第一步:生成 perf.data 文件(加上-p选项可以指定进程或者线程)
perf record -F 99 -a –g [–p sonia_pid]
第二步:用 perf script 工具对 perf.data 进行解析
perf script -i perf.data &> perf.unfold
第三步:将 perf.unfold 中的符号进行折叠
./stackcollapse-perf.pl perf.unfold & > perf.folded
第四步:生成 svg 图片
./flamegraph.pl perf.folded > perf.svg

3.1.3. 火焰图解读

详见:https://blog.csdn.net/qq_25072517/article/details/78433756

4. 可能遇到的问题

4.1. 问题1:Makefile.config:304: *** No gnu/libc-version.h found, please install glibc-dev[el]

答:在 ifeq ($(feature-libelf), 0)前面加上LIBC_SUPPORT := 1

 ifeq   ($(call try-cc,$(SOURCE_GLIBC),$(FLAGS_GLIBC),glibc),y)
          LIBC_SUPPORT   := 1
 endif
 ifeq   ($(BIONIC),1)
         LIBC_SUPPORT   := 1
 endif
 LIBC_SUPPORT :=1
 ifeq   ($(LIBC_SUPPORT),1)
         msg   := $(warning No libelf found, disables 'probe' tool, please install   elfutils-libelf-devel/libelf-dev);
         NO_LIBELF   := 1
         NO_DWARF   := 1
         NO_DEMANGLE   := 1
 else
         msg   := $(error No gnu/libc-version.h found, please install   glibc-dev[el]/glibc-static);
 endif

4.2. 问题2:…/lib/lk/liblk.a: member …/lib/lk/liblk.a(debugfs.o) in archive is not an object

答:liblk.a默认使用的是x86编译的,手动使用arm交叉编译即可。

[lk]$ readelf -h liblk.a 
   
   File: liblk.a(debugfs.o)
   ELF Header:
     Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00   00 00 
     Class:                             ELF64
     Data:                              2's complement,   little endian
     Version:                           1 (current)
     OS/ABI:                            UNIX - System V
     ABI Version:                       0
     Type:                              REL   (Relocatable file)
     Machine:                           Advanced Micro Devices   X86-64
     Version:                           0x1
     Entry point address:               0x0
     Start of program   headers:          0 (bytes into file)
     Start of section   headers:          49528 (bytes into   file)
     Flags:                             0x0
     Size of this header:               64 (bytes)
     Size of program   headers:           0 (bytes)
     Number of program   headers:         0
     Size of section   headers:           64 (bytes)
     Number of section   headers:         31
     Section header string   table index: 28

手动指定交叉编译链make CC=arm-linux-uclibcgnueabihf-gcc:

[lk]$ make CC=arm-linux-uclibcgnueabihf-gcc
       CC debugfs.o
       AR liblk.a
   [lk]$ readelf -h liblk.a 
   
   File:   liblk.a(debugfs.o)
   ELF Header:
     Magic:     7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
     Class:                             ELF32
     Data:                              2's complement,   little endian
     Version:                           1 (current)
     OS/ABI:                            UNIX - System V
     ABI Version:                       0
     Type:                              REL   (Relocatable file)
     Machine:                           ARM
     Version:                           0x1
     Entry point address:               0x0
     Start of program headers:          0 (bytes into file)
     Start of section headers:          82180 (bytes into file)
     Flags:                             0x5000000,   Version5 EABI
     Size of this header:               52 (bytes)
     Size of program headers:           0 (bytes)
     Number of program headers:         0
     Size of section headers:           40 (bytes)
     Number of section headers:         241
     Section header string table index: 238

4.3. 问题3:替换厂商驱动无法正常启动(segment fault)

注意检查SDK是否都完整替换。

5. 参考文献

Logo

更多推荐