pprof 查看goroutine

package main

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

var quit chan struct{} = make(chan struct{})

func f() {
  <-quit
}

func handler(w http.ResponseWriter, r *http.Request) {
  w.Header().Set("Content-Type", "text/plain")

  p := pprof.Lookup("goroutine")
  p.WriteTo(w, 1)
}

func main() {
  for i := 0; i < 10000; i++ {
    go f()
  }

  http.HandleFunc("/", handler)
  http.ListenAndServe(":11181", nil)
}

 

這上面的例子中,咱們啓動了10000個goroutine,並阻塞,而後經過訪問http://localhost:11181/,咱們就能夠獲得整個goroutine的信息,僅列出關鍵信息:函數

goroutine profile: total 10004
10000 @ 0x147d9 0x1485b 0x1dcf8 0x1de28 0x203c 0x14a70
#	0x1dcf8	chanrecv+0x4e8		/private/var/folders/00/0sdwh000h01000cxqpysvccm0035qk/T/makerelease054730537/go/src/pkg/runtime/chan.goc:268
#	0x1de28	runtime.chanrecv1+0x38	/private/var/folders/00/0sdwh000h01000cxqpysvccm0035qk/T/makerelease054730537/go/src/pkg/runtime/chan.goc:352

  

能夠看到,在main.f這個函數中,有10000個goroutine正在執行ui

轉自:http://www.tuicool.com/articles/VBFNNfAblog

相關文章
相關標籤/搜索