版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99)
sudo add-apt-repository ppa:canonical-kernel-team/ppa
sudo apt install linux-tools-$(uname -r)
git clone https://github.com/cobblau/FlameGraph
sudo perf record --call-graph dwarf -p $(pidof qemu-system-x86_64)
sudo perf script | FlameGraph/stackcollapse-perf.pl | FlameGraph/flamegraph.pl > process.svg
sudo perf record -F 99 -g -- sleep 30 $(pidof qemu-system-x86_64)
sudo perf report -n --stdio
sudo perf script | gzip > out.perf01.gz
git clone https://github.com/cobblau/FlameGraph
; http://svgshare.com can be used to share svg
sudo perf script | ./FlameGraph/stackcollapse-perf.pl | sed '/cpu_idle/d;s/\[unknown\];//g' | ./FlameGraph/flamegraph.pl --width 1000 > perf.svg
google-chrome-stable ./perf.svg
参考: https://github.com/bboymimi/perf-utils
# To get the all CPUs profiling callstack from user space to kernel space (both kernel and user space)
sudo perf record -a --call-graph dwarf
# To get the all CPUs profiling of kernel space callstack (just kernel space)
sudo perf record -a -g
git clone https://github.com/bboymimi/perf-utils.git
sudo ./perf-utils/easy-flamegraph.sh -i perf.data
google-chrome-stable ./perf-output/2018-01-04_14:18:15.perf.data.svg
# Enable the sysrq
sudo sysctl -w kernel.sysrq=1
# Dump all active tasks stack - https://www.kernel.org/doc/html/latest/admin-guide/sysrq.html
echo l | sudo tee /proc/sysrq-trigger
# Dumps tasks that are in uninterruptable (blocked) state.
echo p | sudo tee /proc/sysrq-trigger
# Performance profiling and get the flame-graph to see if there is
any performance bottleneck when the problem happened
git clone git://kernel.ubuntu.com/gavinguo/perf-production.git
sudo perf record -a -g sleep 5
sudo ./easy-flamegraph.sh -i ./perf.data -t
Profile Java with Perf
默认的perf只能分析c与c++程序, 其他用户态的perf-java, perf-python, perf-go应当分别学习, 这里说的是perf-java
Profile Java with Perf
# https://cloud.tencent.com/developer/article/1416234
# https://my.oschina.net/u/150599/blog/3023394
sudo apt install -y linux-tools-common linux-tools-generic bindfs
mkdir /tmp/containerrootfs
PID=$(docker inspect --format {{.State.Pid}} cas1)
sudo perf record -g -p $PID
sudo perf report
#fix symbols in container
cp perf.data /tmp/containerrootfs/
bindfs /proc/$PID/root /tmp/containerrootfs
perf report --symfs /tmp/containerrootfs
umount /tmp/containerrootfs
# install java
sudo apt install -y openjdk-8-jdk
dpkg-query -L openjdk-8-jdk
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
# install perf-map-agent to help generate symbols with FlameGraph
sudo apt install -y cmake pkg-config libfuse-dev libfuse2 autoconf
git clone https://github.com/brendangregg/FlameGraph.git
export FLAMEGRAPH_DIR=/home/ubuntu/FlameGraph
git clone https://github.com/jvm-profiling-tools/perf-map-agent.git
cd perf-map-agent/
cmake .
make
sudo bin/create-links-in /usr/local/bin
# Use perf-java-flames (
# Need to add one jvm option in java process: -XX:+PreserveFramePointer
perf-java-flames $PID -g
#perf-java-record-stack $PID -g
chromium-browser /bak/work/perf-map-agent/flamegraph-10286.svg
上下文切换高
启用sar,
sudo apt install sysstat
sudo sed -i 's/^ENABLED="false"/ENABLED="true"/' /etc/default/sysstat
sudo sed -i 's/^5-55\/10/*\/2/' /etc/cron.d/sysstat
sudo systemctl restart sysstat
后可以观测如上文文切换过高
00:00:01 proc/s cswch/s
Average: 143.82 59339.01
可以搜索火焰图(用户态内核态一起收集)
git clone git://kernel.ubuntu.com/gavinguo/perf-production.git
cd perf-production
sudo perf record -a --call-graph dwarf sleep 5
sudo ./easy-flamegraph.sh -i ./perf.data -t
也可以使用pidstat -w 1来确定哪个进程的上下文切换高, 或者:
# https://serverfault.com/questions/190049/find-out-which-task-is-generating-a-lot-of-context-switches-on-linux
sudo perf record -e context-switches -a -g
# then ctrl+c
sudo perf report # inspect the result
**Appendix - error - ERROR: No stack counts found **
遇到下面error是需要: sudo chown -R root:root perf.data, 必须使用root用户, 而非它说的普通用户.
hua@t440p:/bak/work/perf-utils$ sudo ./easy-flamegraph.sh perf.data -f
Use the /bak/work/perf-utils/perf.data as the source of the FlameGraph.
File /bak/work/perf-utils/perf.data not owned by current user or root (use -f to override)
ERROR: No stack counts found
#see what [multithreaded] programs are blocking on - https://poormansprofiler.org/
sudo gdb -ex “set pagination 0” -ex “thread apply all bt” --batch -p $(pidof ovs-vswitchd)
mkdir gdb_ovs; cd gdb_ovs; for i in $(seq 1 600); do gdb -ex “set pagination 0” -ex “thread apply all bt” -batch -p KaTeX parse error: Expected 'EOF', got '&' at position 5: pid &̲> gdb.ovs-vswit…(date -Is) ; sleep 0.1; done
tmate is used to share ssh with others
wget https://raw.githubusercontent.com/torvalds/linux/master/tools/perf/perf-archive.sh -O perf-archive.sh
chmod +x perf-archive.sh
./perf-archive.sh # this can take a while, a few minutes
#sudo cat /proc/kallsyms | tee $(uname -r).kallsyms
#分文件存放, cpu_50p(cpu > 50%), mem_5G(mem > 5G)
sudo add-apt-repository ppa:mimi0213kimo/easyflamegraph
sudo apt-get update
ls /var/log/easy-flamegraph/mem/
grep -r 'USE_EASY_FLAMEGRAPH' /etc/default/easy-flamegraph
[1] http://www.brendangregg.com/FlameGraphs/cpuflamegraphs.html
[2] http://www.brendangregg.com/flamegraphs.html