Golang FlameGraph(火焰图)

2018-01-05  本文已影响0人  zr_hebo

1.安装组件

  1. 安装go-torch
    go get github.com/uber/go-torch
  2. 安装 FlameGraph
    cd $WORK_PATH && git clone https://github.com/brendangregg/FlameGraph.git
    export PATH=$PATH:$WORK_PATH/FlameGraph
  3. 安装graphviz
    yum install graphviz(CentOS, Redhat)

2.代码修改

package main

import (
    "net/http"
    "net/http/pprof"
)

func main() {
    // 主函数中添加
    go func() {
        http.HandleFunc("/debug/pprof/block", pprof.Index)
        http.HandleFunc("/debug/pprof/goroutine", pprof.Index)
        http.HandleFunc("/debug/pprof/heap", pprof.Index)
        http.HandleFunc("/debug/pprof/threadcreate", pprof.Index)

        http.ListenAndServe("0.0.0.0:8888", nil)
    }()

    var finishWaiter chan int
    <-finishWaiter
}

3.查看结果

运行上述程序后,使用如下命令生成CPU火焰图:
go-torch -u http://localhost:8888/debug/pprof/ -p > profile-local.svg
效果图如下:

profile-local.png

生成内存火焰图:
go-torch -u http://localhost:8888/debug/pprof/heap -p > heap-local.svg
效果图如下:

heap-local.png

也可以分步骤查看结果:

  1. go tool pprof --text http://localhost:7080/debug/pprof/profile
  2. 命令会生成一个 profile文件:pprof.samples.cpu.001.pb.gz
  3. go-torch -p ~/Downloads/pprof.samples.cpu.001.pb.gz > ~/Desktop/profile-local.svg

问题排查

1.Centos7 使用报错
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = "en_US.UTF-8",
LC_ALL = "en_US.UTF-8",
LC_PAPER = "en_US.UTF-8",
LC_MONETARY = "en_US.UTF-8",
LC_NUMERIC = "en_US.UTF-8",
LC_MEASUREMENT = "en_US.UTF-8",
LC_CTYPE = "en_US.UTF-8",
LC_TIME = "en_US.UTF-8",
LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
解决方法:
export LC_ALL="C"

上一篇下一篇

猜你喜欢

热点阅读