001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.mmtk.plan.generational.copying;
014    
015    import org.mmtk.plan.generational.Gen;
016    import org.mmtk.plan.generational.GenCollector;
017    import org.mmtk.plan.Plan;
018    import org.mmtk.plan.TraceLocal;
019    import org.mmtk.policy.CopyLocal;
020    import org.mmtk.utility.ForwardingWord;
021    import org.mmtk.utility.HeaderByte;
022    import org.mmtk.utility.alloc.Allocator;
023    import org.mmtk.vm.VM;
024    
025    import org.vmmagic.unboxed.*;
026    import org.vmmagic.pragma.*;
027    
028    /**
029     * This class implements <i>per-collector thread</i> behavior and state for
030     * the <code>GenCopy</code> two-generational copying collector.<p>
031     *
032     * Specifically, this class defines semantics specific to the collection of
033     * the mature generation (<code>GenCollector</code> defines nursery semantics).
034     * In particular the mature space allocator is defined (for collection-time
035     * allocation into the mature space), and the mature space per-collector thread
036     * collection time semantics are defined.<p>
037     *
038     * @see GenCopy for a description of the <code>GenCopy</code> algorithm.
039     *
040     * @see GenCopy
041     * @see GenCopyMutator
042     * @see GenCollector
043     * @see org.mmtk.plan.StopTheWorldCollector
044     * @see org.mmtk.plan.CollectorContext
045     */
046    @Uninterruptible
047    public class GenCopyCollector extends GenCollector {
048    
049      /******************************************************************
050       * Instance fields
051       */
052    
053      /** The allocator for the mature space */
054      private final CopyLocal mature;
055    
056      /** The trace object for full-heap collections */
057      private final GenCopyMatureTraceLocal matureTrace;
058    
059      /****************************************************************************
060       *
061       * Initialization
062       */
063    
064      /**
065       * Constructor
066       */
067      public GenCopyCollector() {
068        mature = new CopyLocal(GenCopy.toSpace());
069        matureTrace = new GenCopyMatureTraceLocal(global().matureTrace, this);
070      }
071    
072      /****************************************************************************
073       *
074       * Collection-time allocation
075       */
076    
077      /**
078       * Allocate space for copying an object (this method <i>does not</i>
079       * copy the object, it only allocates space)
080       *
081       * @param original A reference to the original object
082       * @param bytes The size of the space to be allocated (in bytes)
083       * @param align The requested alignment.
084       * @param offset The alignment offset.
085       * @return The address of the first byte of the allocated region
086       */
087      @Inline
088      public Address allocCopy(ObjectReference original, int bytes,
089          int align, int offset, int allocator) {
090        if (allocator == Plan.ALLOC_LOS) {
091          if (VM.VERIFY_ASSERTIONS) VM.assertions._assert(Allocator.getMaximumAlignedSize(bytes, align) > Plan.MAX_NON_LOS_COPY_BYTES);
092          return los.alloc(bytes, align, offset);
093        } else {
094          if (VM.VERIFY_ASSERTIONS) {
095            VM.assertions._assert(bytes <= Plan.MAX_NON_LOS_COPY_BYTES);
096            VM.assertions._assert(allocator == GenCopy.ALLOC_MATURE_MINORGC ||
097                allocator == GenCopy.ALLOC_MATURE_MAJORGC);
098          }
099          return mature.alloc(bytes, align, offset);
100        }
101      }
102    
103      /**
104       * Perform any post-copy actions.  In this case we clear any bits used
105       * for this object's GC metadata.
106       *
107       * @param object The newly allocated object
108       * @param typeRef the type reference for the instance being created
109       * @param bytes The size of the space to be allocated (in bytes)
110       * @param allocator The allocator to allocate from
111       */
112      @Inline
113      public final void postCopy(ObjectReference object, ObjectReference typeRef,
114          int bytes, int allocator) {
115        ForwardingWord.clearForwardingBits(object);
116        if (allocator == Plan.ALLOC_LOS)
117          Plan.loSpace.initializeHeader(object, false);
118        else if (GenCopy.IGNORE_REMSETS)
119          GenCopy.immortalSpace.traceObject(getCurrentTrace(), object); // FIXME this does not look right
120        if (Gen.USE_OBJECT_BARRIER)
121          HeaderByte.markAsUnlogged(object);
122      }
123    
124    
125      /*****************************************************************************
126       *
127       * Collection
128       */
129    
130      /**
131       * Execute a per-collector collection phase.
132       *
133       * @param phaseId The phase to execute.
134       * @param primary True if this thread should peform local single-threaded
135       * actions.
136       */
137      public void collectionPhase(short phaseId, boolean primary) {
138        if (global().traceFullHeap()) {
139          if (phaseId == GenCopy.PREPARE) {
140            super.collectionPhase(phaseId, primary);
141            if (global().gcFullHeap) mature.rebind(GenCopy.toSpace());
142          }
143          if (phaseId == GenCopy.CLOSURE) {
144            matureTrace.completeTrace();
145            return;
146          }
147          if (phaseId == GenCopy.RELEASE) {
148            matureTrace.release();
149            super.collectionPhase(phaseId, primary);
150            return;
151          }
152        }
153        super.collectionPhase(phaseId, primary);
154      }
155    
156      /*****************************************************************************
157       *
158       * Miscellaneous
159       */
160    
161      /** @return The active global plan as a <code>GenCopy</code> instance. */
162      private static GenCopy global() {
163        return (GenCopy) VM.activePlan.global();
164      }
165    
166      /** Show the status of the mature allocator. */
167      protected final void showMature() {
168        mature.show();
169      }
170    
171      public final TraceLocal getFullHeapTrace() { return matureTrace; }
172    }