src/share/vm/memory/genMarkSweep.cpp

Tue, 23 Nov 2010 13:22:55 -0800

author
stefank
date
Tue, 23 Nov 2010 13:22:55 -0800
changeset 2314
f95d63e2154a
parent 2191
894b1d7c7e01
child 2467
9afee0b9fc1d
permissions
-rw-r--r--

6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg

     1 /*
     2  * Copyright (c) 2001, 2010, 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/javaClasses.hpp"
    27 #include "classfile/symbolTable.hpp"
    28 #include "classfile/systemDictionary.hpp"
    29 #include "classfile/vmSymbols.hpp"
    30 #include "code/codeCache.hpp"
    31 #include "code/icBuffer.hpp"
    32 #include "gc_interface/collectedHeap.inline.hpp"
    33 #include "memory/genCollectedHeap.hpp"
    34 #include "memory/genMarkSweep.hpp"
    35 #include "memory/genOopClosures.inline.hpp"
    36 #include "memory/generation.inline.hpp"
    37 #include "memory/modRefBarrierSet.hpp"
    38 #include "memory/referencePolicy.hpp"
    39 #include "memory/space.hpp"
    40 #include "oops/instanceRefKlass.hpp"
    41 #include "oops/oop.inline.hpp"
    42 #include "prims/jvmtiExport.hpp"
    43 #include "runtime/fprofiler.hpp"
    44 #include "runtime/handles.inline.hpp"
    45 #include "runtime/synchronizer.hpp"
    46 #include "runtime/vmThread.hpp"
    47 #include "utilities/copy.hpp"
    48 #include "utilities/events.hpp"
    49 #ifdef TARGET_OS_FAMILY_linux
    50 # include "thread_linux.inline.hpp"
    51 #endif
    52 #ifdef TARGET_OS_FAMILY_solaris
    53 # include "thread_solaris.inline.hpp"
    54 #endif
    55 #ifdef TARGET_OS_FAMILY_windows
    56 # include "thread_windows.inline.hpp"
    57 #endif
    59 void GenMarkSweep::invoke_at_safepoint(int level, ReferenceProcessor* rp,
    60   bool clear_all_softrefs) {
    61   assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
    63   GenCollectedHeap* gch = GenCollectedHeap::heap();
    64 #ifdef ASSERT
    65   if (gch->collector_policy()->should_clear_all_soft_refs()) {
    66     assert(clear_all_softrefs, "Policy should have been checked earlier");
    67   }
    68 #endif
    70   // hook up weak ref data so it can be used during Mark-Sweep
    71   assert(ref_processor() == NULL, "no stomping");
    72   assert(rp != NULL, "should be non-NULL");
    73   _ref_processor = rp;
    74   rp->setup_policy(clear_all_softrefs);
    76   TraceTime t1("Full GC", PrintGC && !PrintGCDetails, true, gclog_or_tty);
    78   // When collecting the permanent generation methodOops may be moving,
    79   // so we either have to flush all bcp data or convert it into bci.
    80   CodeCache::gc_prologue();
    81   Threads::gc_prologue();
    83   // Increment the invocation count for the permanent generation, since it is
    84   // implicitly collected whenever we do a full mark sweep collection.
    85   gch->perm_gen()->stat_record()->invocations++;
    87   // Capture heap size before collection for printing.
    88   size_t gch_prev_used = gch->used();
    90   // Some of the card table updates below assume that the perm gen is
    91   // also being collected.
    92   assert(level == gch->n_gens() - 1,
    93          "All generations are being collected, ergo perm gen too.");
    95   // Capture used regions for each generation that will be
    96   // subject to collection, so that card table adjustments can
    97   // be made intelligently (see clear / invalidate further below).
    98   gch->save_used_regions(level, true /* perm */);
   100   allocate_stacks();
   102   mark_sweep_phase1(level, clear_all_softrefs);
   104   mark_sweep_phase2();
   106   // Don't add any more derived pointers during phase3
   107   COMPILER2_PRESENT(assert(DerivedPointerTable::is_active(), "Sanity"));
   108   COMPILER2_PRESENT(DerivedPointerTable::set_active(false));
   110   mark_sweep_phase3(level);
   112   VALIDATE_MARK_SWEEP_ONLY(
   113     if (ValidateMarkSweep) {
   114       guarantee(_root_refs_stack->length() == 0, "should be empty by now");
   115     }
   116   )
   118   mark_sweep_phase4();
   120   VALIDATE_MARK_SWEEP_ONLY(
   121     if (ValidateMarkSweep) {
   122       guarantee(_live_oops->length() == _live_oops_moved_to->length(),
   123                 "should be the same size");
   124     }
   125   )
   127   restore_marks();
   129   // Set saved marks for allocation profiler (and other things? -- dld)
   130   // (Should this be in general part?)
   131   gch->save_marks();
   133   deallocate_stacks();
   135   // If compaction completely evacuated all generations younger than this
   136   // one, then we can clear the card table.  Otherwise, we must invalidate
   137   // it (consider all cards dirty).  In the future, we might consider doing
   138   // compaction within generations only, and doing card-table sliding.
   139   bool all_empty = true;
   140   for (int i = 0; all_empty && i < level; i++) {
   141     Generation* g = gch->get_gen(i);
   142     all_empty = all_empty && gch->get_gen(i)->used() == 0;
   143   }
   144   GenRemSet* rs = gch->rem_set();
   145   // Clear/invalidate below make use of the "prev_used_regions" saved earlier.
   146   if (all_empty) {
   147     // We've evacuated all generations below us.
   148     Generation* g = gch->get_gen(level);
   149     rs->clear_into_younger(g, true /* perm */);
   150   } else {
   151     // Invalidate the cards corresponding to the currently used
   152     // region and clear those corresponding to the evacuated region
   153     // of all generations just collected (i.e. level and younger).
   154     rs->invalidate_or_clear(gch->get_gen(level),
   155                             true /* younger */,
   156                             true /* perm */);
   157   }
   159   Threads::gc_epilogue();
   160   CodeCache::gc_epilogue();
   162   if (PrintGC && !PrintGCDetails) {
   163     gch->print_heap_change(gch_prev_used);
   164   }
   166   // refs processing: clean slate
   167   _ref_processor = NULL;
   169   // Update heap occupancy information which is used as
   170   // input to soft ref clearing policy at the next gc.
   171   Universe::update_heap_info_at_gc();
   173   // Update time of last gc for all generations we collected
   174   // (which curently is all the generations in the heap).
   175   gch->update_time_of_last_gc(os::javaTimeMillis());
   176 }
   178 void GenMarkSweep::allocate_stacks() {
   179   GenCollectedHeap* gch = GenCollectedHeap::heap();
   180   // Scratch request on behalf of oldest generation; will do no
   181   // allocation.
   182   ScratchBlock* scratch = gch->gather_scratch(gch->_gens[gch->_n_gens-1], 0);
   184   // $$$ To cut a corner, we'll only use the first scratch block, and then
   185   // revert to malloc.
   186   if (scratch != NULL) {
   187     _preserved_count_max =
   188       scratch->num_words * HeapWordSize / sizeof(PreservedMark);
   189   } else {
   190     _preserved_count_max = 0;
   191   }
   193   _preserved_marks = (PreservedMark*)scratch;
   194   _preserved_count = 0;
   196 #ifdef VALIDATE_MARK_SWEEP
   197   if (ValidateMarkSweep) {
   198     _root_refs_stack    = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true);
   199     _other_refs_stack   = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true);
   200     _adjusted_pointers  = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true);
   201     _live_oops          = new (ResourceObj::C_HEAP) GrowableArray<oop>(100, true);
   202     _live_oops_moved_to = new (ResourceObj::C_HEAP) GrowableArray<oop>(100, true);
   203     _live_oops_size     = new (ResourceObj::C_HEAP) GrowableArray<size_t>(100, true);
   204   }
   205   if (RecordMarkSweepCompaction) {
   206     if (_cur_gc_live_oops == NULL) {
   207       _cur_gc_live_oops           = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
   208       _cur_gc_live_oops_moved_to  = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
   209       _cur_gc_live_oops_size      = new(ResourceObj::C_HEAP) GrowableArray<size_t>(100, true);
   210       _last_gc_live_oops          = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
   211       _last_gc_live_oops_moved_to = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
   212       _last_gc_live_oops_size     = new(ResourceObj::C_HEAP) GrowableArray<size_t>(100, true);
   213     } else {
   214       _cur_gc_live_oops->clear();
   215       _cur_gc_live_oops_moved_to->clear();
   216       _cur_gc_live_oops_size->clear();
   217     }
   218   }
   219 #endif
   220 }
   223 void GenMarkSweep::deallocate_stacks() {
   224   if (!UseG1GC) {
   225     GenCollectedHeap* gch = GenCollectedHeap::heap();
   226     gch->release_scratch();
   227   }
   229   _preserved_mark_stack.clear(true);
   230   _preserved_oop_stack.clear(true);
   231   _marking_stack.clear();
   232   _objarray_stack.clear(true);
   233   _revisit_klass_stack.clear(true);
   234   _revisit_mdo_stack.clear(true);
   236 #ifdef VALIDATE_MARK_SWEEP
   237   if (ValidateMarkSweep) {
   238     delete _root_refs_stack;
   239     delete _other_refs_stack;
   240     delete _adjusted_pointers;
   241     delete _live_oops;
   242     delete _live_oops_size;
   243     delete _live_oops_moved_to;
   244     _live_oops_index = 0;
   245     _live_oops_index_at_perm = 0;
   246   }
   247 #endif
   248 }
   250 void GenMarkSweep::mark_sweep_phase1(int level,
   251                                   bool clear_all_softrefs) {
   252   // Recursively traverse all live objects and mark them
   253   EventMark m("1 mark object");
   254   TraceTime tm("phase 1", PrintGC && Verbose, true, gclog_or_tty);
   255   trace(" 1");
   257   VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
   259   GenCollectedHeap* gch = GenCollectedHeap::heap();
   261   // Because follow_root_closure is created statically, cannot
   262   // use OopsInGenClosure constructor which takes a generation,
   263   // as the Universe has not been created when the static constructors
   264   // are run.
   265   follow_root_closure.set_orig_generation(gch->get_gen(level));
   267   gch->gen_process_strong_roots(level,
   268                                 false, // Younger gens are not roots.
   269                                 true,  // activate StrongRootsScope
   270                                 true,  // Collecting permanent generation.
   271                                 SharedHeap::SO_SystemClasses,
   272                                 &follow_root_closure,
   273                                 true,   // walk code active on stacks
   274                                 &follow_root_closure);
   276   // Process reference objects found during marking
   277   {
   278     ref_processor()->setup_policy(clear_all_softrefs);
   279     ref_processor()->process_discovered_references(
   280       &is_alive, &keep_alive, &follow_stack_closure, NULL);
   281   }
   283   // Follow system dictionary roots and unload classes
   284   bool purged_class = SystemDictionary::do_unloading(&is_alive);
   286   // Follow code cache roots
   287   CodeCache::do_unloading(&is_alive, &keep_alive, purged_class);
   288   follow_stack(); // Flush marking stack
   290   // Update subklass/sibling/implementor links of live klasses
   291   follow_weak_klass_links();
   292   assert(_marking_stack.is_empty(), "just drained");
   294   // Visit memoized MDO's and clear any unmarked weak refs
   295   follow_mdo_weak_refs();
   296   assert(_marking_stack.is_empty(), "just drained");
   298   // Visit symbol and interned string tables and delete unmarked oops
   299   SymbolTable::unlink(&is_alive);
   300   StringTable::unlink(&is_alive);
   302   assert(_marking_stack.is_empty(), "stack should be empty by now");
   303 }
   306 void GenMarkSweep::mark_sweep_phase2() {
   307   // Now all live objects are marked, compute the new object addresses.
   309   // It is imperative that we traverse perm_gen LAST. If dead space is
   310   // allowed a range of dead object may get overwritten by a dead int
   311   // array. If perm_gen is not traversed last a klassOop may get
   312   // overwritten. This is fine since it is dead, but if the class has dead
   313   // instances we have to skip them, and in order to find their size we
   314   // need the klassOop!
   315   //
   316   // It is not required that we traverse spaces in the same order in
   317   // phase2, phase3 and phase4, but the ValidateMarkSweep live oops
   318   // tracking expects us to do so. See comment under phase4.
   320   GenCollectedHeap* gch = GenCollectedHeap::heap();
   321   Generation* pg = gch->perm_gen();
   323   EventMark m("2 compute new addresses");
   324   TraceTime tm("phase 2", PrintGC && Verbose, true, gclog_or_tty);
   325   trace("2");
   327   VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
   329   gch->prepare_for_compaction();
   331   VALIDATE_MARK_SWEEP_ONLY(_live_oops_index_at_perm = _live_oops_index);
   332   CompactPoint perm_cp(pg, NULL, NULL);
   333   pg->prepare_for_compaction(&perm_cp);
   334 }
   336 class GenAdjustPointersClosure: public GenCollectedHeap::GenClosure {
   337 public:
   338   void do_generation(Generation* gen) {
   339     gen->adjust_pointers();
   340   }
   341 };
   343 void GenMarkSweep::mark_sweep_phase3(int level) {
   344   GenCollectedHeap* gch = GenCollectedHeap::heap();
   345   Generation* pg = gch->perm_gen();
   347   // Adjust the pointers to reflect the new locations
   348   EventMark m("3 adjust pointers");
   349   TraceTime tm("phase 3", PrintGC && Verbose, true, gclog_or_tty);
   350   trace("3");
   352   VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
   354   // Needs to be done before the system dictionary is adjusted.
   355   pg->pre_adjust_pointers();
   357   // Because the two closures below are created statically, cannot
   358   // use OopsInGenClosure constructor which takes a generation,
   359   // as the Universe has not been created when the static constructors
   360   // are run.
   361   adjust_root_pointer_closure.set_orig_generation(gch->get_gen(level));
   362   adjust_pointer_closure.set_orig_generation(gch->get_gen(level));
   364   gch->gen_process_strong_roots(level,
   365                                 false, // Younger gens are not roots.
   366                                 true,  // activate StrongRootsScope
   367                                 true,  // Collecting permanent generation.
   368                                 SharedHeap::SO_AllClasses,
   369                                 &adjust_root_pointer_closure,
   370                                 false, // do not walk code
   371                                 &adjust_root_pointer_closure);
   373   // Now adjust pointers in remaining weak roots.  (All of which should
   374   // have been cleared if they pointed to non-surviving objects.)
   375   CodeBlobToOopClosure adjust_code_pointer_closure(&adjust_pointer_closure,
   376                                                    /*do_marking=*/ false);
   377   gch->gen_process_weak_roots(&adjust_root_pointer_closure,
   378                               &adjust_code_pointer_closure,
   379                               &adjust_pointer_closure);
   381   adjust_marks();
   382   GenAdjustPointersClosure blk;
   383   gch->generation_iterate(&blk, true);
   384   pg->adjust_pointers();
   385 }
   387 class GenCompactClosure: public GenCollectedHeap::GenClosure {
   388 public:
   389   void do_generation(Generation* gen) {
   390     gen->compact();
   391   }
   392 };
   394 void GenMarkSweep::mark_sweep_phase4() {
   395   // All pointers are now adjusted, move objects accordingly
   397   // It is imperative that we traverse perm_gen first in phase4. All
   398   // classes must be allocated earlier than their instances, and traversing
   399   // perm_gen first makes sure that all klassOops have moved to their new
   400   // location before any instance does a dispatch through it's klass!
   402   // The ValidateMarkSweep live oops tracking expects us to traverse spaces
   403   // in the same order in phase2, phase3 and phase4. We don't quite do that
   404   // here (perm_gen first rather than last), so we tell the validate code
   405   // to use a higher index (saved from phase2) when verifying perm_gen.
   406   GenCollectedHeap* gch = GenCollectedHeap::heap();
   407   Generation* pg = gch->perm_gen();
   409   EventMark m("4 compact heap");
   410   TraceTime tm("phase 4", PrintGC && Verbose, true, gclog_or_tty);
   411   trace("4");
   413   VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(true));
   415   pg->compact();
   417   VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
   419   GenCompactClosure blk;
   420   gch->generation_iterate(&blk, true);
   422   VALIDATE_MARK_SWEEP_ONLY(compaction_complete());
   424   pg->post_compact(); // Shared spaces verification.
   425 }

mercurial