Mr Dk.'s BlogMr Dk.'s Blog
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 📝 Notes
    • Algorithm
      • Algorithm - Bloom Filter
      • Algorithm - Disjoint Set
      • Algorithm - Fast Power
      • Algorithm - KMP
      • Algorithm - Monotonic Stack
      • Algorithm - RB-Tree
      • Algorithm - Regular Expression
      • Algorithm - Sliding Window
      • Online Judge - I/O
    • C++
      • C++ - Const
      • C++ File I/O
      • C++ - Object Layout
      • C++ - Operator Overload
      • C++ - Polymorphism
      • C++ STL algorithm
      • C++ STL map
      • C++ STL multimap
      • C++ STL priority_queue
      • C++ STL set
      • C++ STL string
      • C++ STL unordered_map
      • C++ STL vector
      • C++ - Smart Pointer
      • C++ - Template & Genericity
    • Compiler
      • ANTLR - Basic
      • Compiler - LLVM Architecture
      • Compiler - Multi-version GCC
    • Cryptography
      • Cryptography - Certbot
      • Cryptography - Digital Signature & PKCS #7
      • Cryptography - GPG
      • Cryptography - JWT
      • Cryptography - Keystore & Certificates
      • Cryptography - OAuth 2.0
      • Cryptography - Java 实现对称与非对称加密算法
      • Cryptography - TLS
    • DevOps
      • DevOps - Travis CI
    • Docker
      • Docker - Image & Storage Management
      • Docker - Image
      • Docker - Libcontainer
      • Docker - Multi-Arch Image
      • Docker - Multi-Stage Build
      • Docker - Network
      • Docker - Orchestration & Deployment
      • Docker - Overview
      • Docker - Service Building
      • Docker - Volume & Network Usage
      • Docker - Volume
      • Linux - Control Group
      • Linux - Namespace
    • Git
      • Git - Branch & Merge
      • Git - Cached
      • Git - Cherry Pick
      • Git - Commit
      • Git - Patch
      • Git - Proxy
      • Git - Rebase
      • Git - Reset
      • Git - Stash
      • Git - Theme for Git-Bash
    • Java
      • JVM - Synchronized
      • JVM - Volatile
      • Java - Annotation 注解
      • Java - BIO & NIO
      • Java - Class Path
      • Java - Condition and LockSupport
      • Java - Current Timestamp
      • Java - Deep Copy
      • Java - 运行环境配置
      • Java - Equals
      • Java - Exporting JAR
      • Java - Javadoc
      • Java - Lock
      • Java - Maven 项目构建工具
      • Java - References
      • Java - Reflection Mechanism
      • Java - String Split
      • Java - Thread Pool
      • Java - Thread
      • Tomcat - Class Loader
      • Tomcat - Container
    • Linux
      • addr2line
      • cut
      • df
      • du
      • fallocate
      • find
      • fio
      • grep
      • groupadd
      • gzip
      • head / tail
      • hexdump
      • iostat
      • iotop
      • kill
      • ldd
      • lsof
      • ltrace / strace
      • mpstat
      • netstat
      • nm
      • pidstat
      • pmap
      • readlink
      • readlink
      • rpm2cpio / rpm2archive
      • sort
      • tee
      • uniq
      • useradd
      • usermod
      • watch
      • wc
      • which
      • xargs
    • MS Office
      • MS Office - Add-in Dev
      • MS Office - Application
    • MySQL
      • InnoDB - Architecture
      • InnoDB - Backup
      • InnoDB - Checkpoint
      • InnoDB - Critical Features
      • InnoDB - Files
      • InnoDB - Index
      • InnoDB - Insert Buffer
      • InnoDB - Lock
      • InnoDB - Partition Table
      • InnoDB - Table Storage
      • MySQL - Server Configuration
      • MySQL - Storage Engine
    • Network
      • Network - ARP
      • Network - FTP
      • Network - GitHub Accelerating
      • HTTP - Message Format
      • HTTP - POST 提交表单的两种方式
      • Network - Proxy Server
      • Network - SCP
      • Network - SSH
      • Network - TCP Congestion Control
      • Network - TCP Connection Management
      • Network - TCP Flow Control
      • Network - TCP Retransmission
      • Network - Traceroute
      • Network - V2Ray
      • Network - WebSocket
      • Network - Windows 10 Mail APP
      • Network - frp
    • Operating System
      • Linux - Kernel Compilation
      • Linux - Multi-OS
      • Linux - Mutex & Condition
      • Linux - Operations
      • Linux: Package Manager
      • Linux - Process Manipulation
      • Linux - User ID
      • Linux - Execve
      • OS - Compile and Link
      • OS - Dynamic Linking
      • OS - ELF
      • Linux - Image
      • OS - Loading
      • OS - Shared Library Organization
      • OS - Static Linking
      • Syzkaller - Architecture
      • Syzkaller - Description Syntax
      • Syzkaller - Usage
      • Ubuntu - Desktop Recover (Python)
      • WSL: CentOS 8
    • Performance
      • Linux Performance - Perf Event
      • Linux Performance - Perf Record
      • Linux Performance - Perf Report
      • Linux Performance - Flame Graphs
      • Linux Performance - Off CPU Analyze
    • PostgreSQL
      • PostgreSQL - ANALYZE
      • PostgreSQL - Atomics
      • PostgreSQL - CREATE INDEX CONCURRENTLY
      • PostgreSQL - COPY FROM
      • PostgreSQL - COPY TO
      • PostgreSQL - Executor: Append
      • PostgreSQL - Executor: Group
      • PostgreSQL - Executor: Limit
      • PostgreSQL - Executor: Material
      • PostgreSQL - Executor: Nest Loop Join
      • PostgreSQL - Executor: Result
      • PostgreSQL - Executor: Sequential Scan
      • PostgreSQL - Executor: Sort
      • PostgreSQL - Executor: Unique
      • PostgreSQL - FDW Asynchronous Execution
      • PostgreSQL - GUC
      • PostgreSQL - Locking
      • PostgreSQL - LWLock
      • PostgreSQL - Multi Insert
      • PostgreSQL - Plan Hint GUC
      • PostgreSQL - Process Activity
      • PostgreSQL - Query Execution
      • PostgreSQL - Spinlock
      • PostgreSQL - Storage Management
      • PostgreSQL - VFD
      • PostgreSQL - WAL Insert
      • PostgreSQL - WAL Prefetch
    • Productivity
      • LaTeX
      • Venn Diagram
      • VuePress
    • Solidity
      • Solidity - ABI Specification
      • Solidity - Contracts
      • Solidity - Expressions and Control Structures
      • Solidity - Layout and Structure
      • Solidity - Remix IDE
      • Solidity - Slither
      • Solidity - Types
      • Solidity - Units and Globally Available Variables
    • Vue.js
      • Vue.js - Environment Variable
    • Web
      • Web - CORS
      • Web - OpenAPI Specification
    • Wireless
      • Wireless - WEP Cracking by Aircrack-ng
      • Wireless - WPS Cracking by Reaver
      • Wireless - wifiphisher

Linux Performance - Flame Graphs

Created by: Mr Dk.

2023 / 09 / 20 23:19

Hangzhou, Zhejiang, China


Background

火焰图(Flame Graph)是性能优化大师 Brendan Gregg 发明的可视化性能分析方法,将采样得到的统计数据以直观的形式通过图表展示出来,解决了采样数据量较大、难以直观分析的问题。火焰图可以有 on-CPU / off-CPU 火焰图、内存火焰图、冷热火焰图、差分火焰图等等。一般来说,通过 perf record 采样的数据能够得到的是 on-CPU 火焰图,展示了 CPU 运行过程中被采样最频繁的函数。然而,这并不能反映出程序因阻塞而离开 CPU 的时间,所以还需要通过其它方式进行采样,并产生 off-CPU 火焰图进行全面的分析。两者结合可以解释 100% 的程序运行时间。

每一簇火焰的宽度表示被采样命中的次数(按字母排序),每一簇火焰的高度表示被采样时的栈深度。一簇火焰越宽,表示采样时遇到它所对应的函数最频繁;一簇火焰越高,表示采样时它的调用栈越深。

flamegraph

Generation

如何得到一张火焰图呢?一般来说需要三步:

  1. 采样数据
  2. 折叠堆栈
  3. 产生火焰图

Sampling

使用 Perf / eBPF / DTrace 都是可以的。

Folding Stacks

采样后的数据需要被加工才能产生火焰图。Brendan Gregg 的 FlameGraph GitHub 仓库中已经提供了加工采样后数据的脚本:

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

仓库内有一堆 stackcollapse-*.pl 的脚本。根据采样时使用的工具,选择相对应的工具来进行加工即可:

$ ls -l | grep stackcollapse-
-rwxrwxr-x 1 postgres postgres     1185 Sep 17 11:28 stackcollapse-aix.pl
-rwxrwxr-x 1 postgres postgres     1901 Sep 17 11:28 stackcollapse-bpftrace.pl
-rwxrwxr-x 1 postgres postgres     3910 Sep 17 11:28 stackcollapse-chrome-tracing.py
-rwxrwxr-x 1 postgres postgres     2304 Sep 17 11:28 stackcollapse-elfutils.pl
-rwxrwxr-x 1 postgres postgres     1816 Sep 17 11:28 stackcollapse-gdb.pl
-rwxrwxr-x 1 postgres postgres     3631 Sep 17 11:28 stackcollapse-go.pl
-rwxrwxr-x 1 postgres postgres      680 Sep 17 11:28 stackcollapse-instruments.pl
-rwxrwxr-x 1 postgres postgres     1786 Sep 17 11:28 stackcollapse-java-exceptions.pl
-rwxrwxr-x 1 postgres postgres     5207 Sep 17 11:28 stackcollapse-jstack.pl
-rwxrwxr-x 1 postgres postgres     1858 Sep 17 11:28 stackcollapse-ljp.awk
-rwxrwxr-x 1 postgres postgres    13179 Sep 17 11:28 stackcollapse-perf.pl
-rwxrwxr-x 1 postgres postgres     6527 Sep 17 11:28 stackcollapse-perf-sched.awk
-rwxrwxr-x 1 postgres postgres     2675 Sep 17 11:28 stackcollapse-pmc.pl
-rwxrwxr-x 1 postgres postgres     1569 Sep 17 11:28 stackcollapse-recursive.pl
-rwxrwxr-x 1 postgres postgres     7871 Sep 17 11:28 stackcollapse-sample.awk
-rwxrwxr-x 1 postgres postgres     2309 Sep 17 11:28 stackcollapse-stap.pl
-rwxrwxr-x 1 postgres postgres     2983 Sep 17 11:28 stackcollapse-vsprof.pl
-rwxrwxr-x 1 postgres postgres     3434 Sep 17 11:28 stackcollapse-vtune-mc.pl
-rw-rw-r-- 1 postgres postgres     3174 Sep 17 11:28 stackcollapse-vtune.pl
-rwxrwxr-x 1 postgres postgres     1938 Sep 17 11:28 stackcollapse-wcp.pl
-rwxrwxr-x 1 postgres postgres     5683 Sep 17 11:28 stackcollapse-xdebug.php

Generating Flame Graph

假设使用了 Perf 采样,当前路径下会有 perf.data。使用如下命令就可以生成火焰图:

perf script | ./stackcollapse-perf.pl > out.perf-folded
./flamegraph.pl out.perf-folded > perf.svg

然后直接通过浏览器访问 perf.svg 即可。

References

Flame Graphs

CPU Flame Graphs

Off-CPU Flame Graphs

Off-CPU Analysis

Edit this page on GitHub
Prev
Linux Performance - Perf Report
Next
Linux Performance - Off CPU Analyze