Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(cmd/ipfs) write mprof file in debug mode #235

Merged
merged 2 commits into from
Oct 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/ipfs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 21 additions & 1 deletion cmd/ipfs/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<flags>] <command> [<args>]",
Expand Down Expand Up @@ -99,12 +101,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()
}

Expand All @@ -115,6 +119,13 @@ func main() {
}
os.Exit(1)
}

if u.Debug {
err := writeHeapProfileToFile()
if err != nil {
log.Critical(err)
}
}
return
}

Expand Down Expand Up @@ -218,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)
}