From 1e847059bca680639549b82beb4be538d2a1fdbc Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Wed, 29 Oct 2014 18:12:41 -0700 Subject: [PATCH 1/2] style(cmd/ipfs/pprof) move defer close to initialization --- cmd/ipfs/Makefile | 8 ++++++++ cmd/ipfs/ipfs.go | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/ipfs/Makefile b/cmd/ipfs/Makefile index fccb80330..aa97f0a63 100644 --- a/cmd/ipfs/Makefile +++ b/cmd/ipfs/Makefile @@ -5,3 +5,11 @@ build: install: build 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 diff --git a/cmd/ipfs/ipfs.go b/cmd/ipfs/ipfs.go index 9ce5d7faf..57b48c33f 100644 --- a/cmd/ipfs/ipfs.go +++ b/cmd/ipfs/ipfs.go @@ -99,12 +99,14 @@ func main() { // if debugging, setup profiling. if u.Debug { ofi, err := os.Create("cpu.prof") + defer ofi.Close() + if err != nil { fmt.Println(err) return } + pprof.StartCPUProfile(ofi) - defer ofi.Close() defer pprof.StopCPUProfile() } From 7510ef2081e3787e97b815e69a676bd6fc75a9cd Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Wed, 29 Oct 2014 18:23:09 -0700 Subject: [PATCH 2/2] feat(cmd/ipfs1) add mem profiling in debug mode TODO add memory profiling to ipfs2 --- cmd/ipfs/ipfs.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cmd/ipfs/ipfs.go b/cmd/ipfs/ipfs.go index 57b48c33f..1494f68c2 100644 --- a/cmd/ipfs/ipfs.go +++ b/cmd/ipfs/ipfs.go @@ -17,6 +17,8 @@ import ( u "github.com/jbenet/go-ipfs/util" ) +const heapProfile = "ipfs.mprof" + // The IPFS command tree. It is an instance of `commander.Command`. var CmdIpfs = &commander.Command{ UsageLine: "ipfs [] []", @@ -117,6 +119,13 @@ func main() { } os.Exit(1) } + + if u.Debug { + err := writeHeapProfileToFile() + if err != nil { + log.Critical(err) + } + } return } @@ -220,3 +229,12 @@ func setupDaemon(confdir string, node *core.IpfsNode) (*daemon.DaemonListener, e go dl.Listen() return dl, nil } + +func writeHeapProfileToFile() error { + mprof, err := os.Create(heapProfile) + if err != nil { + log.Fatal(err) + } + defer mprof.Close() + return pprof.WriteHeapProfile(mprof) +}