tmp: usleep(1) during anyWork() if no work
[ghc-hetmet.git] / rts / sm / Evac.c-inc
1 /* -----------------------------------------------------------------------*-c-*-
2  *
3  * (c) The GHC Team 1998-2006
4  *
5  * Generational garbage collector: evacuation functions
6  *
7  * ---------------------------------------------------------------------------*/
8
9 // We have two versions of evacuate(): one for minor GC, and one for
10 // non-minor, parallel, GC.  This file contains the code for both,
11 // controllled by the CPP symbol MINOR_GC.
12
13 #ifdef MINOR_GC
14 #define copy(a,b,c,d) copy0(a,b,c,d)
15 #define copy_tag(a,b,c,d,e) copy_tag0(a,b,c,d,e)
16 #define copyPart(a,b,c,d,e) copyPart0(a,b,c,d,e)
17 #define evacuate(a) evacuate0(a)
18 #else
19 #undef copy
20 #undef copy_tag
21 #undef copyPart
22 #undef evacuate
23 #endif
24
25 STATIC_INLINE void
26 copy_tag(StgClosure **p, StgClosure *src, nat size, step *stp, StgWord tag)
27 {
28     StgPtr to, tagged_to, from;
29     nat i;
30     StgWord info;
31
32 #if !defined(MINOR_GC) && defined(THREADED_RTS)
33 spin:
34         info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info);
35         // so..  what is it?
36     if (info == (W_)&stg_WHITEHOLE_info) {
37 #ifdef PROF_SPIN
38             whitehole_spin++;
39 #endif
40             goto spin;
41     }
42     if (info == (W_)&stg_EVACUATED_info || info == (W_)&stg_IND_info) {
43         // NB. a closure might be updated with an IND by
44         // unchain_selector_thunks(), hence the test above.
45         src->header.info = (const StgInfoTable *)info;
46         return evacuate(p); // does the failed_to_evac stuff
47     }
48 #else
49     ASSERT(n_gc_threads == 1);
50     info = (W_)src->header.info;
51     src->header.info = &stg_EVACUATED_info;
52 #endif
53
54     to = alloc_for_copy(size,stp);
55     tagged_to = (StgPtr)TAG_CLOSURE(tag,(StgClosure*)to);
56     *p = (StgClosure *)tagged_to;
57     
58     TICK_GC_WORDS_COPIED(size);
59
60     from = (StgPtr)src;
61     to[0] = info;
62     for (i = 1; i < size; i++) { // unroll for small i
63         to[i] = from[i];
64     }
65
66 //  if (to+size+2 < bd->start + BLOCK_SIZE_W) {
67 //      __builtin_prefetch(to + size + 2, 1);
68 //  }
69
70     ((StgEvacuated*)from)->evacuee = (StgClosure *)tagged_to;
71 #if !defined(MINOR_GC) && defined(THREADED_RTS)
72     write_barrier();
73     ((StgEvacuated*)from)->header.info = &stg_EVACUATED_info;
74 #endif
75
76 #ifdef PROFILING
77     // We store the size of the just evacuated object in the LDV word so that
78     // the profiler can guess the position of the next object later.
79     SET_EVACUAEE_FOR_LDV(from, size);
80 #endif
81 }
82
83
84 /* Special version of copy() for when we only want to copy the info
85  * pointer of an object, but reserve some padding after it.  This is
86  * used to optimise evacuation of BLACKHOLEs.
87  */
88 static void
89 copyPart(StgClosure **p, StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
90 {
91     StgPtr to, from;
92     nat i;
93     StgWord info;
94     
95 #if !defined(MINOR_GC) && defined(THREADED_RTS)
96 spin:
97         info = xchg((StgPtr)&src->header.info, (W_)&stg_WHITEHOLE_info);
98         if (info == (W_)&stg_WHITEHOLE_info) {
99 #ifdef PROF_SPIN
100             whitehole_spin++;
101 #endif
102             goto spin;
103         }
104     if (info == (W_)&stg_EVACUATED_info) {
105         src->header.info = (const StgInfoTable *)info;
106         evacuate(p); // does the failed_to_evac stuff
107         return ;
108     }
109 #else
110     info = (W_)src->header.info;
111     src->header.info = &stg_EVACUATED_info;
112 #endif
113
114     to = alloc_for_copy(size_to_reserve, stp);
115     *p = (StgClosure *)to;
116
117     TICK_GC_WORDS_COPIED(size_to_copy);
118
119     from = (StgPtr)src;
120     to[0] = info;
121     for (i = 1; i < size_to_copy; i++) { // unroll for small i
122         to[i] = from[i];
123     }
124     
125     ((StgEvacuated*)from)->evacuee = (StgClosure *)to;
126 #if !defined(MINOR_GC) && defined(THREADED_RTS)
127     write_barrier();
128     ((StgEvacuated*)from)->header.info = &stg_EVACUATED_info;
129 #endif
130     
131 #ifdef PROFILING
132     // We store the size of the just evacuated object in the LDV word so that
133     // the profiler can guess the position of the next object later.
134     SET_EVACUAEE_FOR_LDV(from, size_to_reserve);
135     // fill the slop
136     if (size_to_reserve - size_to_copy > 0)
137         LDV_FILL_SLOP(to + size_to_copy - 1, (int)(size_to_reserve - size_to_copy)); 
138 #endif
139 }
140
141
142 /* Copy wrappers that don't tag the closure after copying */
143 STATIC_INLINE void
144 copy(StgClosure **p, StgClosure *src, nat size, step *stp)
145 {
146     copy_tag(p,src,size,stp,0);
147 }
148
149 /* ----------------------------------------------------------------------------
150    Evacuate
151
152    This is called (eventually) for every live object in the system.
153
154    The caller to evacuate specifies a desired generation in the
155    gct->evac_step thread-local variable.  The following conditions apply to
156    evacuating an object which resides in generation M when we're
157    collecting up to generation N
158
159    if  M >= gct->evac_step 
160            if  M > N     do nothing
161            else          evac to step->to
162
163    if  M < gct->evac_step      evac to gct->evac_step, step 0
164
165    if the object is already evacuated, then we check which generation
166    it now resides in.
167
168    if  M >= gct->evac_step     do nothing
169    if  M <  gct->evac_step     set gct->failed_to_evac flag to indicate that we
170                          didn't manage to evacuate this object into gct->evac_step.
171
172
173    OPTIMISATION NOTES:
174
175    evacuate() is the single most important function performance-wise
176    in the GC.  Various things have been tried to speed it up, but as
177    far as I can tell the code generated by gcc 3.2 with -O2 is about
178    as good as it's going to get.  We pass the argument to evacuate()
179    in a register using the 'regparm' attribute (see the prototype for
180    evacuate() near the top of this file).
181
182    Changing evacuate() to take an (StgClosure **) rather than
183    returning the new pointer seems attractive, because we can avoid
184    writing back the pointer when it hasn't changed (eg. for a static
185    object, or an object in a generation > N).  However, I tried it and
186    it doesn't help.  One reason is that the (StgClosure **) pointer
187    gets spilled to the stack inside evacuate(), resulting in far more
188    extra reads/writes than we save.
189    ------------------------------------------------------------------------- */
190
191 REGPARM1 void
192 evacuate(StgClosure **p)
193 {
194   bdescr *bd = NULL;
195   step *stp;
196   StgClosure *q;
197   const StgInfoTable *info;
198   StgWord tag;
199
200   q = *p;
201
202 loop:
203   /* The tag and the pointer are split, to be merged after evacing */
204   tag = GET_CLOSURE_TAG(q);
205   q = UNTAG_CLOSURE(q);
206
207   ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
208
209   if (!HEAP_ALLOCED(q)) {
210
211 #ifdef MINOR_GC
212       return;
213 #endif
214       if (!major_gc) return;
215
216       info = get_itbl(q);
217       switch (info->type) {
218
219       case THUNK_STATIC:
220           if (info->srt_bitmap != 0 &&
221               *THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
222               ACQUIRE_SPIN_LOCK(&static_objects_sync);
223               if (*THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
224                   *THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
225                   static_objects = (StgClosure *)q;
226               }
227               RELEASE_SPIN_LOCK(&static_objects_sync);
228           }
229           return;
230           
231       case FUN_STATIC:
232           if (info->srt_bitmap != 0 &&
233               *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
234               ACQUIRE_SPIN_LOCK(&static_objects_sync);
235               if (*FUN_STATIC_LINK((StgClosure *)q) == NULL) {
236                   *FUN_STATIC_LINK((StgClosure *)q) = static_objects;
237                   static_objects = (StgClosure *)q;
238               }
239               RELEASE_SPIN_LOCK(&static_objects_sync);
240           }
241           return;
242           
243       case IND_STATIC:
244           /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
245            * on the CAF list, so don't do anything with it here (we'll
246            * scavenge it later).
247            */
248           if (((StgIndStatic *)q)->saved_info == NULL) {
249               ACQUIRE_SPIN_LOCK(&static_objects_sync);
250               if (*IND_STATIC_LINK((StgClosure *)q) == NULL) {
251                   *IND_STATIC_LINK((StgClosure *)q) = static_objects;
252                   static_objects = (StgClosure *)q;
253               }
254               RELEASE_SPIN_LOCK(&static_objects_sync);
255           }
256           return;
257           
258       case CONSTR_STATIC:
259           if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
260               ACQUIRE_SPIN_LOCK(&static_objects_sync);
261               // re-test, after acquiring lock
262               if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
263                   *STATIC_LINK(info,(StgClosure *)q) = static_objects;
264                   static_objects = (StgClosure *)q;
265               }
266               RELEASE_SPIN_LOCK(&static_objects_sync);
267                 /* I am assuming that static_objects pointers are not
268                  * written to other objects, and thus, no need to retag. */
269           }
270           return;
271           
272       case CONSTR_NOCAF_STATIC:
273           /* no need to put these on the static linked list, they don't need
274            * to be scavenged.
275            */
276           return;
277           
278       default:
279           barf("evacuate(static): strange closure type %d", (int)(info->type));
280       }
281   }
282
283   bd = Bdescr((P_)q);
284
285   if (bd->gen_no > N) {
286       /* Can't evacuate this object, because it's in a generation
287        * older than the ones we're collecting.  Let's hope that it's
288        * in gct->evac_step or older, or we will have to arrange to track
289        * this pointer using the mutable list.
290        */
291       if (bd->step < gct->evac_step) {
292           // nope 
293           gct->failed_to_evac = rtsTrue;
294           TICK_GC_FAILED_PROMOTION();
295       }
296       return;
297   }
298
299   if ((bd->flags & (BF_LARGE | BF_COMPACTED | BF_EVACUATED)) != 0) {
300
301       /* pointer into to-space: just return it.  This normally
302        * shouldn't happen, but alllowing it makes certain things
303        * slightly easier (eg. the mutable list can contain the same
304        * object twice, for example).
305        */
306       if (bd->flags & BF_EVACUATED) {
307           if (bd->step < gct->evac_step) {
308               gct->failed_to_evac = rtsTrue;
309               TICK_GC_FAILED_PROMOTION();
310           }
311           return;
312       }
313
314       /* evacuate large objects by re-linking them onto a different list.
315        */
316       if (bd->flags & BF_LARGE) {
317           info = get_itbl(q);
318           if (info->type == TSO && 
319               ((StgTSO *)q)->what_next == ThreadRelocated) {
320               q = (StgClosure *)((StgTSO *)q)->link;
321               *p = q;
322               goto loop;
323           }
324           evacuate_large((P_)q);
325           return;
326       }
327       
328       /* If the object is in a step that we're compacting, then we
329        * need to use an alternative evacuate procedure.
330        */
331       if (bd->flags & BF_COMPACTED) {
332           if (!is_marked((P_)q,bd)) {
333               mark((P_)q,bd);
334               if (mark_stack_full()) {
335                   mark_stack_overflowed = rtsTrue;
336                   reset_mark_stack();
337               }
338               push_mark_stack((P_)q);
339           }
340           return;
341       }
342   }
343       
344   stp = bd->step->to;
345
346   info = get_itbl(q);
347   
348   switch (info->type) {
349
350   case WHITEHOLE:
351       goto loop;
352
353   case MUT_VAR_CLEAN:
354   case MUT_VAR_DIRTY:
355   case MVAR_CLEAN:
356   case MVAR_DIRTY:
357       copy(p,q,sizeW_fromITBL(info),stp);
358       return;
359
360   case CONSTR_0_1:
361   { 
362       StgWord w = (StgWord)q->payload[0];
363       if (q->header.info == Czh_con_info &&
364           // unsigned, so always true:  (StgChar)w >= MIN_CHARLIKE &&  
365           (StgChar)w <= MAX_CHARLIKE) {
366           *p =  TAG_CLOSURE(tag,
367                             (StgClosure *)CHARLIKE_CLOSURE((StgChar)w)
368                            );
369       }
370       else if (q->header.info == Izh_con_info &&
371           (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
372           *p = TAG_CLOSURE(tag,
373                              (StgClosure *)INTLIKE_CLOSURE((StgInt)w)
374                              );
375       }
376       else {
377           copy_tag(p,q,sizeofW(StgHeader)+1,stp,tag);
378       }
379       return;
380   }
381
382   case FUN_0_1:
383   case FUN_1_0:
384   case CONSTR_1_0:
385       copy_tag(p,q,sizeofW(StgHeader)+1,stp,tag);
386       return;
387
388   case THUNK_1_0:
389   case THUNK_0_1:
390       copy(p,q,sizeofW(StgThunk)+1,stp);
391       return;
392
393   case THUNK_1_1:
394   case THUNK_2_0:
395   case THUNK_0_2:
396 #ifdef NO_PROMOTE_THUNKS
397     if (bd->gen_no == 0 && 
398         bd->step->no != 0 &&
399         bd->step->no == generations[bd->gen_no].n_steps-1) {
400       stp = bd->step;
401     }
402 #endif
403     copy(p,q,sizeofW(StgThunk)+2,stp);
404     return;
405
406   case FUN_1_1:
407   case FUN_2_0:
408   case FUN_0_2:
409   case CONSTR_1_1:
410   case CONSTR_2_0:
411       copy_tag(p,q,sizeofW(StgHeader)+2,stp,tag);
412       return;
413
414   case CONSTR_0_2:
415       copy_tag(p,q,sizeofW(StgHeader)+2,stp,tag);
416       return;
417
418   case THUNK:
419       copy(p,q,thunk_sizeW_fromITBL(info),stp);
420       return;
421
422   case FUN:
423   case IND_PERM:
424   case IND_OLDGEN_PERM:
425   case WEAK:
426   case STABLE_NAME:
427   case CONSTR:
428       copy_tag(p,q,sizeW_fromITBL(info),stp,tag);
429       return;
430
431   case BCO:
432       copy(p,q,bco_sizeW((StgBCO *)q),stp);
433       return;
434
435   case CAF_BLACKHOLE:
436   case SE_CAF_BLACKHOLE:
437   case SE_BLACKHOLE:
438   case BLACKHOLE:
439       copyPart(p,q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
440       return;
441
442   case THUNK_SELECTOR:
443       eval_thunk_selector(p, (StgSelector *)q, rtsTrue);
444       return;
445
446   case IND:
447   case IND_OLDGEN:
448     // follow chains of indirections, don't evacuate them 
449     q = ((StgInd*)q)->indirectee;
450     *p = q;
451     goto loop;
452
453   case RET_BCO:
454   case RET_SMALL:
455   case RET_BIG:
456   case RET_DYN:
457   case UPDATE_FRAME:
458   case STOP_FRAME:
459   case CATCH_FRAME:
460   case CATCH_STM_FRAME:
461   case CATCH_RETRY_FRAME:
462   case ATOMICALLY_FRAME:
463     // shouldn't see these 
464     barf("evacuate: stack frame at %p\n", q);
465
466   case PAP:
467       copy(p,q,pap_sizeW((StgPAP*)q),stp);
468       return;
469
470   case AP:
471       copy(p,q,ap_sizeW((StgAP*)q),stp);
472       return;
473
474   case AP_STACK:
475       copy(p,q,ap_stack_sizeW((StgAP_STACK*)q),stp);
476       return;
477
478   case EVACUATED:
479     /* Already evacuated, just return the forwarding address.
480      * HOWEVER: if the requested destination generation (gct->evac_step) is
481      * older than the actual generation (because the object was
482      * already evacuated to a younger generation) then we have to
483      * set the gct->failed_to_evac flag to indicate that we couldn't 
484      * manage to promote the object to the desired generation.
485      */
486     /* 
487      * Optimisation: the check is fairly expensive, but we can often
488      * shortcut it if either the required generation is 0, or the
489      * current object (the EVACUATED) is in a high enough generation.
490      * We know that an EVACUATED always points to an object in the
491      * same or an older generation.  stp is the lowest step that the
492      * current object would be evacuated to, so we only do the full
493      * check if stp is too low.
494      */
495   {
496       StgClosure *e = ((StgEvacuated*)q)->evacuee;
497       *p = e;
498       if (stp < gct->evac_step) {  // optimisation 
499           if (Bdescr((P_)e)->step < gct->evac_step) {
500               gct->failed_to_evac = rtsTrue;
501               TICK_GC_FAILED_PROMOTION();
502           }
503       }
504       return;
505   }
506
507   case ARR_WORDS:
508       // just copy the block 
509       copy(p,q,arr_words_sizeW((StgArrWords *)q),stp);
510       return;
511
512   case MUT_ARR_PTRS_CLEAN:
513   case MUT_ARR_PTRS_DIRTY:
514   case MUT_ARR_PTRS_FROZEN:
515   case MUT_ARR_PTRS_FROZEN0:
516       // just copy the block 
517       copy(p,q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
518       return;
519
520   case TSO:
521     {
522       StgTSO *tso = (StgTSO *)q;
523
524       /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
525        */
526       if (tso->what_next == ThreadRelocated) {
527         q = (StgClosure *)tso->link;
528         *p = q;
529         goto loop;
530       }
531
532       /* To evacuate a small TSO, we need to relocate the update frame
533        * list it contains.  
534        */
535       {
536           StgTSO *new_tso;
537           StgPtr r, s;
538
539           copyPart(p,(StgClosure *)tso, tso_sizeW(tso), sizeofW(StgTSO), stp);
540           new_tso = (StgTSO *)*p;
541           move_TSO(tso, new_tso);
542           for (r = tso->sp, s = new_tso->sp;
543                r < tso->stack+tso->stack_size;) {
544               *s++ = *r++;
545           }
546           return;
547       }
548     }
549
550   case TREC_HEADER: 
551       copy(p,q,sizeofW(StgTRecHeader),stp);
552       return;
553
554   case TVAR_WATCH_QUEUE:
555       copy(p,q,sizeofW(StgTVarWatchQueue),stp);
556       return;
557
558   case TVAR:
559       copy(p,q,sizeofW(StgTVar),stp);
560       return;
561     
562   case TREC_CHUNK:
563       copy(p,q,sizeofW(StgTRecChunk),stp);
564       return;
565
566   case ATOMIC_INVARIANT:
567       copy(p,q,sizeofW(StgAtomicInvariant),stp);
568       return;
569
570   case INVARIANT_CHECK_QUEUE:
571       copy(p,q,sizeofW(StgInvariantCheckQueue),stp);
572       return;
573
574   default:
575     barf("evacuate: strange closure type %d", (int)(info->type));
576   }
577
578   barf("evacuate");
579 }
580
581 #undef copy
582 #undef copy_tag
583 #undef copyPart
584 #undef evacuate