forked from name5566/leaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
215 lines (183 loc) · 4.11 KB
/
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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package console
import (
"fmt"
"github.com/name5566/leaf/chanrpc"
"github.com/name5566/leaf/conf"
"github.com/name5566/leaf/log"
"os"
"path"
"runtime/pprof"
"time"
)
var commands = []Command{
new(CommandHelp),
new(CommandCPUProf),
new(CommandProf),
}
type Command interface {
// must goroutine safe
name() string
// must goroutine safe
help() string
// must goroutine safe
run(args []string) string
}
type ExternalCommand struct {
_name string
_help string
server *chanrpc.Server
}
func (c *ExternalCommand) name() string {
return c._name
}
func (c *ExternalCommand) help() string {
return c._help
}
func (c *ExternalCommand) run(_args []string) string {
args := make([]interface{}, len(_args))
for i, v := range _args {
args[i] = v
}
ret, err := c.server.Open(0).Call1(c._name, args...)
if err != nil {
return err.Error()
}
output, ok := ret.(string)
if !ok {
return "invalid output type"
}
return output
}
// you must call the function before calling console.Init
// goroutine not safe
func Register(name string, help string, f interface{}, server *chanrpc.Server) {
for _, c := range commands {
if c.name() == name {
log.Fatal("command %v is already registered", name)
}
}
server.Register(name, f)
c := new(ExternalCommand)
c._name = name
c._help = help
c.server = server
commands = append(commands, c)
}
// help
type CommandHelp struct{}
func (c *CommandHelp) name() string {
return "help"
}
func (c *CommandHelp) help() string {
return "this help text"
}
func (c *CommandHelp) run([]string) string {
output := "Commands:\r\n"
for _, c := range commands {
output += c.name() + " - " + c.help() + "\r\n"
}
output += "quit - exit console"
return output
}
// cpuprof
type CommandCPUProf struct{}
func (c *CommandCPUProf) name() string {
return "cpuprof"
}
func (c *CommandCPUProf) help() string {
return "CPU profiling for the current process"
}
func (c *CommandCPUProf) usage() string {
return "cpuprof writes runtime profiling data in the format expected by \r\n" +
"the pprof visualization tool\r\n\r\n" +
"Usage: cpuprof start|stop\r\n" +
" start - enables CPU profiling\r\n" +
" stop - stops the current CPU profile"
}
func (c *CommandCPUProf) run(args []string) string {
if len(args) == 0 {
return c.usage()
}
switch args[0] {
case "start":
fn := profileName() + ".cpuprof"
f, err := os.Create(fn)
if err != nil {
return err.Error()
}
err = pprof.StartCPUProfile(f)
if err != nil {
f.Close()
return err.Error()
}
return fn
case "stop":
pprof.StopCPUProfile()
return ""
default:
return c.usage()
}
}
func profileName() string {
now := time.Now()
return path.Join(conf.ProfilePath,
fmt.Sprintf("%d%02d%02d_%02d_%02d_%02d",
now.Year(),
now.Month(),
now.Day(),
now.Hour(),
now.Minute(),
now.Second()))
}
// prof
type CommandProf struct{}
func (c *CommandProf) name() string {
return "prof"
}
func (c *CommandProf) help() string {
return "writes a pprof-formatted snapshot"
}
func (c *CommandProf) 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"
}
func (c *CommandProf) run(args []string) string {
if len(args) == 0 {
return c.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 c.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
}