gc_ms.nim 17 KB

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