func init() { f, err := os.Create("cpu.prof") if err != nil { panic(err) } err = pprof.StartCPUProfile(f) if err != nil { panic(err) } go func() { tick := time.Tick(time.Minute) for { select { case signal := <-profileCleanup: pprof.StopCPUProfile() for _, profile := range pprof.Profiles() { f, err := os.Create(profile.Name() + ".prof") if err != nil { panic(err) } err = profile.WriteTo(f, map[string]int{ "threadcreate": 1, "goroutine": 2, }[profile.Name()]) if err != nil { panic(err) } f.Close() } if signal == os.Interrupt { os.Exit(0) } else { clientCanExit <- struct{}{} } case <-tick: for _, profile := range pprof.Profiles() { f, err := os.Create(profile.Name() + ".prof") if err != nil { panic(err) } err = profile.WriteTo(f, map[string]int{ "threadcreate": 1, "goroutine": 2, }[profile.Name()]) if err != nil { panic(err) } f.Close() } } } }() signal.Notify(profileCleanup, os.Interrupt) }
// URL /profile/ func profile(req *Request) (int, interface{}) { r := JSON{} for _, p := range pprof.Profiles() { r[p.Name()] = p.Count() } return http.StatusOK, r }
func handler() http.Handler { info := struct { Profiles []*pprof.Profile Token string }{ Profiles: pprof.Profiles(), } h := func(w http.ResponseWriter, r *http.Request) { // get the last path, allowing this to be mounted under any prefix split := strings.Split(r.URL.Path, "/") name := split[len(split)-1] switch name { case "": // Index page. if err := indexTmpl.Execute(w, info); err != nil { fmt.Fprintf(w, "something went wrong - %s", err) return } case "cmdline": nhpprof.Cmdline(w, r) case "profile": nhpprof.Profile(w, r) case "trace": nhpprof.Trace(w, r) case "symbol": nhpprof.Symbol(w, r) default: // Provides access to all profiles under runtime/pprof nhpprof.Handler(name).ServeHTTP(w, r) } } return http.HandlerFunc(h) }
func main() { logger.Println("Start...") // profs := pprof.Profiles() for _, p := range profs { logger.Println(p.Name()) } logger.Println("End...") }
// Index responds with the pprof-formatted profile named by the request. // For example, "/debug/pprof/heap" serves the "heap" profile. // Index responds to a request for "/debug/pprof/" with an HTML page // listing the available profiles. func Index(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(r.URL.Path, "/debug/pprof/") { name := strings.TrimPrefix(r.URL.Path, "/debug/pprof/") if name != "" { handler(name).ServeHTTP(w, r) return } } profiles := pprof.Profiles() if err := indexTmpl.Execute(w, profiles); err != nil { log.Print(err) } }
// ReadDirAll implements the ReadDirAll interface. func (pl ProfileList) ReadDirAll(_ context.Context) (res []fuse.Dirent, err error) { profiles := pprof.Profiles() res = make([]fuse.Dirent, 0, len(profiles)) for _, p := range profiles { name := p.Name() if !libfs.IsSupportedProfileName(name) { continue } res = append(res, fuse.Dirent{ Type: fuse.DT_Dir, Name: name, }) } return res, nil }
// Index responds with the pprof-formatted profile named by the request. // For example, "/debug/pprof/heap" serves the "heap" profile. // Index responds to a request for "/debug/pprof/" with an HTML page // listing the available profiles. func Index(e echo.Context) { r := e.Request() w := e.Response() if strings.HasPrefix(r.URL().Path(), "/debug/pprof/") { name := strings.TrimPrefix(r.URL().Path(), "/debug/pprof/") if name != "" { handler(name).ServeHTTP(e) return } } profiles := pprof.Profiles() if err := indexTmpl.Execute(w, profiles); err != nil { log.Print(err) } }
// Index responds with the pprof-formatted profile named by the request. // For example, "/debug/pprof/heap" serves the "heap" profile. // Index responds to a request for "/debug/pprof/" with an HTML page // listing the available profiles. func Index(request *http.Request, pathFragments map[string]string, reply *web.Reply) { if strings.HasPrefix(request.URL.Path, "/debug/pprof/") { name := strings.TrimPrefix(request.URL.Path, "/debug/pprof/") if name != "" { handler(name).RequestHandler(request, pathFragments, reply) return } } r, w := io.Pipe() go func() { defer w.Close() profiles := pprof.Profiles() if err := indexTmpl.Execute(w, profiles); err != nil { logger.Error("出现错误:%s", err) } }() reply.With(r) }
// ServeHTTP serves http request. func (h DebugIndexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(r.URL.Path, h.Path) { name := strings.TrimPrefix(r.URL.Path, h.Path) if name != "" { debugHandler(name).ServeHTTP(w, r) return } } profiles := pprof.Profiles() if err := debugIndexTmpl.Execute(w, struct { Profiles []*pprof.Profile Path string }{ Profiles: profiles, Path: h.Path, }); err != nil { log.Printf("debug intex handler: %s", err) } }
// See pprof_remote_servers.html bundled with the gperftools. func (p Profiler) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "cmdline": for _, arg := range os.Args { fmt.Fprintf(w, "%v\n", arg) } case "profile": sec := r.URL.Query()["seconds"] if len(sec) > 0 { dur, _ := strconv.Atoi(sec[0]) buf := new(bytes.Buffer) pprof.StartCPUProfile(buf) time.Sleep(time.Duration(dur) * time.Second) pprof.StopCPUProfile() buf.WriteTo(w) } else { webFail(w, "invalid profile request, expected seconds=XX") } case "memstats": var m runtime.MemStats runtime.ReadMemStats(&m) buf, err := json.MarshalIndent(m, "", " ") if err != nil { webFail(w, "failed to marshal object: %v", err) } else { w.Write(buf) } case "symbol": if r.Method == "GET" { fmt.Fprintf(w, "num_symbols: 1") return } buf, err := ioutil.ReadAll(r.Body) if err != nil { webFail(w, "failed to read request body: %v", err) return } for _, strAddr := range strings.Split(string(buf), "+") { strAddr = strings.Trim(strAddr, " \r\n\t") desc := "unknownFunc" addr, err := strconv.ParseUint(strAddr, 0, 64) if err == nil { fn := runtime.FuncForPC(uintptr(addr)) if fn != nil { file, line := fn.FileLine(uintptr(addr)) desc = fmt.Sprintf("%v:%v:%v", path.Base(file), line, fn.Name()) } } fmt.Fprintf(w, "%v\t%v\n", strAddr, desc) } case "": for _, p := range pprof.Profiles() { fmt.Fprintf(w, "%v\n", p.Name()) } default: for _, p := range pprof.Profiles() { if p.Name() == r.URL.Path { p.WriteTo(w, 0) return } } webFail(w, "unknown profile: %v", r.URL.Path) } }
func profiles() { for _, p := range pprof.Profiles() { fmt.Println(p.Name()) } }