Skip to content

Commit e981b96

Browse files
committed
Add CPU pprof flag
Related: #23 Modified from: taoky/ayano@b26dcc8
1 parent 94cea48 commit e981b96

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

main.go

+24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import (
55
"flag"
66
"log"
77
"os"
8+
"os/signal"
9+
"runtime/pprof"
810
"strings"
11+
"syscall"
912

1013
"github.com/pelletier/go-toml/v2"
1114
)
@@ -35,8 +38,29 @@ func sshmuxServer(configFile string) (*Server, error) {
3538

3639
func main() {
3740
var configFile string
41+
var pprofFile string
3842
flag.StringVar(&configFile, "c", "/etc/sshmux/config.toml", "config file")
43+
flag.StringVar(&pprofFile, "pprof", "", "write pprof data to file")
3944
flag.Parse()
45+
if pprofFile != "" {
46+
f, err := os.Create(pprofFile)
47+
if err != nil {
48+
log.Fatal("failed to create pprof file: %w", err)
49+
}
50+
defer f.Close()
51+
if err := pprof.StartCPUProfile(f); err != nil {
52+
log.Fatal("failed to start pprof: %w", err)
53+
}
54+
defer pprof.StopCPUProfile()
55+
// Handle SIGINT/SIGTERM
56+
signalChan := make(chan os.Signal, 1)
57+
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
58+
go func() {
59+
<-signalChan
60+
pprof.StopCPUProfile()
61+
os.Exit(0)
62+
}()
63+
}
4064
sshmux, err := sshmuxServer(configFile)
4165
if err != nil {
4266
log.Fatal(err)

0 commit comments

Comments
 (0)