| 
									
										
										
										
											2019-08-21 16:08:55 -04:00
										 |  |  | // Package pprof implements a debug endpoint for getting profiles using the
 | 
					
						
							| 
									
										
										
										
											2016-09-25 08:39:20 +01:00
										 |  |  | // go pprof tooling.
 | 
					
						
							| 
									
										
										
										
											2016-04-28 10:26:58 +01:00
										 |  |  | package pprof
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2016-04-28 10:26:58 +01:00
										 |  |  | 	"net/http"
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 	pp "net/http/pprof"
 | 
					
						
							| 
									
										
										
										
											2019-03-29 02:37:17 -04:00
										 |  |  | 	"runtime"
 | 
					
						
							| 
									
										
										
										
											2019-11-17 23:58:00 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/reuseport"
 | 
					
						
							| 
									
										
										
										
											2016-04-28 10:26:58 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | type handler struct {
 | 
					
						
							| 
									
										
										
										
											2019-03-29 02:37:17 -04:00
										 |  |  | 	addr     string
 | 
					
						
							|  |  |  | 	rateBloc int
 | 
					
						
							|  |  |  | 	ln       net.Listener
 | 
					
						
							|  |  |  | 	mux      *http.ServeMux
 | 
					
						
							| 
									
										
										
										
											2016-04-28 10:26:58 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | func (h *handler) Startup() error {
 | 
					
						
							| 
									
										
										
										
											2019-11-17 23:58:00 -08:00
										 |  |  | 	// Reloading the plugin without changing the listening address results
 | 
					
						
							|  |  |  | 	// in an error unless we reuse the port because Startup is called for
 | 
					
						
							|  |  |  | 	// new handlers before Shutdown is called for the old ones.
 | 
					
						
							|  |  |  | 	ln, err := reuseport.Listen("tcp", h.addr)
 | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2018-04-19 07:41:56 +01:00
										 |  |  | 		log.Errorf("Failed to start pprof handler: %s", err)
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 		return err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | 	h.ln = ln
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 	h.mux = http.NewServeMux()
 | 
					
						
							| 
									
										
										
										
											2019-12-07 03:04:49 +08:00
										 |  |  | 	h.mux.HandleFunc(path, func(rw http.ResponseWriter, req *http.Request) {
 | 
					
						
							|  |  |  | 		http.Redirect(rw, req, path+"/", http.StatusFound)
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 	h.mux.HandleFunc(path+"/", pp.Index)
 | 
					
						
							|  |  |  | 	h.mux.HandleFunc(path+"/cmdline", pp.Cmdline)
 | 
					
						
							|  |  |  | 	h.mux.HandleFunc(path+"/profile", pp.Profile)
 | 
					
						
							|  |  |  | 	h.mux.HandleFunc(path+"/symbol", pp.Symbol)
 | 
					
						
							|  |  |  | 	h.mux.HandleFunc(path+"/trace", pp.Trace)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 02:37:17 -04:00
										 |  |  | 	runtime.SetBlockProfileRate(h.rateBloc)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 10:26:58 +01:00
										 |  |  | 	go func() {
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 		http.Serve(h.ln, h.mux)
 | 
					
						
							| 
									
										
										
										
											2016-04-28 10:26:58 +01:00
										 |  |  | 	}()
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | func (h *handler) Shutdown() error {
 | 
					
						
							| 
									
										
										
										
											2016-04-29 07:28:35 +01:00
										 |  |  | 	if h.ln != nil {
 | 
					
						
							|  |  |  | 		return h.ln.Close()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	path = "/debug/pprof"
 | 
					
						
							|  |  |  | )
 |