Analyzing CPU Usage with Perf (1) - preparation

content

  • [About Perf](#About Perf)
  • [Building Perf-enabled Firmware](#Building Perf-enabled Firmware)
    • [Building Perf-enabled Firmware](#Building Perf-enabled Firmware)
    • [Enable Perf in Linux Kernel Config](#Enable Perf in Linux Kernel Config)
      • [check the result](#check the result)
      • [Building and Installing Perf](#Building and Installing Perf)
      • [Add Thread Names to Application](#Add Thread Names to Application)
      • [Compiling Application for Stack Tracing](#Compiling Application for Stack Tracing)
    • [Downloading Perf-enabled Firmware](#Downloading Perf-enabled Firmware)
    • [Apply Un-Stripped Version of Target Libraries](#Apply Un-Stripped Version of Target Libraries)

About Perf

Perf is a profiling utility shipped with Linux kernel.

Perf based profiling is split in to 3 parts:

  1. Data sources (tracing data that comes from kernel/application)
  2. Data extractor (collection/recording of profiled data)
  3. Data presentation (front-end tools to generate statistics and graphs)

Building Perf-enabled Firmware

Building Perf-enabled Firmware

  1. Clean built binaries, rootfs and Buildroot: (in $(SDK_DIR)/build/):
bash 复制代码
$ make clean; make rootfs-distclean buildroot-clean
  1. Enable the following configs in menuconfig (in (SDK_DIR)/buildroot/src/ , which is later referred to as (BUILDROOT_DIR)):
bash 复制代码
BR2_OPTIMIZE_G=y
BR2_STRIP_NONE=y
  1. Rebuild buildroot and rootfs

The generated debugging version of rootfs is in $(ROOTFS_DIR)/output/target_debug.

User should confirm that libraries within are not stripped.

Enable Perf in Linux Kernel Config

Edit Linux configuration file.

bash 复制代码
$ cd $SDK_DIR/linux
$ make menuconfig

Turn on perf.

  1. Go to "General setup" and then "Kernel Performance Events And Counters".
  2. Select "Kernel performance events and counters".
  3. steps to enable frame pointer
    3.1 in menuconfig, press / to open search window
    3.2 enter CONFIG_FRAME_POINTER and press enter
    3.3 Check first line is Symbol: FRAME_POINTER [=y]
  4. Save the config and exit.

Note: CONFIG_FRAME_POINTER might not show up in menuconfig.
Force to turn on frame pointer (Eg. Add "select ARCH_WANT_FRAME_POINTERS" to arch/arm/Kconfig) in this case will fail during compilation.

check the result

You can double check the .config file.

vi SDK_DIR/linux/.config

You will see the following options enabled.

bash 复制代码
CONFIG_PERF_EVENTS=y
CONFIG_HW_PERF_EVENTS=y
CONFIG_HAVE_PERF_EVENTS=y
CONFIG_FRAME_POINTER=y

Save the config and exit.

bash 复制代码
$ cd SDK_DIR/linux/
$ make savedefconfig

Update from top level.

The kernel with perf enabled will be built during the process.

bash 复制代码
$ cd $SDK_DIR/build
$ make clean;  make <prod config> ; make 

Building and Installing Perf

If you need to compile from source, just follow these steps.

  1. Unload japan-linu-sdk.lua if loaded
    On build server, default japan-linux-sdk env. setting would use Augentix toolchain in /tool. We are to disable it first:
bash 复制代码
$ module unload japan-linux-sdk
  1. Downlaod zlib
bash 复制代码
wget http://downloads.sourceforge.net/project/libpng/zlib/1.2.8/zlib-1.2.8.tar.gz
  1. Configure and install zlib
bash 复制代码
$ tar -zxvf zlib-1.2.8.tar.gz
$ cd  zlib-1.2.8
$ ./configure --host=arm-xxxx-linux-gnueabi --prefix $SDKSRC_DIR/toolchain/arm-xxxx-linux-gnueabi/arm-xxxx-linux-gnueabi/sysroot/usr/
$ make && make install
  1. Download elfutils
bash 复制代码
$ 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
  1. Configure and compile elfutils and libelf:
bash 复制代码
$ cd elfutils-0.161
$ ./configure --host=arm-xxxx-linux-gnueabi --prefix $SDKSRC_DIR/toolchain/arm-xxxx-linux-gnueabi/arm-xxxx-linux-gnueabi/sysroot/usr/
$ make && make install
  1. Apply env. setting for toolchain in SDK
bash 复制代码
export PATH=$(SDKSRC_DIR)/toolchain/arm-xxxx-linux-gnueabi/bin:$PATH
  1. Compile perf
    (In $(SDKSRC_DIR)/linux/tool/perf)
bash 复制代码
$ make LDFLAGS="-static -L$(SDKSRC_DIR)/toolchain/arm-xxxx-linux-gnueabi/arm-xxxx-linux-gnueabi/sysroot/usr/lib -lelf -IL$SDKSRC_DIR/toolchain/arm-xxxx-linux-gnueabi/arm-xxxx-linux-gnueabi/sysroot/usr/include" ARCH=arm CROSS_COMPILE=arm-xxxx-linux-gnueabi-

Add Thread Names to Application

If you are profiling an application with threads, adding thread name is highly recommended.

The following code illustrates how to set thread name using the pthread_setname_np() function.

c 复制代码
if (pthread_create(&ctx->tid_movdet, NULL, runOd, NULL) != 0) {
        fprintf(stderr, "Create thread to run IVA object failed.\n");
        goto err_getchn;
}

char *name = "object detect"; /* max. length is 16 */
err = pthread_setname_np(ctx->tid_movdet, name);
if (err != 0) {
        printf("Create object detection thread to config failed. err = %d\n", err);
}

Compiling Application for Stack Tracing

Compile the application with frame pointers preserved allows you to track calling stack when profiling.

Set environment variable before you build firmware.

bash 复制代码
$ export PERF_NO_OMIT_FP=-fno-omit-frame-pointer

Downloading Perf-enabled Firmware

Apply Un-Stripped Version of Target Libraries

After boot up, user should selectively copy un-stripped library to target platform via NFS or SD card.

The unstripped binaries, modules and libraries can be found in

bash 复制代码
$(SDK_DIR)/fs/rootfs/output/target_debug

Copy applications, modules or libraries of interest from above location to corresponding directory on target platform.

Note that if you are to copy libraries from NFS over Windows, you must:

  1. Create a copy of target_debug directory in Linux environment
  2. Remove symbolic links first (in the copied target_debug directory):
bash 复制代码
$ find . -type l -delete
  1. Copy the directory to NFS directory in Windows.

Debugging (unstripped) version of following libs and binaries are required:

  • libc and libgcc in /lib libpthread in /lib
  • libstdc++ in /lib
  • libsensor_*.so in /system/lib
  • target binary in /system/bin

Since executable binaries are copied from Windows NFS, we have to change access permission to make it executable:

bash 复制代码
$ chmod a+x /system/bin/<target bin>

Note that you must not copy /lib/ld-2.22.so at runtime, or the system will fail.

相关推荐
GISer_Jing2 分钟前
[总结篇]个人网站
前端·javascript
疯狂的沙粒23 分钟前
在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?
前端·uni-app·html
小妖66627 分钟前
html 滚动条滚动过快会留下边框线
前端·html
heroboyluck41 分钟前
Svelte 核心语法详解:Vue/React 开发者如何快速上手?
前端·svelte
海的诗篇_42 分钟前
前端开发面试题总结-JavaScript篇(二)
开发语言·前端·javascript·typescript
琹箐1 小时前
ant-design4.xx实现数字输入框; 某些输入法数字需要连续输入两次才显示
前端·javascript·anti-design-vue
程序员-小李1 小时前
VuePress完美整合Toast消息提示
前端·javascript·vue.js
Uyker2 小时前
从零开始制作小程序简单概述
前端·微信小程序·小程序
EndingCoder6 小时前
React从基础入门到高级实战:React 实战项目 - 项目三:实时聊天应用
前端·react.js·架构·前端框架
阿阳微客7 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏