Files
sing-box-extended-mirror/cmd/sing-box/debug.go
T

45 lines
1.1 KiB
Go
Raw Normal View History

2022-07-30 14:50:33 +08:00
//go:build debug
package main
import (
2022-08-11 23:59:22 +08:00
"encoding/json"
2022-07-30 14:50:33 +08:00
"net/http"
_ "net/http/pprof"
2022-08-11 12:41:54 +08:00
"runtime"
2022-08-11 23:59:22 +08:00
"runtime/debug"
2022-08-09 14:49:17 +08:00
2022-08-11 23:59:22 +08:00
"github.com/sagernet/sing-box/common/badjson"
2022-08-09 14:49:17 +08:00
"github.com/sagernet/sing-box/log"
2022-08-11 12:41:54 +08:00
"github.com/dustin/go-humanize"
2022-07-30 14:50:33 +08:00
)
func init() {
2022-08-11 12:41:54 +08:00
http.HandleFunc("/debug/gc", func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusNoContent)
go debug.FreeOSMemory()
})
http.HandleFunc("/debug/memory", func(writer http.ResponseWriter, request *http.Request) {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
var memObject badjson.JSONObject
2023-03-16 11:15:55 +08:00
memObject.Put("heap", humanize.IBytes(memStats.HeapInuse))
memObject.Put("stack", humanize.IBytes(memStats.StackInuse))
memObject.Put("idle", humanize.IBytes(memStats.HeapIdle-memStats.HeapReleased))
2022-08-11 12:41:54 +08:00
memObject.Put("goroutines", runtime.NumGoroutine())
memObject.Put("rss", rusageMaxRSS())
encoder := json.NewEncoder(writer)
encoder.SetIndent("", " ")
encoder.Encode(memObject)
})
2022-08-09 14:49:17 +08:00
go func() {
err := http.ListenAndServe("0.0.0.0:8964", nil)
if err != nil {
log.Debug(err)
}
}()
2022-07-30 14:50:33 +08:00
}