-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/signal: move signal.DumpStacks() to a separate package
It is not directly related to signal-handling, so can well live in its own package. Also added a variant that doesn't take a directory to write files to, for easier consumption / better match to how it's used. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
- Loading branch information
Showing
10 changed files
with
108 additions
and
68 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
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,8 @@ | ||
package signal | ||
|
||
import "github.com/docker/docker/pkg/stack" | ||
|
||
// DumpStacks appends the runtime stack into file in dir and returns full path | ||
// to that file. | ||
// Deprecated: use github.com/docker/docker/pkg/stack.Dump instead. | ||
var DumpStacks = stack.DumpToFile |
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,57 @@ | ||
package stack // import "github.com/docker/docker/pkg/stack" | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const stacksLogNameTemplate = "goroutine-stacks-%s.log" | ||
|
||
// Dump outputs the runtime stack to os.StdErr. | ||
func Dump() { | ||
_ = dump(os.Stderr) | ||
} | ||
|
||
// DumpToFile appends the runtime stack into a file named "goroutine-stacks-<timestamp>.log" | ||
// in dir and returns the full path to that file. If no directory name is | ||
// provided, it outputs to os.Stderr. | ||
func DumpToFile(dir string) (string, error) { | ||
var f *os.File | ||
if dir != "" { | ||
path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1))) | ||
var err error | ||
f, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666) | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to open file to write the goroutine stacks") | ||
} | ||
defer f.Close() | ||
defer f.Sync() | ||
} else { | ||
f = os.Stderr | ||
} | ||
return f.Name(), dump(f) | ||
} | ||
|
||
func dump(f *os.File) error { | ||
var ( | ||
buf []byte | ||
stackSize int | ||
) | ||
bufferLen := 16384 | ||
for stackSize == len(buf) { | ||
buf = make([]byte, bufferLen) | ||
stackSize = runtime.Stack(buf, true) | ||
bufferLen *= 2 | ||
} | ||
buf = buf[:stackSize] | ||
if _, err := f.Write(buf); err != nil { | ||
return errors.Wrap(err, "failed to write goroutine stacks") | ||
} | ||
return nil | ||
} |
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,31 @@ | ||
package stack // import "github.com/docker/docker/pkg/stack" | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
is "gotest.tools/v3/assert/cmp" | ||
) | ||
|
||
func TestDump(t *testing.T) { | ||
Dump() | ||
} | ||
|
||
func TestDumpToFile(t *testing.T) { | ||
directory, err := ioutil.TempDir("", "test-dump-tasks") | ||
assert.Check(t, err) | ||
defer os.RemoveAll(directory) | ||
dumpPath, err := DumpToFile(directory) | ||
assert.Check(t, err) | ||
readFile, _ := ioutil.ReadFile(dumpPath) | ||
fileData := string(readFile) | ||
assert.Check(t, is.Contains(fileData, "goroutine")) | ||
} | ||
|
||
func TestDumpToFileWithEmptyInput(t *testing.T) { | ||
path, err := DumpToFile("") | ||
assert.Check(t, err) | ||
assert.Check(t, is.Equal(os.Stderr.Name(), path)) | ||
} |