Add debug pprof support

This commit is contained in:
yaotthaha 2023-04-14 13:18:23 +08:00
parent 7f49c45673
commit 2eae1feeb0
5 changed files with 48 additions and 3 deletions

2
box.go
View File

@ -48,7 +48,7 @@ func New(options Options) (*Box, error) {
}
createdAt := time.Now()
experimentalOptions := common.PtrValueOrDefault(options.Experimental)
applyDebugOptions(common.PtrValueOrDefault(experimentalOptions.Debug))
applyDebugOptions(ctx, common.PtrValueOrDefault(experimentalOptions.Debug))
var needClashAPI bool
var needV2RayAPI bool
if experimentalOptions.ClashAPI != nil && experimentalOptions.ClashAPI.ExternalController != "" {

View File

@ -3,13 +3,15 @@
package box
import (
"context"
"github.com/sagernet/sing-box/experimental/pprof"
"runtime/debug"
"github.com/sagernet/sing-box/common/dialer/conntrack"
"github.com/sagernet/sing-box/option"
)
func applyDebugOptions(options option.DebugOptions) {
func applyDebugOptions(ctx context.Context, options option.DebugOptions) {
if options.GCPercent != nil {
debug.SetGCPercent(*options.GCPercent)
}
@ -32,4 +34,7 @@ func applyDebugOptions(options option.DebugOptions) {
if options.OOMKiller != nil {
conntrack.KillerEnabled = *options.OOMKiller
}
if options.Pprof != "" {
pprof.NewPprof(ctx, options.Pprof)
}
}

View File

@ -3,13 +3,15 @@
package box
import (
"context"
"github.com/sagernet/sing-box/experimental/pprof"
"runtime/debug"
"github.com/sagernet/sing-box/common/dialer/conntrack"
"github.com/sagernet/sing-box/option"
)
func applyDebugOptions(options option.DebugOptions) {
func applyDebugOptions(ctx context.Context, options option.DebugOptions) {
if options.GCPercent != nil {
debug.SetGCPercent(*options.GCPercent)
}
@ -32,4 +34,7 @@ func applyDebugOptions(options option.DebugOptions) {
if options.OOMKiller != nil {
conntrack.KillerEnabled = *options.OOMKiller
}
if options.Pprof != "" {
pprof.NewPprof(ctx, options.Pprof)
}
}

View File

@ -0,0 +1,34 @@
package pprof
import (
"context"
"github.com/sagernet/sing-box/log"
"net/http"
"net/http/pprof"
)
func NewPprof(ctx context.Context, listen string) {
server := &http.Server{}
serverMux := http.NewServeMux()
serverMux.HandleFunc("/debug/pprof/", pprof.Index)
serverMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
serverMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
serverMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
serverMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
server.Handler = serverMux
server.Addr = listen
go func() {
<-ctx.Done()
server.Shutdown(context.Background())
}()
go func() {
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatal("pprof server fail: ", err)
}
}()
}

View File

@ -14,6 +14,7 @@ type DebugOptions struct {
TraceBack string `json:"trace_back,omitempty"`
MemoryLimit BytesLength `json:"memory_limit,omitempty"`
OOMKiller *bool `json:"oom_killer,omitempty"`
Pprof string `json:"pprof,omitempty"`
}
type BytesLength int64