This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
task: allow start pprof at runtime by unix signal (#372)
* task: allow start pprof at runtime by unix sig * utils: fix CI, and test dynamic port allocate * utils: fix a mis-used lock * tests: use SimpleHTTPServer instead of nc * tests: remove test dynamic port * tests: give CI more time * tests: add retry on bind pprof address * utils: use OS assigned port insteadof fixed port * utils: start pprof at 0.0.0.0:0 * utils: remove unused variables * *: apply some suggestions from code review - start pprof by import for side-effect - change the dynamic pprof hint to DEBUG level - check whther pprof started outside the pprof gorotuine * tests: use pkill instead of kill to send signal * utils: print log after success to listen TCP addr * utils: don't log dynamic pprof enabled log * Update pkg/utils/pprof.go Co-authored-by: kennytm <kennytm@gmail.com> Co-authored-by: 3pointer <luancheng@pingcap.com> Co-authored-by: kennytm <kennytm@gmail.com>
- Loading branch information
1 parent
1a63149
commit aa4b7d5
Showing
6 changed files
with
120 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// +build !linux,!darwin,!freebsd,!unix | ||
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package utils | ||
|
||
// StartDynamicPProfListener starts the listener that will enable pprof when received `startPProfSignal` | ||
func StartDynamicPProfListener() { | ||
// nothing to do on no posix signal supporting systems. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// +build linux darwin freebsd unix | ||
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package utils | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/pingcap/log" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
startPProfSignal = syscall.SIGUSR1 | ||
) | ||
|
||
var ( | ||
signalChan = make(chan os.Signal, 1) | ||
) | ||
|
||
// StartDynamicPProfListener starts the listener that will enable pprof when received `startPProfSignal`. | ||
func StartDynamicPProfListener() { | ||
signal.Notify(signalChan, startPProfSignal) | ||
go onSignalStartPProf(signalChan) | ||
} | ||
|
||
func onSignalStartPProf(signals <-chan os.Signal) { | ||
for sig := range signals { | ||
if sig == startPProfSignal { | ||
log.Info("signal received, starting pprof...", zap.Stringer("signal", sig)) | ||
StartPProfListener("0.0.0.0:0") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"os" | ||
|
||
// #nosec | ||
// register HTTP handler for /debug/pprof | ||
_ "net/http/pprof" | ||
"sync" | ||
|
||
"github.com/pingcap/failpoint" | ||
"github.com/pingcap/log" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var ( | ||
startedPProf = "" | ||
mu sync.Mutex | ||
) | ||
|
||
// StartPProfListener forks a new goroutine listening on specified port and provide pprof info. | ||
func StartPProfListener(statusAddr string) { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
if startedPProf != "" { | ||
log.Warn("Try to start pprof when it has been started, nothing will happen", zap.String("address", startedPProf)) | ||
return | ||
} | ||
failpoint.Inject("determined-pprof-port", func(v failpoint.Value) { | ||
port := v.(int) | ||
statusAddr = fmt.Sprintf(":%d", port) | ||
log.Info("injecting failpoint, pprof will start at determined port", zap.Int("port", port)) | ||
}) | ||
listener, err := net.Listen("tcp", statusAddr) | ||
if err != nil { | ||
log.Warn("failed to start pprof", zap.String("addr", statusAddr), zap.Error(err)) | ||
return | ||
} | ||
startedPProf = listener.Addr().String() | ||
log.Info("bound pprof to addr", zap.String("addr", startedPProf)) | ||
_, _ = fmt.Fprintf(os.Stderr, "bound pprof to addr %s\n", startedPProf) | ||
|
||
go func() { | ||
if e := http.Serve(listener, nil); e != nil { | ||
log.Warn("failed to serve pprof", zap.String("addr", startedPProf), zap.Error(e)) | ||
mu.Lock() | ||
startedPProf = "" | ||
mu.Unlock() | ||
return | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters