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正在执行

转自:http://www.tuicool.com/articles/VBFNNfA

 

转载于:https://www.cnblogs.com/wangxusummer/p/4054564.html

你可能感兴趣的:(pprof 查看goroutine)