Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

better mutex and block pprof endpoints #737

Merged
merged 1 commit into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ func NewServer() (*Server, error) {
m := macaron.New()
m.Use(macaron.Logger())
m.Use(macaron.Recovery())
// route pprof to where it belongs
// route pprof to where it belongs, except for our own extensions
m.Use(func(ctx *macaron.Context) {
if strings.HasPrefix(ctx.Req.URL.Path, "/debug/") {
if strings.HasPrefix(ctx.Req.URL.Path, "/debug/") &&
!strings.HasPrefix(ctx.Req.URL.Path, "/debug/pprof/block") &&
!strings.HasPrefix(ctx.Req.URL.Path, "/debug/pprof/mutex") {
http.DefaultServeMux.ServeHTTP(ctx.Resp, ctx.Req.Request)
}
})
Expand Down
59 changes: 59 additions & 0 deletions api/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package api

import (
"net/http"
"runtime"
rpprof "runtime/pprof"
"strconv"
"time"
)

// blockhandler writes out a blocking profile
// similar to the standard library handler,
// except it allows to specify a rate.
// The profiler aims to sample an average of one blocking event
// per rate nanoseconds spent blocked.
//
// To include every blocking event in the profile, pass rate = 1.
// Defaults to 10k (10 microseconds)
func blockHandler(w http.ResponseWriter, r *http.Request) {
debug, _ := strconv.Atoi(r.FormValue("debug"))
sec, _ := strconv.ParseInt(r.FormValue("seconds"), 10, 64)
if sec == 0 {
sec = 30
}
rate, _ := strconv.Atoi(r.FormValue("rate"))
if rate == 0 {
rate = 10000
}

w.Header().Set("Content-Type", "application/octet-stream")
runtime.SetBlockProfileRate(rate)
time.Sleep(time.Duration(sec) * time.Second)
runtime.SetBlockProfileRate(0)
p := rpprof.Lookup("block")
p.WriteTo(w, debug)
}

// mutexHandler writes out a mutex profile similar to the
// standard library handler,
// except it allows to specify a mutex profiling rate.
// On average 1/rate events are reported. The default is 1000
func mutexHandler(w http.ResponseWriter, r *http.Request) {
debug, _ := strconv.Atoi(r.FormValue("debug"))
sec, _ := strconv.ParseInt(r.FormValue("seconds"), 10, 64)
if sec == 0 {
sec = 30
}
rate, _ := strconv.Atoi(r.FormValue("rate"))
if rate == 0 {
rate = 1000
}

w.Header().Set("Content-Type", "application/octet-stream")
runtime.SetMutexProfileFraction(rate)
time.Sleep(time.Duration(sec) * time.Second)
runtime.SetMutexProfileFraction(0)
p := rpprof.Lookup("mutex")
p.WriteTo(w, debug)
}
2 changes: 2 additions & 0 deletions api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func (s *Server) RegisterRoutes() {
r.Get("/", s.appStatus)
r.Get("/node", s.getNodeStatus)
r.Post("/node", bind(models.NodeStatus{}), s.setNodeStatus)
r.Get("/debug/pprof/block", blockHandler)
r.Get("/debug/pprof/mutex", mutexHandler)

r.Get("/cluster", s.getClusterStatus)
r.Post("/cluster", bind(models.ClusterMembers{}), s.postClusterMembers)
Expand Down