platform: Add OOM Report & Crash Report

This commit is contained in:
世界
2026-04-02 16:39:34 +08:00
parent a45639370f
commit d4dacfc480
34 changed files with 2491 additions and 603 deletions
+41 -2
View File
@@ -1,13 +1,17 @@
package libbox
import (
"math"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"time"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/experimental/locale"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/service/oomkiller"
"github.com/sagernet/sing/common/byteformats"
)
@@ -22,6 +26,10 @@ var (
sCommandServerSecret string
sLogMaxLines int
sDebug bool
sCrashReportSource string
sOOMKillerEnabled bool
sOOMKillerDisabled bool
sOOMMemoryLimit int64
)
func init() {
@@ -38,9 +46,13 @@ type SetupOptions struct {
CommandServerSecret string
LogMaxLines int
Debug bool
CrashReportSource string
OomKillerEnabled bool
OomKillerDisabled bool
OomMemoryLimit int64
}
func Setup(options *SetupOptions) error {
func applySetupOptions(options *SetupOptions) {
sBasePath = options.BasePath
sWorkingPath = options.WorkingPath
sTempPath = options.TempPath
@@ -56,10 +68,33 @@ func Setup(options *SetupOptions) error {
sCommandServerSecret = options.CommandServerSecret
sLogMaxLines = options.LogMaxLines
sDebug = options.Debug
sCrashReportSource = options.CrashReportSource
ReloadSetupOptions(options)
}
func ReloadSetupOptions(options *SetupOptions) {
sOOMKillerEnabled = options.OomKillerEnabled
sOOMKillerDisabled = options.OomKillerDisabled
sOOMMemoryLimit = options.OomMemoryLimit
if sOOMKillerEnabled {
if sOOMMemoryLimit == 0 && C.IsIos {
sOOMMemoryLimit = oomkiller.DefaultAppleNetworkExtensionMemoryLimit
}
if sOOMMemoryLimit > 0 {
debug.SetMemoryLimit(sOOMMemoryLimit * 3 / 4)
} else {
debug.SetMemoryLimit(math.MaxInt64)
}
} else {
debug.SetMemoryLimit(math.MaxInt64)
}
}
func Setup(options *SetupOptions) error {
applySetupOptions(options)
os.MkdirAll(sWorkingPath, 0o777)
os.MkdirAll(sTempPath, 0o777)
return nil
return redirectStderr(filepath.Join(sWorkingPath, "CrashReport-"+sCrashReportSource+".log"))
}
func SetLocale(localeId string) {
@@ -70,6 +105,10 @@ func Version() string {
return C.Version
}
func GoVersion() string {
return runtime.Version() + ", " + runtime.GOOS + "/" + runtime.GOARCH
}
func FormatBytes(length int64) string {
return byteformats.FormatKBytes(uint64(length))
}