Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1194 from hashicorp/ceb-debug
Browse files Browse the repository at this point in the history
cmd/waypoint-entrypoint: dump heap profile on SIGUSR1
  • Loading branch information
mitchellh committed Mar 16, 2021
1 parent 7ee02b9 commit a191409
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
61 changes: 61 additions & 0 deletions cmd/waypoint-entrypoint/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"context"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime/pprof"
"time"

"github.com/hashicorp/go-hclog"
"golang.org/x/sys/unix"
)

// debugSignalHandler writes a heap profile to the system temporary
// directory when SIGUSR1 is received. The output path for each dump
// is logged.
//
// NOTE(mitchellh): In the future, I expect that we'll dump a lot more
// data and perhaps store a tar file or some other format. Given the
// debug nature of this file, I'm not going to worry too much about tagging
// the file type and so on.
func debugSignalHandler(ctx context.Context, log hclog.Logger) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, unix.SIGUSR1)
defer signal.Stop(ch)

for {
select {
case <-ctx.Done():
return

case <-ch:
}

// Dump the profile to a timestamped file
path := filepath.Join(os.TempDir(), fmt.Sprintf(
"waypoint_heap_%d", time.Now().Unix()))
log.Warn("SIGUSR1 received, dumping heap profile", "path", path)

// Get the profile, this is predefined by Go and should always exist.
profile := pprof.Lookup("heap")
if profile == nil {
log.Error("heap profile not found, this should not be possible")
continue
}

// Write it
f, err := os.Create(path)
if err != nil {
log.Error("error opening file to dump profile", "err", err)
continue
}
err = profile.WriteTo(f, 0)
f.Close()
if err != nil {
log.Error("error writing heap profile", "err", err)
}
}
}
3 changes: 3 additions & 0 deletions cmd/waypoint-entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func realMain() int {
ctx, closer := signalcontext.WithInterrupt(context.Background(), log)
defer closer()

// Start our debug signal handler
go debugSignalHandler(ctx, log.Named("debug"))

// Run our core logic
err := ceb.Run(ctx,
ceb.WithEnvDefaults(),
Expand Down

0 comments on commit a191409

Please sign in to comment.