项目作者: Marslanali

项目描述 :
Perf is a performance analyzing tool in Linux
高级语言: C++
项目地址: git://github.com/Marslanali/linux-perf-tools.git
创建时间: 2020-07-08T14:10:54Z
项目社区:https://github.com/Marslanali/linux-perf-tools

开源协议:

下载


linux-perf-tools

Perf is a performance analyzing tool in Linux.

Perf (Linux profiling with performance counter) is powerful: 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 began as a tool for using the performance counters subsystem in Linux, and has had various enhancements to add tracing capabilities.

Section1: C++ application using perf

You may need to install linux-tools-common and other packages for the specific kernel:

  1. sudo apt install linux-tools-common
  1. linux-tools-4.15.0-88-generic
  2. linux-cloud-tools-4.15.0-88-generic

Optionally

You may also want to install one of the following packages to keep up to date:

  1. linux-tools-generic
  2. Linux-cloud-tools-generic

Perf analysis on C++ Fibonacci Number (Naive Algorithm)

The first approach to test Fibonacci number is using recursion and this will be indeed slow for greater value of n.

Computing for n > 40 takes considerable time.


drawing


Fig. 1 Pusedo code for Fibonacci number using recursion

In a new terminal type:

  1. cd test-code-flame-graph
  2. mkdir build
  3. cd build
  4. cmake ..
  5. make

Binaries will be placed in build folder and this test had been done with following CMake settings:

  1. cmake_minimum_required(VERSION 3.5.1)
  2. project(test-code-flame-graph)
  3. set(CMAKE_CXX_STANDARD 11)
  4. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

Creating a Perf.data for C++ Fibonacci Number

In a new terminal type:

  1. sudo perf record --call-graph dwarf ./test-code-flame-graph

Generating flameGraph from perf.data

In new separate terminal type:

  1. sudo perf script | ./stackcollapse-perf.pl | ./famegraph.pl > perf.svg

Generated flameGraph





Fig. 2 Generated flameGraph for Fibonacci number using recursion

Section2: C++ shared library profiling using perf

Similarly, we can only profile C++ shared libraray using same way.

  1. perf report -d library.so

Generated flameGraph for shared library of C++ Fibonacci






Fig. 3 Generated flameGraph for C++ Fibonacci number shared library using recursion

Section3: Python script with perf

In new separate terminal type:

  1. git clone git@github.com:brendangregg/FlameGraph.git
  1. cd FlaneGraph
  2. python -m flamegraph -o perf.log myscript.py

This will generate a perf.log file in root folder of FlameGraph.

where myscript.py is supposed to be your python scripts which you wants to have analyze.

Now to run Brendan Gregg’s FlameGraph tool against the output:

  1. flamegraph.pl --title "MyScript CPU" perf.log > perf.svg

Where flamegraph.pl is located in root folder of FlameGraph, the generated file perf.svg
will be saved in same root folder i.e. FlameGraph.






Fig. 4. CPU FlameGraph for myscript.py. Fig. 2. CPU FlameGraph for myscript.py