orc.nim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2020 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Cycle collector based on
  10. # https://researcher.watson.ibm.com/researcher/files/us-bacon/Bacon01Concurrent.pdf
  11. # And ideas from Lins' in 2008 by the notion of "critical links", see
  12. # "Cyclic reference counting" by Rafael Dueire Lins
  13. # R.D. Lins / Information Processing Letters 109 (2008) 71–78
  14. #
  15. include cellseqs_v2
  16. const
  17. colBlack = 0b000
  18. colGray = 0b001
  19. colWhite = 0b010
  20. maybeCycle = 0b100 # possibly part of a cycle; this has to be a "sticky" bit
  21. jumpStackFlag = 0b1000
  22. colorMask = 0b011
  23. logOrc = defined(nimArcIds)
  24. type
  25. TraceProc = proc (p, env: pointer) {.nimcall, benign.}
  26. DisposeProc = proc (p: pointer) {.nimcall, benign.}
  27. template color(c): untyped = c.rc and colorMask
  28. template setColor(c, col) =
  29. when col == colBlack:
  30. c.rc = c.rc and not colorMask
  31. else:
  32. c.rc = c.rc and not colorMask or col
  33. const
  34. optimizedOrc = false # not defined(nimOldOrc)
  35. # XXX Still incorrect, see tests/arc/tdestroy_in_loopcond
  36. proc nimIncRefCyclic(p: pointer; cyclic: bool) {.compilerRtl, inl.} =
  37. let h = head(p)
  38. inc h.rc, rcIncrement
  39. when optimizedOrc:
  40. if cyclic:
  41. h.rc = h.rc or maybeCycle
  42. proc nimMarkCyclic(p: pointer) {.compilerRtl, inl.} =
  43. when optimizedOrc:
  44. if p != nil:
  45. let h = head(p)
  46. h.rc = h.rc or maybeCycle
  47. proc unsureAsgnRef(dest: ptr pointer, src: pointer) {.inline.} =
  48. # This is only used by the old RTTI mechanism and we know
  49. # that 'dest[]' is nil and needs no destruction. Which is really handy
  50. # as we cannot destroy the object reliably if it's an object of unknown
  51. # compile-time type.
  52. dest[] = src
  53. if src != nil: nimIncRefCyclic(src, true)
  54. const
  55. useJumpStack = false # for thavlak the jump stack doesn't improve the performance at all
  56. type
  57. GcEnv = object
  58. traceStack: CellSeq[ptr pointer]
  59. when useJumpStack:
  60. jumpStack: CellSeq[ptr pointer] # Lins' jump stack in order to speed up traversals
  61. toFree: CellSeq[Cell]
  62. freed, touched, edges, rcSum: int
  63. keepThreshold: bool
  64. proc trace(s: Cell; desc: PNimTypeV2; j: var GcEnv) {.inline.} =
  65. if desc.traceImpl != nil:
  66. var p = s +! sizeof(RefHeader)
  67. cast[TraceProc](desc.traceImpl)(p, addr(j))
  68. when logOrc:
  69. proc writeCell(msg: cstring; s: Cell; desc: PNimTypeV2) =
  70. cfprintf(cstderr, "%s %s %ld root index: %ld; RC: %ld; color: %ld\n",
  71. msg, desc.name, s.refId, s.rootIdx, s.rc shr rcShift, s.color)
  72. proc free(s: Cell; desc: PNimTypeV2) {.inline.} =
  73. when traceCollector:
  74. cprintf("[From ] %p rc %ld color %ld\n", s, s.rc shr rcShift, s.color)
  75. let p = s +! sizeof(RefHeader)
  76. when logOrc: writeCell("free", s, desc)
  77. if desc.destructor != nil:
  78. cast[DestructorProc](desc.destructor)(p)
  79. when false:
  80. cstderr.rawWrite desc.name
  81. cstderr.rawWrite " "
  82. if desc.destructor == nil:
  83. cstderr.rawWrite "lacks dispose"
  84. if desc.traceImpl != nil:
  85. cstderr.rawWrite ", but has trace\n"
  86. else:
  87. cstderr.rawWrite ", and lacks trace\n"
  88. else:
  89. cstderr.rawWrite "has dispose!\n"
  90. nimRawDispose(p, desc.align)
  91. template orcAssert(cond, msg) =
  92. when logOrc:
  93. if not cond:
  94. cfprintf(cstderr, "[Bug!] %s\n", msg)
  95. quit 1
  96. when logOrc:
  97. proc strstr(s, sub: cstring): cstring {.header: "<string.h>", importc.}
  98. proc nimTraceRef(q: pointer; desc: PNimTypeV2; env: pointer) {.compilerRtl, inline.} =
  99. let p = cast[ptr pointer](q)
  100. if p[] != nil:
  101. orcAssert strstr(desc.name, "TType") == nil, "following a TType but it's acyclic!"
  102. var j = cast[ptr GcEnv](env)
  103. j.traceStack.add(p, desc)
  104. proc nimTraceRefDyn(q: pointer; env: pointer) {.compilerRtl, inline.} =
  105. let p = cast[ptr pointer](q)
  106. if p[] != nil:
  107. var j = cast[ptr GcEnv](env)
  108. j.traceStack.add(p, cast[ptr PNimTypeV2](p[])[])
  109. var
  110. roots {.threadvar.}: CellSeq[Cell]
  111. proc unregisterCycle(s: Cell) =
  112. # swap with the last element. O(1)
  113. let idx = s.rootIdx-1
  114. when false:
  115. if idx >= roots.len or idx < 0:
  116. cprintf("[Bug!] %ld\n", idx)
  117. quit 1
  118. roots.d[idx] = roots.d[roots.len-1]
  119. roots.d[idx][0].rootIdx = idx+1
  120. dec roots.len
  121. s.rootIdx = 0
  122. proc scanBlack(s: Cell; desc: PNimTypeV2; j: var GcEnv) =
  123. #[
  124. proc scanBlack(s: Cell) =
  125. setColor(s, colBlack)
  126. for t in sons(s):
  127. t.rc = t.rc + rcIncrement
  128. if t.color != colBlack:
  129. scanBlack(t)
  130. ]#
  131. s.setColor colBlack
  132. let until = j.traceStack.len
  133. trace(s, desc, j)
  134. when logOrc: writeCell("root still alive", s, desc)
  135. while j.traceStack.len > until:
  136. let (entry, desc) = j.traceStack.pop()
  137. let t = head entry[]
  138. inc t.rc, rcIncrement
  139. if t.color != colBlack:
  140. t.setColor colBlack
  141. trace(t, desc, j)
  142. when logOrc: writeCell("child still alive", t, desc)
  143. proc markGray(s: Cell; desc: PNimTypeV2; j: var GcEnv) =
  144. #[
  145. proc markGray(s: Cell) =
  146. if s.color != colGray:
  147. setColor(s, colGray)
  148. for t in sons(s):
  149. t.rc = t.rc - rcIncrement
  150. if t.color != colGray:
  151. markGray(t)
  152. ]#
  153. if s.color != colGray:
  154. s.setColor colGray
  155. inc j.touched
  156. # keep in mind that refcounts are zero based so add 1 here:
  157. inc j.rcSum, (s.rc shr rcShift) + 1
  158. orcAssert(j.traceStack.len == 0, "markGray: trace stack not empty")
  159. trace(s, desc, j)
  160. while j.traceStack.len > 0:
  161. let (entry, desc) = j.traceStack.pop()
  162. let t = head entry[]
  163. dec t.rc, rcIncrement
  164. inc j.edges
  165. when useJumpStack:
  166. if (t.rc shr rcShift) >= 0 and (t.rc and jumpStackFlag) == 0:
  167. t.rc = t.rc or jumpStackFlag
  168. when traceCollector:
  169. cprintf("[Now in jumpstack] %p %ld color %ld in jumpstack %ld\n", t, t.rc shr rcShift, t.color, t.rc and jumpStackFlag)
  170. j.jumpStack.add(entry, desc)
  171. if t.color != colGray:
  172. t.setColor colGray
  173. inc j.touched
  174. # we already decremented its refcount so account for that:
  175. inc j.rcSum, (t.rc shr rcShift) + 2
  176. trace(t, desc, j)
  177. proc scan(s: Cell; desc: PNimTypeV2; j: var GcEnv) =
  178. #[
  179. proc scan(s: Cell) =
  180. if s.color == colGray:
  181. if s.rc > 0:
  182. scanBlack(s)
  183. else:
  184. s.setColor(colWhite)
  185. for t in sons(s): scan(t)
  186. ]#
  187. if s.color == colGray:
  188. if (s.rc shr rcShift) >= 0:
  189. scanBlack(s, desc, j)
  190. # XXX this should be done according to Lins' paper but currently breaks
  191. #when useJumpStack:
  192. # s.setColor colPurple
  193. else:
  194. when useJumpStack:
  195. # first we have to repair all the nodes we have seen
  196. # that are still alive; we also need to mark what they
  197. # refer to as alive:
  198. while j.jumpStack.len > 0:
  199. let (entry, desc) = j.jumpStack.pop
  200. let t = head entry[]
  201. # not in jump stack anymore!
  202. t.rc = t.rc and not jumpStackFlag
  203. if t.color == colGray and (t.rc shr rcShift) >= 0:
  204. scanBlack(t, desc, j)
  205. # XXX this should be done according to Lins' paper but currently breaks
  206. #t.setColor colPurple
  207. when traceCollector:
  208. cprintf("[jump stack] %p %ld\n", t, t.rc shr rcShift)
  209. orcAssert(j.traceStack.len == 0, "scan: trace stack not empty")
  210. s.setColor(colWhite)
  211. trace(s, desc, j)
  212. while j.traceStack.len > 0:
  213. let (entry, desc) = j.traceStack.pop()
  214. let t = head entry[]
  215. if t.color == colGray:
  216. if (t.rc shr rcShift) >= 0:
  217. scanBlack(t, desc, j)
  218. else:
  219. when useJumpStack:
  220. # first we have to repair all the nodes we have seen
  221. # that are still alive; we also need to mark what they
  222. # refer to as alive:
  223. while j.jumpStack.len > 0:
  224. let (entry, desc) = j.jumpStack.pop
  225. let t = head entry[]
  226. # not in jump stack anymore!
  227. t.rc = t.rc and not jumpStackFlag
  228. if t.color == colGray and (t.rc shr rcShift) >= 0:
  229. scanBlack(t, desc, j)
  230. # XXX this should be done according to Lins' paper but currently breaks
  231. #t.setColor colPurple
  232. when traceCollector:
  233. cprintf("[jump stack] %p %ld\n", t, t.rc shr rcShift)
  234. t.setColor(colWhite)
  235. trace(t, desc, j)
  236. when false:
  237. proc writeCell(msg: cstring; s: Cell) =
  238. cfprintf(cstderr, "%s %p root index: %ld; RC: %ld; color: %ld\n",
  239. msg, s, s.rootIdx, s.rc shr rcShift, s.color)
  240. proc collectColor(s: Cell; desc: PNimTypeV2; col: int; j: var GcEnv) =
  241. #[
  242. was: 'collectWhite'.
  243. proc collectWhite(s: Cell) =
  244. if s.color == colWhite and not buffered(s):
  245. s.setColor(colBlack)
  246. for t in sons(s):
  247. collectWhite(t)
  248. free(s) # watch out, a bug here!
  249. ]#
  250. if s.color == col and s.rootIdx == 0:
  251. orcAssert(j.traceStack.len == 0, "collectWhite: trace stack not empty")
  252. s.setColor(colBlack)
  253. j.toFree.add(s, desc)
  254. trace(s, desc, j)
  255. while j.traceStack.len > 0:
  256. let (entry, desc) = j.traceStack.pop()
  257. let t = head entry[]
  258. entry[] = nil # ensure that the destructor does touch moribund objects!
  259. if t.color == col and t.rootIdx == 0:
  260. j.toFree.add(t, desc)
  261. t.setColor(colBlack)
  262. trace(t, desc, j)
  263. proc collectCyclesBacon(j: var GcEnv; lowMark: int) =
  264. # pretty direct translation from
  265. # https://researcher.watson.ibm.com/researcher/files/us-bacon/Bacon01Concurrent.pdf
  266. # Fig. 2. Synchronous Cycle Collection
  267. #[
  268. for s in roots:
  269. markGray(s)
  270. for s in roots:
  271. scan(s)
  272. for s in roots:
  273. remove s from roots
  274. s.buffered = false
  275. collectWhite(s)
  276. ]#
  277. let last = roots.len - 1
  278. when logOrc:
  279. for i in countdown(last, lowMark):
  280. writeCell("root", roots.d[i][0], roots.d[i][1])
  281. for i in countdown(last, lowMark):
  282. markGray(roots.d[i][0], roots.d[i][1], j)
  283. var colToCollect = colWhite
  284. if j.rcSum == j.edges:
  285. # short-cut: we know everything is garbage:
  286. colToCollect = colGray
  287. # remember the fact that we got so lucky:
  288. j.keepThreshold = true
  289. else:
  290. for i in countdown(last, lowMark):
  291. scan(roots.d[i][0], roots.d[i][1], j)
  292. init j.toFree
  293. for i in 0 ..< roots.len:
  294. let s = roots.d[i][0]
  295. s.rootIdx = 0
  296. collectColor(s, roots.d[i][1], colToCollect, j)
  297. for i in 0 ..< j.toFree.len:
  298. free(j.toFree.d[i][0], j.toFree.d[i][1])
  299. inc j.freed, j.toFree.len
  300. deinit j.toFree
  301. #roots.len = 0
  302. const
  303. defaultThreshold = when defined(nimFixedOrc): 10_000 else: 128
  304. when defined(nimStressOrc):
  305. const rootsThreshold = 10 # broken with -d:nimStressOrc: 10 and for havlak iterations 1..8
  306. else:
  307. var rootsThreshold = defaultThreshold
  308. proc partialCollect(lowMark: int) =
  309. when false:
  310. if roots.len < 10 + lowMark: return
  311. when logOrc:
  312. cfprintf(cstderr, "[partialCollect] begin\n")
  313. var j: GcEnv
  314. init j.traceStack
  315. collectCyclesBacon(j, lowMark)
  316. when logOrc:
  317. cfprintf(cstderr, "[partialCollect] end; freed %ld touched: %ld work: %ld\n", j.freed, j.touched,
  318. roots.len - lowMark)
  319. roots.len = lowMark
  320. deinit j.traceStack
  321. proc collectCycles() =
  322. ## Collect cycles.
  323. when logOrc:
  324. cfprintf(cstderr, "[collectCycles] begin\n")
  325. var j: GcEnv
  326. init j.traceStack
  327. when useJumpStack:
  328. init j.jumpStack
  329. collectCyclesBacon(j, 0)
  330. while j.jumpStack.len > 0:
  331. let (t, desc) = j.jumpStack.pop
  332. # not in jump stack anymore!
  333. t.rc = t.rc and not jumpStackFlag
  334. deinit j.jumpStack
  335. else:
  336. collectCyclesBacon(j, 0)
  337. deinit j.traceStack
  338. deinit roots
  339. when not defined(nimStressOrc):
  340. # compute the threshold based on the previous history
  341. # of the cycle collector's effectiveness:
  342. # we're effective when we collected 50% or more of the nodes
  343. # we touched. If we're effective, we can reset the threshold:
  344. if j.keepThreshold and rootsThreshold <= defaultThreshold:
  345. discard
  346. elif j.freed * 2 >= j.touched:
  347. when not defined(nimFixedOrc):
  348. rootsThreshold = max(rootsThreshold div 3 * 2, 16)
  349. else:
  350. rootsThreshold = defaultThreshold
  351. #cfprintf(cstderr, "[collectCycles] freed %ld, touched %ld new threshold %ld\n", j.freed, j.touched, rootsThreshold)
  352. elif rootsThreshold < high(int) div 4:
  353. rootsThreshold = rootsThreshold * 3 div 2
  354. when logOrc:
  355. cfprintf(cstderr, "[collectCycles] end; freed %ld new threshold %ld touched: %ld mem: %ld rcSum: %ld edges: %ld\n", j.freed, rootsThreshold, j.touched,
  356. getOccupiedMem(), j.rcSum, j.edges)
  357. proc registerCycle(s: Cell; desc: PNimTypeV2) =
  358. s.rootIdx = roots.len+1
  359. if roots.d == nil: init(roots)
  360. add(roots, s, desc)
  361. if roots.len >= rootsThreshold:
  362. collectCycles()
  363. when logOrc:
  364. writeCell("[added root]", s, desc)
  365. orcAssert strstr(desc.name, "TType") == nil, "added a TType as a root!"
  366. proc GC_runOrc* =
  367. ## Forces a cycle collection pass.
  368. collectCycles()
  369. proc GC_enableOrc*() =
  370. ## Enables the cycle collector subsystem of `--gc:orc`. This is a `--gc:orc`
  371. ## specific API. Check with `when defined(gcOrc)` for its existence.
  372. when not defined(nimStressOrc):
  373. rootsThreshold = defaultThreshold
  374. proc GC_disableOrc*() =
  375. ## Disables the cycle collector subsystem of `--gc:orc`. This is a `--gc:orc`
  376. ## specific API. Check with `when defined(gcOrc)` for its existence.
  377. when not defined(nimStressOrc):
  378. rootsThreshold = high(int)
  379. proc GC_prepareOrc*(): int {.inline.} = roots.len
  380. proc GC_partialCollect*(limit: int) =
  381. partialCollect(limit)
  382. proc GC_fullCollect* =
  383. ## Forces a full garbage collection pass. With `--gc:orc` triggers the cycle
  384. ## collector. This is an alias for `GC_runOrc`.
  385. collectCycles()
  386. proc GC_enableMarkAndSweep*() =
  387. ## For `--gc:orc` an alias for `GC_enableOrc`.
  388. GC_enableOrc()
  389. proc GC_disableMarkAndSweep*() =
  390. ## For `--gc:orc` an alias for `GC_disableOrc`.
  391. GC_disableOrc()
  392. const
  393. acyclicFlag = 1 # see also cggtypes.nim, proc genTypeInfoV2Impl
  394. when optimizedOrc:
  395. template markedAsCyclic(s: Cell; desc: PNimTypeV2): bool =
  396. (desc.flags and acyclicFlag) == 0 and (s.rc and maybeCycle) != 0
  397. else:
  398. template markedAsCyclic(s: Cell; desc: PNimTypeV2): bool =
  399. (desc.flags and acyclicFlag) == 0
  400. proc rememberCycle(isDestroyAction: bool; s: Cell; desc: PNimTypeV2) {.noinline.} =
  401. if isDestroyAction:
  402. if s.rootIdx > 0:
  403. unregisterCycle(s)
  404. else:
  405. # do not call 'rememberCycle' again unless this cell
  406. # got an 'incRef' event:
  407. if s.rootIdx == 0 and markedAsCyclic(s, desc):
  408. s.setColor colBlack
  409. registerCycle(s, desc)
  410. proc nimDecRefIsLastCyclicDyn(p: pointer): bool {.compilerRtl, inl.} =
  411. if p != nil:
  412. var cell = head(p)
  413. if (cell.rc and not rcMask) == 0:
  414. result = true
  415. #cprintf("[DESTROY] %p\n", p)
  416. else:
  417. dec cell.rc, rcIncrement
  418. #if cell.color == colPurple:
  419. rememberCycle(result, cell, cast[ptr PNimTypeV2](p)[])
  420. proc nimDecRefIsLastCyclicStatic(p: pointer; desc: PNimTypeV2): bool {.compilerRtl, inl.} =
  421. if p != nil:
  422. var cell = head(p)
  423. if (cell.rc and not rcMask) == 0:
  424. result = true
  425. #cprintf("[DESTROY] %p %s\n", p, desc.name)
  426. else:
  427. dec cell.rc, rcIncrement
  428. #if cell.color == colPurple:
  429. rememberCycle(result, cell, desc)