forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceability.go
51 lines (43 loc) · 1.02 KB
/
serviceability.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
package serviceability
import (
"os"
"os/signal"
"syscall"
"github.com/pkg/profile"
)
type Stop interface {
Stop()
}
type stopper struct {
profile bool
}
func (stopper) Stop() {
}
func Profile(mode string) Stop {
var stop Stop
switch mode {
case "mem":
stop = profileOnExit(profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook))
case "cpu":
stop = profileOnExit(profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.NoShutdownHook))
case "block":
stop = profileOnExit(profile.Start(profile.BlockProfile, profile.ProfilePath("."), profile.NoShutdownHook))
default:
stop = stopper{}
}
return stop
}
func profileOnExit(s Stop) Stop {
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
// Programs with more sophisticated signal handling
// should ensure the Stop() function returned from
// Start() is called during shutdown.
// See http://godoc.org/github.com/pkg/profile
s.Stop()
os.Exit(1)
}()
return s
}