Skip to content

Commit

Permalink
Improve conntrack
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Sep 20, 2023
1 parent 85c8f00 commit 12dd1ac
Show file tree
Hide file tree
Showing 22 changed files with 223 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewConn(conn net.Conn) (net.Conn, error) {
element := openConnection.PushBack(conn)
connAccess.Unlock()
if KillerEnabled {
err := killerCheck()
err := KillerCheck()
if err != nil {
conn.Close()
return nil, err
Expand Down
11 changes: 4 additions & 7 deletions common/dialer/conntrack/killer.go → common/conntrack/killer.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package conntrack

import (
"runtime"
runtimeDebug "runtime/debug"
"time"

E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/memory"
)

var (
KillerEnabled bool
MemoryLimit int64
MemoryLimit uint64
killerLastCheck time.Time
)

func killerCheck() error {
func KillerCheck() error {
if !KillerEnabled {
return nil
}
Expand All @@ -23,10 +23,7 @@ func killerCheck() error {
return nil
}
killerLastCheck = nowTime
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
inuseMemory := int64(memStats.StackInuse + memStats.HeapInuse + memStats.HeapIdle - memStats.HeapReleased)
if inuseMemory > MemoryLimit {
if memory.Total() > MemoryLimit {
Close()
go func() {
time.Sleep(time.Second)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewPacketConn(conn net.PacketConn) (net.PacketConn, error) {
element := openConnection.PushBack(conn)
connAccess.Unlock()
if KillerEnabled {
err := killerCheck()
err := KillerCheck()
if err != nil {
conn.Close()
return nil, err
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion common/dialer/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/dialer/conntrack"
"github.com/sagernet/sing-box/common/conntrack"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common/control"
Expand Down
158 changes: 158 additions & 0 deletions common/humanize/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package humanize

import (
"fmt"
"math"
"strconv"
"strings"
"unicode"
)

// IEC Sizes.
// kibis of bits
const (
Byte = 1 << (iota * 10)
KiByte
MiByte
GiByte
TiByte
PiByte
EiByte
)

// SI Sizes.
const (
IByte = 1
KByte = IByte * 1000
MByte = KByte * 1000
GByte = MByte * 1000
TByte = GByte * 1000
PByte = TByte * 1000
EByte = PByte * 1000
)

var defaultSizeTable = map[string]uint64{
"b": Byte,
"kib": KiByte,
"kb": KByte,
"mib": MiByte,
"mb": MByte,
"gib": GiByte,
"gb": GByte,
"tib": TiByte,
"tb": TByte,
"pib": PiByte,
"pb": PByte,
"eib": EiByte,
"eb": EByte,
// Without suffix
"": Byte,
"ki": KiByte,
"k": KByte,
"mi": MiByte,
"m": MByte,
"gi": GiByte,
"g": GByte,
"ti": TiByte,
"t": TByte,
"pi": PiByte,
"p": PByte,
"ei": EiByte,
"e": EByte,
}

var memorysSizeTable = map[string]uint64{
"b": Byte,
"kb": KiByte,
"mb": MiByte,
"gb": GiByte,
"tb": TiByte,
"pb": PiByte,
"eb": EiByte,
"": Byte,
"k": KiByte,
"m": MiByte,
"g": GiByte,
"t": TiByte,
"p": PiByte,
"e": EiByte,
}

var (
defaultSizes = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}
iSizes = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
)

func Bytes(s uint64) string {
return humanateBytes(s, 1000, defaultSizes)
}

func MemoryBytes(s uint64) string {
return humanateBytes(s, 1024, defaultSizes)
}

func IBytes(s uint64) string {
return humanateBytes(s, 1024, iSizes)
}

func logn(n, b float64) float64 {
return math.Log(n) / math.Log(b)
}

func humanateBytes(s uint64, base float64, sizes []string) string {
if s < 10 {
return fmt.Sprintf("%d B", s)
}
e := math.Floor(logn(float64(s), base))
suffix := sizes[int(e)]
val := math.Floor(float64(s)/math.Pow(base, e)*10+0.5) / 10
f := "%.0f %s"
if val < 10 {
f = "%.1f %s"
}

return fmt.Sprintf(f, val, suffix)
}

func ParseBytes(s string) (uint64, error) {
return parseBytes0(s, defaultSizeTable)
}

func ParseMemoryBytes(s string) (uint64, error) {
return parseBytes0(s, memorysSizeTable)
}

func parseBytes0(s string, sizeTable map[string]uint64) (uint64, error) {
lastDigit := 0
hasComma := false
for _, r := range s {
if !(unicode.IsDigit(r) || r == '.' || r == ',') {
break
}
if r == ',' {
hasComma = true
}
lastDigit++
}

num := s[:lastDigit]
if hasComma {
num = strings.Replace(num, ",", "", -1)
}

f, err := strconv.ParseFloat(num, 64)
if err != nil {
return 0, err
}

extra := strings.ToLower(strings.TrimSpace(s[lastDigit:]))
if m, ok := sizeTable[extra]; ok {
f *= float64(m)
if f >= math.MaxUint64 {
return 0, fmt.Errorf("too large: %v", s)
}
return uint64(f), nil
}

return 0, fmt.Errorf("unhandled size name: %v", extra)
}
4 changes: 2 additions & 2 deletions debug_go118.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package box
import (
"runtime/debug"

"github.com/sagernet/sing-box/common/dialer/conntrack"
"github.com/sagernet/sing-box/common/conntrack"
"github.com/sagernet/sing-box/option"
)

Expand All @@ -28,7 +28,7 @@ func applyDebugOptions(options option.DebugOptions) {
}
if options.MemoryLimit != 0 {
// debug.SetMemoryLimit(int64(options.MemoryLimit))
conntrack.MemoryLimit = int64(options.MemoryLimit)
conntrack.MemoryLimit = uint64(options.MemoryLimit)
}
if options.OOMKiller != nil {
conntrack.KillerEnabled = *options.OOMKiller
Expand Down
4 changes: 2 additions & 2 deletions debug_go119.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package box
import (
"runtime/debug"

"github.com/sagernet/sing-box/common/dialer/conntrack"
"github.com/sagernet/sing-box/common/conntrack"
"github.com/sagernet/sing-box/option"
)

Expand All @@ -28,7 +28,7 @@ func applyDebugOptions(options option.DebugOptions) {
}
if options.MemoryLimit != 0 {
debug.SetMemoryLimit(int64(options.MemoryLimit))
conntrack.MemoryLimit = int64(options.MemoryLimit)
conntrack.MemoryLimit = uint64(options.MemoryLimit)
}
if options.OOMKiller != nil {
conntrack.KillerEnabled = *options.OOMKiller
Expand Down
8 changes: 4 additions & 4 deletions debug_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"runtime/debug"

"github.com/sagernet/sing-box/common/badjson"
"github.com/sagernet/sing-box/common/humanize"
"github.com/sagernet/sing-box/common/json"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
E "github.com/sagernet/sing/common/exceptions"

"github.com/dustin/go-humanize"
"github.com/go-chi/chi/v5"
)

Expand All @@ -37,9 +37,9 @@ func applyDebugListenOption(options option.DebugOptions) {
runtime.ReadMemStats(&memStats)

var memObject badjson.JSONObject
memObject.Put("heap", humanize.IBytes(memStats.HeapInuse))
memObject.Put("stack", humanize.IBytes(memStats.StackInuse))
memObject.Put("idle", humanize.IBytes(memStats.HeapIdle-memStats.HeapReleased))
memObject.Put("heap", humanize.MemoryBytes(memStats.HeapInuse))
memObject.Put("stack", humanize.MemoryBytes(memStats.StackInuse))
memObject.Put("idle", humanize.MemoryBytes(memStats.HeapIdle-memStats.HeapReleased))
memObject.Put("goroutines", runtime.NumGoroutine())
memObject.Put("rss", rusageMaxRSS())

Expand Down
2 changes: 1 addition & 1 deletion experimental/libbox/command_conntrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
runtimeDebug "runtime/debug"
"time"

"github.com/sagernet/sing-box/common/dialer/conntrack"
"github.com/sagernet/sing-box/common/conntrack"
)

func (c *CommandClient) CloseConnections() error {
Expand Down
8 changes: 4 additions & 4 deletions experimental/libbox/command_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"runtime"
"time"

"github.com/sagernet/sing-box/common/dialer/conntrack"
"github.com/sagernet/sing-box/common/conntrack"
"github.com/sagernet/sing-box/experimental/clashapi"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/memory"
)

type StatusMessage struct {
Memory int64
MemoryInuse int64
Goroutines int32
ConnectionsIn int32
ConnectionsOut int32
Expand All @@ -24,10 +26,8 @@ type StatusMessage struct {
}

func (s *CommandServer) readStatus() StatusMessage {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
var message StatusMessage
message.Memory = int64(memStats.StackInuse + memStats.HeapInuse + memStats.HeapIdle - memStats.HeapReleased)
message.Memory = int64(memory.Inuse())
message.Goroutines = int32(runtime.NumGoroutine())
message.ConnectionsOut = int32(conntrack.Count())

Expand Down
7 changes: 4 additions & 3 deletions experimental/libbox/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"math"
runtimeDebug "runtime/debug"

"github.com/sagernet/sing-box/common/dialer/conntrack"
"github.com/sagernet/sing-box/common/conntrack"
)

func SetMemoryLimit(enabled bool) {
const memoryLimit = 30 * 1024 * 1024
const memoryLimit = 45 * 1024 * 1024
const memoryLimitGo = memoryLimit / 1.5
if enabled {
runtimeDebug.SetGCPercent(10)
runtimeDebug.SetMemoryLimit(memoryLimit)
runtimeDebug.SetMemoryLimit(memoryLimitGo)
conntrack.KillerEnabled = true
conntrack.MemoryLimit = memoryLimit
} else {
Expand Down
9 changes: 6 additions & 3 deletions experimental/libbox/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"os/user"
"strconv"

"github.com/sagernet/sing-box/common/humanize"
C "github.com/sagernet/sing-box/constant"

"github.com/dustin/go-humanize"
)

var (
Expand Down Expand Up @@ -46,7 +45,11 @@ func Version() string {
}

func FormatBytes(length int64) string {
return humanize.IBytes(uint64(length))
return humanize.Bytes(uint64(length))
}

func FormatMemoryBytes(length int64) string {
return humanize.MemoryBytes(uint64(length))
}

func ProxyDisplayType(proxyType string) string {
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/caddyserver/certmagic v0.19.2
github.com/cloudflare/circl v1.3.3
github.com/cretz/bine v0.2.0
github.com/dustin/go-humanize v1.0.1
github.com/fsnotify/fsnotify v1.6.0
github.com/go-chi/chi/v5 v5.0.10
github.com/go-chi/cors v1.2.1
Expand All @@ -28,14 +27,14 @@ require (
github.com/sagernet/gvisor v0.0.0-20230627031050-1ab0276e0dd2
github.com/sagernet/quic-go v0.0.0-20230919101909-0cc6c5dcecee
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691
github.com/sagernet/sing v0.2.10-0.20230912050851-1453c7c8c20d
github.com/sagernet/sing v0.2.10-0.20230920060554-3c4a2b06a988
github.com/sagernet/sing-dns v0.1.9-0.20230919110447-d24aeae07601
github.com/sagernet/sing-mux v0.1.3-0.20230908032617-759a1886a400
github.com/sagernet/sing-quic v0.0.0-20230919102644-5874c56aae1c
github.com/sagernet/sing-shadowsocks v0.2.5-0.20230907005610-126234728ca0
github.com/sagernet/sing-shadowsocks2 v0.1.4-0.20230907005906-5d2917b29248
github.com/sagernet/sing-shadowtls v0.1.4
github.com/sagernet/sing-tun v0.1.12-0.20230821065522-7545dc2d5641
github.com/sagernet/sing-tun v0.1.12-0.20230920060816-9c933ea55308
github.com/sagernet/sing-vmess v0.1.8-0.20230907010359-161fb0ac716b
github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37
github.com/sagernet/tfo-go v0.0.0-20230816093905-5a5c285d44a6
Expand All @@ -58,7 +57,6 @@ require (
)

//replace github.com/sagernet/sing => ../sing

require (
github.com/Dreamacro/protobytes v0.0.0-20230617041236-6500a9f4f158 // indirect
github.com/ajg/form v1.5.1 // indirect
Expand Down
Loading

0 comments on commit 12dd1ac

Please sign in to comment.