remove EVACUATED: store the forwarding pointer in the info pointer
[ghc-hetmet.git] / rts / sm / Scav.c
index d8d158a..5d156ed 100644 (file)
@@ -1,6 +1,6 @@
 /* -----------------------------------------------------------------------------
  *
- * (c) The GHC Team 1998-2006
+ * (c) The GHC Team 1998-2008
  *
  * Generational garbage collector: scavenging functions
  *
@@ -16,6 +16,7 @@
 #include "Storage.h"
 #include "MBlock.h"
 #include "GC.h"
+#include "GCThread.h"
 #include "GCUtils.h"
 #include "Compact.h"
 #include "Evac.h"
@@ -131,6 +132,17 @@ scavenge_fun_srt(const StgInfoTable *info)
    Scavenge a TSO.
    -------------------------------------------------------------------------- */
 
+STATIC_INLINE void
+scavenge_TSO_link (StgTSO *tso)
+{
+    // We don't always chase the link field: TSOs on the blackhole
+    // queue are not automatically alive, so the link field is a
+    // "weak" pointer in that case.
+    if (tso->why_blocked != BlockedOnBlackHole) {
+        evacuate((StgClosure **)&tso->_link);
+    }
+}
+
 static void
 scavengeTSO (StgTSO *tso)
 {
@@ -155,13 +167,6 @@ scavengeTSO (StgTSO *tso)
     }
     evacuate((StgClosure **)&tso->blocked_exceptions);
     
-    // We don't always chase the link field: TSOs on the blackhole
-    // queue are not automatically alive, so the link field is a
-    // "weak" pointer in that case.
-    if (tso->why_blocked != BlockedOnBlackHole) {
-       evacuate((StgClosure **)&tso->link);
-    }
-
     // scavange current transaction record
     evacuate((StgClosure **)&tso->trec);
     
@@ -170,8 +175,15 @@ scavengeTSO (StgTSO *tso)
 
     if (gct->failed_to_evac) {
         tso->flags |= TSO_DIRTY;
+        scavenge_TSO_link(tso);
     } else {
         tso->flags &= ~TSO_DIRTY;
+        scavenge_TSO_link(tso);
+        if (gct->failed_to_evac) {
+            tso->flags |= TSO_LINK_DIRTY;
+        } else {
+            tso->flags &= ~TSO_LINK_DIRTY;
+        }
     }
 
     gct->eager_promotion = saved_eager;
@@ -516,7 +528,6 @@ linear_scan:
        case TSO:
        { 
             scavengeTSO((StgTSO*)p);
-           gct->failed_to_evac = rtsTrue; // always on the mutable list
            break;
        }
 
@@ -836,7 +847,6 @@ scavenge_one(StgPtr p)
     case TSO:
     {
        scavengeTSO((StgTSO*)p);
-       gct->failed_to_evac = rtsTrue; // always on the mutable list
        break;
     }
   
@@ -1025,14 +1035,17 @@ scavenge_mutable_list(generation *gen)
            case TSO: {
                StgTSO *tso = (StgTSO *)p;
                if ((tso->flags & TSO_DIRTY) == 0) {
-                   // A clean TSO: we don't have to traverse its
-                   // stack.  However, we *do* follow the link field:
-                   // we don't want to have to mark a TSO dirty just
-                   // because we put it on a different queue.
-                   if (tso->why_blocked != BlockedOnBlackHole) {
-                       evacuate((StgClosure **)&tso->link);
-                   }
-                   recordMutableGen_GC((StgClosure *)p,gen);
+                    // Must be on the mutable list because its link
+                    // field is dirty.
+                    ASSERT(tso->flags & TSO_LINK_DIRTY);
+
+                    scavenge_TSO_link(tso);
+                    if (gct->failed_to_evac) {
+                        recordMutableGen_GC((StgClosure *)p,gen);
+                        gct->failed_to_evac = rtsFalse;
+                    } else {
+                        tso->flags &= ~TSO_LINK_DIRTY;
+                    }
                    continue;
                }
            }
@@ -1067,6 +1080,8 @@ scavenge_static(void)
   StgClosure* p;
   const StgInfoTable *info;
 
+  debugTrace(DEBUG_gc, "scavenging static objects");
+
   /* Always evacuate straight to the oldest generation for static
    * objects */
   gct->evac_step = &oldest_gen->steps[0];
@@ -1236,17 +1251,22 @@ scavenge_stack(StgPtr p, StgPtr stack_end)
        // discarding it.
     {
         nat type;
-        type = get_itbl(((StgUpdateFrame *)p)->updatee)->type;
-       if (type == IND) {
-           ((StgUpdateFrame *)p)->updatee->header.info = 
-               (StgInfoTable *)&stg_IND_PERM_info;
-       } else if (type == IND_OLDGEN) {
-           ((StgUpdateFrame *)p)->updatee->header.info = 
-               (StgInfoTable *)&stg_IND_OLDGEN_PERM_info;
-        }            
-       evacuate(&((StgUpdateFrame *)p)->updatee);
-       p += sizeofW(StgUpdateFrame);
-       continue;
+        const StgInfoTable *i;
+
+        i = ((StgUpdateFrame *)p)->updatee->header.info;
+        if (!IS_FORWARDING_PTR(i)) {
+            type = get_itbl(((StgUpdateFrame *)p)->updatee)->type;
+            if (type == IND) {
+                ((StgUpdateFrame *)p)->updatee->header.info = 
+                    (StgInfoTable *)&stg_IND_PERM_info;
+            } else if (type == IND_OLDGEN) {
+                ((StgUpdateFrame *)p)->updatee->header.info = 
+                    (StgInfoTable *)&stg_IND_OLDGEN_PERM_info;
+            }            
+            evacuate(&((StgUpdateFrame *)p)->updatee);
+            p += sizeofW(StgUpdateFrame);
+            continue;
+        }
     }
 
       // small bitmap (< 32 entries, or 64 on a 64-bit machine) 
@@ -1353,7 +1373,7 @@ scavenge_large (step_workspace *ws)
     bdescr *bd;
     StgPtr p;
 
-    gct->evac_step = ws->stp;
+    gct->evac_step = ws->step;
 
     bd = ws->todo_large_objects;
     
@@ -1365,17 +1385,20 @@ scavenge_large (step_workspace *ws)
        // the front when evacuating.
        ws->todo_large_objects = bd->link;
        
-       ACQUIRE_SPIN_LOCK(&ws->stp->sync_large_objects);
-       dbl_link_onto(bd, &ws->stp->scavenged_large_objects);
-       ws->stp->n_scavenged_large_blocks += bd->blocks;
-       RELEASE_SPIN_LOCK(&ws->stp->sync_large_objects);
+       ACQUIRE_SPIN_LOCK(&ws->step->sync_large_objects);
+       dbl_link_onto(bd, &ws->step->scavenged_large_objects);
+       ws->step->n_scavenged_large_blocks += bd->blocks;
+       RELEASE_SPIN_LOCK(&ws->step->sync_large_objects);
        
        p = bd->start;
        if (scavenge_one(p)) {
-           if (ws->stp->gen_no > 0) {
-               recordMutableGen_GC((StgClosure *)p, ws->stp->gen);
+           if (ws->step->gen_no > 0) {
+               recordMutableGen_GC((StgClosure *)p, ws->step->gen);
            }
        }
+
+        // stats
+        gct->scanned += closure_sizeW((StgClosure*)p);
     }
 }
 
@@ -1383,129 +1406,93 @@ scavenge_large (step_workspace *ws)
    Scavenge a block
    ------------------------------------------------------------------------- */
 
-#define MINOR_GC
+#undef PARALLEL_GC
 #include "Scav.c-inc"
-#undef MINOR_GC
+
+#ifdef THREADED_RTS
+#define PARALLEL_GC
 #include "Scav.c-inc"
+#endif
 
 /* ----------------------------------------------------------------------------
-   Find the oldest full block to scavenge, and scavenge it.
+   Look for work to do.
+
+   We look for the oldest step that has either a todo block that can
+   be scanned, or a block of work on the global queue that we can
+   scan.
+
+   It is important to take work from the *oldest* generation that we
+   has work available, because that minimizes the likelihood of
+   evacuating objects into a young generation when they should have
+   been eagerly promoted.  This really does make a difference (the
+   cacheprof benchmark is one that is affected).
+
+   We also want to scan the todo block if possible before grabbing
+   work from the global queue, the reason being that we don't want to
+   steal work from the global queue and starve other threads if there
+   is other work we can usefully be doing.
    ------------------------------------------------------------------------- */
 
 static rtsBool
-scavenge_find_global_work (void)
+scavenge_find_work (void)
 {
-    bdescr *bd;
     int s;
-    rtsBool flag;
     step_workspace *ws;
+    rtsBool did_something, did_anything;
+    bdescr *bd;
 
-    gct->scav_global_work++;
+    gct->scav_find_work++;
 
-    flag = rtsFalse;
-    for (s = total_steps-1; s>=0; s--)
-    {
+    did_anything = rtsFalse;
+
+loop:
+    did_something = rtsFalse;
+    for (s = total_steps-1; s >= 0; s--) {
         if (s == 0 && RtsFlags.GcFlags.generations > 1) { 
             continue; 
         }
         ws = &gct->steps[s];
+        
+        gct->scan_bd = NULL;
+
+        // If we have a scan block with some work to do,
+        // scavenge everything up to the free pointer.
+        if (ws->todo_bd->u.scan < ws->todo_free)
+        {
+            if (n_gc_threads == 1) {
+                scavenge_block1(ws->todo_bd);
+            } else {
+                scavenge_block(ws->todo_bd);
+            }
+            did_something = rtsTrue;
+            break;
+        }
 
         // If we have any large objects to scavenge, do them now.
         if (ws->todo_large_objects) {
             scavenge_large(ws);
-            flag = rtsTrue;
+            did_something = rtsTrue;
+            break;
         }
 
         if ((bd = grab_todo_block(ws)) != NULL) {
-            // no need to assign this to ws->scan_bd, we're going
-            // to scavenge the whole thing and then push it on
-            // our scavd list.  This saves pushing out the
-            // scan_bd block, which might be partial.
-            if (N == 0) {
-                scavenge_block0(bd, bd->start);
+            if (n_gc_threads == 1) {
+                scavenge_block1(bd);
             } else {
-                scavenge_block(bd, bd->start);
+                scavenge_block(bd);
             }
-            push_scan_block(bd, ws);
-            return rtsTrue;
+            did_something = rtsTrue;
+            break;
         }
-
-        if (flag) return rtsTrue;
     }
-    return rtsFalse;
-}
-
-/* ----------------------------------------------------------------------------
-   Look for local work to do.
-
-   We can have outstanding scavenging to do if, for any of the workspaces,
-
-     - the scan block is the same as the todo block, and new objects
-       have been evacuated to the todo block.
-
-     - the scan block *was* the same as the todo block, but the todo
-       block filled up and a new one has been allocated.
-   ------------------------------------------------------------------------- */
-
-static rtsBool
-scavenge_find_local_work (void)
-{
-    int s;
-    step_workspace *ws;
-    rtsBool flag;
-
-    gct->scav_local_work++;
 
-    flag = rtsFalse;
-    for (s = total_steps-1; s >= 0; s--) {
-        if (s == 0 && RtsFlags.GcFlags.generations > 1) { 
-            continue; 
-        }
-        ws = &gct->steps[s];
-
-        if (ws->todo_bd != NULL)
-        {
-            ws->todo_bd->free = ws->todo_free;
-        }
-        
-        // If we have a todo block and no scan block, start
-        // scanning the todo block.
-        if (ws->scan_bd == NULL && ws->todo_bd != NULL)
-        {
-            ws->scan_bd = ws->todo_bd;
-            ws->scan = ws->scan_bd->start;
-        }
-        
-        // If we have a scan block with some work to do,
-        // scavenge everything up to the free pointer.
-        if (ws->scan != NULL && ws->scan < ws->scan_bd->free)
-        {
-            if (N == 0) {
-                scavenge_block0(ws->scan_bd, ws->scan);
-            } else {
-                scavenge_block(ws->scan_bd, ws->scan);
-            }
-            ws->scan = ws->scan_bd->free;
-            flag = rtsTrue;
-        }
-        
-        if (ws->scan_bd != NULL && ws->scan == ws->scan_bd->free
-            && ws->scan_bd != ws->todo_bd)
-        {
-            // we're not going to evac any more objects into
-            // this block, so push it now.
-            push_scan_block(ws->scan_bd, ws);
-            ws->scan_bd = NULL;
-            ws->scan = NULL;
-            // we might be able to scan the todo block now.  But
-            // don't do it right away: there might be full blocks
-            // waiting to be scanned as a result of scavenge_block above.
-            flag = rtsTrue; 
-        }
-        
-        if (flag) return rtsTrue;
+    if (did_something) {
+        did_anything = rtsTrue;
+        goto loop;
     }
-    return rtsFalse;
+    // only return when there is no more work to do
+
+    return did_anything;
 }
 
 /* ----------------------------------------------------------------------------
@@ -1538,8 +1525,7 @@ loop:
     // local work.  Only if all the global work has been exhausted
     // do we start scavenging the fragments of blocks in the local
     // workspaces.
-    if (scavenge_find_global_work()) goto loop;
-    if (scavenge_find_local_work())  goto loop;
+    if (scavenge_find_work()) goto loop;
     
     if (work_to_do) goto loop;
 }
@@ -1569,7 +1555,7 @@ any_work (void)
         }
         ws = &gct->steps[s];
         if (ws->todo_large_objects) return rtsTrue;
-        if (ws->stp->todos) return rtsTrue;
+        if (ws->step->todos) return rtsTrue;
     }
 
     gct->no_work++;