Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Add stack dump. #366

Merged
merged 1 commit into from
Oct 28, 2017
Merged
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
27 changes: 27 additions & 0 deletions cmd/cri-containerd/cri_containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import (
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"

"github.com/docker/docker/pkg/reexec"
"github.com/golang/glog"
Expand Down Expand Up @@ -118,6 +121,7 @@ func main() {
cmd.AddCommand(loadImageCommand())

cmd.RunE = func(cmd *cobra.Command, args []string) error {
setupDumpStacksTrap()
if err := o.InitFlags(cmd.Flags()); err != nil {
return fmt.Errorf("failed to init CRI containerd flags: %v", err)
}
Expand Down Expand Up @@ -155,3 +159,26 @@ func validateConfig(o *options.CRIContainerdOptions) {
selinux.SetDisabled()
}
}

func setupDumpStacksTrap() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1)
go func() {
for range c {
dumpStacks()
}
}()
}

func dumpStacks() {
Copy link
Member

@mikebrow mikebrow Oct 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from: https://golang.org/src/runtime/debug/stack.go, just a little cleaner with a modification to dump all (true)

func stack() []byte {
	buf := make([]byte, 1024)
  	for {
  		n := runtime.Stack(buf, true)
  		if n < len(buf) {
  			return buf[:n]
  		}
  		buf = make([]byte, 2*len(buf))
  	}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
buf = buf[:n]
break
}
buf = make([]byte, 2*len(buf))
}
glog.V(0).Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
}