[NOD-857] Add generalized profiler package and use it everwhere (#679)

* [NOD-857] Add generalized profiler package and use it everwhere

* [NOD-857] Dependency-inject log into profiling.Start()
This commit is contained in:
Svarog 2020-03-31 12:41:21 +03:00 committed by GitHub
parent 6aa5e0b5a8
commit 024edc30a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 19 deletions

View File

@ -2,8 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"net"
"net/http"
"os" "os"
"github.com/kaspanet/kaspad/version" "github.com/kaspanet/kaspad/version"
@ -14,6 +12,7 @@ import (
"github.com/kaspanet/kaspad/signal" "github.com/kaspanet/kaspad/signal"
"github.com/kaspanet/kaspad/util/panics" "github.com/kaspanet/kaspad/util/panics"
"github.com/kaspanet/kaspad/util/profiling"
) )
func main() { func main() {
@ -35,13 +34,7 @@ func main() {
// Enable http profiling server if requested. // Enable http profiling server if requested.
if cfg.Profile != "" { if cfg.Profile != "" {
spawn(func() { profiling.Start(cfg.Profile, log)
listenAddr := net.JoinHostPort("", cfg.Profile)
log.Infof("Profile server listening on %s", listenAddr)
profileRedirect := http.RedirectHandler("/debug/pprof", http.StatusSeeOther)
http.Handle("/", profileRedirect)
log.Errorf("%s", http.ListenAndServe(listenAddr, nil))
})
} }
client, err := connectToServer(cfg) client, err := connectToServer(cfg)

View File

@ -6,9 +6,6 @@ package main
import ( import (
"fmt" "fmt"
"github.com/pkg/errors"
"net"
"net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"path/filepath" "path/filepath"
@ -17,6 +14,8 @@ import (
"runtime/pprof" "runtime/pprof"
"strings" "strings"
"github.com/pkg/errors"
"github.com/kaspanet/kaspad/blockdag/indexers" "github.com/kaspanet/kaspad/blockdag/indexers"
"github.com/kaspanet/kaspad/config" "github.com/kaspanet/kaspad/config"
"github.com/kaspanet/kaspad/database" "github.com/kaspanet/kaspad/database"
@ -26,6 +25,7 @@ import (
"github.com/kaspanet/kaspad/signal" "github.com/kaspanet/kaspad/signal"
"github.com/kaspanet/kaspad/util/fs" "github.com/kaspanet/kaspad/util/fs"
"github.com/kaspanet/kaspad/util/panics" "github.com/kaspanet/kaspad/util/panics"
"github.com/kaspanet/kaspad/util/profiling"
"github.com/kaspanet/kaspad/version" "github.com/kaspanet/kaspad/version"
) )
@ -72,12 +72,7 @@ func kaspadMain(serverChan chan<- *server.Server) error {
// Enable http profiling server if requested. // Enable http profiling server if requested.
if cfg.Profile != "" { if cfg.Profile != "" {
spawn(func() { spawn(func() {
listenAddr := net.JoinHostPort("", cfg.Profile) profiling.Start(cfg.Profile, kasdLog)
kasdLog.Infof("Profile server listening on %s", listenAddr)
profileRedirect := http.RedirectHandler("/debug/pprof",
http.StatusSeeOther)
http.Handle("/", profileRedirect)
kasdLog.Errorf("%s", http.ListenAndServe(listenAddr, nil))
}) })
} }

View File

@ -45,6 +45,7 @@ var (
syncLog = BackendLog.Logger("SYNC") syncLog = BackendLog.Logger("SYNC")
txmpLog = BackendLog.Logger("TXMP") txmpLog = BackendLog.Logger("TXMP")
utilLog = BackendLog.Logger("UTIL") utilLog = BackendLog.Logger("UTIL")
profLog = BackendLog.Logger("PROF")
) )
// SubsystemTags is an enum of all sub system tags // SubsystemTags is an enum of all sub system tags
@ -65,7 +66,8 @@ var SubsystemTags = struct {
SRVR, SRVR,
SYNC, SYNC,
TXMP, TXMP,
UTIL string UTIL,
PROF string
}{ }{
ADXR: "ADXR", ADXR: "ADXR",
AMGR: "AMGR", AMGR: "AMGR",
@ -84,6 +86,7 @@ var SubsystemTags = struct {
SYNC: "SYNC", SYNC: "SYNC",
TXMP: "TXMP", TXMP: "TXMP",
UTIL: "UTIL", UTIL: "UTIL",
PROF: "PROF",
} }
// subsystemLoggers maps each subsystem identifier to its associated logger. // subsystemLoggers maps each subsystem identifier to its associated logger.
@ -105,6 +108,7 @@ var subsystemLoggers = map[string]*logs.Logger{
SubsystemTags.SYNC: syncLog, SubsystemTags.SYNC: syncLog,
SubsystemTags.TXMP: txmpLog, SubsystemTags.TXMP: txmpLog,
SubsystemTags.UTIL: utilLog, SubsystemTags.UTIL: utilLog,
SubsystemTags.PROF: profLog,
} }
// InitLog attaches log file and error log file to the backend log. // InitLog attaches log file and error log file to the backend log.

View File

@ -0,0 +1,24 @@
package profiling
import (
"net"
"net/http"
// Required for profiling
_ "net/http/pprof"
"github.com/kaspanet/kaspad/logs"
"github.com/kaspanet/kaspad/util/panics"
)
// Start starts the profiling server
func Start(port string, log *logs.Logger) {
spawn := panics.GoroutineWrapperFunc(log)
spawn(func() {
listenAddr := net.JoinHostPort("", port)
log.Infof("Profile server listening on %s", listenAddr)
profileRedirect := http.RedirectHandler("/debug/pprof", http.StatusSeeOther)
http.Handle("/", profileRedirect)
log.Error(http.ListenAndServe(listenAddr, nil))
})
}