mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-25 18:11:28 +00:00
Show memory stats in debug
This commit is contained in:
parent
7b30815938
commit
c9226aeaaf
|
@ -5,11 +5,36 @@ package main
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/log"
|
"github.com/sagernet/sing-box/log"
|
||||||
|
|
||||||
|
"github.com/dustin/go-humanize"
|
||||||
|
"runtime/debug"
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/sagernet/sing-box/common/badjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
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() {
|
go func() {
|
||||||
err := http.ListenAndServe("0.0.0.0:8964", nil)
|
err := http.ListenAndServe("0.0.0.0:8964", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
25
cmd/sing-box/debug_linux.go
Normal file
25
cmd/sing-box/debug_linux.go
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
//go:build debug
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"runtime"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func rusageMaxRSS() float64 {
|
||||||
|
ru := syscall.Rusage{}
|
||||||
|
err := syscall.Getrusage(syscall.RUSAGE_SELF, &ru)
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
rss := float64(ru.Maxrss)
|
||||||
|
if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
|
||||||
|
rss /= 1 << 20 // ru_maxrss is bytes on darwin
|
||||||
|
} else {
|
||||||
|
// ru_maxrss is kilobytes elsewhere (linux, openbsd, etc)
|
||||||
|
rss /= 1 << 10
|
||||||
|
}
|
||||||
|
return rss
|
||||||
|
}
|
7
cmd/sing-box/debug_stub.go
Normal file
7
cmd/sing-box/debug_stub.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
//go:build debug && !linux
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
func rusageMaxRSS() float64 {
|
||||||
|
return -1
|
||||||
|
}
|
|
@ -13,7 +13,7 @@ type JSONObject struct {
|
||||||
linkedhashmap.Map[string, any]
|
linkedhashmap.Map[string, any]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *JSONObject) MarshalJSON() ([]byte, error) {
|
func (m JSONObject) MarshalJSON() ([]byte, error) {
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
buffer.WriteString("{")
|
buffer.WriteString("{")
|
||||||
items := m.Entries()
|
items := m.Entries()
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -4,6 +4,7 @@ go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/database64128/tfo-go v1.1.0
|
github.com/database64128/tfo-go v1.1.0
|
||||||
|
github.com/dustin/go-humanize v1.0.0
|
||||||
github.com/fsnotify/fsnotify v1.5.4
|
github.com/fsnotify/fsnotify v1.5.4
|
||||||
github.com/go-chi/chi/v5 v5.0.7
|
github.com/go-chi/chi/v5 v5.0.7
|
||||||
github.com/go-chi/cors v1.2.1
|
github.com/go-chi/cors v1.2.1
|
||||||
|
|
1
go.sum
1
go.sum
|
@ -24,6 +24,7 @@ github.com/database64128/tfo-go v1.1.0/go.mod h1:95pOT8bnV3P2Lmu9upHNWFHz6dYGJ9c
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
|
||||||
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
|
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
|
||||||
github.com/eycorsican/go-tun2socks v1.16.11 h1:+hJDNgisrYaGEqoSxhdikMgMJ4Ilfwm/IZDrWRrbaH8=
|
github.com/eycorsican/go-tun2socks v1.16.11 h1:+hJDNgisrYaGEqoSxhdikMgMJ4Ilfwm/IZDrWRrbaH8=
|
||||||
github.com/eycorsican/go-tun2socks v1.16.11/go.mod h1:wgB2BFT8ZaPKyKOQ/5dljMG/YIow+AIXyq4KBwJ5sGQ=
|
github.com/eycorsican/go-tun2socks v1.16.11/go.mod h1:wgB2BFT8ZaPKyKOQ/5dljMG/YIow+AIXyq4KBwJ5sGQ=
|
||||||
|
|
Loading…
Reference in a new issue