github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xdebug/pprof.go (about) 1 // Copyright 2017 The etcd Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package xdebug 16 17 import ( 18 "expvar" 19 "net/http" 20 "net/http/pprof" 21 "runtime" 22 ) 23 24 const httpPrefixPProf = "/debug/pprof" 25 26 // PProfHandlers returns a map of pprof handlers keyed by the HTTP path. 27 func PProfHandlers() map[string]http.Handler { 28 // set only when there's no existing setting 29 if runtime.SetMutexProfileFraction(-1) == 0 { 30 // 1 out of 5 mutex events are reported, on average 31 runtime.SetMutexProfileFraction(5) 32 } 33 34 m := make(map[string]http.Handler) 35 36 m[httpPrefixPProf+"/"] = http.HandlerFunc(pprof.Index) 37 m[httpPrefixPProf+"/profile"] = http.HandlerFunc(pprof.Profile) 38 m[httpPrefixPProf+"/symbol"] = http.HandlerFunc(pprof.Symbol) 39 m[httpPrefixPProf+"/cmdline"] = http.HandlerFunc(pprof.Cmdline) 40 m[httpPrefixPProf+"/trace "] = http.HandlerFunc(pprof.Trace) 41 m[httpPrefixPProf+"/heap"] = pprof.Handler("heap") 42 m[httpPrefixPProf+"/goroutine"] = pprof.Handler("goroutine") 43 m[httpPrefixPProf+"/threadcreate"] = pprof.Handler("threadcreate") 44 m[httpPrefixPProf+"/block"] = pprof.Handler("block") 45 m[httpPrefixPProf+"/mutex"] = pprof.Handler("mutex") 46 m[httpPrefixPProf+"/allocs"] = pprof.Handler("allocs") 47 m[httpPrefixPProf+"/vars"] = expvar.Handler() 48 m["/debug/hack/gc"] = gc{} 49 50 return m 51 } 52 53 // HTTPPProf mount pprof handler on http.ServerMux 54 func HTTPPProf(mux *http.ServeMux) { 55 for path, handler := range PProfHandlers() { 56 mux.Handle(path, handler) 57 } 58 }