forked from name5566/leaf
/
prof_command.go
65 lines (56 loc) · 1.38 KB
/
prof_command.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
package command
import (
"os"
"runtime/pprof"
)
type ProfCommand struct{}
func (command *ProfCommand) Name() string {
return "prof"
}
func (command *ProfCommand) Help() string {
return "writes a pprof-formatted snapshot"
}
func (command *ProfCommand) Run(args []string) string {
if len(args) == 0 {
return command.usage()
}
var (
p *pprof.Profile
fn string
)
switch args[0] {
case "goroutine":
p = pprof.Lookup("goroutine")
fn = profileName() + ".gprof"
case "heap":
p = pprof.Lookup("heap")
fn = profileName() + ".hprof"
case "thread":
p = pprof.Lookup("threadcreate")
fn = profileName() + ".tprof"
case "block":
p = pprof.Lookup("block")
fn = profileName() + ".bprof"
default:
return command.usage()
}
f, err := os.Create(fn)
if err != nil {
return err.Error()
}
defer f.Close()
err = p.WriteTo(f, 0)
if err != nil {
return err.Error()
}
return fn
}
func (command *ProfCommand) usage() string {
return "prof writes runtime profiling data in the format expected by \r\n" +
"the pprof visualization tool\r\n\r\n" +
"Usage: prof goroutine|heap|thread|block\r\n" +
" goroutine - stack traces of all current goroutines\r\n" +
" heap - a sampling of all heap allocations\r\n" +
" thread - stack traces that led to the creation of new OS threads\r\n" +
" block - stack traces that led to blocking on synchronization primitives"
}