1 /*
   2  * Copyright (c) 2005, 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  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "gc_implementation/parallelScavenge/gcTaskManager.hpp"
  28 #include "gc_implementation/parallelScavenge/objectStartArray.hpp"
  29 #include "gc_implementation/parallelScavenge/parMarkBitMap.hpp"
  30 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
  31 #include "gc_implementation/parallelScavenge/psCompactionManager.hpp"
  32 #include "gc_implementation/parallelScavenge/psOldGen.hpp"
  33 #include "gc_implementation/parallelScavenge/psParallelCompact.hpp"
  34 #include "oops/objArrayKlass.inline.hpp"
  35 #include "oops/oop.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "oops/oop.pcgc.inline.hpp"
  38 
  39 PSOldGen*            ParCompactionManager::_old_gen = NULL;
  40 ParCompactionManager**  ParCompactionManager::_manager_array = NULL;
  41 OopTaskQueueSet*     ParCompactionManager::_stack_array = NULL;
  42 ParCompactionManager::ObjArrayTaskQueueSet*
  43   ParCompactionManager::_objarray_queues = NULL;
  44 ObjectStartArray*    ParCompactionManager::_start_array = NULL;
  45 ParMarkBitMap*       ParCompactionManager::_mark_bitmap = NULL;
  46 RegionTaskQueueSet*  ParCompactionManager::_region_array = NULL;
  47 
  48 ParCompactionManager::ParCompactionManager() :
  49     _action(CopyAndUpdate) {
  50 
  51   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
  52   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
  53 
  54   _old_gen = heap->old_gen();
  55   _start_array = old_gen()->start_array();
  56 
  57   marking_stack()->initialize();
  58   _objarray_stack.initialize();
  59   region_stack()->initialize();
  60 
  61   // Note that _revisit_klass_stack is allocated out of the
  62   // C heap (as opposed to out of ResourceArena).
  63   int size =
  64     (SystemDictionary::number_of_classes() * 2) * 2 / ParallelGCThreads;
  65   _revisit_klass_stack = new (ResourceObj::C_HEAP) GrowableArray<Klass*>(size, true);
  66   // From some experiments (#klass/k)^2 for k = 10 seems a better fit, but this will
  67   // have to do for now until we are able to investigate a more optimal setting.
  68   _revisit_mdo_stack = new (ResourceObj::C_HEAP) GrowableArray<DataLayout*>(size*2, true);
  69 }
  70 
  71 ParCompactionManager::~ParCompactionManager() {
  72   delete _revisit_klass_stack;
  73   delete _revisit_mdo_stack;
  74   // _manager_array and _stack_array are statics
  75   // shared with all instances of ParCompactionManager
  76   // should not be deallocated.
  77 }
  78 
  79 void ParCompactionManager::initialize(ParMarkBitMap* mbm) {
  80   assert(PSParallelCompact::gc_task_manager() != NULL,
  81     "Needed for initialization");
  82 
  83   _mark_bitmap = mbm;
  84 
  85   uint parallel_gc_threads = PSParallelCompact::gc_task_manager()->workers();
  86 
  87   assert(_manager_array == NULL, "Attempt to initialize twice");
  88   _manager_array = NEW_C_HEAP_ARRAY(ParCompactionManager*, parallel_gc_threads+1 );
  89   guarantee(_manager_array != NULL, "Could not allocate manager_array");
  90 
  91   _stack_array = new OopTaskQueueSet(parallel_gc_threads);
  92   guarantee(_stack_array != NULL, "Could not allocate stack_array");
  93   _objarray_queues = new ObjArrayTaskQueueSet(parallel_gc_threads);
  94   guarantee(_objarray_queues != NULL, "Could not allocate objarray_queues");
  95   _region_array = new RegionTaskQueueSet(parallel_gc_threads);
  96   guarantee(_region_array != NULL, "Could not allocate region_array");
  97 
  98   // Create and register the ParCompactionManager(s) for the worker threads.
  99   for(uint i=0; i<parallel_gc_threads; i++) {
 100     _manager_array[i] = new ParCompactionManager();
 101     guarantee(_manager_array[i] != NULL, "Could not create ParCompactionManager");
 102     stack_array()->register_queue(i, _manager_array[i]->marking_stack());
 103     _objarray_queues->register_queue(i, &_manager_array[i]->_objarray_stack);
 104     region_array()->register_queue(i, _manager_array[i]->region_stack());
 105   }
 106 
 107   // The VMThread gets its own ParCompactionManager, which is not available
 108   // for work stealing.
 109   _manager_array[parallel_gc_threads] = new ParCompactionManager();
 110   guarantee(_manager_array[parallel_gc_threads] != NULL,
 111     "Could not create ParCompactionManager");
 112   assert(PSParallelCompact::gc_task_manager()->workers() != 0,
 113     "Not initialized?");
 114 }
 115 
 116 bool ParCompactionManager::should_update() {
 117   assert(action() != NotValid, "Action is not set");
 118   return (action() == ParCompactionManager::Update) ||
 119          (action() == ParCompactionManager::CopyAndUpdate) ||
 120          (action() == ParCompactionManager::UpdateAndCopy);
 121 }
 122 
 123 bool ParCompactionManager::should_copy() {
 124   assert(action() != NotValid, "Action is not set");
 125   return (action() == ParCompactionManager::Copy) ||
 126          (action() == ParCompactionManager::CopyAndUpdate) ||
 127          (action() == ParCompactionManager::UpdateAndCopy);
 128 }
 129 
 130 bool ParCompactionManager::should_verify_only() {
 131   assert(action() != NotValid, "Action is not set");
 132   return action() == ParCompactionManager::VerifyUpdate;
 133 }
 134 
 135 bool ParCompactionManager::should_reset_only() {
 136   assert(action() != NotValid, "Action is not set");
 137   return action() == ParCompactionManager::ResetObjects;
 138 }
 139 
 140 ParCompactionManager*
 141 ParCompactionManager::gc_thread_compaction_manager(int index) {
 142   assert(index >= 0 && index < (int)ParallelGCThreads, "index out of range");
 143   assert(_manager_array != NULL, "Sanity");
 144   return _manager_array[index];
 145 }
 146 
 147 void ParCompactionManager::reset() {
 148   for(uint i=0; i<ParallelGCThreads+1; i++) {
 149     manager_array(i)->revisit_klass_stack()->clear();
 150     manager_array(i)->revisit_mdo_stack()->clear();
 151   }
 152 }
 153 
 154 void ParCompactionManager::follow_marking_stacks() {
 155   do {
 156     // Drain the overflow stack first, to allow stealing from the marking stack.
 157     oop obj;
 158     while (marking_stack()->pop_overflow(obj)) {
 159       obj->follow_contents(this);
 160     }
 161     while (marking_stack()->pop_local(obj)) {
 162       obj->follow_contents(this);
 163     }
 164 
 165     // Process ObjArrays one at a time to avoid marking stack bloat.
 166     ObjArrayTask task;
 167     if (_objarray_stack.pop_overflow(task)) {
 168       objArrayKlass* const k = (objArrayKlass*)task.obj()->blueprint();
 169       k->oop_follow_contents(this, task.obj(), task.index());
 170     } else if (_objarray_stack.pop_local(task)) {
 171       objArrayKlass* const k = (objArrayKlass*)task.obj()->blueprint();
 172       k->oop_follow_contents(this, task.obj(), task.index());
 173     }
 174   } while (!marking_stacks_empty());
 175 
 176   assert(marking_stacks_empty(), "Sanity");
 177 }
 178 
 179 void ParCompactionManager::drain_region_stacks() {
 180   do {
 181     // Drain overflow stack first so other threads can steal.
 182     size_t region_index;
 183     while (region_stack()->pop_overflow(region_index)) {
 184       PSParallelCompact::fill_and_update_region(this, region_index);
 185     }
 186 
 187     while (region_stack()->pop_local(region_index)) {
 188       PSParallelCompact::fill_and_update_region(this, region_index);
 189     }
 190   } while (!region_stack()->is_empty());
 191 }
 192 
 193 #ifdef ASSERT
 194 bool ParCompactionManager::stacks_have_been_allocated() {
 195   return (revisit_klass_stack()->data_addr() != NULL &&
 196           revisit_mdo_stack()->data_addr() != NULL);
 197 }
 198 #endif