gc_ms.nim 17 KB

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