Linux性能调优之perf使用方法

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. 参考文献

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux下进行性能调优是简化和优化系统的过程,以提高系统的响应能力和效率。以下是一些在perf工具下进行性能调优方法: 1. Prof命令:perf工具的一项主要功能是提供给用户使用perf命令来收集系统性能信息,如CPU使用率、内存使用率等。perf命令可以使用不同的选项来定制性能调优需求,如perf top命令可以收集正在运行的进程的CPU使用情况,perf stat命令可以收集系统的CPU性能统计。 2. 火焰图:火焰图是一种用于可视化性能剖析数据的图形工具。使用perf工具可以生成火焰图,通过查看火焰图可以更直观地了解系统中的性能瓶颈。火焰图可以帮助定位优化的重点,如高CPU占用的函数、频繁调用的函数等。 3. 事件采样:perf工具还可以通过事件采样来获取性能数据。事件采样是指在一段时间内按某种规则采集(抽样)应用程序执行的指令。例如,perf record命令可以记录指定进程在一段时间内的指令执行信息,perf report命令可以生成分析报告。 4. 这是一个完全不一样的方案。powertop是一个基于perf的工具,用于识别有待优化的电源消耗问题。powertop可以检测和监视各种硬件组件和系统功能的能耗,如CPU频率、磁盘使用、网络流量、睡眠模式等。通过优化这些方面,可以减少系统的能耗,延长电池寿命。 总之,perf工具提供了一种在Linux系统下进行性能调优的方式。通过使用perf命令、火焰图、事件采样等功能,可以获取和分析性能数据,进而定位和优化系统中的性能问题,以提高系统的响应能力和效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值