mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-02 03:28:25 +08:00
Merge pull request #235 from jbenet/feat/mprof-in-cli-ipfs1-debug
perf(cmd/ipfs) write mprof file in debug mode
This commit is contained in:
@ -5,3 +5,11 @@ build:
|
|||||||
|
|
||||||
install: build
|
install: build
|
||||||
go install
|
go install
|
||||||
|
|
||||||
|
# cpu profiling: `go tool pprof ipfs cpu.prof`
|
||||||
|
# mem profiling: `go tool pprof ipfs ipfs.mprof`
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f cpu.prof
|
||||||
|
rm -f ipfs.mprof
|
||||||
|
rm -f ipfs
|
||||||
|
@ -17,6 +17,8 @@ import (
|
|||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const heapProfile = "ipfs.mprof"
|
||||||
|
|
||||||
// The IPFS command tree. It is an instance of `commander.Command`.
|
// The IPFS command tree. It is an instance of `commander.Command`.
|
||||||
var CmdIpfs = &commander.Command{
|
var CmdIpfs = &commander.Command{
|
||||||
UsageLine: "ipfs [<flags>] <command> [<args>]",
|
UsageLine: "ipfs [<flags>] <command> [<args>]",
|
||||||
@ -99,12 +101,14 @@ func main() {
|
|||||||
// if debugging, setup profiling.
|
// if debugging, setup profiling.
|
||||||
if u.Debug {
|
if u.Debug {
|
||||||
ofi, err := os.Create("cpu.prof")
|
ofi, err := os.Create("cpu.prof")
|
||||||
|
defer ofi.Close()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
pprof.StartCPUProfile(ofi)
|
pprof.StartCPUProfile(ofi)
|
||||||
defer ofi.Close()
|
|
||||||
defer pprof.StopCPUProfile()
|
defer pprof.StopCPUProfile()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,6 +119,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if u.Debug {
|
||||||
|
err := writeHeapProfileToFile()
|
||||||
|
if err != nil {
|
||||||
|
log.Critical(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,3 +229,12 @@ func setupDaemon(confdir string, node *core.IpfsNode) (*daemon.DaemonListener, e
|
|||||||
go dl.Listen()
|
go dl.Listen()
|
||||||
return dl, nil
|
return dl, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeHeapProfileToFile() error {
|
||||||
|
mprof, err := os.Create(heapProfile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer mprof.Close()
|
||||||
|
return pprof.WriteHeapProfile(mprof)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user