はじめに
net/http/pprof
を使ってCPUプロファイリングをしようとしているのに、
profile is empty
とでて、プロファイリングできない。
pprof の使い方は
などを参考にしてください。
これはプロファイリングできる
コード
main.go
package main
import (
"log"
"net/http"
_ "net/http/pprof"
"runtime"
)
func main() {
runtime.SetBlockProfileRate(1)
go func() {
log.Println(http.ListenAndServe("0.0.0.0:6060", nil))
}()
go func() {
var count int
for {
count++
}
}()
var ch chan int
<-ch
}
コマンド
go build -o sample
./sample
別のターミナルにて
$ go tool pprof sample http://localhost:6060/debug/pprof/profile
Fetching profile from http://localhost:6060/debug/pprof/profile
Please wait... (30s)
Saved profile in /Users/hogehoge/pprof/pprof.sample.localhost:6060.samples.cpu.001.pb.gz
Entering interactive mode (type "help" for commands)
(pprof) top
25.32s of 25.34s total (99.92%)
Dropped 3 nodes (cum <= 0.13s)
flat flat% sum% cum cum%
23.82s 94.00% 94.00% 23.82s 94.00% main.main.func2
1.50s 5.92% 99.92% 1.50s 5.92% runtime.usleep
0 0% 99.92% 23.82s 94.00% runtime.goexit
0 0% 99.92% 1.52s 6.00% runtime.mstart
0 0% 99.92% 1.52s 6.00% runtime.mstart1
0 0% 99.92% 1.52s 6.00% runtime.sysmon
(pprof)
ちゃんとプロファイリングできている。
signal.Ignore() をかますと
コード
main.go
package main
import (
"log"
"net/http"
_ "net/http/pprof"
"os/signal"
"runtime"
)
func main() {
runtime.SetBlockProfileRate(1)
go func() {
log.Println(http.ListenAndServe("0.0.0.0:6060", nil))
}()
signal.Ignore() // <- 軽い気持ちで足してみる
go func() {
var count int
for {
count++
}
}()
var ch chan int
<-ch
}
コマンド
同様にやると
$ go tool pprof sample http://localhost:6060/debug/pprof/profile
Fetching profile from http://localhost:6060/debug/pprof/profile
Please wait... (30s)
Saved profile in /Users/hogehoge/pprof/pprof.sample.localhost:6060.samples.cpu.002.pb.gz
Entering interactive mode (type "help" for commands)
(pprof) top
profile is empty # <- ??????????
(pprof)
profile is empty
となってプロファイリングできていない。
結論
pprof
を使うときは signal.Ignore()
をコメントアウトしよう。
なんでや