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