1 /*
  2  * Copyright (c) 2005, 2021, 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 "gc/parallel/objectStartArray.hpp"
 27 #include "gc/parallel/parMarkBitMap.inline.hpp"
 28 #include "gc/parallel/parallelScavengeHeap.hpp"
 29 #include "gc/parallel/psCompactionManager.inline.hpp"
 30 #include "gc/parallel/psOldGen.hpp"
 31 #include "gc/parallel/psParallelCompact.inline.hpp"
 32 #include "gc/shared/taskqueue.inline.hpp"
 33 #include "logging/log.hpp"
 34 #include "memory/iterator.inline.hpp"
 35 #include "oops/access.inline.hpp"
 36 #include "oops/compressedOops.inline.hpp"
 37 #include "oops/flatArrayKlass.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 ParCompactionManager::OopTaskQueueSet*      ParCompactionManager::_oop_task_queues = NULL;
 47 ParCompactionManager::ObjArrayTaskQueueSet* ParCompactionManager::_objarray_task_queues = NULL;
 48 ParCompactionManager::RegionTaskQueueSet*   ParCompactionManager::_region_task_queues = NULL;
 49 
 50 ObjectStartArray*    ParCompactionManager::_start_array = NULL;
 51 ParMarkBitMap*       ParCompactionManager::_mark_bitmap = NULL;
 52 GrowableArray<size_t >* ParCompactionManager::_shadow_region_array = NULL;
 53 Monitor*                ParCompactionManager::_shadow_region_monitor = NULL;
 54 
 55 ParCompactionManager::ParCompactionManager() {
 56 
 57   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 58 
 59   _old_gen = heap->old_gen();
 60   _start_array = old_gen()->start_array();
 61 
 62   reset_bitmap_query_cache();
 63 }
 64 
 65 void ParCompactionManager::initialize(ParMarkBitMap* mbm) {
 66   assert(ParallelScavengeHeap::heap() != NULL,
 67     "Needed for initialization");
 68 
 69   _mark_bitmap = mbm;
 70 
 71   uint parallel_gc_threads = ParallelScavengeHeap::heap()->workers().total_workers();
 72 
 73   assert(_manager_array == NULL, "Attempt to initialize twice");
 74   _manager_array = NEW_C_HEAP_ARRAY(ParCompactionManager*, parallel_gc_threads+1, mtGC);
 75 
 76   _oop_task_queues = new OopTaskQueueSet(parallel_gc_threads);
 77   _objarray_task_queues = new ObjArrayTaskQueueSet(parallel_gc_threads);
 78   _region_task_queues = new RegionTaskQueueSet(parallel_gc_threads);
 79 
 80   // Create and register the ParCompactionManager(s) for the worker threads.
 81   for(uint i=0; i<parallel_gc_threads; i++) {
 82     _manager_array[i] = new ParCompactionManager();
 83     oop_task_queues()->register_queue(i, _manager_array[i]->marking_stack());
 84     _objarray_task_queues->register_queue(i, &_manager_array[i]->_objarray_stack);
 85     region_task_queues()->register_queue(i, _manager_array[i]->region_stack());
 86   }
 87 
 88   // The VMThread gets its own ParCompactionManager, which is not available
 89   // for work stealing.
 90   _manager_array[parallel_gc_threads] = new ParCompactionManager();
 91   assert(ParallelScavengeHeap::heap()->workers().total_workers() != 0,
 92     "Not initialized?");
 93 
 94   _shadow_region_array = new (ResourceObj::C_HEAP, mtGC) GrowableArray<size_t >(10, mtGC);
 95 
 96   _shadow_region_monitor = new Monitor(Mutex::nosafepoint, "CompactionManager_lock",
 97                                        Monitor::_safepoint_check_never);
 98 }
 99 
100 void ParCompactionManager::reset_all_bitmap_query_caches() {
101   uint parallel_gc_threads = ParallelScavengeHeap::heap()->workers().total_workers();
102   for (uint i=0; i<=parallel_gc_threads; i++) {
103     _manager_array[i]->reset_bitmap_query_cache();
104   }
105 }
106 
107 void ParCompactionManager::flush_all_string_dedup_requests() {
108   uint parallel_gc_threads = ParallelScavengeHeap::heap()->workers().total_workers();
109   for (uint i=0; i<=parallel_gc_threads; i++) {
110     _manager_array[i]->flush_string_dedup_requests();
111   }
112 }
113 
114 ParCompactionManager*
115 ParCompactionManager::gc_thread_compaction_manager(uint index) {
116   assert(index < ParallelGCThreads, "index out of range");
117   assert(_manager_array != NULL, "Sanity");
118   return _manager_array[index];
119 }
120 
121 void ParCompactionManager::follow_marking_stacks() {
122   do {
123     // Drain the overflow stack first, to allow stealing from the marking stack.
124     oop obj;
125     while (marking_stack()->pop_overflow(obj)) {
126       follow_contents(obj);
127     }
128     while (marking_stack()->pop_local(obj)) {
129       follow_contents(obj);
130     }
131 
132     // Process ObjArrays one at a time to avoid marking stack bloat.
133     ObjArrayTask task;
134     if (_objarray_stack.pop_overflow(task) || _objarray_stack.pop_local(task)) {
135       follow_array((objArrayOop)task.obj(), task.index());
136     }
137   } while (!marking_stacks_empty());
138 
139   assert(marking_stacks_empty(), "Sanity");
140 }
141 
142 void ParCompactionManager::drain_region_stacks() {
143   do {
144     // Drain overflow stack first so other threads can steal.
145     size_t region_index;
146     while (region_stack()->pop_overflow(region_index)) {
147       PSParallelCompact::fill_and_update_region(this, region_index);
148     }
149 
150     while (region_stack()->pop_local(region_index)) {
151       PSParallelCompact::fill_and_update_region(this, region_index);
152     }
153   } while (!region_stack()->is_empty());
154 }
155 
156 size_t ParCompactionManager::pop_shadow_region_mt_safe(PSParallelCompact::RegionData* region_ptr) {
157   MonitorLocker ml(_shadow_region_monitor, Mutex::_no_safepoint_check_flag);
158   while (true) {
159     if (!_shadow_region_array->is_empty()) {
160       return _shadow_region_array->pop();
161     }
162     // Check if the corresponding heap region is available now.
163     // If so, we don't need to get a shadow region anymore, and
164     // we return InvalidShadow to indicate such a case.
165     if (region_ptr->claimed()) {
166       return InvalidShadow;
167     }
168     ml.wait(1);
169   }
170 }
171 
172 void ParCompactionManager::push_shadow_region_mt_safe(size_t shadow_region) {
173   MonitorLocker ml(_shadow_region_monitor, Mutex::_no_safepoint_check_flag);
174   _shadow_region_array->push(shadow_region);
175   ml.notify();
176 }
177 
178 void ParCompactionManager::push_shadow_region(size_t shadow_region) {
179   _shadow_region_array->push(shadow_region);
180 }
181 
182 void ParCompactionManager::remove_all_shadow_regions() {
183   _shadow_region_array->clear();
184 }
185 
186 #ifdef ASSERT
187 void ParCompactionManager::verify_all_marking_stack_empty() {
188   uint parallel_gc_threads = ParallelGCThreads;
189   for (uint i = 0; i <= parallel_gc_threads; i++) {
190     assert(_manager_array[i]->marking_stacks_empty(), "Marking stack should be empty");
191   }
192 }
193 
194 void ParCompactionManager::verify_all_region_stack_empty() {
195   uint parallel_gc_threads = ParallelGCThreads;
196   for (uint i = 0; i <= parallel_gc_threads; i++) {
197     assert(_manager_array[i]->region_stack()->is_empty(), "Region stack should be empty");
198   }
199 }
200 #endif