mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
Add debug pprof support
This commit is contained in:
parent
7f49c45673
commit
2eae1feeb0
2
box.go
2
box.go
@ -48,7 +48,7 @@ func New(options Options) (*Box, error) {
|
|||||||
}
|
}
|
||||||
createdAt := time.Now()
|
createdAt := time.Now()
|
||||||
experimentalOptions := common.PtrValueOrDefault(options.Experimental)
|
experimentalOptions := common.PtrValueOrDefault(options.Experimental)
|
||||||
applyDebugOptions(common.PtrValueOrDefault(experimentalOptions.Debug))
|
applyDebugOptions(ctx, common.PtrValueOrDefault(experimentalOptions.Debug))
|
||||||
var needClashAPI bool
|
var needClashAPI bool
|
||||||
var needV2RayAPI bool
|
var needV2RayAPI bool
|
||||||
if experimentalOptions.ClashAPI != nil && experimentalOptions.ClashAPI.ExternalController != "" {
|
if experimentalOptions.ClashAPI != nil && experimentalOptions.ClashAPI.ExternalController != "" {
|
||||||
|
7
debug.go
7
debug.go
@ -3,13 +3,15 @@
|
|||||||
package box
|
package box
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/sagernet/sing-box/experimental/pprof"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/common/dialer/conntrack"
|
"github.com/sagernet/sing-box/common/dialer/conntrack"
|
||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
)
|
)
|
||||||
|
|
||||||
func applyDebugOptions(options option.DebugOptions) {
|
func applyDebugOptions(ctx context.Context, options option.DebugOptions) {
|
||||||
if options.GCPercent != nil {
|
if options.GCPercent != nil {
|
||||||
debug.SetGCPercent(*options.GCPercent)
|
debug.SetGCPercent(*options.GCPercent)
|
||||||
}
|
}
|
||||||
@ -32,4 +34,7 @@ func applyDebugOptions(options option.DebugOptions) {
|
|||||||
if options.OOMKiller != nil {
|
if options.OOMKiller != nil {
|
||||||
conntrack.KillerEnabled = *options.OOMKiller
|
conntrack.KillerEnabled = *options.OOMKiller
|
||||||
}
|
}
|
||||||
|
if options.Pprof != "" {
|
||||||
|
pprof.NewPprof(ctx, options.Pprof)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,15 @@
|
|||||||
package box
|
package box
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/sagernet/sing-box/experimental/pprof"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/common/dialer/conntrack"
|
"github.com/sagernet/sing-box/common/dialer/conntrack"
|
||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
)
|
)
|
||||||
|
|
||||||
func applyDebugOptions(options option.DebugOptions) {
|
func applyDebugOptions(ctx context.Context, options option.DebugOptions) {
|
||||||
if options.GCPercent != nil {
|
if options.GCPercent != nil {
|
||||||
debug.SetGCPercent(*options.GCPercent)
|
debug.SetGCPercent(*options.GCPercent)
|
||||||
}
|
}
|
||||||
@ -32,4 +34,7 @@ func applyDebugOptions(options option.DebugOptions) {
|
|||||||
if options.OOMKiller != nil {
|
if options.OOMKiller != nil {
|
||||||
conntrack.KillerEnabled = *options.OOMKiller
|
conntrack.KillerEnabled = *options.OOMKiller
|
||||||
}
|
}
|
||||||
|
if options.Pprof != "" {
|
||||||
|
pprof.NewPprof(ctx, options.Pprof)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
34
experimental/pprof/pprof.go
Normal file
34
experimental/pprof/pprof.go
Normal 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)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
@ -14,6 +14,7 @@ type DebugOptions struct {
|
|||||||
TraceBack string `json:"trace_back,omitempty"`
|
TraceBack string `json:"trace_back,omitempty"`
|
||||||
MemoryLimit BytesLength `json:"memory_limit,omitempty"`
|
MemoryLimit BytesLength `json:"memory_limit,omitempty"`
|
||||||
OOMKiller *bool `json:"oom_killer,omitempty"`
|
OOMKiller *bool `json:"oom_killer,omitempty"`
|
||||||
|
Pprof string `json:"pprof,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type BytesLength int64
|
type BytesLength int64
|
||||||
|
Loading…
x
Reference in New Issue
Block a user