timer.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package oomkiller
  2. import (
  3. runtimeDebug "runtime/debug"
  4. "sync"
  5. "time"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/log"
  8. "github.com/sagernet/sing-box/option"
  9. "github.com/sagernet/sing/common/byteformats"
  10. E "github.com/sagernet/sing/common/exceptions"
  11. "github.com/sagernet/sing/common/memory"
  12. )
  13. const (
  14. defaultMinInterval = 100 * time.Millisecond
  15. defaultArmedInterval = time.Second
  16. defaultMaxInterval = 10 * time.Second
  17. defaultSafetyMargin = 5 * 1024 * 1024
  18. defaultAvailableTriggerMarginMin = 32 * 1024 * 1024
  19. defaultAvailableTriggerMarginMax = 128 * 1024 * 1024
  20. )
  21. type pressureState uint8
  22. const (
  23. pressureStateNormal pressureState = iota
  24. pressureStateArmed
  25. pressureStateTriggered
  26. )
  27. type memorySample struct {
  28. usage uint64
  29. available uint64
  30. availableKnown bool
  31. }
  32. type pressureThresholds struct {
  33. trigger uint64
  34. armed uint64
  35. resume uint64
  36. }
  37. type timerConfig struct {
  38. memoryLimit uint64
  39. safetyMargin uint64
  40. hasSafetyMargin bool
  41. minInterval time.Duration
  42. armedInterval time.Duration
  43. maxInterval time.Duration
  44. policyMode policyMode
  45. killerDisabled bool
  46. }
  47. func buildTimerConfig(options option.OOMKillerServiceOptions, memoryLimit uint64, policyMode policyMode, killerDisabled bool) (timerConfig, error) {
  48. minInterval := defaultMinInterval
  49. if options.MinInterval != 0 {
  50. minInterval = time.Duration(options.MinInterval.Build())
  51. if minInterval <= 0 {
  52. return timerConfig{}, E.New("min_interval must be greater than 0")
  53. }
  54. }
  55. maxInterval := defaultMaxInterval
  56. if options.MaxInterval != 0 {
  57. maxInterval = time.Duration(options.MaxInterval.Build())
  58. if maxInterval <= 0 {
  59. return timerConfig{}, E.New("max_interval must be greater than 0")
  60. }
  61. }
  62. if maxInterval < minInterval {
  63. return timerConfig{}, E.New("max_interval must be greater than or equal to min_interval")
  64. }
  65. var (
  66. safetyMargin uint64
  67. hasSafetyMargin bool
  68. )
  69. if options.SafetyMargin != nil && options.SafetyMargin.Value() > 0 {
  70. safetyMargin = options.SafetyMargin.Value()
  71. hasSafetyMargin = true
  72. } else if memoryLimit > 0 {
  73. safetyMargin = defaultSafetyMargin
  74. hasSafetyMargin = true
  75. }
  76. return timerConfig{
  77. memoryLimit: memoryLimit,
  78. safetyMargin: safetyMargin,
  79. hasSafetyMargin: hasSafetyMargin,
  80. minInterval: minInterval,
  81. armedInterval: max(min(defaultArmedInterval, maxInterval), minInterval),
  82. maxInterval: maxInterval,
  83. policyMode: policyMode,
  84. killerDisabled: killerDisabled,
  85. }, nil
  86. }
  87. type adaptiveTimer struct {
  88. timerConfig
  89. logger log.ContextLogger
  90. router adapter.Router
  91. onTriggered func(uint64)
  92. limitThresholds pressureThresholds
  93. access sync.Mutex
  94. timer *time.Timer
  95. state pressureState
  96. currentInterval time.Duration
  97. forceMinInterval bool
  98. pendingPressureBaseline bool
  99. pressureBaseline memorySample
  100. pressureBaselineTime time.Time
  101. }
  102. func newAdaptiveTimer(logger log.ContextLogger, router adapter.Router, config timerConfig, onTriggered func(uint64)) *adaptiveTimer {
  103. t := &adaptiveTimer{
  104. timerConfig: config,
  105. logger: logger,
  106. router: router,
  107. onTriggered: onTriggered,
  108. }
  109. if config.policyMode == policyModeMemoryLimit || config.policyMode == policyModeNetworkExtension {
  110. t.limitThresholds = computeLimitThresholds(config.memoryLimit, config.safetyMargin)
  111. }
  112. return t
  113. }
  114. func (t *adaptiveTimer) start() {
  115. t.access.Lock()
  116. defer t.access.Unlock()
  117. t.startLocked()
  118. }
  119. func (t *adaptiveTimer) notifyPressure() {
  120. t.access.Lock()
  121. t.startLocked()
  122. t.forceMinInterval = true
  123. t.pendingPressureBaseline = true
  124. t.access.Unlock()
  125. t.poll()
  126. }
  127. func (t *adaptiveTimer) startLocked() {
  128. if t.timer != nil {
  129. return
  130. }
  131. t.state = pressureStateNormal
  132. t.forceMinInterval = false
  133. t.timer = time.AfterFunc(t.minInterval, t.poll)
  134. }
  135. func (t *adaptiveTimer) stop() {
  136. t.access.Lock()
  137. defer t.access.Unlock()
  138. if t.timer != nil {
  139. t.timer.Stop()
  140. t.timer = nil
  141. }
  142. }
  143. func (t *adaptiveTimer) poll() {
  144. if t.timerConfig.policyMode == policyModeNetworkExtension {
  145. runtimeDebug.FreeOSMemory()
  146. }
  147. var triggered bool
  148. var rateTriggered bool
  149. sample := readMemorySample(t.policyMode)
  150. t.access.Lock()
  151. if t.timer == nil {
  152. t.access.Unlock()
  153. return
  154. }
  155. if t.pendingPressureBaseline {
  156. t.pressureBaseline = sample
  157. t.pressureBaselineTime = time.Now()
  158. t.pendingPressureBaseline = false
  159. }
  160. previousState := t.state
  161. t.state = t.nextState(sample)
  162. if t.state == pressureStateNormal {
  163. t.forceMinInterval = false
  164. if !t.pressureBaselineTime.IsZero() && time.Since(t.pressureBaselineTime) > t.maxInterval {
  165. t.pressureBaselineTime = time.Time{}
  166. }
  167. }
  168. t.timer.Reset(t.intervalForState())
  169. triggered = previousState != pressureStateTriggered && t.state == pressureStateTriggered
  170. if !triggered && !t.pressureBaselineTime.IsZero() && t.memoryLimit > 0 &&
  171. sample.usage > t.pressureBaseline.usage && sample.usage < t.memoryLimit {
  172. elapsed := time.Since(t.pressureBaselineTime)
  173. if elapsed >= t.minInterval/2 {
  174. growth := sample.usage - t.pressureBaseline.usage
  175. ratePerSecond := float64(growth) / elapsed.Seconds()
  176. headroom := t.memoryLimit - sample.usage
  177. timeToLimit := time.Duration(float64(headroom)/ratePerSecond) * time.Second
  178. if timeToLimit < t.minInterval {
  179. triggered = true
  180. rateTriggered = true
  181. t.state = pressureStateTriggered
  182. }
  183. }
  184. }
  185. t.access.Unlock()
  186. if !triggered {
  187. return
  188. }
  189. t.onTriggered(sample.usage)
  190. if rateTriggered {
  191. if t.killerDisabled {
  192. t.logger.Warn("memory growth rate critical (report only), usage: ", byteformats.FormatMemoryBytes(sample.usage), t.logDetails(sample))
  193. } else {
  194. t.logger.Error("memory growth rate critical, usage: ", byteformats.FormatMemoryBytes(sample.usage), t.logDetails(sample), ", resetting network")
  195. t.router.ResetNetwork()
  196. }
  197. } else {
  198. if t.killerDisabled {
  199. t.logger.Warn("memory threshold reached (report only), usage: ", byteformats.FormatMemoryBytes(sample.usage), t.logDetails(sample))
  200. } else {
  201. t.logger.Error("memory threshold reached, usage: ", byteformats.FormatMemoryBytes(sample.usage), t.logDetails(sample), ", resetting network")
  202. t.router.ResetNetwork()
  203. }
  204. }
  205. runtimeDebug.FreeOSMemory()
  206. }
  207. func (t *adaptiveTimer) nextState(sample memorySample) pressureState {
  208. switch t.policyMode {
  209. case policyModeMemoryLimit, policyModeNetworkExtension:
  210. return nextPressureState(t.state,
  211. sample.usage >= t.limitThresholds.trigger,
  212. sample.usage >= t.limitThresholds.armed,
  213. sample.usage >= t.limitThresholds.resume,
  214. )
  215. case policyModeAvailable:
  216. if !sample.availableKnown {
  217. return pressureStateNormal
  218. }
  219. thresholds := t.availableThresholds(sample)
  220. return nextPressureState(t.state,
  221. sample.available <= thresholds.trigger,
  222. sample.available <= thresholds.armed,
  223. sample.available <= thresholds.resume,
  224. )
  225. default:
  226. return pressureStateNormal
  227. }
  228. }
  229. func computeLimitThresholds(memoryLimit uint64, safetyMargin uint64) pressureThresholds {
  230. triggerMargin := min(safetyMargin, memoryLimit)
  231. armedMargin := min(triggerMargin*2, memoryLimit)
  232. resumeMargin := min(triggerMargin*4, memoryLimit)
  233. return pressureThresholds{
  234. trigger: memoryLimit - triggerMargin,
  235. armed: memoryLimit - armedMargin,
  236. resume: memoryLimit - resumeMargin,
  237. }
  238. }
  239. func (t *adaptiveTimer) availableThresholds(sample memorySample) pressureThresholds {
  240. var triggerMargin uint64
  241. if t.hasSafetyMargin {
  242. triggerMargin = t.safetyMargin
  243. } else if sample.usage == 0 {
  244. triggerMargin = defaultAvailableTriggerMarginMin
  245. } else {
  246. triggerMargin = max(defaultAvailableTriggerMarginMin, min(sample.usage/4, defaultAvailableTriggerMarginMax))
  247. }
  248. return pressureThresholds{
  249. trigger: triggerMargin,
  250. armed: triggerMargin * 2,
  251. resume: triggerMargin * 4,
  252. }
  253. }
  254. func (t *adaptiveTimer) intervalForState() time.Duration {
  255. switch {
  256. case t.forceMinInterval || t.state == pressureStateTriggered:
  257. t.currentInterval = t.minInterval
  258. case t.state == pressureStateArmed:
  259. t.currentInterval = t.armedInterval
  260. default:
  261. if t.currentInterval == 0 {
  262. t.currentInterval = t.maxInterval
  263. } else {
  264. t.currentInterval = min(t.currentInterval*2, t.maxInterval)
  265. }
  266. }
  267. return t.currentInterval
  268. }
  269. func (t *adaptiveTimer) logDetails(sample memorySample) string {
  270. switch t.policyMode {
  271. case policyModeMemoryLimit, policyModeNetworkExtension:
  272. headroom := uint64(0)
  273. if sample.usage < t.memoryLimit {
  274. headroom = t.memoryLimit - sample.usage
  275. }
  276. return ", limit: " + byteformats.FormatMemoryBytes(t.memoryLimit) + ", headroom: " + byteformats.FormatMemoryBytes(headroom)
  277. case policyModeAvailable:
  278. if sample.availableKnown {
  279. return ", available: " + byteformats.FormatMemoryBytes(sample.available)
  280. }
  281. }
  282. return ""
  283. }
  284. func nextPressureState(current pressureState, shouldTrigger, shouldArm, shouldStayTriggered bool) pressureState {
  285. if current == pressureStateTriggered {
  286. if shouldStayTriggered {
  287. return pressureStateTriggered
  288. }
  289. return pressureStateNormal
  290. }
  291. if shouldTrigger {
  292. return pressureStateTriggered
  293. }
  294. if shouldArm {
  295. return pressureStateArmed
  296. }
  297. return pressureStateNormal
  298. }
  299. func readMemorySample(mode policyMode) memorySample {
  300. sample := memorySample{
  301. usage: memory.Total(),
  302. }
  303. if mode == policyModeAvailable {
  304. sample.availableKnown = true
  305. sample.available = memory.Available()
  306. }
  307. return sample
  308. }