diff --git a/cmd/kaspaminer/main.go b/cmd/kaspaminer/main.go index 44fc65ef9..e8110503d 100644 --- a/cmd/kaspaminer/main.go +++ b/cmd/kaspaminer/main.go @@ -2,8 +2,6 @@ package main import ( "fmt" - "net" - "net/http" "os" "github.com/kaspanet/kaspad/version" @@ -14,6 +12,7 @@ import ( "github.com/kaspanet/kaspad/signal" "github.com/kaspanet/kaspad/util/panics" + "github.com/kaspanet/kaspad/util/profiling" ) func main() { @@ -35,13 +34,7 @@ func main() { // Enable http profiling server if requested. if cfg.Profile != "" { - spawn(func() { - 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)) - }) + profiling.Start(cfg.Profile, log) } client, err := connectToServer(cfg) diff --git a/kaspad.go b/kaspad.go index df42c6982..b35cbcef6 100644 --- a/kaspad.go +++ b/kaspad.go @@ -6,9 +6,6 @@ package main import ( "fmt" - "github.com/pkg/errors" - "net" - "net/http" _ "net/http/pprof" "os" "path/filepath" @@ -17,6 +14,8 @@ import ( "runtime/pprof" "strings" + "github.com/pkg/errors" + "github.com/kaspanet/kaspad/blockdag/indexers" "github.com/kaspanet/kaspad/config" "github.com/kaspanet/kaspad/database" @@ -26,6 +25,7 @@ import ( "github.com/kaspanet/kaspad/signal" "github.com/kaspanet/kaspad/util/fs" "github.com/kaspanet/kaspad/util/panics" + "github.com/kaspanet/kaspad/util/profiling" "github.com/kaspanet/kaspad/version" ) @@ -72,12 +72,7 @@ func kaspadMain(serverChan chan<- *server.Server) error { // Enable http profiling server if requested. if cfg.Profile != "" { spawn(func() { - listenAddr := net.JoinHostPort("", cfg.Profile) - 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)) + profiling.Start(cfg.Profile, kasdLog) }) } diff --git a/logger/logger.go b/logger/logger.go index ac54c966b..06fe2431a 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -45,6 +45,7 @@ var ( syncLog = BackendLog.Logger("SYNC") txmpLog = BackendLog.Logger("TXMP") utilLog = BackendLog.Logger("UTIL") + profLog = BackendLog.Logger("PROF") ) // SubsystemTags is an enum of all sub system tags @@ -65,7 +66,8 @@ var SubsystemTags = struct { SRVR, SYNC, TXMP, - UTIL string + UTIL, + PROF string }{ ADXR: "ADXR", AMGR: "AMGR", @@ -84,6 +86,7 @@ var SubsystemTags = struct { SYNC: "SYNC", TXMP: "TXMP", UTIL: "UTIL", + PROF: "PROF", } // subsystemLoggers maps each subsystem identifier to its associated logger. @@ -105,6 +108,7 @@ var subsystemLoggers = map[string]*logs.Logger{ SubsystemTags.SYNC: syncLog, SubsystemTags.TXMP: txmpLog, SubsystemTags.UTIL: utilLog, + SubsystemTags.PROF: profLog, } // InitLog attaches log file and error log file to the backend log. diff --git a/util/profiling/profiling.go b/util/profiling/profiling.go new file mode 100644 index 000000000..a0fca42d2 --- /dev/null +++ b/util/profiling/profiling.go @@ -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)) + }) +}