Go tool pprof性能監控調試工具基本使用說明


Jack Liu's Github地址:
iotd@Github:tool-pprof.md

Go tool pprof使用方式

go中有pprof包來做代碼的性能監控主要涉及兩個pkg:

#web服務器:
import (
    "net/http"
    _ "net/http/pprof"
)

#一般應用程序(實際應用無web交互)
import (
    "net/http"
    _ "runtime/pprof"
)

net/http/pprof中只是使用runtime/pprof包來進行封裝了一下,並在http端口上暴露出來。

Go tool pprof輔助工具安裝(圖形工具graphviz為例)

  • Windows
    1.官方下載安裝包: http://www.graphviz.org/download/
    下載Stable穩定版本(.msi)
    2.配置PATH系統環境變量:
    C:\Program Files (x86)\Graphviz2.38\bin

  • Linux(例:Centos)

方式1).添加repo依賴http://204.178.9.49/graphviz-rhel.repo

yum list available 'graphviz*'
yum install 'graphviz*'  --skip-broken
#備注:--skip-broken可選:跳過錯誤依賴,不加這個參數會提示安裝包依賴錯誤,因為這里並不需要其它的安裝包,所以跳過即可。

--skip-broken可選:跳過錯誤依賴,不加這個參數會提示安裝包依賴錯誤,因為這里並不需要其它的安裝包,所以跳過即可。

方式2).源碼包編譯安裝
./configure
make
make install

  • MacOS:
    brew install graphviz

Go tool pprof常用基本調試基本命令(默認30s采集時間,可通過--seconds)

HTTP場景(參數可選:--text):

Heap profile:

go tool pprof --text http://localhost:8080/debug/pprof/heap  

CPU profile:

go tool pprof --text http://localhost:8080/debug/pprof/profile  

Goroutine blocking profile:

go tool pprof --text http://localhost:8080/debug/pprof/block  

1.實時通過地址查看瀏覽器: http://localhost:8080/debug/pprof/;
2.通過生成的profile文件分析;
選擇指定的profile壓縮gz文件(.gz),使用go tool pprof進入

go tool pprof http://localhost:8080/debug/pprof/profile
#結束后直接進入交互:
(pprof)
  web
(pprof)

如查看歷史調試文件信息,通過指定的profile文件進入即可:
go tool pprof [*.gz]

pprof交互基本命令:web 直接生成web瀏覽器可訪問的svg圖;
(其他命令自行摸索)
Windows下自動生成.svg文件且調用默認瀏覽器訪問;
MacOS下自動生成.gz文件,系統限制可根據提示文件路徑通過手動訪問查看;

【注意事項】:
profile文件為空的問題, heap和block一般不受影響。
執行交互web命令會報:

(pprof) web
profile is empty
(pprof) 

產生原因:
pprof內存分析器采取抽樣的方式,它僅僅從一些內存分配的子集中收集信息。有可能對一個對象的采樣與被采樣對象的大小成比例。通過使用go test --memprofilerate標識,或者通過程序啟動時 的運行配置中的MemProfileRate變量來改變調整這個采樣的比例。如果比例為1,則會導致全部申請的信息都會被收集,但是這樣的話將會使得執行變慢。默認的采樣比例是每512KB的內存申請就采樣一次。

  • 方法1).在進行調試時,指定運行參數,或運行代碼中動態調整參數
go tool pprof --text http://localhost:8080/debug/pprof/profile

此命令將會打印耗費最多CPU時間的函數列表。
這里有幾種可用的輸出形式,最實用的有 --text, --web 和 --list。運行 "go tool pprof" 來得到完整的列表。

【備注】:
實際測試時,MacOS下基本是空的,需要指定參數。

  • 方法2).設置環境變量(此方法極不推薦!)設置Go環境變量 GODEBUG="memprofilerate=1".

  • 通過控制采樣的比例和行為,可以達到性能調試粒度的控制!

附pprof 交互命令help說(部分命令需要第三方工具支持):

(pprof) help
 
 Commands:
   cmd [n] [--cum] [focus_regex]* [-ignore_regex]*
       Produce a text report with the top n entries.
       Include samples matching focus_regex, and exclude ignore_regex.
       Add --cum to sort using cumulative data.
       Available commands:
         callgrind    Outputs a graph in callgrind format
         disasm       Output annotated assembly for functions matching regexp or address
         dot          Outputs a graph in DOT format
         eog          Visualize graph through eog
         evince       Visualize graph through evince
         gif          Outputs a graph image in GIF format
         gv           Visualize graph through gv
         list         Output annotated source for functions matching regexp
         pdf          Outputs a graph in PDF format
         peek         Output callers/callees of functions matching regexp
         png          Outputs a graph image in PNG format
         proto        Outputs the profile in compressed protobuf format
         ps           Outputs a graph in PS format
         raw          Outputs a text representation of the raw profile
         svg          Outputs a graph in SVG format
         tags         Outputs all tags in the profile
         text         Outputs top entries in text form
         top          Outputs top entries in text form
         tree         Outputs a text rendering of call graph
         web          Visualize graph through web browser
         weblist      Output annotated source in HTML for functions matching regexp or address
   peek func_regex
       Display callers and callees of functions matching func_regex.
 
   dot [n] [focus_regex]* [-ignore_regex]* [>file]
       Produce an annotated callgraph with the top n entries.
       Include samples matching focus_regex, and exclude ignore_regex.
       For other outputs, replace dot with:
       - Graphic formats: dot, svg, pdf, ps, gif, png (use > to name output file)
       - Graph viewer:    gv, web, evince, eog
 
   callgrind [n] [focus_regex]* [-ignore_regex]* [>file]
       Produce a file in callgrind-compatible format.
       Include samples matching focus_regex, and exclude ignore_regex.
 
   weblist func_regex [-ignore_regex]*
       Show annotated source with interspersed assembly in a web browser.
 
   list func_regex [-ignore_regex]*
       Print source for routines matching func_regex, and exclude ignore_regex.
 
   disasm func_regex [-ignore_regex]*
       Disassemble routines matching func_regex, and exclude ignore_regex.
 
   tags tag_regex [-ignore_regex]*
       List tags with key:value matching tag_regex and exclude ignore_regex.
 
   quit/exit/^D
         Exit pprof.
 
   option=value
       The following options can be set individually:
           cum/flat:           Sort entries based on cumulative or flat data
           call_tree:          Build context-sensitive call trees
           nodecount:          Max number of entries to display
           nodefraction:       Min frequency ratio of nodes to display
           edgefraction:       Min frequency ratio of edges to display
           focus/ignore:       Regexp to include/exclude samples by name/file
           tagfocus/tagignore: Regexp or value range to filter samples by tag
                               eg "1mb", "1mb:2mb", ":64kb"
 
           functions:          Level of aggregation for sample data
           files:
           lines:
           addresses:
 
           unit:               Measurement unit to use on reports
 
           Sample value selection by index:
            sample_index:      Index of sample value to display
            mean:              Average sample value over first value
 
           Sample value selection by name:
            alloc_space        for heap profiles
            alloc_objects
            inuse_space
            inuse_objects
 
            total_delay        for contention profiles
            mean_delay
            contentions
 
   :   Clear focus/ignore/hide/tagfocus/tagignore
(pprof)

Jack Liu's Github地址:
iotd@Github:tool-pprof.md


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM