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

feat: add endpoint for enabling block profiling #8469

Merged
merged 1 commit into from
Mar 11, 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
1 change: 1 addition & 0 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
defaultMux("/debug/vars"),
defaultMux("/debug/pprof/"),
corehttp.MutexFractionOption("/debug/pprof-mutex/"),
corehttp.BlockProfileRateOption("/debug/pprof-block/"),
corehttp.MetricsScrapingOption("/debug/metrics/prometheus"),
corehttp.LogOption(),
}
Expand Down
35 changes: 35 additions & 0 deletions core/corehttp/mutex_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,38 @@ func MutexFractionOption(path string) ServeOption {
return mux, nil
}
}

// BlockProfileRateOption allows to set runtime.SetBlockProfileRate via HTTP
// using POST request with parameter 'rate'.
// The profiler tries to sample 1 event every <rate> nanoseconds.
// If rate == 1, then the profiler samples every blocking event.
// To disable, set rate = 0.
func BlockProfileRateOption(path string) ServeOption {
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "only POST allowed", http.StatusMethodNotAllowed)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

rateStr := r.Form.Get("rate")
if len(rateStr) == 0 {
http.Error(w, "parameter 'rate' must be set", http.StatusBadRequest)
return
}

rate, err := strconv.Atoi(rateStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Infof("Setting BlockProfileRate to %d", rate)
runtime.SetBlockProfileRate(rate)
})
return mux, nil
}
}
13 changes: 13 additions & 0 deletions test/sharness/t0110-gateway.sh
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ test_expect_success "test failure conditions of mutex pprof endpoint" '
test_must_fail curl -f -X GET "http://127.0.0.1:$apiport/debug/pprof-mutex/?fraction=-1"
'

curl_pprofblock() {
curl -f -X POST "http://127.0.0.1:$apiport/debug/pprof-block/?rate=$1"
}

test_expect_success "set blocking profiler rate for pprof (0 so it doesn't enable)" '
curl_pprofblock 0
'

test_expect_success "test failure conditions of mutex block endpoint" '
test_must_fail curl_pprofblock &&
test_must_fail curl_pprofblock that_is_string &&
test_must_fail curl -f -X GET "http://127.0.0.1:$apiport/debug/pprof-block/?rate=0"
'

test_expect_success "setup index hash" '
mkdir index &&
Expand Down