rts: detabify/dewhitespace sm/MarkWeak.c
[ghc.git] / rts / sm / Evac.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team 1998-2008
4 *
5 * Generational garbage collector: evacuation functions
6 *
7 * Documentation on the architecture of the Garbage Collector can be
8 * found in the online commentary:
9 *
10 * http://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC
11 *
12 * ---------------------------------------------------------------------------*/
13
14 #include "PosixSource.h"
15 #include "Rts.h"
16
17 #include "Evac.h"
18 #include "Storage.h"
19 #include "GC.h"
20 #include "GCThread.h"
21 #include "GCTDecl.h"
22 #include "GCUtils.h"
23 #include "Compact.h"
24 #include "MarkStack.h"
25 #include "Prelude.h"
26 #include "Trace.h"
27 #include "LdvProfile.h"
28
29 #if defined(PROF_SPIN) && defined(THREADED_RTS) && defined(PARALLEL_GC)
30 StgWord64 whitehole_spin = 0;
31 #endif
32
33 #if defined(THREADED_RTS) && !defined(PARALLEL_GC)
34 #define evacuate(p) evacuate1(p)
35 #define HEAP_ALLOCED_GC(p) HEAP_ALLOCED(p)
36 #endif
37
38 #if !defined(PARALLEL_GC) || defined(PROFILING)
39 #define copy_tag_nolock(p, info, src, size, stp, tag) \
40 copy_tag(p, info, src, size, stp, tag)
41 #endif
42
43 /* Used to avoid long recursion due to selector thunks
44 */
45 #define MAX_THUNK_SELECTOR_DEPTH 16
46
47 static void eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool);
48 STATIC_INLINE void evacuate_large(StgPtr p);
49
50 /* -----------------------------------------------------------------------------
51 Allocate some space in which to copy an object.
52 -------------------------------------------------------------------------- */
53
54 STATIC_INLINE StgPtr
55 alloc_for_copy (nat size, nat gen_no)
56 {
57 StgPtr to;
58 gen_workspace *ws;
59
60 /* Find out where we're going, using the handy "to" pointer in
61 * the gen of the source object. If it turns out we need to
62 * evacuate to an older generation, adjust it here (see comment
63 * by evacuate()).
64 */
65 if (gen_no < gct->evac_gen_no) {
66 if (gct->eager_promotion) {
67 gen_no = gct->evac_gen_no;
68 } else {
69 gct->failed_to_evac = rtsTrue;
70 }
71 }
72
73 ws = &gct->gens[gen_no]; // zero memory references here
74
75 /* chain a new block onto the to-space for the destination gen if
76 * necessary.
77 */
78 to = ws->todo_free;
79 ws->todo_free += size;
80 if (ws->todo_free > ws->todo_lim) {
81 to = todo_block_full(size, ws);
82 }
83 ASSERT(ws->todo_free >= ws->todo_bd->free && ws->todo_free <= ws->todo_lim);
84
85 return to;
86 }
87
88 /* -----------------------------------------------------------------------------
89 The evacuate() code
90 -------------------------------------------------------------------------- */
91
92 STATIC_INLINE GNUC_ATTR_HOT void
93 copy_tag(StgClosure **p, const StgInfoTable *info,
94 StgClosure *src, nat size, nat gen_no, StgWord tag)
95 {
96 StgPtr to, from;
97 nat i;
98
99 to = alloc_for_copy(size,gen_no);
100
101 from = (StgPtr)src;
102 to[0] = (W_)info;
103 for (i = 1; i < size; i++) { // unroll for small i
104 to[i] = from[i];
105 }
106
107 // if (to+size+2 < bd->start + BLOCK_SIZE_W) {
108 // __builtin_prefetch(to + size + 2, 1);
109 // }
110
111 #if defined(PARALLEL_GC)
112 {
113 const StgInfoTable *new_info;
114 new_info = (const StgInfoTable *)cas((StgPtr)&src->header.info, (W_)info, MK_FORWARDING_PTR(to));
115 if (new_info != info) {
116 #ifdef PROFILING
117 // We copied this object at the same time as another
118 // thread. We'll evacuate the object again and the copy
119 // we just made will be discarded at the next GC, but we
120 // may have copied it after the other thread called
121 // SET_EVACUAEE_FOR_LDV(), which would confuse the LDV
122 // profiler when it encounters this closure in
123 // processHeapClosureForDead. So we reset the LDVW field
124 // here.
125 LDVW(to) = 0;
126 #endif
127 return evacuate(p); // does the failed_to_evac stuff
128 } else {
129 *p = TAG_CLOSURE(tag,(StgClosure*)to);
130 }
131 }
132 #else
133 src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
134 *p = TAG_CLOSURE(tag,(StgClosure*)to);
135 #endif
136
137 #ifdef PROFILING
138 // We store the size of the just evacuated object in the LDV word so that
139 // the profiler can guess the position of the next object later.
140 // This is safe only if we are sure that no other thread evacuates
141 // the object again, so we cannot use copy_tag_nolock when PROFILING.
142 SET_EVACUAEE_FOR_LDV(from, size);
143 #endif
144 }
145
146 #if defined(PARALLEL_GC) && !defined(PROFILING)
147 STATIC_INLINE void
148 copy_tag_nolock(StgClosure **p, const StgInfoTable *info,
149 StgClosure *src, nat size, nat gen_no, StgWord tag)
150 {
151 StgPtr to, from;
152 nat i;
153
154 to = alloc_for_copy(size,gen_no);
155
156 from = (StgPtr)src;
157 to[0] = (W_)info;
158 for (i = 1; i < size; i++) { // unroll for small i
159 to[i] = from[i];
160 }
161
162 // if somebody else reads the forwarding pointer, we better make
163 // sure there's a closure at the end of it.
164 write_barrier();
165 *p = TAG_CLOSURE(tag,(StgClosure*)to);
166 src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
167
168 // if (to+size+2 < bd->start + BLOCK_SIZE_W) {
169 // __builtin_prefetch(to + size + 2, 1);
170 // }
171
172 #ifdef PROFILING
173 // We store the size of the just evacuated object in the LDV word so that
174 // the profiler can guess the position of the next object later.
175 SET_EVACUAEE_FOR_LDV(from, size);
176 #endif
177 }
178 #endif
179
180 /* Special version of copy() for when we only want to copy the info
181 * pointer of an object, but reserve some padding after it. This is
182 * used to optimise evacuation of TSOs.
183 */
184 static rtsBool
185 copyPart(StgClosure **p, StgClosure *src, nat size_to_reserve,
186 nat size_to_copy, nat gen_no)
187 {
188 StgPtr to, from;
189 nat i;
190 StgWord info;
191
192 #if defined(PARALLEL_GC)
193 spin:
194 info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info);
195 if (info == (W_)&stg_WHITEHOLE_info) {
196 #ifdef PROF_SPIN
197 whitehole_spin++;
198 #endif
199 goto spin;
200 }
201 if (IS_FORWARDING_PTR(info)) {
202 src->header.info = (const StgInfoTable *)info;
203 evacuate(p); // does the failed_to_evac stuff
204 return rtsFalse;
205 }
206 #else
207 info = (W_)src->header.info;
208 #endif
209
210 to = alloc_for_copy(size_to_reserve, gen_no);
211
212 from = (StgPtr)src;
213 to[0] = info;
214 for (i = 1; i < size_to_copy; i++) { // unroll for small i
215 to[i] = from[i];
216 }
217
218 write_barrier();
219 src->header.info = (const StgInfoTable*)MK_FORWARDING_PTR(to);
220 *p = (StgClosure *)to;
221
222 #ifdef PROFILING
223 // We store the size of the just evacuated object in the LDV word so that
224 // the profiler can guess the position of the next object later.
225 SET_EVACUAEE_FOR_LDV(from, size_to_reserve);
226 // fill the slop
227 if (size_to_reserve - size_to_copy > 0)
228 LDV_FILL_SLOP(to + size_to_copy, (int)(size_to_reserve - size_to_copy));
229 #endif
230
231 return rtsTrue;
232 }
233
234
235 /* Copy wrappers that don't tag the closure after copying */
236 STATIC_INLINE GNUC_ATTR_HOT void
237 copy(StgClosure **p, const StgInfoTable *info,
238 StgClosure *src, nat size, nat gen_no)
239 {
240 copy_tag(p,info,src,size,gen_no,0);
241 }
242
243 /* -----------------------------------------------------------------------------
244 Evacuate a large object
245
246 This just consists of removing the object from the (doubly-linked)
247 gen->large_objects list, and linking it on to the (singly-linked)
248 gen->new_large_objects list, from where it will be scavenged later.
249
250 Convention: bd->flags has BF_EVACUATED set for a large object
251 that has been evacuated, or unset otherwise.
252 -------------------------------------------------------------------------- */
253
254 STATIC_INLINE void
255 evacuate_large(StgPtr p)
256 {
257 bdescr *bd;
258 generation *gen, *new_gen;
259 nat gen_no, new_gen_no;
260 gen_workspace *ws;
261
262 bd = Bdescr(p);
263 gen = bd->gen;
264 gen_no = bd->gen_no;
265 ACQUIRE_SPIN_LOCK(&gen->sync);
266
267 // already evacuated?
268 if (bd->flags & BF_EVACUATED) {
269 /* Don't forget to set the gct->failed_to_evac flag if we didn't get
270 * the desired destination (see comments in evacuate()).
271 */
272 if (gen_no < gct->evac_gen_no) {
273 gct->failed_to_evac = rtsTrue;
274 TICK_GC_FAILED_PROMOTION();
275 }
276 RELEASE_SPIN_LOCK(&gen->sync);
277 return;
278 }
279
280 // remove from large_object list
281 if (bd->u.back) {
282 bd->u.back->link = bd->link;
283 } else { // first object in the list
284 gen->large_objects = bd->link;
285 }
286 if (bd->link) {
287 bd->link->u.back = bd->u.back;
288 }
289
290 /* link it on to the evacuated large object list of the destination gen
291 */
292 new_gen_no = bd->dest_no;
293
294 if (new_gen_no < gct->evac_gen_no) {
295 if (gct->eager_promotion) {
296 new_gen_no = gct->evac_gen_no;
297 } else {
298 gct->failed_to_evac = rtsTrue;
299 }
300 }
301
302 ws = &gct->gens[new_gen_no];
303 new_gen = &generations[new_gen_no];
304
305 bd->flags |= BF_EVACUATED;
306 initBdescr(bd, new_gen, new_gen->to);
307
308 // If this is a block of pinned objects, we don't have to scan
309 // these objects, because they aren't allowed to contain any
310 // pointers. For these blocks, we skip the scavenge stage and put
311 // them straight on the scavenged_large_objects list.
312 if (bd->flags & BF_PINNED) {
313 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS);
314 if (new_gen != gen) { ACQUIRE_SPIN_LOCK(&new_gen->sync); }
315 dbl_link_onto(bd, &new_gen->scavenged_large_objects);
316 new_gen->n_scavenged_large_blocks += bd->blocks;
317 if (new_gen != gen) { RELEASE_SPIN_LOCK(&new_gen->sync); }
318 } else {
319 bd->link = ws->todo_large_objects;
320 ws->todo_large_objects = bd;
321 }
322
323 RELEASE_SPIN_LOCK(&gen->sync);
324 }
325
326 /* ----------------------------------------------------------------------------
327 Evacuate
328
329 This is called (eventually) for every live object in the system.
330
331 The caller to evacuate specifies a desired generation in the
332 gct->evac_gen thread-local variable. The following conditions apply to
333 evacuating an object which resides in generation M when we're
334 collecting up to generation N
335
336 if M >= gct->evac_gen
337 if M > N do nothing
338 else evac to gen->to
339
340 if M < gct->evac_gen evac to gct->evac_gen, step 0
341
342 if the object is already evacuated, then we check which generation
343 it now resides in.
344
345 if M >= gct->evac_gen do nothing
346 if M < gct->evac_gen set gct->failed_to_evac flag to indicate that we
347 didn't manage to evacuate this object into gct->evac_gen.
348
349
350 OPTIMISATION NOTES:
351
352 evacuate() is the single most important function performance-wise
353 in the GC. Various things have been tried to speed it up, but as
354 far as I can tell the code generated by gcc 3.2 with -O2 is about
355 as good as it's going to get. We pass the argument to evacuate()
356 in a register using the 'regparm' attribute (see the prototype for
357 evacuate() near the top of this file).
358
359 Changing evacuate() to take an (StgClosure **) rather than
360 returning the new pointer seems attractive, because we can avoid
361 writing back the pointer when it hasn't changed (eg. for a static
362 object, or an object in a generation > N). However, I tried it and
363 it doesn't help. One reason is that the (StgClosure **) pointer
364 gets spilled to the stack inside evacuate(), resulting in far more
365 extra reads/writes than we save.
366 ------------------------------------------------------------------------- */
367
368 REGPARM1 GNUC_ATTR_HOT void
369 evacuate(StgClosure **p)
370 {
371 bdescr *bd = NULL;
372 nat gen_no;
373 StgClosure *q;
374 const StgInfoTable *info;
375 StgWord tag;
376
377 q = *p;
378
379 loop:
380 /* The tag and the pointer are split, to be merged after evacing */
381 tag = GET_CLOSURE_TAG(q);
382 q = UNTAG_CLOSURE(q);
383
384 ASSERTM(LOOKS_LIKE_CLOSURE_PTR(q), "invalid closure, info=%p", q->header.info);
385
386 if (!HEAP_ALLOCED_GC(q)) {
387
388 if (!major_gc) return;
389
390 info = get_itbl(q);
391 switch (info->type) {
392
393 case THUNK_STATIC:
394 if (info->srt_bitmap != 0) {
395 if (*THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
396 #ifndef THREADED_RTS
397 *THUNK_STATIC_LINK((StgClosure *)q) = gct->static_objects;
398 gct->static_objects = (StgClosure *)q;
399 #else
400 StgPtr link;
401 link = (StgPtr)cas((StgPtr)THUNK_STATIC_LINK((StgClosure *)q),
402 (StgWord)NULL,
403 (StgWord)gct->static_objects);
404 if (link == NULL) {
405 gct->static_objects = (StgClosure *)q;
406 }
407 #endif
408 }
409 }
410 return;
411
412 case FUN_STATIC:
413 if (info->srt_bitmap != 0 &&
414 *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
415 #ifndef THREADED_RTS
416 *FUN_STATIC_LINK((StgClosure *)q) = gct->static_objects;
417 gct->static_objects = (StgClosure *)q;
418 #else
419 StgPtr link;
420 link = (StgPtr)cas((StgPtr)FUN_STATIC_LINK((StgClosure *)q),
421 (StgWord)NULL,
422 (StgWord)gct->static_objects);
423 if (link == NULL) {
424 gct->static_objects = (StgClosure *)q;
425 }
426 #endif
427 }
428 return;
429
430 case IND_STATIC:
431 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
432 * on the CAF list, so don't do anything with it here (we'll
433 * scavenge it later).
434 */
435 if (*IND_STATIC_LINK((StgClosure *)q) == NULL) {
436 #ifndef THREADED_RTS
437 *IND_STATIC_LINK((StgClosure *)q) = gct->static_objects;
438 gct->static_objects = (StgClosure *)q;
439 #else
440 StgPtr link;
441 link = (StgPtr)cas((StgPtr)IND_STATIC_LINK((StgClosure *)q),
442 (StgWord)NULL,
443 (StgWord)gct->static_objects);
444 if (link == NULL) {
445 gct->static_objects = (StgClosure *)q;
446 }
447 #endif
448 }
449 return;
450
451 case CONSTR_STATIC:
452 if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
453 #ifndef THREADED_RTS
454 *STATIC_LINK(info,(StgClosure *)q) = gct->static_objects;
455 gct->static_objects = (StgClosure *)q;
456 #else
457 StgPtr link;
458 link = (StgPtr)cas((StgPtr)STATIC_LINK(info,(StgClosure *)q),
459 (StgWord)NULL,
460 (StgWord)gct->static_objects);
461 if (link == NULL) {
462 gct->static_objects = (StgClosure *)q;
463 }
464 #endif
465 }
466 /* I am assuming that static_objects pointers are not
467 * written to other objects, and thus, no need to retag. */
468 return;
469
470 case CONSTR_NOCAF_STATIC:
471 /* no need to put these on the static linked list, they don't need
472 * to be scavenged.
473 */
474 return;
475
476 default:
477 barf("evacuate(static): strange closure type %d", (int)(info->type));
478 }
479 }
480
481 bd = Bdescr((P_)q);
482
483 if ((bd->flags & (BF_LARGE | BF_MARKED | BF_EVACUATED)) != 0) {
484
485 // pointer into to-space: just return it. It might be a pointer
486 // into a generation that we aren't collecting (> N), or it
487 // might just be a pointer into to-space. The latter doesn't
488 // happen often, but allowing it makes certain things a bit
489 // easier; e.g. scavenging an object is idempotent, so it's OK to
490 // have an object on the mutable list multiple times.
491 if (bd->flags & BF_EVACUATED) {
492 // We aren't copying this object, so we have to check
493 // whether it is already in the target generation. (this is
494 // the write barrier).
495 if (bd->gen_no < gct->evac_gen_no) {
496 gct->failed_to_evac = rtsTrue;
497 TICK_GC_FAILED_PROMOTION();
498 }
499 return;
500 }
501
502 /* evacuate large objects by re-linking them onto a different list.
503 */
504 if (bd->flags & BF_LARGE) {
505 evacuate_large((P_)q);
506 return;
507 }
508
509 /* If the object is in a gen that we're compacting, then we
510 * need to use an alternative evacuate procedure.
511 */
512 if (!is_marked((P_)q,bd)) {
513 mark((P_)q,bd);
514 push_mark_stack((P_)q);
515 }
516 return;
517 }
518
519 gen_no = bd->dest_no;
520
521 info = q->header.info;
522 if (IS_FORWARDING_PTR(info))
523 {
524 /* Already evacuated, just return the forwarding address.
525 * HOWEVER: if the requested destination generation (gct->evac_gen) is
526 * older than the actual generation (because the object was
527 * already evacuated to a younger generation) then we have to
528 * set the gct->failed_to_evac flag to indicate that we couldn't
529 * manage to promote the object to the desired generation.
530 */
531 /*
532 * Optimisation: the check is fairly expensive, but we can often
533 * shortcut it if either the required generation is 0, or the
534 * current object (the EVACUATED) is in a high enough generation.
535 * We know that an EVACUATED always points to an object in the
536 * same or an older generation. gen is the lowest generation that the
537 * current object would be evacuated to, so we only do the full
538 * check if gen is too low.
539 */
540 StgClosure *e = (StgClosure*)UN_FORWARDING_PTR(info);
541 *p = TAG_CLOSURE(tag,e);
542 if (gen_no < gct->evac_gen_no) { // optimisation
543 if (Bdescr((P_)e)->gen_no < gct->evac_gen_no) {
544 gct->failed_to_evac = rtsTrue;
545 TICK_GC_FAILED_PROMOTION();
546 }
547 }
548 return;
549 }
550
551 switch (INFO_PTR_TO_STRUCT(info)->type) {
552
553 case WHITEHOLE:
554 goto loop;
555
556 // For ints and chars of low value, save space by replacing references to
557 // these with closures with references to common, shared ones in the RTS.
558 //
559 // * Except when compiling into Windows DLLs which don't support cross-package
560 // data references very well.
561 //
562 case CONSTR_0_1:
563 {
564 #if defined(COMPILING_WINDOWS_DLL)
565 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,gen_no,tag);
566 #else
567 StgWord w = (StgWord)q->payload[0];
568 if (info == Czh_con_info &&
569 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
570 (StgChar)w <= MAX_CHARLIKE) {
571 *p = TAG_CLOSURE(tag,
572 (StgClosure *)CHARLIKE_CLOSURE((StgChar)w)
573 );
574 }
575 else if (info == Izh_con_info &&
576 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
577 *p = TAG_CLOSURE(tag,
578 (StgClosure *)INTLIKE_CLOSURE((StgInt)w)
579 );
580 }
581 else {
582 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,gen_no,tag);
583 }
584 #endif
585 return;
586 }
587
588 case FUN_0_1:
589 case FUN_1_0:
590 case CONSTR_1_0:
591 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,gen_no,tag);
592 return;
593
594 case THUNK_1_0:
595 case THUNK_0_1:
596 copy(p,info,q,sizeofW(StgThunk)+1,gen_no);
597 return;
598
599 case THUNK_1_1:
600 case THUNK_2_0:
601 case THUNK_0_2:
602 #ifdef NO_PROMOTE_THUNKS
603 #error bitrotted
604 #endif
605 copy(p,info,q,sizeofW(StgThunk)+2,gen_no);
606 return;
607
608 case FUN_1_1:
609 case FUN_2_0:
610 case FUN_0_2:
611 case CONSTR_1_1:
612 case CONSTR_2_0:
613 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,gen_no,tag);
614 return;
615
616 case CONSTR_0_2:
617 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,gen_no,tag);
618 return;
619
620 case THUNK:
621 copy(p,info,q,thunk_sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no);
622 return;
623
624 case FUN:
625 case IND_PERM:
626 case CONSTR:
627 copy_tag_nolock(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no,tag);
628 return;
629
630 case BLACKHOLE:
631 {
632 StgClosure *r;
633 const StgInfoTable *i;
634 r = ((StgInd*)q)->indirectee;
635 if (GET_CLOSURE_TAG(r) == 0) {
636 i = r->header.info;
637 if (IS_FORWARDING_PTR(i)) {
638 r = (StgClosure *)UN_FORWARDING_PTR(i);
639 i = r->header.info;
640 }
641 if (i == &stg_TSO_info
642 || i == &stg_WHITEHOLE_info
643 || i == &stg_BLOCKING_QUEUE_CLEAN_info
644 || i == &stg_BLOCKING_QUEUE_DIRTY_info) {
645 copy(p,info,q,sizeofW(StgInd),gen_no);
646 return;
647 }
648 ASSERT(i != &stg_IND_info);
649 }
650 q = r;
651 *p = r;
652 goto loop;
653 }
654
655 case MUT_VAR_CLEAN:
656 case MUT_VAR_DIRTY:
657 case MVAR_CLEAN:
658 case MVAR_DIRTY:
659 case TVAR:
660 case BLOCKING_QUEUE:
661 case WEAK:
662 case PRIM:
663 case MUT_PRIM:
664 copy(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),gen_no);
665 return;
666
667 case BCO:
668 copy(p,info,q,bco_sizeW((StgBCO *)q),gen_no);
669 return;
670
671 case THUNK_SELECTOR:
672 eval_thunk_selector(p, (StgSelector *)q, rtsTrue);
673 return;
674
675 case IND:
676 // follow chains of indirections, don't evacuate them
677 q = ((StgInd*)q)->indirectee;
678 *p = q;
679 goto loop;
680
681 case RET_BCO:
682 case RET_SMALL:
683 case RET_BIG:
684 case UPDATE_FRAME:
685 case UNDERFLOW_FRAME:
686 case STOP_FRAME:
687 case CATCH_FRAME:
688 case CATCH_STM_FRAME:
689 case CATCH_RETRY_FRAME:
690 case ATOMICALLY_FRAME:
691 // shouldn't see these
692 barf("evacuate: stack frame at %p\n", q);
693
694 case PAP:
695 copy(p,info,q,pap_sizeW((StgPAP*)q),gen_no);
696 return;
697
698 case AP:
699 copy(p,info,q,ap_sizeW((StgAP*)q),gen_no);
700 return;
701
702 case AP_STACK:
703 copy(p,info,q,ap_stack_sizeW((StgAP_STACK*)q),gen_no);
704 return;
705
706 case ARR_WORDS:
707 // just copy the block
708 copy(p,info,q,arr_words_sizeW((StgArrWords *)q),gen_no);
709 return;
710
711 case MUT_ARR_PTRS_CLEAN:
712 case MUT_ARR_PTRS_DIRTY:
713 case MUT_ARR_PTRS_FROZEN:
714 case MUT_ARR_PTRS_FROZEN0:
715 // just copy the block
716 copy(p,info,q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),gen_no);
717 return;
718
719 case SMALL_MUT_ARR_PTRS_CLEAN:
720 case SMALL_MUT_ARR_PTRS_DIRTY:
721 case SMALL_MUT_ARR_PTRS_FROZEN:
722 case SMALL_MUT_ARR_PTRS_FROZEN0:
723 // just copy the block
724 copy(p,info,q,small_mut_arr_ptrs_sizeW((StgSmallMutArrPtrs *)q),gen_no);
725 return;
726
727 case TSO:
728 copy(p,info,q,sizeofW(StgTSO),gen_no);
729 return;
730
731 case STACK:
732 {
733 StgStack *stack = (StgStack *)q;
734
735 /* To evacuate a small STACK, we need to adjust the stack pointer
736 */
737 {
738 StgStack *new_stack;
739 StgPtr r, s;
740 rtsBool mine;
741
742 mine = copyPart(p,(StgClosure *)stack, stack_sizeW(stack),
743 sizeofW(StgStack), gen_no);
744 if (mine) {
745 new_stack = (StgStack *)*p;
746 move_STACK(stack, new_stack);
747 for (r = stack->sp, s = new_stack->sp;
748 r < stack->stack + stack->stack_size;) {
749 *s++ = *r++;
750 }
751 }
752 return;
753 }
754 }
755
756 case TREC_CHUNK:
757 copy(p,info,q,sizeofW(StgTRecChunk),gen_no);
758 return;
759
760 default:
761 barf("evacuate: strange closure type %d", (int)(INFO_PTR_TO_STRUCT(info)->type));
762 }
763
764 barf("evacuate");
765 }
766
767 /* -----------------------------------------------------------------------------
768 Evaluate a THUNK_SELECTOR if possible.
769
770 p points to a THUNK_SELECTOR that we want to evaluate. The
771 result of "evaluating" it will be evacuated and a pointer to the
772 to-space closure will be returned.
773
774 If the THUNK_SELECTOR could not be evaluated (its selectee is still
775 a THUNK, for example), then the THUNK_SELECTOR itself will be
776 evacuated.
777 -------------------------------------------------------------------------- */
778 static void
779 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
780 {
781 StgSelector *prev;
782
783 prev = NULL;
784 while (p)
785 {
786 ASSERT(p->header.info == &stg_WHITEHOLE_info);
787 // val must be in to-space. Not always: when we recursively
788 // invoke eval_thunk_selector(), the recursive calls will not
789 // evacuate the value (because we want to select on the value,
790 // not evacuate it), so in this case val is in from-space.
791 // ASSERT(!HEAP_ALLOCED_GC(val) || Bdescr((P_)val)->gen_no > N || (Bdescr((P_)val)->flags & BF_EVACUATED));
792
793 prev = (StgSelector*)((StgClosure *)p)->payload[0];
794
795 // Update the THUNK_SELECTOR with an indirection to the
796 // value. The value is still in from-space at this stage.
797 //
798 // (old note: Why not do upd_evacuee(q,p)? Because we have an
799 // invariant that an EVACUATED closure always points to an
800 // object in the same or an older generation (required by
801 // the short-cut test in the EVACUATED case, below).
802 if ((StgClosure *)p == val) {
803 // must be a loop; just leave a BLACKHOLE in place. This
804 // can happen when we have a chain of selectors that
805 // eventually loops back on itself. We can't leave an
806 // indirection pointing to itself, and we want the program
807 // to deadlock if it ever enters this closure, so
808 // BLACKHOLE is correct.
809
810 // XXX we do not have BLACKHOLEs any more; replace with
811 // a THUNK_SELECTOR again. This will go into a loop if it is
812 // entered, and should result in a NonTermination exception.
813 ((StgThunk *)p)->payload[0] = val;
814 write_barrier();
815 SET_INFO((StgClosure *)p, &stg_sel_0_upd_info);
816 } else {
817 ((StgInd *)p)->indirectee = val;
818 write_barrier();
819 SET_INFO((StgClosure *)p, &stg_IND_info);
820 }
821
822 // For the purposes of LDV profiling, we have created an
823 // indirection.
824 LDV_RECORD_CREATE(p);
825
826 p = prev;
827 }
828 }
829
830 static void
831 eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool evac)
832 // NB. for legacy reasons, p & q are swapped around :(
833 {
834 nat field;
835 StgInfoTable *info;
836 StgWord info_ptr;
837 StgClosure *selectee;
838 StgSelector *prev_thunk_selector;
839 bdescr *bd;
840 StgClosure *val;
841
842 prev_thunk_selector = NULL;
843 // this is a chain of THUNK_SELECTORs that we are going to update
844 // to point to the value of the current THUNK_SELECTOR. Each
845 // closure on the chain is a WHITEHOLE, and points to the next in the
846 // chain with payload[0].
847
848 selector_chain:
849
850 bd = Bdescr((StgPtr)p);
851 if (HEAP_ALLOCED_GC(p)) {
852 // If the THUNK_SELECTOR is in to-space or in a generation that we
853 // are not collecting, then bale out early. We won't be able to
854 // save any space in any case, and updating with an indirection is
855 // trickier in a non-collected gen: we would have to update the
856 // mutable list.
857 if (bd->flags & BF_EVACUATED) {
858 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
859 *q = (StgClosure *)p;
860 // shortcut, behave as for: if (evac) evacuate(q);
861 if (evac && bd->gen_no < gct->evac_gen_no) {
862 gct->failed_to_evac = rtsTrue;
863 TICK_GC_FAILED_PROMOTION();
864 }
865 return;
866 }
867 // we don't update THUNK_SELECTORS in the compacted
868 // generation, because compaction does not remove the INDs
869 // that result, this causes confusion later
870 // (scavenge_mark_stack doesn't deal with IND). BEWARE! This
871 // bit is very tricky to get right. If you make changes
872 // around here, test by compiling stage 3 with +RTS -c -RTS.
873 if (bd->flags & BF_MARKED) {
874 // must call evacuate() to mark this closure if evac==rtsTrue
875 *q = (StgClosure *)p;
876 if (evac) evacuate(q);
877 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
878 return;
879 }
880 }
881
882
883 // WHITEHOLE the selector thunk, since it is now under evaluation.
884 // This is important to stop us going into an infinite loop if
885 // this selector thunk eventually refers to itself.
886 #if defined(THREADED_RTS)
887 // In threaded mode, we'll use WHITEHOLE to lock the selector
888 // thunk while we evaluate it.
889 {
890 do {
891 info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
892 } while (info_ptr == (W_)&stg_WHITEHOLE_info);
893
894 // make sure someone else didn't get here first...
895 if (IS_FORWARDING_PTR(info_ptr) ||
896 INFO_PTR_TO_STRUCT((StgInfoTable *)info_ptr)->type != THUNK_SELECTOR) {
897 // v. tricky now. The THUNK_SELECTOR has been evacuated
898 // by another thread, and is now either a forwarding ptr or IND.
899 // We need to extract ourselves from the current situation
900 // as cleanly as possible.
901 // - unlock the closure
902 // - update *q, we may have done *some* evaluation
903 // - if evac, we need to call evacuate(), because we
904 // need the write-barrier stuff.
905 // - undo the chain we've built to point to p.
906 SET_INFO((StgClosure *)p, (const StgInfoTable *)info_ptr);
907 *q = (StgClosure *)p;
908 if (evac) evacuate(q);
909 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
910 return;
911 }
912 }
913 #else
914 // Save the real info pointer (NOTE: not the same as get_itbl()).
915 info_ptr = (StgWord)p->header.info;
916 SET_INFO((StgClosure *)p,&stg_WHITEHOLE_info);
917 #endif
918
919 field = INFO_PTR_TO_STRUCT((StgInfoTable *)info_ptr)->layout.selector_offset;
920
921 // The selectee might be a constructor closure,
922 // so we untag the pointer.
923 selectee = UNTAG_CLOSURE(p->selectee);
924
925 selector_loop:
926 // selectee now points to the closure that we're trying to select
927 // a field from. It may or may not be in to-space: we try not to
928 // end up in to-space, but it's impractical to avoid it in
929 // general. The compacting GC scatters to-space pointers in
930 // from-space during marking, for example. We rely on the property
931 // that evacuate() doesn't mind if it gets passed a to-space pointer.
932
933 info = (StgInfoTable*)selectee->header.info;
934
935 if (IS_FORWARDING_PTR(info)) {
936 // We don't follow pointers into to-space; the constructor
937 // has already been evacuated, so we won't save any space
938 // leaks by evaluating this selector thunk anyhow.
939 goto bale_out;
940 }
941
942 info = INFO_PTR_TO_STRUCT(info);
943 switch (info->type) {
944 case WHITEHOLE:
945 goto bale_out; // about to be evacuated by another thread (or a loop).
946
947 case CONSTR:
948 case CONSTR_1_0:
949 case CONSTR_0_1:
950 case CONSTR_2_0:
951 case CONSTR_1_1:
952 case CONSTR_0_2:
953 case CONSTR_STATIC:
954 case CONSTR_NOCAF_STATIC:
955 {
956 // check that the size is in range
957 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
958 info->layout.payload.nptrs));
959
960 // Select the right field from the constructor
961 val = selectee->payload[field];
962
963 #ifdef PROFILING
964 // For the purposes of LDV profiling, we have destroyed
965 // the original selector thunk, p.
966 if (era > 0) {
967 // Only modify the info pointer when LDV profiling is
968 // enabled. Note that this is incompatible with parallel GC,
969 // because it would allow other threads to start evaluating
970 // the same selector thunk.
971 SET_INFO((StgClosure*)p, (StgInfoTable *)info_ptr);
972 OVERWRITING_CLOSURE((StgClosure*)p);
973 SET_INFO((StgClosure*)p, &stg_WHITEHOLE_info);
974 }
975 #endif
976
977 // the closure in val is now the "value" of the
978 // THUNK_SELECTOR in p. However, val may itself be a
979 // THUNK_SELECTOR, in which case we want to continue
980 // evaluating until we find the real value, and then
981 // update the whole chain to point to the value.
982 val_loop:
983 info_ptr = (StgWord)UNTAG_CLOSURE(val)->header.info;
984 if (!IS_FORWARDING_PTR(info_ptr))
985 {
986 info = INFO_PTR_TO_STRUCT((StgInfoTable *)info_ptr);
987 switch (info->type) {
988 case IND:
989 case IND_PERM:
990 case IND_STATIC:
991 val = ((StgInd *)val)->indirectee;
992 goto val_loop;
993 case THUNK_SELECTOR:
994 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
995 prev_thunk_selector = p;
996 p = (StgSelector*)val;
997 goto selector_chain;
998 default:
999 break;
1000 }
1001 }
1002 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
1003 prev_thunk_selector = p;
1004
1005 *q = val;
1006
1007 // update the other selectors in the chain *before*
1008 // evacuating the value. This is necessary in the case
1009 // where the value turns out to be one of the selectors
1010 // in the chain (i.e. we have a loop), and evacuating it
1011 // would corrupt the chain.
1012 unchain_thunk_selectors(prev_thunk_selector, val);
1013
1014 // evacuate() cannot recurse through
1015 // eval_thunk_selector(), because we know val is not
1016 // a THUNK_SELECTOR.
1017 if (evac) evacuate(q);
1018 return;
1019 }
1020
1021 case IND:
1022 case IND_PERM:
1023 case IND_STATIC:
1024 // Again, we might need to untag a constructor.
1025 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
1026 goto selector_loop;
1027
1028 case BLACKHOLE:
1029 {
1030 StgClosure *r;
1031 const StgInfoTable *i;
1032 r = ((StgInd*)selectee)->indirectee;
1033
1034 // establish whether this BH has been updated, and is now an
1035 // indirection, as in evacuate().
1036 if (GET_CLOSURE_TAG(r) == 0) {
1037 i = r->header.info;
1038 if (IS_FORWARDING_PTR(i)) {
1039 r = (StgClosure *)UN_FORWARDING_PTR(i);
1040 i = r->header.info;
1041 }
1042 if (i == &stg_TSO_info
1043 || i == &stg_WHITEHOLE_info
1044 || i == &stg_BLOCKING_QUEUE_CLEAN_info
1045 || i == &stg_BLOCKING_QUEUE_DIRTY_info) {
1046 goto bale_out;
1047 }
1048 ASSERT(i != &stg_IND_info);
1049 }
1050
1051 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
1052 goto selector_loop;
1053 }
1054
1055 case THUNK_SELECTOR:
1056 {
1057 StgClosure *val;
1058
1059 // recursively evaluate this selector. We don't want to
1060 // recurse indefinitely, so we impose a depth bound.
1061 if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
1062 goto bale_out;
1063 }
1064
1065 gct->thunk_selector_depth++;
1066 // rtsFalse says "don't evacuate the result". It will,
1067 // however, update any THUNK_SELECTORs that are evaluated
1068 // along the way.
1069 eval_thunk_selector(&val, (StgSelector*)selectee, rtsFalse);
1070 gct->thunk_selector_depth--;
1071
1072 // did we actually manage to evaluate it?
1073 if (val == selectee) goto bale_out;
1074
1075 // Of course this pointer might be tagged...
1076 selectee = UNTAG_CLOSURE(val);
1077 goto selector_loop;
1078 }
1079
1080 case AP:
1081 case AP_STACK:
1082 case THUNK:
1083 case THUNK_1_0:
1084 case THUNK_0_1:
1085 case THUNK_2_0:
1086 case THUNK_1_1:
1087 case THUNK_0_2:
1088 case THUNK_STATIC:
1089 // not evaluated yet
1090 goto bale_out;
1091
1092 default:
1093 barf("eval_thunk_selector: strange selectee %d",
1094 (int)(info->type));
1095 }
1096
1097 bale_out:
1098 // We didn't manage to evaluate this thunk; restore the old info
1099 // pointer. But don't forget: we still need to evacuate the thunk itself.
1100 SET_INFO((StgClosure *)p, (const StgInfoTable *)info_ptr);
1101 // THREADED_RTS: we just unlocked the thunk, so another thread
1102 // might get in and update it. copy() will lock it again and
1103 // check whether it was updated in the meantime.
1104 *q = (StgClosure *)p;
1105 if (evac) {
1106 copy(q,(const StgInfoTable *)info_ptr,(StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->dest_no);
1107 }
1108 unchain_thunk_selectors(prev_thunk_selector, *q);
1109 return;
1110 }
1111
1112 // Local Variables:
1113 // mode: C
1114 // fill-column: 80
1115 // indent-tabs-mode: nil
1116 // c-basic-offset: 4
1117 // buffer-file-coding-system: utf-8-unix
1118 // End: