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