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