Skip to content

feat: add full goroutine stack dump #8790

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

Merged
merged 1 commit into from
Mar 16, 2022
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
2 changes: 2 additions & 0 deletions bin/collect-profiles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ fi
echo Collecting goroutine stacks
curl -s -o goroutines.stacks "$SOURCE_URL"'/debug/pprof/goroutine?debug=2'

curl -s -o goroutines.stacks.full "$SOURCE_URL"'/debug/stack'

echo Collecting goroutine profile
go tool pprof -symbolize=remote -svg -output goroutine.svg "$SOURCE_URL/debug/pprof/goroutine"

Expand Down
1 change: 1 addition & 0 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
corehttp.VersionOption(),
defaultMux("/debug/vars"),
defaultMux("/debug/pprof/"),
defaultMux("/debug/stack"),
corehttp.MutexFractionOption("/debug/pprof-mutex/"),
corehttp.BlockProfileRateOption("/debug/pprof-block/"),
corehttp.MetricsScrapingOption("/debug/metrics/prometheus"),
Expand Down
15 changes: 15 additions & 0 deletions cmd/ipfs/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"net/http"

"github.com/ipfs/go-ipfs/core/commands"
)

func init() {
http.HandleFunc("/debug/stack",
func(w http.ResponseWriter, _ *http.Request) {
_ = commands.WriteAllGoroutineStacks(w)
},
)
}
30 changes: 30 additions & 0 deletions core/commands/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ However, it could reveal:
},
}

func WriteAllGoroutineStacks(w io.Writer) error {
// this is based on pprof.writeGoroutineStacks, and removes the 64 MB limit
buf := make([]byte, 1<<20)
for i := 0; ; i++ {
n := runtime.Stack(buf, true)
if n < len(buf) {
buf = buf[:n]
break
}
// if len(buf) >= 64<<20 {
// // Filled 64 MB - stop there.
// break
// }
buf = make([]byte, 2*len(buf))
}
_, err := w.Write(buf)
return err
}

func writeProfiles(ctx context.Context, cpuProfileTime time.Duration, w io.Writer) error {
archive := zip.NewWriter(w)

Expand All @@ -143,6 +162,17 @@ func writeProfiles(ctx context.Context, cpuProfileTime time.Duration, w io.Write
file: "heap.pprof",
}}

{
out, err := archive.Create("goroutines-all.stacks")
if err != nil {
return err
}
err = WriteAllGoroutineStacks(out)
if err != nil {
return err
}
}

for _, profile := range profiles {
prof := pprof.Lookup(profile.name)
out, err := archive.Create(profile.file)
Expand Down
4 changes: 4 additions & 0 deletions test/sharness/t0152-profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ test_expect_success "goroutines stacktrace is valid" '
grep -q "goroutine" "profiles/goroutines.stacks"
'

test_expect_success "full goroutines stacktrace is valid" '
grep -q "goroutine" "profiles/goroutines-all.stacks"
'

test_done