1 /*
   2  * Copyright (c) 2005, 2018, 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/parallel/gcTaskManager.hpp"
  28 #include "gc/parallel/objectStartArray.hpp"
  29 #include "gc/parallel/parMarkBitMap.inline.hpp"
  30 #include "gc/parallel/parallelScavengeHeap.hpp"
  31 #include "gc/parallel/psCompactionManager.inline.hpp"
  32 #include "gc/parallel/psOldGen.hpp"
  33 #include "gc/parallel/psParallelCompact.inline.hpp"
  34 #include "gc/shared/taskqueue.inline.hpp"
  35 #include "logging/log.hpp"
  36 #include "memory/iterator.inline.hpp"
  37 #include "oops/access.inline.hpp"
  38 #include "oops/compressedOops.inline.hpp"
  39 #include "oops/instanceKlass.inline.hpp"
  40 #include "oops/instanceMirrorKlass.inline.hpp"
  41 #include "oops/objArrayKlass.inline.hpp"
  42 #include "oops/oop.inline.hpp"
  43 #include "oops/valueArrayKlass.inline.hpp"
  44 #include "runtime/atomic.hpp"
  45 
  46 PSOldGen*            ParCompactionManager::_old_gen = NULL;
  47 ParCompactionManager**  ParCompactionManager::_manager_array = NULL;
  48 
  49 OopTaskQueueSet*     ParCompactionManager::_stack_array = NULL;
  50 ParCompactionManager::ObjArrayTaskQueueSet*
  51   ParCompactionManager::_objarray_queues = NULL;
  52 ObjectStartArray*    ParCompactionManager::_start_array = NULL;
  53 ParMarkBitMap*       ParCompactionManager::_mark_bitmap = NULL;
  54 RegionTaskQueueSet*  ParCompactionManager::_region_array = NULL;
  55 
  56 ParCompactionManager::ParCompactionManager() :
  57     _action(CopyAndUpdate) {
  58 
  59   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
  60 
  61   _old_gen = heap->old_gen();
  62   _start_array = old_gen()->start_array();
  63 
  64   marking_stack()->initialize();
  65   _objarray_stack.initialize();
  66   _region_stack.initialize();
  67 
  68   reset_bitmap_query_cache();
  69 }
  70 
  71 void ParCompactionManager::initialize(ParMarkBitMap* mbm) {
  72   assert(PSParallelCompact::gc_task_manager() != NULL,
  73     "Needed for initialization");
  74 
  75   _mark_bitmap = mbm;
  76 
  77   uint parallel_gc_threads = PSParallelCompact::gc_task_manager()->workers();
  78 
  79   assert(_manager_array == NULL, "Attempt to initialize twice");
  80   _manager_array = NEW_C_HEAP_ARRAY(ParCompactionManager*, parallel_gc_threads+1, mtGC);
  81   guarantee(_manager_array != NULL, "Could not allocate manager_array");
  82 
  83   _stack_array = new OopTaskQueueSet(parallel_gc_threads);
  84   guarantee(_stack_array != NULL, "Could not allocate stack_array");
  85   _objarray_queues = new ObjArrayTaskQueueSet(parallel_gc_threads);
  86   guarantee(_objarray_queues != NULL, "Could not allocate objarray_queues");
  87   _region_array = new RegionTaskQueueSet(parallel_gc_threads);
  88   guarantee(_region_array != NULL, "Could not allocate region_array");
  89 
  90   // Create and register the ParCompactionManager(s) for the worker threads.
  91   for(uint i=0; i<parallel_gc_threads; i++) {
  92     _manager_array[i] = new ParCompactionManager();
  93     guarantee(_manager_array[i] != NULL, "Could not create ParCompactionManager");
  94     stack_array()->register_queue(i, _manager_array[i]->marking_stack());
  95     _objarray_queues->register_queue(i, &_manager_array[i]->_objarray_stack);
  96     region_array()->register_queue(i, _manager_array[i]->region_stack());
  97   }
  98 
  99   // The VMThread gets its own ParCompactionManager, which is not available
 100   // for work stealing.
 101   _manager_array[parallel_gc_threads] = new ParCompactionManager();
 102   guarantee(_manager_array[parallel_gc_threads] != NULL,
 103     "Could not create ParCompactionManager");
 104   assert(PSParallelCompact::gc_task_manager()->workers() != 0,
 105     "Not initialized?");
 106 }
 107 
 108 void ParCompactionManager::reset_all_bitmap_query_caches() {
 109   uint parallel_gc_threads = PSParallelCompact::gc_task_manager()->workers();
 110   for (uint i=0; i<=parallel_gc_threads; i++) {
 111     _manager_array[i]->reset_bitmap_query_cache();
 112   }
 113 }
 114 
 115 bool ParCompactionManager::should_update() {
 116   assert(action() != NotValid, "Action is not set");
 117   return (action() == ParCompactionManager::Update) ||
 118          (action() == ParCompactionManager::CopyAndUpdate) ||
 119          (action() == ParCompactionManager::UpdateAndCopy);
 120 }
 121 
 122 bool ParCompactionManager::should_copy() {
 123   assert(action() != NotValid, "Action is not set");
 124   return (action() == ParCompactionManager::Copy) ||
 125          (action() == ParCompactionManager::CopyAndUpdate) ||
 126          (action() == ParCompactionManager::UpdateAndCopy);
 127 }
 128 
 129 ParCompactionManager*
 130 ParCompactionManager::gc_thread_compaction_manager(uint index) {
 131   assert(index < ParallelGCThreads, "index out of range");
 132   assert(_manager_array != NULL, "Sanity");
 133   return _manager_array[index];
 134 }
 135 
 136 void ParCompactionManager::follow_marking_stacks() {
 137   do {
 138     // Drain the overflow stack first, to allow stealing from the marking stack.
 139     oop obj;
 140     while (marking_stack()->pop_overflow(obj)) {
 141       follow_contents(obj);
 142     }
 143     while (marking_stack()->pop_local(obj)) {
 144       follow_contents(obj);
 145     }
 146 
 147     // Process ObjArrays one at a time to avoid marking stack bloat.
 148     ObjArrayTask task;
 149     if (_objarray_stack.pop_overflow(task) || _objarray_stack.pop_local(task)) {
 150       follow_array((objArrayOop)task.obj(), task.index());
 151     }
 152   } while (!marking_stacks_empty());
 153 
 154   assert(marking_stacks_empty(), "Sanity");
 155 }
 156 
 157 void ParCompactionManager::drain_region_stacks() {
 158   do {
 159     // Drain overflow stack first so other threads can steal.
 160     size_t region_index;
 161     while (region_stack()->pop_overflow(region_index)) {
 162       PSParallelCompact::fill_and_update_region(this, region_index);
 163     }
 164 
 165     while (region_stack()->pop_local(region_index)) {
 166       PSParallelCompact::fill_and_update_region(this, region_index);
 167     }
 168   } while (!region_stack()->is_empty());
 169 }