為golang程式使用pprof遠程查看httpserver運行堆棧,cpu耗時等資訊

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

pprof是個神馬玩意兒?

pprof - manual page for pprof (part of gperftools)

是gperftools工具的一部分

gperftools又是啥?

These tools are for use by developers so that they can create more robust applications. Especially of use to those developing multi-threaded applications in C++ with templates. Includes TCMalloc, heap-checker, heap-profiler and cpu-profiler.

一個效能分析的工具,可以查看堆棧、cpu資訊等等。

 

在golang中如何使用呢?下面就來看看。

 

1. 在一個httpserver中,只需要一行代碼

 

package mainimport ("flag""log""net/http"_ "net/http/pprof""sync""time")func main() {flag.Parse()//這裡實現了遠程擷取pprof資料的介面go func() {log.Println(http.ListenAndServe("localhost:6060", nil))}()var wg sync.WaitGroupwg.Add(10)for i := 0; i < 10; i++ {go work(&wg)}wg.Wait()// Wait to see the global run queue deplete.time.Sleep(3 * time.Second)}func work(wg *sync.WaitGroup) {time.Sleep(time.Second)var counter intfor i := 0; i < 1e10; i++ {time.Sleep(time.Millisecond * 100)counter++}wg.Done()}

2.編譯運行這個程式

go build trace_example.go

./trace_example

 

3. 通過瀏覽器訪問

http://localhost:6060/debug/pprof/

能夠查看到程式的overview

 

 

 

4.你也可以通過終端命令查看

Then use the pprof tool to look at the heap profile:

go tool pprof http://localhost:6060/debug/pprof/heap

Or to look at a 30-second CPU profile:

go tool pprof http://localhost:6060/debug/pprof/profile

Or to look at the goroutine blocking profile:

go tool pprof http://localhost:6060/debug/pprof/block


qpzhang@qpzhangdeMac-mini:~/gocode $go tool pprof http://localhost:6060/debug/pprof/profileRead http://localhost:6060/debug/pprof/symbolGathering CPU profile from http://localhost:6060/debug/pprof/profile?seconds=30 for 30 seconds to  /var/folders/2l/195zcc1n0sn2wjfjwf9hl9d80000gn/T/mmBnpdkv0DBe patient...Wrote profile to /var/folders/2l/195zcc1n0sn2wjfjwf9hl9d80000gn/T/mmBnpdkv0DWelcome to pprof!  For help, type 'help'.(pprof) top10Total: 9 samples       6  66.7%  66.7%        6  66.7% runtime.mach_semaphore_wait       1  11.1%  77.8%        1  11.1% runtime.kevent       1  11.1%  88.9%        1  11.1% runtime.mach_semaphore_signal       1  11.1% 100.0%        1  11.1% runtime.mach_semaphore_timedwait       0   0.0% 100.0%        7  77.8% System       0   0.0% 100.0%        1  11.1% notetsleep       0   0.0% 100.0%        1  11.1% ready       0   0.0% 100.0%        2  22.2% runtime.gosched0       0   0.0% 100.0%        1  11.1% runtime.mach_semrelease       0   0.0% 100.0%        1  11.1% runtime.notetsleepg(pprof) webTotal: 9 samplesLoading web page file:////var/folders/2l/195zcc1n0sn2wjfjwf9hl9d80000gn/T/SlFUZhzBDB.0.svg
web命令產生了很cool的svg圖片,在瀏覽器中開啟。







除了遠程拉取,你還可以自己編寫代碼,把這些資料寫入檔案(實現 net/http/pprof 中的幾個路由,開啟或關閉追蹤資訊)。
具體可參考:http://blog.golang.org/profiling-go-programs
https://github.com/hyper-carrot/go_command_tutorial/blob/master/0.12.md

期間:web命令依賴一個svg產生包,在mac下面,你可以通過http://www.graphviz.org/Download_macos.php進行下載。

 -------------------------------------

2016.11.14 updated

基於uninx domain socket 的agent,可以不用走http

https://github.com/google/gops

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.