Show memory stats in debug

This commit is contained in:
世界
2022-08-11 12:41:54 +08:00
parent 7b30815938
commit c9226aeaaf
6 changed files with 60 additions and 1 deletions

View File

@@ -5,11 +5,36 @@ package main
import (
"net/http"
_ "net/http/pprof"
"runtime"
"github.com/sagernet/sing-box/log"
"github.com/dustin/go-humanize"
"runtime/debug"
"encoding/json"
"github.com/sagernet/sing-box/common/badjson"
)
func init() {
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
memObject.Put("heap", humanize.Bytes(memStats.HeapInuse))
memObject.Put("stack", humanize.Bytes(memStats.StackInuse))
memObject.Put("idle", humanize.Bytes(memStats.HeapIdle-memStats.HeapReleased))
memObject.Put("goroutines", runtime.NumGoroutine())
memObject.Put("rss", rusageMaxRSS())
encoder := json.NewEncoder(writer)
encoder.SetIndent("", " ")
encoder.Encode(memObject)
})
go func() {
err := http.ListenAndServe("0.0.0.0:8964", nil)
if err != nil {