非web程序查看程序实时运行信息的方式,使用pprof包
启用一个监听端口 11181,可以通过网页(http://127.0.0.1:11181/)实时查看程序的运行状况 。
协程相关信息,协程开启的数量,以及协程的运行堆栈信息。
p := pprof.Lookup(“goroutine”)
Debug.go
package main
import (
"net/http"
"runtime/pprof"
)
func goppf() {
http.HandleFunc("/", handler)
http.ListenAndServe(":11181", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
p := pprof.Lookup("goroutine")
p.WriteTo(w, 1)
}
main.go
package main
import (
"time"
)
var quit chan struct{} = make(chan struct{})
func f() {
<-quit
}
func main() {
for i := 0; i < 10000; i++ {
go f()
}
go goppf() //启用跟踪查看
for {
time.Sleep(1 * time.Second)
}
}
网址 http://127.0.0.1:11181/ 访问结果,可查看的详细
goroutine profile: total 10010
1 @ 0x474c5d 0x4749f4 0x470a7c 0x40115f 0x452f48 0x4546e4 0x455071 0x452a7e 0x43fde1
# 0x474c5d runtime/pprof.writeRuntimeProfile+0xdd D:/APPInstall/Go/src/runtime/pprof/pprof.go:540
# 0x4749f4 runtime/pprof.writeGoroutine+0xa4 D:/APPInstall/Go/src/runtime/pprof/pprof.go:502
# 0x470a7c runtime/pprof.(*Profile).WriteTo+0xdc D:/APPInstall/Go/src/runtime/pprof/pprof.go:229
# 0x40115f main.handler+0xdf D:/YSHWork/GoWorkSpace/src/GoPProf/Debug.go:17
# 0x452f48 net/http.HandlerFunc.ServeHTTP+0x48 D:/APPInstall/Go/src/net/http/server.go:1265
# 0x4546e4 net/http.(*ServeMux).ServeHTTP+0x184 D:/APPInstall/Go/src/net/http/server.go:1541
# 0x455071 net/http.serverHandler.ServeHTTP+0x1a1 D:/APPInstall/Go/src/net/http/server.go:1703
# 0x452a7e net/http.(*conn).serve+0xb5e D:/APPInstall/Go/src/net/http/server.go:1204
1 @ 0x41528c 0x4152ff 0x41c0ac 0x401234 0x414fba 0x43fde1
# 0x401234 main.main+0x74 D:/YSHWork/GoWorkSpace/src/GoPProf/main.go:20
# 0x414fba runtime.main+0xfa D:/APPInstall/Go/src/runtime/proc.go:63
1 @ 0x41528c 0x4152ff 0x415125 0x43fde1
# 0x41528c runtime.gopark+0x10c D:/APPInstall/Go/src/runtime/proc.go:131
# 0x4152ff runtime.goparkunlock+0x4f D:/APPInstall/Go/src/runtime/proc.go:136
# 0x415125 runtime.forcegchelper+0xd5 D:/APPInstall/Go/src/runtime/proc.go:99
1 @ 0x41528c 0x4152ff 0x40e9b3 0x43fde1
# 0x41528c runtime.gopark+0x10c D:/APPInstall/Go/src/runtime/proc.go:131
# 0x4152ff runtime.goparkunlock+0x4f D:/APPInstall/Go/src/runtime/proc.go:136
# 0x40e9b3 runtime.bgsweep+0xc3 D:/APPInstall/Go/src/runtime/mgc0.go:98
1 @ 0x41528c 0x4152ff 0x40dfa1 0x43fde1
# 0x41528c runtime.gopark+0x10c D:/APPInstall/Go/src/runtime/proc.go:131
# 0x4152ff runtime.goparkunlock+0x4f D:/APPInstall/Go/src/runtime/proc.go:136
# 0x40dfa1 runtime.runfinq+0xc1 D:/APPInstall/Go/src/runtime/malloc.go:727
10000 @ 0x41528c 0x4152ff 0x403989 0x4034eb 0x4011b6 0x43fde1
# 0x4011b6 main.f+0x46 D:/YSHWork/GoWorkSpace/src/GoPProf/main.go:10
1 @ 0x41528c 0x411c77 0x4110df 0x4c4eae 0x4c6175 0x4c92e7 0x4c970a 0x4de8c5 0x455ee3 0x4552e9 0x45521b 0x455821 0x401075 0x43fde1
# 0x4c4eae net.(*pollDesc).Wait+0x4e D:/APPInstall/Go/src/net/fd_poll_runtime.go:84
# 0x4c6175 net.(*ioSrv).ExecIO+0x305 D:/APPInstall/Go/src/net/fd_windows.go:188
# 0x4c92e7 net.(*netFD).acceptOne+0x547 D:/APPInstall/Go/src/net/fd_windows.go:558
# 0x4c970a net.(*netFD).accept+0x17a D:/APPInstall/Go/src/net/fd_windows.go:585
# 0x4de8c5 net.(*TCPListener).AcceptTCP+0x55 D:/APPInstall/Go/src/net/tcpsock_posix.go:234
# 0x455ee3 net/http.tcpKeepAliveListener.Accept+0x53 D:/APPInstall/Go/src/net/http/server.go:1976
# 0x4552e9 net/http.(*Server).Serve+0x99 D:/APPInstall/Go/src/net/http/server.go:1728
# 0x45521b net/http.(*Server).ListenAndServe+0x15b D:/APPInstall/Go/src/net/http/server.go:1718
# 0x455821 net/http.ListenAndServe+0xc1 D:/APPInstall/Go/src/net/http/server.go:1808
# 0x401075 main.goppf+0x75 D:/YSHWork/GoWorkSpace/src/GoPProf/Debug.go:10
1 @ 0x40c4e3 0x41c711 0x43fde1
# 0x40c4e3 runtime.notetsleepg+0x93 D:/APPInstall/Go/src/runtime/lock_sema.go:267
# 0x41c711 runtime.timerproc+0x101 D:/APPInstall/Go/src/runtime/time.go:207
3 @ 0x41528c 0x411c77 0x4110df 0x4c4eae 0x4c6175 0x4c85a0 0x4d84c3 0x44c6b2 0x4a70d5 0x515515 0x51631e 0x516399 0x505575 0x5052c6 0x4479a2 0x44ee26 0x4525c5 0x43fde1
# 0x4c4eae net.(*pollDesc).Wait+0x4e D:/APPInstall/Go/src/net/fd_poll_runtime.go:84
# 0x4c6175 net.(*ioSrv).ExecIO+0x305 D:/APPInstall/Go/src/net/fd_windows.go:188
# 0x4c85a0 net.(*netFD).Read+0x180 D:/APPInstall/Go/src/net/fd_windows.go:470
# 0x4d84c3 net.(*conn).Read+0xe3 D:/APPInstall/Go/src/net/net.go:121
# 0x44c6b2 net/http.(*liveSwitchReader).Read+0xb2 D:/APPInstall/Go/src/net/http/server.go:214
# 0x4a70d5 io.(*LimitedReader).Read+0xd5 D:/APPInstall/Go/src/io/io.go:408
# 0x515515 bufio.(*Reader).fill+0x1d5 D:/APPInstall/Go/src/bufio/bufio.go:97
# 0x51631e bufio.(*Reader).ReadSlice+0x25e D:/APPInstall/Go/src/bufio/bufio.go:295
# 0x516399 bufio.(*Reader).ReadLine+0x69 D:/APPInstall/Go/src/bufio/bufio.go:324
# 0x505575 net/textproto.(*Reader).readLineSlice+0xa5 D:/APPInstall/Go/src/net/textproto/reader.go:55
# 0x5052c6 net/textproto.(*Reader).ReadLine+0x56 D:/APPInstall/Go/src/net/textproto/reader.go:36
# 0x4479a2 net/http.ReadRequest+0xd2 D:/APPInstall/Go/src/net/http/request.go:598
# 0x44ee26 net/http.(*conn).readRequest+0x276 D:/APPInstall/Go/src/net/http/server.go:586
# 0x4525c5 net/http.(*conn).serve+0x6a5 D:/APPInstall/Go/src/net/http/server.go:1162
例如
10010 个协程正在运行
goroutine profile: total 10010
根据提示的详细的文件行信息查找最后的执行操作<-quit 操作
一下表示10000个协程正在执行此操作信息
10000 @ 0x41528c 0x4152ff 0x403989 0x4034eb 0x4011b6 0x43fde1
# 0x4011b6 main.f+0x46 D:/YSHWork/GoWorkSpace/src/GoPProf/main.go:10