src/share/vm/gc_implementation/parallelScavenge/psMarkSweepDecorator.cpp

Fri, 16 Mar 2012 16:14:04 +0100

author
nloodin
date
Fri, 16 Mar 2012 16:14:04 +0100
changeset 3665
8a729074feae
parent 3290
d06a2d7fcd5b
child 4037
da91efe96a93
permissions
-rw-r--r--

7154517: Build error in hotspot-gc without precompiled headers
Reviewed-by: jcoomes, brutisso

     1 /*
     2  * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #include "precompiled.hpp"
    26 #include "classfile/systemDictionary.hpp"
    27 #include "gc_implementation/parallelScavenge/objectStartArray.hpp"
    28 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
    29 #include "gc_implementation/parallelScavenge/psMarkSweep.hpp"
    30 #include "gc_implementation/parallelScavenge/psMarkSweepDecorator.hpp"
    31 #include "gc_implementation/shared/liveRange.hpp"
    32 #include "gc_implementation/shared/markSweep.inline.hpp"
    33 #include "gc_implementation/shared/spaceDecorator.hpp"
    34 #include "oops/oop.inline.hpp"
    36 PSMarkSweepDecorator* PSMarkSweepDecorator::_destination_decorator = NULL;
    39 void PSMarkSweepDecorator::set_destination_decorator_tenured() {
    40   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
    41   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
    43   _destination_decorator = heap->old_gen()->object_mark_sweep();
    44 }
    46 void PSMarkSweepDecorator::set_destination_decorator_perm_gen() {
    47   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
    48   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
    50   _destination_decorator = heap->perm_gen()->object_mark_sweep();
    51 }
    53 void PSMarkSweepDecorator::advance_destination_decorator() {
    54   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
    55   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
    57   assert(_destination_decorator != NULL, "Sanity");
    58   guarantee(_destination_decorator != heap->perm_gen()->object_mark_sweep(), "Cannot advance perm gen decorator");
    60   PSMarkSweepDecorator* first = heap->old_gen()->object_mark_sweep();
    61   PSMarkSweepDecorator* second = heap->young_gen()->eden_mark_sweep();
    62   PSMarkSweepDecorator* third = heap->young_gen()->from_mark_sweep();
    63   PSMarkSweepDecorator* fourth = heap->young_gen()->to_mark_sweep();
    65   if ( _destination_decorator == first ) {
    66     _destination_decorator = second;
    67   } else if ( _destination_decorator == second ) {
    68     _destination_decorator = third;
    69   } else if ( _destination_decorator == third ) {
    70     _destination_decorator = fourth;
    71   } else {
    72     fatal("PSMarkSweep attempting to advance past last compaction area");
    73   }
    74 }
    76 PSMarkSweepDecorator* PSMarkSweepDecorator::destination_decorator() {
    77   assert(_destination_decorator != NULL, "Sanity");
    79   return _destination_decorator;
    80 }
    82 // FIX ME FIX ME FIX ME FIX ME!!!!!!!!!
    83 // The object forwarding code is duplicated. Factor this out!!!!!
    84 //
    85 // This method "precompacts" objects inside its space to dest. It places forwarding
    86 // pointers into markOops for use by adjust_pointers. If "dest" should overflow, we
    87 // finish by compacting into our own space.
    89 void PSMarkSweepDecorator::precompact() {
    90   // Reset our own compact top.
    91   set_compaction_top(space()->bottom());
    93   /* We allow some amount of garbage towards the bottom of the space, so
    94    * we don't start compacting before there is a significant gain to be made.
    95    * Occasionally, we want to ensure a full compaction, which is determined
    96    * by the MarkSweepAlwaysCompactCount parameter. This is a significant
    97    * performance improvement!
    98    */
    99   bool skip_dead = (MarkSweepAlwaysCompactCount < 1)
   100     || ((PSMarkSweep::total_invocations() % MarkSweepAlwaysCompactCount) != 0);
   102   size_t allowed_deadspace = 0;
   103   if (skip_dead) {
   104     const size_t ratio = allowed_dead_ratio();
   105     allowed_deadspace = space()->capacity_in_words() * ratio / 100;
   106   }
   108   // Fetch the current destination decorator
   109   PSMarkSweepDecorator* dest = destination_decorator();
   110   ObjectStartArray* start_array = dest->start_array();
   112   HeapWord* compact_top = dest->compaction_top();
   113   HeapWord* compact_end = dest->space()->end();
   115   HeapWord* q = space()->bottom();
   116   HeapWord* t = space()->top();
   118   HeapWord*  end_of_live= q;    /* One byte beyond the last byte of the last
   119                                    live object. */
   120   HeapWord*  first_dead = space()->end(); /* The first dead object. */
   121   LiveRange* liveRange  = NULL; /* The current live range, recorded in the
   122                                    first header of preceding free area. */
   123   _first_dead = first_dead;
   125   const intx interval = PrefetchScanIntervalInBytes;
   127   while (q < t) {
   128     assert(oop(q)->mark()->is_marked() || oop(q)->mark()->is_unlocked() ||
   129            oop(q)->mark()->has_bias_pattern(),
   130            "these are the only valid states during a mark sweep");
   131     if (oop(q)->is_gc_marked()) {
   132       /* prefetch beyond q */
   133       Prefetch::write(q, interval);
   134       size_t size = oop(q)->size();
   136       size_t compaction_max_size = pointer_delta(compact_end, compact_top);
   138       // This should only happen if a space in the young gen overflows the
   139       // old gen. If that should happen, we null out the start_array, because
   140       // the young spaces are not covered by one.
   141       while(size > compaction_max_size) {
   142         // First record the last compact_top
   143         dest->set_compaction_top(compact_top);
   145         // Advance to the next compaction decorator
   146         advance_destination_decorator();
   147         dest = destination_decorator();
   149         // Update compaction info
   150         start_array = dest->start_array();
   151         compact_top = dest->compaction_top();
   152         compact_end = dest->space()->end();
   153         assert(compact_top == dest->space()->bottom(), "Advanced to space already in use");
   154         assert(compact_end > compact_top, "Must always be space remaining");
   155         compaction_max_size =
   156           pointer_delta(compact_end, compact_top);
   157       }
   159       // store the forwarding pointer into the mark word
   160       if (q != compact_top) {
   161         oop(q)->forward_to(oop(compact_top));
   162         assert(oop(q)->is_gc_marked(), "encoding the pointer should preserve the mark");
   163       } else {
   164         // if the object isn't moving we can just set the mark to the default
   165         // mark and handle it specially later on.
   166         oop(q)->init_mark();
   167         assert(oop(q)->forwardee() == NULL, "should be forwarded to NULL");
   168       }
   170       // Update object start array
   171       if (start_array) {
   172         start_array->allocate_block(compact_top);
   173       }
   175       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::register_live_oop(oop(q), size));
   176       compact_top += size;
   177       assert(compact_top <= dest->space()->end(),
   178         "Exceeding space in destination");
   180       q += size;
   181       end_of_live = q;
   182     } else {
   183       /* run over all the contiguous dead objects */
   184       HeapWord* end = q;
   185       do {
   186         /* prefetch beyond end */
   187         Prefetch::write(end, interval);
   188         end += oop(end)->size();
   189       } while (end < t && (!oop(end)->is_gc_marked()));
   191       /* see if we might want to pretend this object is alive so that
   192        * we don't have to compact quite as often.
   193        */
   194       if (allowed_deadspace > 0 && q == compact_top) {
   195         size_t sz = pointer_delta(end, q);
   196         if (insert_deadspace(allowed_deadspace, q, sz)) {
   197           size_t compaction_max_size = pointer_delta(compact_end, compact_top);
   199           // This should only happen if a space in the young gen overflows the
   200           // old gen. If that should happen, we null out the start_array, because
   201           // the young spaces are not covered by one.
   202           while (sz > compaction_max_size) {
   203             // First record the last compact_top
   204             dest->set_compaction_top(compact_top);
   206             // Advance to the next compaction decorator
   207             advance_destination_decorator();
   208             dest = destination_decorator();
   210             // Update compaction info
   211             start_array = dest->start_array();
   212             compact_top = dest->compaction_top();
   213             compact_end = dest->space()->end();
   214             assert(compact_top == dest->space()->bottom(), "Advanced to space already in use");
   215             assert(compact_end > compact_top, "Must always be space remaining");
   216             compaction_max_size =
   217               pointer_delta(compact_end, compact_top);
   218           }
   220           // store the forwarding pointer into the mark word
   221           if (q != compact_top) {
   222             oop(q)->forward_to(oop(compact_top));
   223             assert(oop(q)->is_gc_marked(), "encoding the pointer should preserve the mark");
   224           } else {
   225             // if the object isn't moving we can just set the mark to the default
   226             // mark and handle it specially later on.
   227             oop(q)->init_mark();
   228             assert(oop(q)->forwardee() == NULL, "should be forwarded to NULL");
   229           }
   231           // Update object start array
   232           if (start_array) {
   233             start_array->allocate_block(compact_top);
   234           }
   236           VALIDATE_MARK_SWEEP_ONLY(MarkSweep::register_live_oop(oop(q), sz));
   237           compact_top += sz;
   238           assert(compact_top <= dest->space()->end(),
   239             "Exceeding space in destination");
   241           q = end;
   242           end_of_live = end;
   243           continue;
   244         }
   245       }
   247       /* for the previous LiveRange, record the end of the live objects. */
   248       if (liveRange) {
   249         liveRange->set_end(q);
   250       }
   252       /* record the current LiveRange object.
   253        * liveRange->start() is overlaid on the mark word.
   254        */
   255       liveRange = (LiveRange*)q;
   256       liveRange->set_start(end);
   257       liveRange->set_end(end);
   259       /* see if this is the first dead region. */
   260       if (q < first_dead) {
   261         first_dead = q;
   262       }
   264       /* move on to the next object */
   265       q = end;
   266     }
   267   }
   269   assert(q == t, "just checking");
   270   if (liveRange != NULL) {
   271     liveRange->set_end(q);
   272   }
   273   _end_of_live = end_of_live;
   274   if (end_of_live < first_dead) {
   275     first_dead = end_of_live;
   276   }
   277   _first_dead = first_dead;
   279   // Update compaction top
   280   dest->set_compaction_top(compact_top);
   281 }
   283 bool PSMarkSweepDecorator::insert_deadspace(size_t& allowed_deadspace_words,
   284                                             HeapWord* q, size_t deadlength) {
   285   if (allowed_deadspace_words >= deadlength) {
   286     allowed_deadspace_words -= deadlength;
   287     CollectedHeap::fill_with_object(q, deadlength);
   288     oop(q)->set_mark(oop(q)->mark()->set_marked());
   289     assert((int) deadlength == oop(q)->size(), "bad filler object size");
   290     // Recall that we required "q == compaction_top".
   291     return true;
   292   } else {
   293     allowed_deadspace_words = 0;
   294     return false;
   295   }
   296 }
   298 void PSMarkSweepDecorator::adjust_pointers() {
   299   // adjust all the interior pointers to point at the new locations of objects
   300   // Used by MarkSweep::mark_sweep_phase3()
   302   HeapWord* q = space()->bottom();
   303   HeapWord* t = _end_of_live;  // Established by "prepare_for_compaction".
   305   assert(_first_dead <= _end_of_live, "Stands to reason, no?");
   307   if (q < t && _first_dead > q &&
   308       !oop(q)->is_gc_marked()) {
   309     // we have a chunk of the space which hasn't moved and we've
   310     // reinitialized the mark word during the previous pass, so we can't
   311     // use is_gc_marked for the traversal.
   312     HeapWord* end = _first_dead;
   314     while (q < end) {
   315       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::track_interior_pointers(oop(q)));
   316       // point all the oops to the new location
   317       size_t size = oop(q)->adjust_pointers();
   318       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::check_interior_pointers());
   319       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::validate_live_oop(oop(q), size));
   320       q += size;
   321     }
   323     if (_first_dead == t) {
   324       q = t;
   325     } else {
   326       // $$$ This is funky.  Using this to read the previously written
   327       // LiveRange.  See also use below.
   328       q = (HeapWord*)oop(_first_dead)->mark()->decode_pointer();
   329     }
   330   }
   331   const intx interval = PrefetchScanIntervalInBytes;
   333   debug_only(HeapWord* prev_q = NULL);
   334   while (q < t) {
   335     // prefetch beyond q
   336     Prefetch::write(q, interval);
   337     if (oop(q)->is_gc_marked()) {
   338       // q is alive
   339       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::track_interior_pointers(oop(q)));
   340       // point all the oops to the new location
   341       size_t size = oop(q)->adjust_pointers();
   342       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::check_interior_pointers());
   343       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::validate_live_oop(oop(q), size));
   344       debug_only(prev_q = q);
   345       q += size;
   346     } else {
   347       // q is not a live object, so its mark should point at the next
   348       // live object
   349       debug_only(prev_q = q);
   350       q = (HeapWord*) oop(q)->mark()->decode_pointer();
   351       assert(q > prev_q, "we should be moving forward through memory");
   352     }
   353   }
   355   assert(q == t, "just checking");
   356 }
   358 void PSMarkSweepDecorator::compact(bool mangle_free_space ) {
   359   // Copy all live objects to their new location
   360   // Used by MarkSweep::mark_sweep_phase4()
   362   HeapWord*       q = space()->bottom();
   363   HeapWord* const t = _end_of_live;
   364   debug_only(HeapWord* prev_q = NULL);
   366   if (q < t && _first_dead > q &&
   367       !oop(q)->is_gc_marked()) {
   368 #ifdef ASSERT
   369     // we have a chunk of the space which hasn't moved and we've reinitialized the
   370     // mark word during the previous pass, so we can't use is_gc_marked for the
   371     // traversal.
   372     HeapWord* const end = _first_dead;
   374     while (q < end) {
   375       size_t size = oop(q)->size();
   376       assert(!oop(q)->is_gc_marked(), "should be unmarked (special dense prefix handling)");
   377       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::live_oop_moved_to(q, size, q));
   378       debug_only(prev_q = q);
   379       q += size;
   380     }
   381 #endif
   383     if (_first_dead == t) {
   384       q = t;
   385     } else {
   386       // $$$ Funky
   387       q = (HeapWord*) oop(_first_dead)->mark()->decode_pointer();
   388     }
   389   }
   391   const intx scan_interval = PrefetchScanIntervalInBytes;
   392   const intx copy_interval = PrefetchCopyIntervalInBytes;
   394   while (q < t) {
   395     if (!oop(q)->is_gc_marked()) {
   396       // mark is pointer to next marked oop
   397       debug_only(prev_q = q);
   398       q = (HeapWord*) oop(q)->mark()->decode_pointer();
   399       assert(q > prev_q, "we should be moving forward through memory");
   400     } else {
   401       // prefetch beyond q
   402       Prefetch::read(q, scan_interval);
   404       // size and destination
   405       size_t size = oop(q)->size();
   406       HeapWord* compaction_top = (HeapWord*)oop(q)->forwardee();
   408       // prefetch beyond compaction_top
   409       Prefetch::write(compaction_top, copy_interval);
   411       // copy object and reinit its mark
   412       VALIDATE_MARK_SWEEP_ONLY(MarkSweep::live_oop_moved_to(q, size, compaction_top));
   413       assert(q != compaction_top, "everything in this pass should be moving");
   414       Copy::aligned_conjoint_words(q, compaction_top, size);
   415       oop(compaction_top)->init_mark();
   416       assert(oop(compaction_top)->klass() != NULL, "should have a class");
   418       debug_only(prev_q = q);
   419       q += size;
   420     }
   421   }
   423   assert(compaction_top() >= space()->bottom() && compaction_top() <= space()->end(),
   424          "should point inside space");
   425   space()->set_top(compaction_top());
   427   if (mangle_free_space) {
   428     space()->mangle_unused_area();
   429   }
   430 }

mercurial