/
cmds.go
130 lines (120 loc) · 3.69 KB
/
cmds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package cmds
import (
"fmt"
"os"
"os/exec"
"time"
"golang.org/x/sync/errgroup"
units "github.com/docker/go-units"
"github.com/pachyderm/pachyderm/src/client"
"github.com/pachyderm/pachyderm/src/server/pkg/cmdutil"
"github.com/spf13/cobra"
)
// Cmds returns a slice containing debug commands.
func Cmds() []*cobra.Command {
var commands []*cobra.Command
dump := &cobra.Command{
Short: "Return a dump of running goroutines.",
Long: "Return a dump of running goroutines.",
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
client, err := client.NewOnUserMachine("debug-dump")
if err != nil {
return err
}
defer client.Close()
return client.Dump(os.Stdout)
}),
}
commands = append(commands, cmdutil.CreateAlias(dump, "debug dump"))
var duration time.Duration
profile := &cobra.Command{
Use: "{{alias}} <profile>",
Short: "Return a profile from the server.",
Long: "Return a profile from the server.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := client.NewOnUserMachine("debug-dump")
if err != nil {
return err
}
defer client.Close()
return client.Profile(args[0], duration, os.Stdout)
}),
}
profile.Flags().DurationVarP(&duration, "duration", "d", time.Minute, "Duration to run a CPU profile for.")
commands = append(commands, cmdutil.CreateAlias(profile, "debug profile"))
binary := &cobra.Command{
Short: "Return the binary the server is running.",
Long: "Return the binary the server is running.",
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
client, err := client.NewOnUserMachine("debug-dump")
if err != nil {
return err
}
defer client.Close()
return client.Binary(os.Stdout)
}),
}
commands = append(commands, cmdutil.CreateAlias(binary, "debug binary"))
var profileFile string
var binaryFile string
pprof := &cobra.Command{
Use: "{{alias}} <profile>",
Short: "Analyze a profile of pachd in pprof.",
Long: "Analyze a profile of pachd in pprof.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := client.NewOnUserMachine("debug-dump")
if err != nil {
return err
}
defer client.Close()
var eg errgroup.Group
// Download the profile
eg.Go(func() (retErr error) {
if args[0] == "cpu" {
fmt.Printf("Downloading cpu profile, this will take %s...", units.HumanDuration(duration))
}
f, err := os.Create(profileFile)
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil && retErr == nil {
retErr = err
}
}()
return client.Profile(args[0], duration, f)
})
// Download the binary
eg.Go(func() (retErr error) {
f, err := os.Create(binaryFile)
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil && retErr == nil {
retErr = err
}
}()
return client.Binary(f)
})
if err := eg.Wait(); err != nil {
return err
}
cmd := exec.Command("go", "tool", "pprof", binaryFile, profileFile)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}),
}
pprof.Flags().StringVar(&profileFile, "profile-file", "profile", "File to write the profile to.")
pprof.Flags().StringVar(&binaryFile, "binary-file", "binary", "File to write the binary to.")
pprof.Flags().DurationVarP(&duration, "duration", "d", time.Minute, "Duration to run a CPU profile for.")
commands = append(commands, cmdutil.CreateAlias(pprof, "debug pprof"))
debug := &cobra.Command{
Short: "Debug commands for analyzing a running cluster.",
Long: "Debug commands for analyzing a running cluster.",
}
commands = append(commands, cmdutil.CreateAlias(debug, "debug"))
return commands
}