gc_ms.nim 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # A simple mark&sweep garbage collector for Nim. Define the
  10. # symbol ``gcUseBitvectors`` to generate a variant of this GC.
  11. {.push profiler:off.}
  12. const
  13. InitialThreshold = 4*1024*1024 # X MB because marking&sweeping is slow
  14. withBitvectors = defined(gcUseBitvectors)
  15. # bitvectors are significantly faster for GC-bench, but slower for
  16. # bootstrapping and use more memory
  17. rcWhite = 0
  18. rcGrey = 1 # unused
  19. rcBlack = 2
  20. template mulThreshold(x): untyped = x * 2
  21. when defined(memProfiler):
  22. proc nimProfile(requestedSize: int)
  23. when hasThreadSupport:
  24. import sharedlist
  25. type
  26. WalkOp = enum
  27. waMarkGlobal, # we need to mark conservatively for global marker procs
  28. # as these may refer to a global var and not to a thread
  29. # local
  30. waMarkPrecise # fast precise marking
  31. Finalizer {.compilerproc.} = proc (self: pointer) {.nimcall, benign.}
  32. # A ref type can have a finalizer that is called before the object's
  33. # storage is freed.
  34. GcStat = object
  35. collections: int # number of performed full collections
  36. maxThreshold: int # max threshold that has been set
  37. maxStackSize: int # max stack size
  38. freedObjects: int # max entries in cycle table
  39. GcStack {.final, pure.} = object
  40. when nimCoroutines:
  41. prev: ptr GcStack
  42. next: ptr GcStack
  43. maxStackSize: int # Used to track statistics because we can not use
  44. # GcStat.maxStackSize when multiple stacks exist.
  45. bottom: pointer
  46. when nimCoroutines:
  47. pos: pointer
  48. GcHeap = object # this contains the zero count and
  49. # non-zero count table
  50. stack: GcStack
  51. when nimCoroutines:
  52. activeStack: ptr GcStack # current executing coroutine stack.
  53. cycleThreshold: int
  54. when useCellIds:
  55. idGenerator: int
  56. when withBitvectors:
  57. allocated, marked: CellSet
  58. tempStack: CellSeq # temporary stack for recursion elimination
  59. recGcLock: int # prevent recursion via finalizers; no thread lock
  60. region: MemRegion # garbage collected region
  61. stat: GcStat
  62. when hasThreadSupport:
  63. toDispose: SharedList[pointer]
  64. gcThreadId: int
  65. additionalRoots: CellSeq # dummy roots for GC_ref/unref
  66. when defined(nimTracing):
  67. tracing: bool
  68. indentation: int
  69. var
  70. gch {.rtlThreadVar.}: GcHeap
  71. when not defined(useNimRtl):
  72. instantiateForRegion(gch.region)
  73. template acquire(gch: GcHeap) =
  74. when hasThreadSupport and hasSharedHeap:
  75. acquireSys(HeapLock)
  76. template release(gch: GcHeap) =
  77. when hasThreadSupport and hasSharedHeap:
  78. releaseSys(HeapLock)
  79. template gcAssert(cond: bool, msg: string) =
  80. when defined(useGcAssert):
  81. if not cond:
  82. echo "[GCASSERT] ", msg
  83. quit 1
  84. proc cellToUsr(cell: PCell): pointer {.inline.} =
  85. # convert object (=pointer to refcount) to pointer to userdata
  86. result = cast[pointer](cast[ByteAddress](cell)+%ByteAddress(sizeof(Cell)))
  87. proc usrToCell(usr: pointer): PCell {.inline.} =
  88. # convert pointer to userdata to object (=pointer to refcount)
  89. result = cast[PCell](cast[ByteAddress](usr)-%ByteAddress(sizeof(Cell)))
  90. proc canbeCycleRoot(c: PCell): bool {.inline.} =
  91. result = ntfAcyclic notin c.typ.flags
  92. proc extGetCellType(c: pointer): PNimType {.compilerproc.} =
  93. # used for code generation concerning debugging
  94. result = usrToCell(c).typ
  95. proc unsureAsgnRef(dest: PPointer, src: pointer) {.inline.} =
  96. dest[] = src
  97. proc internRefcount(p: pointer): int {.exportc: "getRefcount".} =
  98. result = 0
  99. # this that has to equals zero, otherwise we have to round up UnitsPerPage:
  100. when BitsPerPage mod (sizeof(int)*8) != 0:
  101. {.error: "(BitsPerPage mod BitsPerUnit) should be zero!".}
  102. # forward declarations:
  103. proc collectCT(gch: var GcHeap; size: int) {.benign.}
  104. proc forAllChildren(cell: PCell, op: WalkOp) {.benign.}
  105. proc doOperation(p: pointer, op: WalkOp) {.benign.}
  106. proc forAllChildrenAux(dest: pointer, mt: PNimType, op: WalkOp) {.benign.}
  107. # we need the prototype here for debugging purposes
  108. when defined(nimGcRefLeak):
  109. const
  110. MaxTraceLen = 20 # tracking the last 20 calls is enough
  111. type
  112. GcStackTrace = object
  113. lines: array[0..MaxTraceLen-1, cstring]
  114. files: array[0..MaxTraceLen-1, cstring]
  115. proc captureStackTrace(f: PFrame, st: var GcStackTrace) =
  116. const
  117. firstCalls = 5
  118. var
  119. it = f
  120. i = 0
  121. total = 0
  122. while it != nil and i <= high(st.lines)-(firstCalls-1):
  123. # the (-1) is for the "..." entry
  124. st.lines[i] = it.procname
  125. st.files[i] = it.filename
  126. inc(i)
  127. inc(total)
  128. it = it.prev
  129. var b = it
  130. while it != nil:
  131. inc(total)
  132. it = it.prev
  133. for j in 1..total-i-(firstCalls-1):
  134. if b != nil: b = b.prev
  135. if total != i:
  136. st.lines[i] = "..."
  137. st.files[i] = "..."
  138. inc(i)
  139. while b != nil and i <= high(st.lines):
  140. st.lines[i] = b.procname
  141. st.files[i] = b.filename
  142. inc(i)
  143. b = b.prev
  144. var ax: array[10_000, GcStackTrace]
  145. proc nimGCref(p: pointer) {.compilerProc.} =
  146. # we keep it from being collected by pretending it's not even allocated:
  147. when false:
  148. when withBitvectors: excl(gch.allocated, usrToCell(p))
  149. else: usrToCell(p).refcount = rcBlack
  150. when defined(nimGcRefLeak):
  151. captureStackTrace(framePtr, ax[gch.additionalRoots.len])
  152. add(gch.additionalRoots, usrToCell(p))
  153. proc nimGCunref(p: pointer) {.compilerProc.} =
  154. let cell = usrToCell(p)
  155. var L = gch.additionalRoots.len-1
  156. var i = L
  157. let d = gch.additionalRoots.d
  158. while i >= 0:
  159. if d[i] == cell:
  160. d[i] = d[L]
  161. when defined(nimGcRefLeak):
  162. ax[i] = ax[L]
  163. dec gch.additionalRoots.len
  164. break
  165. dec(i)
  166. when false:
  167. when withBitvectors: incl(gch.allocated, usrToCell(p))
  168. else: usrToCell(p).refcount = rcWhite
  169. when defined(nimGcRefLeak):
  170. proc writeLeaks() =
  171. for i in 0..gch.additionalRoots.len-1:
  172. c_fprintf(stdout, "[Heap] NEW STACK TRACE\n")
  173. for ii in 0..MaxTraceLen-1:
  174. let line = ax[i].lines[ii]
  175. let file = ax[i].files[ii]
  176. if isNil(line): break
  177. c_fprintf(stdout, "[Heap] %s(%s)\n", file, line)
  178. include gc_common
  179. proc initGC() =
  180. when not defined(useNimRtl):
  181. gch.cycleThreshold = InitialThreshold
  182. gch.stat.collections = 0
  183. gch.stat.maxThreshold = 0
  184. gch.stat.maxStackSize = 0
  185. init(gch.tempStack)
  186. init(gch.additionalRoots)
  187. when withBitvectors:
  188. init(gch.allocated)
  189. init(gch.marked)
  190. when hasThreadSupport:
  191. init(gch.toDispose)
  192. gch.gcThreadId = atomicInc(gHeapidGenerator) - 1
  193. gcAssert(gch.gcThreadId >= 0, "invalid computed thread ID")
  194. proc forAllSlotsAux(dest: pointer, n: ptr TNimNode, op: WalkOp) {.benign.} =
  195. var d = cast[ByteAddress](dest)
  196. case n.kind
  197. of nkSlot: forAllChildrenAux(cast[pointer](d +% n.offset), n.typ, op)
  198. of nkList:
  199. for i in 0..n.len-1:
  200. forAllSlotsAux(dest, n.sons[i], op)
  201. of nkCase:
  202. var m = selectBranch(dest, n)
  203. if m != nil: forAllSlotsAux(dest, m, op)
  204. of nkNone: sysAssert(false, "forAllSlotsAux")
  205. proc forAllChildrenAux(dest: pointer, mt: PNimType, op: WalkOp) =
  206. var d = cast[ByteAddress](dest)
  207. if dest == nil: return # nothing to do
  208. if ntfNoRefs notin mt.flags:
  209. case mt.kind
  210. of tyRef, tyOptAsRef, tyString, tySequence: # leaf:
  211. doOperation(cast[PPointer](d)[], op)
  212. of tyObject, tyTuple:
  213. forAllSlotsAux(dest, mt.node, op)
  214. of tyArray, tyArrayConstr, tyOpenArray:
  215. for i in 0..(mt.size div mt.base.size)-1:
  216. forAllChildrenAux(cast[pointer](d +% i *% mt.base.size), mt.base, op)
  217. else: discard
  218. proc forAllChildren(cell: PCell, op: WalkOp) =
  219. gcAssert(cell != nil, "forAllChildren: 1")
  220. gcAssert(cell.typ != nil, "forAllChildren: 2")
  221. gcAssert cell.typ.kind in {tyRef, tyOptAsRef, tySequence, tyString}, "forAllChildren: 3"
  222. let marker = cell.typ.marker
  223. if marker != nil:
  224. marker(cellToUsr(cell), op.int)
  225. else:
  226. case cell.typ.kind
  227. of tyRef, tyOptAsRef: # common case
  228. forAllChildrenAux(cellToUsr(cell), cell.typ.base, op)
  229. of tySequence:
  230. when not defined(gcDestructors):
  231. var d = cast[ByteAddress](cellToUsr(cell))
  232. var s = cast[PGenericSeq](d)
  233. if s != nil:
  234. for i in 0..s.len-1:
  235. forAllChildrenAux(cast[pointer](d +% i *% cell.typ.base.size +%
  236. GenericSeqSize), cell.typ.base, op)
  237. else: discard
  238. proc rawNewObj(typ: PNimType, size: int, gch: var GcHeap): pointer =
  239. # generates a new object and sets its reference counter to 0
  240. incTypeSize typ, size
  241. acquire(gch)
  242. gcAssert(typ.kind in {tyRef, tyOptAsRef, tyString, tySequence}, "newObj: 1")
  243. collectCT(gch, size + sizeof(Cell))
  244. var res = cast[PCell](rawAlloc(gch.region, size + sizeof(Cell)))
  245. gcAssert((cast[ByteAddress](res) and (MemAlign-1)) == 0, "newObj: 2")
  246. # now it is buffered in the ZCT
  247. res.typ = typ
  248. when leakDetector and not hasThreadSupport:
  249. if framePtr != nil and framePtr.prev != nil:
  250. res.filename = framePtr.prev.filename
  251. res.line = framePtr.prev.line
  252. res.refcount = 0
  253. release(gch)
  254. when withBitvectors: incl(gch.allocated, res)
  255. when useCellIds:
  256. inc gch.idGenerator
  257. res.id = gch.idGenerator
  258. result = cellToUsr(res)
  259. when useCellIds:
  260. proc getCellId*[T](x: ref T): int =
  261. let p = usrToCell(cast[pointer](x))
  262. result = p.id
  263. {.pop.}
  264. proc newObj(typ: PNimType, size: int): pointer {.compilerRtl.} =
  265. result = rawNewObj(typ, size, gch)
  266. zeroMem(result, size)
  267. when defined(memProfiler): nimProfile(size)
  268. proc newObjNoInit(typ: PNimType, size: int): pointer {.compilerRtl.} =
  269. result = rawNewObj(typ, size, gch)
  270. when defined(memProfiler): nimProfile(size)
  271. proc newObjRC1(typ: PNimType, size: int): pointer {.compilerRtl.} =
  272. result = rawNewObj(typ, size, gch)
  273. zeroMem(result, size)
  274. when defined(memProfiler): nimProfile(size)
  275. when not defined(gcDestructors):
  276. proc newSeq(typ: PNimType, len: int): pointer {.compilerRtl.} =
  277. # `newObj` already uses locks, so no need for them here.
  278. let size = addInt(mulInt(len, typ.base.size), GenericSeqSize)
  279. result = newObj(typ, size)
  280. cast[PGenericSeq](result).len = len
  281. cast[PGenericSeq](result).reserved = len
  282. when defined(memProfiler): nimProfile(size)
  283. proc newSeqRC1(typ: PNimType, len: int): pointer {.compilerRtl.} =
  284. let size = addInt(mulInt(len, typ.base.size), GenericSeqSize)
  285. result = newObj(typ, size)
  286. cast[PGenericSeq](result).len = len
  287. cast[PGenericSeq](result).reserved = len
  288. when defined(memProfiler): nimProfile(size)
  289. proc growObj(old: pointer, newsize: int, gch: var GcHeap): pointer =
  290. acquire(gch)
  291. collectCT(gch, newsize + sizeof(Cell))
  292. var ol = usrToCell(old)
  293. sysAssert(ol.typ != nil, "growObj: 1")
  294. gcAssert(ol.typ.kind in {tyString, tySequence}, "growObj: 2")
  295. var res = cast[PCell](rawAlloc(gch.region, newsize + sizeof(Cell)))
  296. var elemSize = 1
  297. if ol.typ.kind != tyString: elemSize = ol.typ.base.size
  298. incTypeSize ol.typ, newsize
  299. var oldsize = cast[PGenericSeq](old).len*elemSize + GenericSeqSize
  300. copyMem(res, ol, oldsize + sizeof(Cell))
  301. zeroMem(cast[pointer](cast[ByteAddress](res)+% oldsize +% sizeof(Cell)),
  302. newsize-oldsize)
  303. sysAssert((cast[ByteAddress](res) and (MemAlign-1)) == 0, "growObj: 3")
  304. when withBitvectors: incl(gch.allocated, res)
  305. when useCellIds:
  306. inc gch.idGenerator
  307. res.id = gch.idGenerator
  308. release(gch)
  309. result = cellToUsr(res)
  310. when defined(memProfiler): nimProfile(newsize-oldsize)
  311. proc growObj(old: pointer, newsize: int): pointer {.rtl.} =
  312. result = growObj(old, newsize, gch)
  313. {.push profiler:off.}
  314. # ----------------- collector -----------------------------------------------
  315. proc mark(gch: var GcHeap, c: PCell) =
  316. when hasThreadSupport:
  317. for c in gch.toDispose:
  318. nimGCunref(c)
  319. when withBitvectors:
  320. incl(gch.marked, c)
  321. gcAssert gch.tempStack.len == 0, "stack not empty!"
  322. forAllChildren(c, waMarkPrecise)
  323. while gch.tempStack.len > 0:
  324. dec gch.tempStack.len
  325. var d = gch.tempStack.d[gch.tempStack.len]
  326. if not containsOrIncl(gch.marked, d):
  327. forAllChildren(d, waMarkPrecise)
  328. else:
  329. # XXX no 'if c.refCount != rcBlack' here?
  330. when defined(nimTracing):
  331. if gch.tracing:
  332. for i in 1..gch.indentation: c_fprintf(stdout, " ")
  333. c_fprintf(stdout, "start marking %p of type %s ((\n",
  334. c, c.typ.name)
  335. inc gch.indentation, 2
  336. c.refCount = rcBlack
  337. gcAssert gch.tempStack.len == 0, "stack not empty!"
  338. forAllChildren(c, waMarkPrecise)
  339. while gch.tempStack.len > 0:
  340. dec gch.tempStack.len
  341. var d = gch.tempStack.d[gch.tempStack.len]
  342. if d.refcount == rcWhite:
  343. d.refCount = rcBlack
  344. forAllChildren(d, waMarkPrecise)
  345. when defined(nimTracing):
  346. if gch.tracing:
  347. dec gch.indentation, 2
  348. for i in 1..gch.indentation: c_fprintf(stdout, " ")
  349. c_fprintf(stdout, "finished marking %p of type %s))\n",
  350. c, c.typ.name)
  351. proc doOperation(p: pointer, op: WalkOp) =
  352. if p == nil: return
  353. var c: PCell = usrToCell(p)
  354. gcAssert(c != nil, "doOperation: 1")
  355. case op
  356. of waMarkGlobal: mark(gch, c)
  357. of waMarkPrecise:
  358. when defined(nimTracing):
  359. if c.refcount == rcWhite: mark(gch, c)
  360. else:
  361. add(gch.tempStack, c)
  362. proc nimGCvisit(d: pointer, op: int) {.compilerRtl.} =
  363. doOperation(d, WalkOp(op))
  364. proc freeCyclicCell(gch: var GcHeap, c: PCell) =
  365. inc gch.stat.freedObjects
  366. prepareDealloc(c)
  367. when reallyDealloc: rawDealloc(gch.region, c)
  368. else:
  369. gcAssert(c.typ != nil, "freeCyclicCell")
  370. zeroMem(c, sizeof(Cell))
  371. proc sweep(gch: var GcHeap) =
  372. when withBitvectors:
  373. for c in gch.allocated.elementsExcept(gch.marked):
  374. gch.allocated.excl(c)
  375. freeCyclicCell(gch, c)
  376. else:
  377. for x in allObjects(gch.region):
  378. if isCell(x):
  379. # cast to PCell is correct here:
  380. var c = cast[PCell](x)
  381. if c.refcount == rcBlack: c.refcount = rcWhite
  382. else: freeCyclicCell(gch, c)
  383. when false:
  384. proc newGcInvariant*() =
  385. for x in allObjects(gch.region):
  386. if isCell(x):
  387. var c = cast[PCell](x)
  388. if c.typ == nil:
  389. writeStackTrace()
  390. quit 1
  391. proc markGlobals(gch: var GcHeap) =
  392. if gch.gcThreadId == 0:
  393. when defined(nimTracing):
  394. if gch.tracing:
  395. c_fprintf(stdout, "------- globals marking phase:\n")
  396. for i in 0 .. globalMarkersLen-1: globalMarkers[i]()
  397. when defined(nimTracing):
  398. if gch.tracing:
  399. c_fprintf(stdout, "------- thread locals marking phase:\n")
  400. for i in 0 .. threadLocalMarkersLen-1: threadLocalMarkers[i]()
  401. when defined(nimTracing):
  402. if gch.tracing:
  403. c_fprintf(stdout, "------- additional roots marking phase:\n")
  404. let d = gch.additionalRoots.d
  405. for i in 0 .. gch.additionalRoots.len-1: mark(gch, d[i])
  406. proc gcMark(gch: var GcHeap, p: pointer) {.inline.} =
  407. # the addresses are not as cells on the stack, so turn them to cells:
  408. var cell = usrToCell(p)
  409. var c = cast[ByteAddress](cell)
  410. if c >% PageSize:
  411. # fast check: does it look like a cell?
  412. var objStart = cast[PCell](interiorAllocatedPtr(gch.region, cell))
  413. if objStart != nil:
  414. mark(gch, objStart)
  415. proc markStackAndRegisters(gch: var GcHeap) {.noinline, cdecl.} =
  416. forEachStackSlot(gch, gcMark)
  417. proc collectCTBody(gch: var GcHeap) =
  418. when not nimCoroutines:
  419. gch.stat.maxStackSize = max(gch.stat.maxStackSize, stackSize())
  420. when defined(nimTracing):
  421. if gch.tracing:
  422. c_fprintf(stdout, "------- stack marking phase:\n")
  423. prepareForInteriorPointerChecking(gch.region)
  424. markStackAndRegisters(gch)
  425. markGlobals(gch)
  426. sweep(gch)
  427. inc(gch.stat.collections)
  428. when withBitvectors:
  429. deinit(gch.marked)
  430. init(gch.marked)
  431. gch.cycleThreshold = max(InitialThreshold, getOccupiedMem().mulThreshold)
  432. gch.stat.maxThreshold = max(gch.stat.maxThreshold, gch.cycleThreshold)
  433. sysAssert(allocInv(gch.region), "collectCT: end")
  434. proc collectCT(gch: var GcHeap; size: int) =
  435. if (getOccupiedMem(gch.region) >= gch.cycleThreshold or
  436. size > getFreeMem(gch.region)) and gch.recGcLock == 0:
  437. collectCTBody(gch)
  438. when not defined(useNimRtl):
  439. proc GC_disable() =
  440. when hasThreadSupport and hasSharedHeap:
  441. atomicInc(gch.recGcLock, 1)
  442. else:
  443. inc(gch.recGcLock)
  444. proc GC_enable() =
  445. if gch.recGcLock <= 0:
  446. raise newException(AssertionError,
  447. "API usage error: GC_enable called but GC is already enabled")
  448. when hasThreadSupport and hasSharedHeap:
  449. atomicDec(gch.recGcLock, 1)
  450. else:
  451. dec(gch.recGcLock)
  452. proc GC_setStrategy(strategy: GC_Strategy) = discard
  453. proc GC_enableMarkAndSweep() =
  454. gch.cycleThreshold = InitialThreshold
  455. proc GC_disableMarkAndSweep() =
  456. gch.cycleThreshold = high(gch.cycleThreshold)-1
  457. # set to the max value to suppress the cycle detector
  458. when defined(nimTracing):
  459. proc GC_logTrace*() =
  460. gch.tracing = true
  461. proc GC_fullCollect() =
  462. acquire(gch)
  463. var oldThreshold = gch.cycleThreshold
  464. gch.cycleThreshold = 0 # forces cycle collection
  465. collectCT(gch, 0)
  466. gch.cycleThreshold = oldThreshold
  467. release(gch)
  468. proc GC_getStatistics(): string =
  469. result = "[GC] total memory: " & $getTotalMem() & "\n" &
  470. "[GC] occupied memory: " & $getOccupiedMem() & "\n" &
  471. "[GC] collections: " & $gch.stat.collections & "\n" &
  472. "[GC] max threshold: " & $gch.stat.maxThreshold & "\n" &
  473. "[GC] freed objects: " & $gch.stat.freedObjects & "\n"
  474. when nimCoroutines:
  475. result.add "[GC] number of stacks: " & $gch.stack.len & "\n"
  476. for stack in items(gch.stack):
  477. result.add "[GC] stack " & stack.bottom.repr & "[GC] max stack size " & $stack.maxStackSize & "\n"
  478. else:
  479. result.add "[GC] max stack size: " & $gch.stat.maxStackSize & "\n"
  480. {.pop.}