zoukankan      html  css  js  c++  java
  • cpu高占用率排查工具

    概览

    1.使用top -H -p pid,去查看一个线程的cpu使用
    2.使用pstack 打印运行堆栈,从堆栈观察高出现函数

    正文

    1.火焰图

    使用perf采集数据,-F 99 每秒99次; -p 999 追踪进程999; -g 记录调用栈; --proc-map-timeout 1000, cpu太高的时候,进入该进程要时间,设置跟入该进程的超市时间; sleep 30, 持续30秒

    perf record -F 99 -p 999 -g --proc-map-timeout 1000 -- sleep 30
    perf script > out.perf
    

    下载火焰图制造工具FlameGraph, 设置环境变量

    git clone https://github.com/brendangregg/FlameGraph.git
    export PATH=$PATH:$WORK_PATH/FlameGraph-master
    

    生成火焰图

    stackcollapse-perf.pl out.perf >out.folded
    flamegraph.pl out.folded > cpu.svg
    

    一键化脚本,将以下脚本保存成genFrmae.sh, 然后使用 genFrame.sh pid就可以生成火焰图 cpu.svg

    #!/bin/bash
    
    perf record -F 99 -p $1 -g --proc-map-timeout 1000 -- sleep 30
    
    perf script > out.perf
    stackcollapse-perf.pl out.perf >out.folded
    flamegraph.pl out.folded > cpu.svg
    

    2.pstack

    原理就是使用gdb attach 上去之后打印运行堆栈,ubuntu下安装的pstack不知道为什么有问题,可将以下脚本直接保存成pstack.sh, 使用./pstack.sh pid的方式运行查看进程当前的运行堆栈

    #!/bin/sh
    
    if test $# -ne 1; then
            echo "Usage: `basename $0 .sh` <process-id>" 1>&2
                exit 1
    fi
    
    if test ! -r /proc/$1; then
            echo "Process $1 not found." 1>&2
                exit 1
    fi
    
    # GDB doesn't allow "thread apply all bt" when the process isn't
    # threaded; need to peek at the process to determine if that or the
    # simpler "bt" should be used.
    
    backtrace="bt"
    if test -d /proc/$1/task ; then
            # Newer kernel; has a task/ directory.
                if test `/bin/ls /proc/$1/task | /usr/bin/wc -l` -gt 1 2>/dev/null ; then
                        backtrace="thread apply all bt"
                            fi
                        elif test -f /proc/$1/maps ; then
                                # Older kernel; go by it loading libpthread.
                                    if /bin/grep -e libpthread /proc/$1/maps > /dev/null 2>&1 ; then
                                            backtrace="thread apply all bt"
                                                fi
    fi
    
    GDB=${GDB:-/usr/bin/gdb}
    
    if $GDB -nx --quiet --batch --readnever > /dev/null 2>&1; then
            readnever=--readnever
        else
                readnever=
    fi
    
    # Run GDB, strip out unwanted noise.
    $GDB --quiet $readnever -nx /proc/$1/exe $1 <<EOF 2>&1 | 
    set width 0
    set height 0
    set pagination no
    $backtrace
    EOF
    /bin/sed -n 
            -e 's/^((gdb) )*//' 
                -e '/^#/p' 
                    -e '/^Thread/p'
    
    
    

    3.strace

    用来跟踪系统调用的工具

    strace -f -F -o ~/straceout.txt myserver
    

    -f -F选项告诉strace同时跟踪fork和vfork出来的进程,-o选项把所有strace输出写到~/straceout.txt里 面,myserver是要启动和调试的程序。

    ref

    1.火焰图的使用
    2.strace的使用

  • 相关阅读:
    iOS开发之Masonry框架源码解析
    iOS开发针对对Masonry下的FPS优化讨论
    React-native Android环境搭建
    Android中ListView使用总结
    Android开发布局方式
    轮播图
    大文件断点下载
    基于第三方库FMDB的数据库的二次封装
    md5加密
    AssignToObject文件(字典转模型、字典数组转模型数组)
  • 原文地址:https://www.cnblogs.com/ishen/p/12728157.html
Copyright © 2011-2022 走看看