orc.nim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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://www.cs.purdue.edu/homes/hosking/690M/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. include threadids
  69. when logOrc:
  70. proc writeCell(msg: cstring; s: Cell; desc: PNimTypeV2) =
  71. cfprintf(cstderr, "%s %s %ld root index: %ld; RC: %ld; color: %ld; thread: %ld\n",
  72. msg, desc.name, s.refId, s.rootIdx, s.rc shr rcShift, s.color, getThreadId())
  73. proc free(s: Cell; desc: PNimTypeV2) {.inline.} =
  74. when traceCollector:
  75. cprintf("[From ] %p rc %ld color %ld\n", s, s.rc shr rcShift, s.color)
  76. let p = s +! sizeof(RefHeader)
  77. when logOrc: writeCell("free", s, desc)
  78. if desc.destructor != nil:
  79. cast[DestructorProc](desc.destructor)(p)
  80. when false:
  81. cstderr.rawWrite desc.name
  82. cstderr.rawWrite " "
  83. if desc.destructor == nil:
  84. cstderr.rawWrite "lacks dispose"
  85. if desc.traceImpl != nil:
  86. cstderr.rawWrite ", but has trace\n"
  87. else:
  88. cstderr.rawWrite ", and lacks trace\n"
  89. else:
  90. cstderr.rawWrite "has dispose!\n"
  91. nimRawDispose(p, desc.align)
  92. template orcAssert(cond, msg) =
  93. when logOrc:
  94. if not cond:
  95. cfprintf(cstderr, "[Bug!] %s\n", msg)
  96. rawQuit 1
  97. when logOrc:
  98. proc strstr(s, sub: cstring): cstring {.header: "<string.h>", importc.}
  99. proc nimTraceRef(q: pointer; desc: PNimTypeV2; env: pointer) {.compilerRtl, inl.} =
  100. let p = cast[ptr pointer](q)
  101. if p[] != nil:
  102. orcAssert strstr(desc.name, "TType") == nil, "following a TType but it's acyclic!"
  103. var j = cast[ptr GcEnv](env)
  104. j.traceStack.add(p, desc)
  105. proc nimTraceRefDyn(q: pointer; env: pointer) {.compilerRtl, inl.} =
  106. let p = cast[ptr pointer](q)
  107. if p[] != nil:
  108. var j = cast[ptr GcEnv](env)
  109. j.traceStack.add(p, cast[ptr PNimTypeV2](p[])[])
  110. var
  111. roots {.threadvar.}: CellSeq[Cell]
  112. proc unregisterCycle(s: Cell) =
  113. # swap with the last element. O(1)
  114. let idx = s.rootIdx-1
  115. when false:
  116. if idx >= roots.len or idx < 0:
  117. cprintf("[Bug!] %ld\n", idx)
  118. rawQuit 1
  119. roots.d[idx] = roots.d[roots.len-1]
  120. roots.d[idx][0].rootIdx = idx+1
  121. dec roots.len
  122. s.rootIdx = 0
  123. proc scanBlack(s: Cell; desc: PNimTypeV2; j: var GcEnv) =
  124. #[
  125. proc scanBlack(s: Cell) =
  126. setColor(s, colBlack)
  127. for t in sons(s):
  128. t.rc = t.rc + rcIncrement
  129. if t.color != colBlack:
  130. scanBlack(t)
  131. ]#
  132. s.setColor colBlack
  133. let until = j.traceStack.len
  134. trace(s, desc, j)
  135. when logOrc: writeCell("root still alive", s, desc)
  136. while j.traceStack.len > until:
  137. let (entry, desc) = j.traceStack.pop()
  138. let t = head entry[]
  139. inc t.rc, rcIncrement
  140. if t.color != colBlack:
  141. t.setColor colBlack
  142. trace(t, desc, j)
  143. when logOrc: writeCell("child still alive", t, desc)
  144. proc markGray(s: Cell; desc: PNimTypeV2; j: var GcEnv) =
  145. #[
  146. proc markGray(s: Cell) =
  147. if s.color != colGray:
  148. setColor(s, colGray)
  149. for t in sons(s):
  150. t.rc = t.rc - rcIncrement
  151. if t.color != colGray:
  152. markGray(t)
  153. ]#
  154. if s.color != colGray:
  155. s.setColor colGray
  156. inc j.touched
  157. # keep in mind that refcounts are zero based so add 1 here:
  158. inc j.rcSum, (s.rc shr rcShift) + 1
  159. orcAssert(j.traceStack.len == 0, "markGray: trace stack not empty")
  160. trace(s, desc, j)
  161. while j.traceStack.len > 0:
  162. let (entry, desc) = j.traceStack.pop()
  163. let t = head entry[]
  164. dec t.rc, rcIncrement
  165. inc j.edges
  166. when useJumpStack:
  167. if (t.rc shr rcShift) >= 0 and (t.rc and jumpStackFlag) == 0:
  168. t.rc = t.rc or jumpStackFlag
  169. when traceCollector:
  170. cprintf("[Now in jumpstack] %p %ld color %ld in jumpstack %ld\n", t, t.rc shr rcShift, t.color, t.rc and jumpStackFlag)
  171. j.jumpStack.add(entry, desc)
  172. if t.color != colGray:
  173. t.setColor colGray
  174. inc j.touched
  175. # we already decremented its refcount so account for that:
  176. inc j.rcSum, (t.rc shr rcShift) + 2
  177. trace(t, desc, j)
  178. proc scan(s: Cell; desc: PNimTypeV2; j: var GcEnv) =
  179. #[
  180. proc scan(s: Cell) =
  181. if s.color == colGray:
  182. if s.rc > 0:
  183. scanBlack(s)
  184. else:
  185. s.setColor(colWhite)
  186. for t in sons(s): scan(t)
  187. ]#
  188. if s.color == colGray:
  189. if (s.rc shr rcShift) >= 0:
  190. scanBlack(s, desc, j)
  191. # XXX this should be done according to Lins' paper but currently breaks
  192. #when useJumpStack:
  193. # s.setColor colPurple
  194. else:
  195. when useJumpStack:
  196. # first we have to repair all the nodes we have seen
  197. # that are still alive; we also need to mark what they
  198. # refer to as alive:
  199. while j.jumpStack.len > 0:
  200. let (entry, desc) = j.jumpStack.pop
  201. let t = head entry[]
  202. # not in jump stack anymore!
  203. t.rc = t.rc and not jumpStackFlag
  204. if t.color == colGray and (t.rc shr rcShift) >= 0:
  205. scanBlack(t, desc, j)
  206. # XXX this should be done according to Lins' paper but currently breaks
  207. #t.setColor colPurple
  208. when traceCollector:
  209. cprintf("[jump stack] %p %ld\n", t, t.rc shr rcShift)
  210. orcAssert(j.traceStack.len == 0, "scan: trace stack not empty")
  211. s.setColor(colWhite)
  212. trace(s, desc, j)
  213. while j.traceStack.len > 0:
  214. let (entry, desc) = j.traceStack.pop()
  215. let t = head entry[]
  216. if t.color == colGray:
  217. if (t.rc shr rcShift) >= 0:
  218. scanBlack(t, desc, j)
  219. else:
  220. when useJumpStack:
  221. # first we have to repair all the nodes we have seen
  222. # that are still alive; we also need to mark what they
  223. # refer to as alive:
  224. while j.jumpStack.len > 0:
  225. let (entry, desc) = j.jumpStack.pop
  226. let t = head entry[]
  227. # not in jump stack anymore!
  228. t.rc = t.rc and not jumpStackFlag
  229. if t.color == colGray and (t.rc shr rcShift) >= 0:
  230. scanBlack(t, desc, j)
  231. # XXX this should be done according to Lins' paper but currently breaks
  232. #t.setColor colPurple
  233. when traceCollector:
  234. cprintf("[jump stack] %p %ld\n", t, t.rc shr rcShift)
  235. t.setColor(colWhite)
  236. trace(t, desc, j)
  237. when false:
  238. proc writeCell(msg: cstring; s: Cell) =
  239. cfprintf(cstderr, "%s %p root index: %ld; RC: %ld; color: %ld\n",
  240. msg, s, s.rootIdx, s.rc shr rcShift, s.color)
  241. proc collectColor(s: Cell; desc: PNimTypeV2; col: int; j: var GcEnv) =
  242. #[
  243. was: 'collectWhite'.
  244. proc collectWhite(s: Cell) =
  245. if s.color == colWhite and not buffered(s):
  246. s.setColor(colBlack)
  247. for t in sons(s):
  248. collectWhite(t)
  249. free(s) # watch out, a bug here!
  250. ]#
  251. if s.color == col and s.rootIdx == 0:
  252. orcAssert(j.traceStack.len == 0, "collectWhite: trace stack not empty")
  253. s.setColor(colBlack)
  254. j.toFree.add(s, desc)
  255. trace(s, desc, j)
  256. while j.traceStack.len > 0:
  257. let (entry, desc) = j.traceStack.pop()
  258. let t = head entry[]
  259. entry[] = nil # ensure that the destructor does touch moribund objects!
  260. if t.color == col and t.rootIdx == 0:
  261. j.toFree.add(t, desc)
  262. t.setColor(colBlack)
  263. trace(t, desc, j)
  264. proc collectCyclesBacon(j: var GcEnv; lowMark: int) =
  265. # pretty direct translation from
  266. # https://researcher.watson.ibm.com/researcher/files/us-bacon/Bacon01Concurrent.pdf
  267. # Fig. 2. Synchronous Cycle Collection
  268. #[
  269. for s in roots:
  270. markGray(s)
  271. for s in roots:
  272. scan(s)
  273. for s in roots:
  274. remove s from roots
  275. s.buffered = false
  276. collectWhite(s)
  277. ]#
  278. let last = roots.len - 1
  279. when logOrc:
  280. for i in countdown(last, lowMark):
  281. writeCell("root", roots.d[i][0], roots.d[i][1])
  282. for i in countdown(last, lowMark):
  283. markGray(roots.d[i][0], roots.d[i][1], j)
  284. var colToCollect = colWhite
  285. if j.rcSum == j.edges:
  286. # short-cut: we know everything is garbage:
  287. colToCollect = colGray
  288. # remember the fact that we got so lucky:
  289. j.keepThreshold = true
  290. else:
  291. for i in countdown(last, lowMark):
  292. scan(roots.d[i][0], roots.d[i][1], j)
  293. init j.toFree
  294. for i in 0 ..< roots.len:
  295. let s = roots.d[i][0]
  296. s.rootIdx = 0
  297. collectColor(s, roots.d[i][1], colToCollect, j)
  298. for i in 0 ..< j.toFree.len:
  299. free(j.toFree.d[i][0], j.toFree.d[i][1])
  300. inc j.freed, j.toFree.len
  301. deinit j.toFree
  302. #roots.len = 0
  303. const
  304. defaultThreshold = when defined(nimFixedOrc): 10_000 else: 128
  305. when defined(nimStressOrc):
  306. const rootsThreshold = 10 # broken with -d:nimStressOrc: 10 and for havlak iterations 1..8
  307. else:
  308. var rootsThreshold = defaultThreshold
  309. proc partialCollect(lowMark: int) =
  310. when false:
  311. if roots.len < 10 + lowMark: return
  312. when logOrc:
  313. cfprintf(cstderr, "[partialCollect] begin\n")
  314. var j: GcEnv
  315. init j.traceStack
  316. collectCyclesBacon(j, lowMark)
  317. when logOrc:
  318. cfprintf(cstderr, "[partialCollect] end; freed %ld touched: %ld work: %ld\n", j.freed, j.touched,
  319. roots.len - lowMark)
  320. roots.len = lowMark
  321. deinit j.traceStack
  322. proc collectCycles() =
  323. ## Collect cycles.
  324. when logOrc:
  325. cfprintf(cstderr, "[collectCycles] begin\n")
  326. var j: GcEnv
  327. init j.traceStack
  328. when useJumpStack:
  329. init j.jumpStack
  330. collectCyclesBacon(j, 0)
  331. while j.jumpStack.len > 0:
  332. let (t, desc) = j.jumpStack.pop
  333. # not in jump stack anymore!
  334. t.rc = t.rc and not jumpStackFlag
  335. deinit j.jumpStack
  336. else:
  337. collectCyclesBacon(j, 0)
  338. deinit j.traceStack
  339. deinit roots
  340. when not defined(nimStressOrc):
  341. # compute the threshold based on the previous history
  342. # of the cycle collector's effectiveness:
  343. # we're effective when we collected 50% or more of the nodes
  344. # we touched. If we're effective, we can reset the threshold:
  345. if j.keepThreshold and rootsThreshold <= defaultThreshold:
  346. discard
  347. elif j.freed * 2 >= j.touched:
  348. when not defined(nimFixedOrc):
  349. rootsThreshold = max(rootsThreshold div 3 * 2, 16)
  350. else:
  351. rootsThreshold = defaultThreshold
  352. #cfprintf(cstderr, "[collectCycles] freed %ld, touched %ld new threshold %ld\n", j.freed, j.touched, rootsThreshold)
  353. elif rootsThreshold < high(int) div 4:
  354. rootsThreshold = rootsThreshold * 3 div 2
  355. when logOrc:
  356. cfprintf(cstderr, "[collectCycles] end; freed %ld new threshold %ld touched: %ld mem: %ld rcSum: %ld edges: %ld\n", j.freed, rootsThreshold, j.touched,
  357. getOccupiedMem(), j.rcSum, j.edges)
  358. proc registerCycle(s: Cell; desc: PNimTypeV2) =
  359. s.rootIdx = roots.len+1
  360. if roots.d == nil: init(roots)
  361. add(roots, s, desc)
  362. if roots.len >= rootsThreshold:
  363. collectCycles()
  364. when logOrc:
  365. writeCell("[added root]", s, desc)
  366. orcAssert strstr(desc.name, "TType") == nil, "added a TType as a root!"
  367. proc GC_runOrc* =
  368. ## Forces a cycle collection pass.
  369. collectCycles()
  370. orcAssert roots.len == 0, "roots not empty!"
  371. proc GC_enableOrc*() =
  372. ## Enables the cycle collector subsystem of `--gc:orc`. This is a `--gc:orc`
  373. ## specific API. Check with `when defined(gcOrc)` for its existence.
  374. when not defined(nimStressOrc):
  375. rootsThreshold = defaultThreshold
  376. proc GC_disableOrc*() =
  377. ## Disables the cycle collector subsystem of `--gc:orc`. This is a `--gc:orc`
  378. ## specific API. Check with `when defined(gcOrc)` for its existence.
  379. when not defined(nimStressOrc):
  380. rootsThreshold = high(int)
  381. proc GC_prepareOrc*(): int {.inline.} = roots.len
  382. proc GC_partialCollect*(limit: int) =
  383. partialCollect(limit)
  384. proc GC_fullCollect* =
  385. ## Forces a full garbage collection pass. With `--gc:orc` triggers the cycle
  386. ## collector. This is an alias for `GC_runOrc`.
  387. collectCycles()
  388. proc GC_enableMarkAndSweep*() =
  389. ## For `--gc:orc` an alias for `GC_enableOrc`.
  390. GC_enableOrc()
  391. proc GC_disableMarkAndSweep*() =
  392. ## For `--gc:orc` an alias for `GC_disableOrc`.
  393. GC_disableOrc()
  394. const
  395. acyclicFlag = 1 # see also cggtypes.nim, proc genTypeInfoV2Impl
  396. when optimizedOrc:
  397. template markedAsCyclic(s: Cell; desc: PNimTypeV2): bool =
  398. (desc.flags and acyclicFlag) == 0 and (s.rc and maybeCycle) != 0
  399. else:
  400. template markedAsCyclic(s: Cell; desc: PNimTypeV2): bool =
  401. (desc.flags and acyclicFlag) == 0
  402. proc rememberCycle(isDestroyAction: bool; s: Cell; desc: PNimTypeV2) {.noinline.} =
  403. if isDestroyAction:
  404. if s.rootIdx > 0:
  405. unregisterCycle(s)
  406. else:
  407. # do not call 'rememberCycle' again unless this cell
  408. # got an 'incRef' event:
  409. if s.rootIdx == 0 and markedAsCyclic(s, desc):
  410. s.setColor colBlack
  411. registerCycle(s, desc)
  412. proc nimDecRefIsLastCyclicDyn(p: pointer): bool {.compilerRtl, inl.} =
  413. if p != nil:
  414. var cell = head(p)
  415. if (cell.rc and not rcMask) == 0:
  416. result = true
  417. #cprintf("[DESTROY] %p\n", p)
  418. else:
  419. dec cell.rc, rcIncrement
  420. #if cell.color == colPurple:
  421. rememberCycle(result, cell, cast[ptr PNimTypeV2](p)[])
  422. proc nimDecRefIsLastDyn(p: pointer): bool {.compilerRtl, inl.} =
  423. if p != nil:
  424. var cell = head(p)
  425. if (cell.rc and not rcMask) == 0:
  426. result = true
  427. #cprintf("[DESTROY] %p\n", p)
  428. else:
  429. dec cell.rc, rcIncrement
  430. #if cell.color == colPurple:
  431. if result:
  432. if cell.rootIdx > 0:
  433. unregisterCycle(cell)
  434. proc nimDecRefIsLastCyclicStatic(p: pointer; desc: PNimTypeV2): bool {.compilerRtl, inl.} =
  435. if p != nil:
  436. var cell = head(p)
  437. if (cell.rc and not rcMask) == 0:
  438. result = true
  439. #cprintf("[DESTROY] %p %s\n", p, desc.name)
  440. else:
  441. dec cell.rc, rcIncrement
  442. #if cell.color == colPurple:
  443. rememberCycle(result, cell, desc)