summary refs log tree commit diff stats
path: root/lib/system/gc2.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2018-01-15 17:41:05 +0100
committerAraq <rumpf_a@web.de>2018-01-15 17:41:05 +0100
commitf1089db1755c1e8cc5bcf965cac64014005cac3a (patch)
treeafdfea52b76b32cc8b96d0d068f5ff130dd4c8d2 /lib/system/gc2.nim
parent24a6583fa76289bfd725b61b9bd5effad7f5765a (diff)
downloadNim-f1089db1755c1e8cc5bcf965cac64014005cac3a.tar.gz
GC: enable precise global/thread local storage tracing
Diffstat (limited to 'lib/system/gc2.nim')
-rw-r--r--lib/system/gc2.nim59
1 files changed, 26 insertions, 33 deletions
diff --git a/lib/system/gc2.nim b/lib/system/gc2.nim
index cd90c6d62..ca2a35f60 100644
--- a/lib/system/gc2.nim
+++ b/lib/system/gc2.nim
@@ -104,7 +104,7 @@ type
     pDumpHeapFile: pointer # File that is used for GC_dumpHeap
     when hasThreadSupport:
       toDispose: SharedList[pointer]
-    isMainThread: bool
+    gcThreadId: int
 
 var
   gch {.rtlThreadVar.}: GcHeap
@@ -120,23 +120,6 @@ template release(gch: GcHeap) =
   when hasThreadSupport and hasSharedHeap:
     releaseSys(HeapLock)
 
-proc initGC() =
-  when not defined(useNimRtl):
-    gch.red = (1-gch.black)
-    gch.cycleThreshold = InitialCycleThreshold
-    gch.stat.stackScans = 0
-    gch.stat.completedCollections = 0
-    gch.stat.maxThreshold = 0
-    gch.stat.maxStackSize = 0
-    gch.stat.maxStackCells = 0
-    gch.stat.cycleTableSize = 0
-    # init the rt
-    init(gch.additionalRoots)
-    init(gch.greyStack)
-    when hasThreadSupport:
-      init(gch.toDispose)
-    gch.isMainThread = true
-
 # Which color to use for new objects is tricky: When we're marking,
 # they have to be *white* so that everything is marked that is only
 # reachable from them. However, when we are sweeping, they have to
@@ -342,6 +325,24 @@ proc gcInvariant*() =
 
 include gc_common
 
+proc initGC() =
+  when not defined(useNimRtl):
+    gch.red = (1-gch.black)
+    gch.cycleThreshold = InitialCycleThreshold
+    gch.stat.stackScans = 0
+    gch.stat.completedCollections = 0
+    gch.stat.maxThreshold = 0
+    gch.stat.maxStackSize = 0
+    gch.stat.maxStackCells = 0
+    gch.stat.cycleTableSize = 0
+    # init the rt
+    init(gch.additionalRoots)
+    init(gch.greyStack)
+    when hasThreadSupport:
+      init(gch.toDispose)
+    gch.gcThreadId = atomicInc(gHeapidGenerator) - 1
+    gcAssert(gch.gcThreadId >= 0, "invalid computed thread ID")
+
 proc rawNewObj(typ: PNimType, size: int, gch: var GcHeap): pointer =
   # generates a new object and sets its reference counter to 0
   sysAssert(allocInv(gch.region), "rawNewObj begin")
@@ -480,7 +481,7 @@ proc GC_dumpHeap*(file: File) =
         c_fprintf(file, "onstack %p\n", d[i])
       else:
         c_fprintf(file, "onstack_invalid %p\n", d[i])
-  if gch.isMainThread:
+  if gch.gcThreadId == 0:
     for i in 0 .. globalMarkersLen-1: globalMarkers[i]()
   for i in 0 .. threadLocalMarkersLen-1: threadLocalMarkers[i]()
   while true:
@@ -569,7 +570,7 @@ proc markIncremental(gch: var GcHeap): bool =
   result = true
 
 proc markGlobals(gch: var GcHeap) =
-  if gch.isMainThread:
+  if gch.gcThreadId == 0:
     for i in 0 .. globalMarkersLen-1: globalMarkers[i]()
   for i in 0 .. threadLocalMarkersLen-1: threadLocalMarkers[i]()
 
@@ -591,22 +592,14 @@ proc doOperation(p: pointer, op: WalkOp) =
         markRoot(gch, c)
       else:
         dumpRoot(gch, c)
-    when hasThreadSupport:
-      # could point to a cell which we don't own and don't want to touch/trace
-      if isAllocatedPtr(gch.region, c): handleRoot()
-    else:
-      #gcAssert(isAllocatedPtr(gch.region, c), "doOperation: waMarkGlobal")
+    handleRoot()
+    discard allocInv(gch.region)
+  of waMarkGrey:
+    when false:
       if not isAllocatedPtr(gch.region, c):
-        c_fprintf(stdout, "[GC] not allocated anymore: MarkGlobal %p\n", c)
+        c_fprintf(stdout, "[GC] not allocated anymore: MarkGrey %p\n", c)
         #GC_dumpHeap()
         sysAssert(false, "wtf")
-      handleRoot()
-    discard allocInv(gch.region)
-  of waMarkGrey:
-    if not isAllocatedPtr(gch.region, c):
-      c_fprintf(stdout, "[GC] not allocated anymore: MarkGrey %p\n", c)
-      #GC_dumpHeap()
-      sysAssert(false, "wtf")
     if c.color == 1-gch.black:
       c.setColor(rcGrey)
       add(gch.greyStack, c)
ter Araq <rumpf_a@web.de> 2011-06-15 02:09:02 +0200 compiler can emulate thread local variables' href='/ahoang/Nim/commit/lib/system/excpt.nim?h=devel&id=4fa80956b89c2ee06d8940018101240efec7dc02'>4fa80956b ^
866572e2e ^

4fa80956b ^
866572e2e ^

4fa80956b ^

866572e2e ^

4fa80956b ^
405b86068
93b3c03db ^

e25384db8 ^



93b3c03db ^
48e3b3e0f ^
93b3c03db ^












67a30d837 ^



93b3c03db ^

67a30d837 ^



93b3c03db ^















e25384db8 ^
93b3c03db ^





4fa80956b ^



db4f617af ^
405b86068
4fa80956b ^



439aa2d04 ^
405b86068



4fa80956b ^
439aa2d04 ^
4fa80956b ^
405b86068


439aa2d04 ^
405b86068


439aa2d04 ^

405b86068
4fa80956b ^
439aa2d04 ^
4fa80956b ^

439aa2d04 ^

405b86068
4fa80956b ^
439aa2d04 ^

db4f617af ^
439aa2d04 ^
fc0b66a7f ^
4fa80956b ^

fc0b66a7f ^
4fa80956b ^
fc0b66a7f ^

4fa80956b ^
e25384db8 ^















866572e2e ^
e25384db8 ^
405b86068

07d5a8085 ^
405b86068

07d5a8085 ^
405b86068
3f82352c2 ^
2b323c638 ^



4fa80956b ^
865d9cc6e ^


6a8a409f1 ^
3f82352c2 ^
e25384db8 ^
405b86068
e25384db8 ^

de338526e ^

5c94a9e1a ^

4fa80956b ^
3f82352c2 ^
4fa80956b ^

405b86068
e25384db8 ^











3f82352c2 ^
e25384db8 ^

405b86068
405b86068
3f82352c2 ^






866572e2e ^
4fa80956b ^
866572e2e ^

3f82352c2 ^
405b86068
405b86068
e25384db8 ^





405b86068
c323ec015 ^






29b63be98 ^





e25384db8 ^


405b86068

e5bd3b5b9 ^
e25384db8 ^












405b86068
e25384db8 ^












405b86068









8b2a9401a ^

405b86068
b2746c465 ^




439aa2d04 ^
07d5a8085 ^
405b86068
439aa2d04 ^
405b86068

439aa2d04 ^
07d5a8085 ^

405b86068









07d5a8085 ^
405b86068




07d5a8085 ^
405b86068




07d5a8085 ^
405b86068















8b2a9401a ^








1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378