diff --git a/CHANGELOG.md b/CHANGELOG.md index 480449f3..353e559e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ NOTE: Add new changes BELOW THIS COMMENT. ### Added +- Better profiling information when `debug_pprof` is set to `true`. - IPv6 support in Safe Search for some services. - The ability to make bootstrap DNS lookups prefer IPv6 addresses to IPv4 ones using the new `dns.bootstrap_prefer_ipv6` configuration file property diff --git a/internal/home/home.go b/internal/home/home.go index 2e4751c5..d777a5e9 100644 --- a/internal/home/home.go +++ b/internal/home/home.go @@ -9,7 +9,6 @@ import ( "io/fs" "net" "net/http" - "net/http/pprof" "net/netip" "net/url" "os" @@ -470,26 +469,8 @@ func run(opts options, clientBuildFS fs.FS) { fatalOnError(err) if config.DebugPProf { - mux := http.NewServeMux() - mux.HandleFunc("/debug/pprof/", pprof.Index) - mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) - mux.HandleFunc("/debug/pprof/profile", pprof.Profile) - mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) - mux.HandleFunc("/debug/pprof/trace", pprof.Trace) - - // See profileSupportsDelta in src/net/http/pprof/pprof.go. - mux.Handle("/debug/pprof/allocs", pprof.Handler("allocs")) - mux.Handle("/debug/pprof/block", pprof.Handler("block")) - mux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine")) - mux.Handle("/debug/pprof/heap", pprof.Handler("heap")) - mux.Handle("/debug/pprof/mutex", pprof.Handler("mutex")) - mux.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate")) - - go func() { - log.Info("pprof: listening on localhost:6060") - lerr := http.ListenAndServe("localhost:6060", mux) - log.Error("Error while running the pprof server: %s", lerr) - }() + // TODO(a.garipov): Make the address configurable. + startPprof("localhost:6060") } } diff --git a/internal/home/pprof.go b/internal/home/pprof.go new file mode 100644 index 00000000..b8ef8e74 --- /dev/null +++ b/internal/home/pprof.go @@ -0,0 +1,39 @@ +package home + +import ( + "net/http" + "net/http/pprof" + "runtime" + + "github.com/AdguardTeam/golibs/log" +) + +// startPprof launches the debug and profiling server on addr. +func startPprof(addr string) { + runtime.SetBlockProfileRate(1) + runtime.SetMutexProfileFraction(1) + + mux := http.NewServeMux() + + mux.HandleFunc("/debug/pprof/", pprof.Index) + mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) + mux.HandleFunc("/debug/pprof/profile", pprof.Profile) + mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) + mux.HandleFunc("/debug/pprof/trace", pprof.Trace) + + // See profileSupportsDelta in src/net/http/pprof/pprof.go. + mux.Handle("/debug/pprof/allocs", pprof.Handler("allocs")) + mux.Handle("/debug/pprof/block", pprof.Handler("block")) + mux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine")) + mux.Handle("/debug/pprof/heap", pprof.Handler("heap")) + mux.Handle("/debug/pprof/mutex", pprof.Handler("mutex")) + mux.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate")) + + go func() { + defer log.OnPanic("pprof server") + + log.Info("pprof: listening on %q", addr) + err := http.ListenAndServe(addr, mux) + log.Info("pprof server errors: %v", err) + }() +}