forked from wheelcomplex/kmg
/
kmgProfile.go
136 lines (121 loc) · 3.54 KB
/
kmgProfile.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
package kmgProfile
import (
"expvar"
"fmt"
"github.com/bronze1man/kmg/kmgCmd"
"github.com/bronze1man/kmg/kmgFile"
"github.com/bronze1man/kmg/kmgSys"
"github.com/bronze1man/kmg/kmgView"
"github.com/bronze1man/kmg/kmgView/kmgBootstrap"
"net/http"
"net/http/pprof"
"os"
"runtime/debug"
runtimePprof "runtime/pprof"
"strings"
"sync"
"time"
)
// 可以使用PrefixPath提高安全性
// 如果你不关心安全性 可以使用 / 以便使用golang的默认值. (kmgHttp.ClearHttpDefaultServer() 避免多次注册)
func RegisterProfile(prefixPath string) {
registerProfile(prefixPath, http.DefaultServeMux)
}
func StartProfileOnAddr(prefixPath string, profileAddr string) {
mux := http.NewServeMux()
registerProfile(prefixPath, mux)
go func() {
err := http.ListenAndServe(profileAddr, mux)
if err != nil {
panic(err)
}
}()
}
var initOnce sync.Once
func registerProfile(prefixPath string, mux *http.ServeMux) {
prefixPath = strings.Trim(prefixPath, "/")
if prefixPath != "" {
prefixPath = "/" + prefixPath
}
mux.HandleFunc(prefixPath+"/pprof/profile", pprof.Profile)
mux.HandleFunc(prefixPath+"/pprof/symbol", pprof.Symbol)
mux.HandleFunc(prefixPath+"/pprof/heap", heap)
mux.Handle(prefixPath+"/pprof/block", pprof.Handler("block"))
mux.Handle(prefixPath+"/pprof/goroutine", pprof.Handler("goroutine"))
mux.Handle(prefixPath+"/pprof/threadcreate", pprof.Handler("threadcreate"))
mux.HandleFunc(prefixPath+"/pprof/", pprof.Index)
mux.HandleFunc(prefixPath+"/vars", ExpvarHandler)
mux.HandleFunc(prefixPath+"/gc", GcHandler)
mux.HandleFunc(prefixPath+"/", Index)
initOnce.Do(func() {
gStartTime = time.Now()
expvar.Publish("startTime", expvar.Func(startTime))
expvar.Publish("uptime", expvar.Func(uptime))
})
}
// Replicated from expvar.go as not public.
func ExpvarHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "{\n")
first := true
expvar.Do(func(kv expvar.KeyValue) {
if !first {
fmt.Fprintf(w, ",\n")
}
first = false
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
})
fmt.Fprintf(w, "\n}\n")
}
func GcHandler(w http.ResponseWriter, r *http.Request) {
debug.FreeOSMemory()
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte("SUCCESS"))
}
func heap(w http.ResponseWriter, r *http.Request) {
debug.FreeOSMemory()
pprof.Handler("heap").ServeHTTP(w, r)
}
func Index(w http.ResponseWriter, r *http.Request) {
content := kmgBootstrap.Table{}
for _, url := range []string{
"gc",
"vars",
//"pprof/profile?debug=1",
"pprof/heap?debug=1",
"pprof/threadcreate?debug=1",
"pprof/goroutine?debug=1",
"pprof/goroutine?debug=2",
} {
content.DataList = append(content.DataList, []kmgView.HtmlRenderer{
kmgBootstrap.A{Href: url, Title: url},
})
}
w.Write([]byte(kmgBootstrap.NewWrap("debug page", content).HtmlRender()))
}
var gStartTime time.Time
func startTime() interface{} {
return gStartTime.String()
}
func uptime() interface{} {
return time.Since(gStartTime).String()
}
func CpuProfile(funcer func()) {
selfPath, err := kmgSys.GetCurrentExecutePath()
if err != nil {
panic(err)
}
tmpPath := kmgFile.NewTmpFilePath()
f, err := os.Create(tmpPath)
if err != nil {
panic(err)
}
defer f.Close()
runtimePprof.StartCPUProfile(f)
defer runtimePprof.StopCPUProfile()
funcer()
runtimePprof.StopCPUProfile()
f.Close()
kmgCmd.CmdSlice([]string{"go", "tool", "pprof", "-top", "-cum", selfPath, tmpPath}).MustRun()
kmgCmd.CmdSlice([]string{"go", "tool", "pprof", "-top", "-cum", "-lines", selfPath, tmpPath}).MustRun()
}