Skip to content

Commit

Permalink
Add CPU pprof flag
Browse files Browse the repository at this point in the history
Related: #23
Modified from: taoky/ayano@b26dcc8
  • Loading branch information
taoky committed Sep 14, 2024
1 parent 94cea48 commit e981b96
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"flag"
"log"
"os"
"os/signal"
"runtime/pprof"
"strings"
"syscall"

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

func main() {
var configFile string
var pprofFile string
flag.StringVar(&configFile, "c", "/etc/sshmux/config.toml", "config file")
flag.StringVar(&pprofFile, "pprof", "", "write pprof data to file")
flag.Parse()
if pprofFile != "" {
f, err := os.Create(pprofFile)
if err != nil {
log.Fatal("failed to create pprof file: %w", err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("failed to start pprof: %w", err)
}
defer pprof.StopCPUProfile()
// Handle SIGINT/SIGTERM
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-signalChan
pprof.StopCPUProfile()
os.Exit(0)
}()
}
sshmux, err := sshmuxServer(configFile)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit e981b96

Please sign in to comment.