1 /*
  2  * Copyright (c) 2002, 2024, 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/javaClasses.inline.hpp"
 27 #include "gc/parallel/mutableSpace.hpp"
 28 #include "gc/parallel/parallelScavengeHeap.hpp"
 29 #include "gc/parallel/psOldGen.hpp"
 30 #include "gc/parallel/psPromotionManager.inline.hpp"
 31 #include "gc/parallel/psScavenge.inline.hpp"
 32 #include "gc/shared/continuationGCSupport.inline.hpp"
 33 #include "gc/shared/gcTrace.hpp"
 34 #include "gc/shared/partialArrayState.hpp"
 35 #include "gc/shared/partialArrayTaskStepper.inline.hpp"
 36 #include "gc/shared/preservedMarks.inline.hpp"
 37 #include "gc/shared/taskqueue.inline.hpp"
 38 #include "logging/log.hpp"
 39 #include "logging/logStream.hpp"
 40 #include "memory/allocation.inline.hpp"
 41 #include "memory/iterator.inline.hpp"
 42 #include "memory/memRegion.hpp"
 43 #include "memory/padded.inline.hpp"
 44 #include "memory/resourceArea.hpp"
 45 #include "oops/access.inline.hpp"
 46 #include "oops/compressedOops.inline.hpp"
 47 #include "utilities/checkedCast.hpp"
 48 
 49 PaddedEnd<PSPromotionManager>* PSPromotionManager::_manager_array = nullptr;
 50 PSPromotionManager::PSScannerTasksQueueSet* PSPromotionManager::_stack_array_depth = nullptr;
 51 PreservedMarksSet*             PSPromotionManager::_preserved_marks_set = nullptr;
 52 PSOldGen*                      PSPromotionManager::_old_gen = nullptr;
 53 MutableSpace*                  PSPromotionManager::_young_space = nullptr;
 54 PartialArrayStateAllocator*    PSPromotionManager::_partial_array_state_allocator = nullptr;
 55 
 56 void PSPromotionManager::initialize() {
 57   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 58 
 59   _old_gen = heap->old_gen();
 60   _young_space = heap->young_gen()->to_space();
 61 
 62   const uint promotion_manager_num = ParallelGCThreads;
 63 
 64   // To prevent false sharing, we pad the PSPromotionManagers
 65   // and make sure that the first instance starts at a cache line.
 66   assert(_manager_array == nullptr, "Attempt to initialize twice");
 67   _manager_array = PaddedArray<PSPromotionManager, mtGC>::create_unfreeable(promotion_manager_num);
 68 
 69   assert(_partial_array_state_allocator == nullptr, "Attempt to initialize twice");
 70   _partial_array_state_allocator
 71     = new PartialArrayStateAllocator(ParallelGCThreads);
 72 
 73   _stack_array_depth = new PSScannerTasksQueueSet(ParallelGCThreads);
 74 
 75   // Create and register the PSPromotionManager(s) for the worker threads.
 76   for(uint i=0; i<ParallelGCThreads; i++) {
 77     stack_array_depth()->register_queue(i, _manager_array[i].claimed_stack_depth());
 78     _manager_array[i]._partial_array_state_allocator_index = i;
 79   }
 80   // The VMThread gets its own PSPromotionManager, which is not available
 81   // for work stealing.
 82 
 83   assert(_preserved_marks_set == nullptr, "Attempt to initialize twice");
 84   _preserved_marks_set = new PreservedMarksSet(true /* in_c_heap */);
 85   _preserved_marks_set->init(promotion_manager_num);
 86   for (uint i = 0; i < promotion_manager_num; i += 1) {
 87     _manager_array[i].register_preserved_marks(_preserved_marks_set->get(i));
 88   }
 89 }
 90 
 91 // Helper functions to get around the circular dependency between
 92 // psScavenge.inline.hpp and psPromotionManager.inline.hpp.
 93 bool PSPromotionManager::should_scavenge(oop* p, bool check_to_space) {
 94   return PSScavenge::should_scavenge(p, check_to_space);
 95 }
 96 bool PSPromotionManager::should_scavenge(narrowOop* p, bool check_to_space) {
 97   return PSScavenge::should_scavenge(p, check_to_space);
 98 }
 99 
100 PSPromotionManager* PSPromotionManager::gc_thread_promotion_manager(uint index) {
101   assert(index < ParallelGCThreads, "index out of range");
102   assert(_manager_array != nullptr, "Sanity");
103   return &_manager_array[index];
104 }
105 
106 PSPromotionManager* PSPromotionManager::vm_thread_promotion_manager() {
107   assert(_manager_array != nullptr, "Sanity");
108   return &_manager_array[0];
109 }
110 
111 void PSPromotionManager::pre_scavenge() {
112   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
113 
114   _preserved_marks_set->assert_empty();
115   _young_space = heap->young_gen()->to_space();
116 
117   for(uint i=0; i<ParallelGCThreads; i++) {
118     manager_array(i)->reset();
119   }
120 }
121 
122 bool PSPromotionManager::post_scavenge(YoungGCTracer& gc_tracer) {
123   bool promotion_failure_occurred = false;
124 
125   TASKQUEUE_STATS_ONLY(print_taskqueue_stats());
126   for (uint i = 0; i < ParallelGCThreads; i++) {
127     PSPromotionManager* manager = manager_array(i);
128     assert(manager->claimed_stack_depth()->is_empty(), "should be empty");
129     if (manager->_promotion_failed_info.has_failed()) {
130       gc_tracer.report_promotion_failed(manager->_promotion_failed_info);
131       promotion_failure_occurred = true;
132     }
133     manager->flush_labs();
134     manager->flush_string_dedup_requests();
135   }
136   // All PartialArrayStates have been returned to the allocator, since the
137   // claimed_stack_depths are all empty.  Leave them there for use by future
138   // collections.
139 
140   if (!promotion_failure_occurred) {
141     // If there was no promotion failure, the preserved mark stacks
142     // should be empty.
143     _preserved_marks_set->assert_empty();
144   }
145   return promotion_failure_occurred;
146 }
147 
148 #if TASKQUEUE_STATS
149 void
150 PSPromotionManager::print_local_stats(outputStream* const out, uint i) const {
151   #define FMT " " SIZE_FORMAT_W(10)
152   out->print_cr("%3u" FMT FMT FMT FMT,
153                 i, _array_chunk_pushes, _array_chunk_steals,
154                 _arrays_chunked, _array_chunks_processed);
155   #undef FMT
156 }
157 
158 static const char* const pm_stats_hdr[] = {
159   "    ----partial array----     arrays      array",
160   "thr       push      steal    chunked     chunks",
161   "--- ---------- ---------- ---------- ----------"
162 };
163 
164 void PSPromotionManager::print_taskqueue_stats() {
165   if (!log_is_enabled(Trace, gc, task, stats)) {
166     return;
167   }
168   Log(gc, task, stats) log;
169   ResourceMark rm;
170   LogStream ls(log.trace());
171 
172   stack_array_depth()->print_taskqueue_stats(&ls, "Oop Queue");
173 
174   const uint hlines = sizeof(pm_stats_hdr) / sizeof(pm_stats_hdr[0]);
175   for (uint i = 0; i < hlines; ++i) ls.print_cr("%s", pm_stats_hdr[i]);
176   for (uint i = 0; i < ParallelGCThreads; ++i) {
177     manager_array(i)->print_local_stats(&ls, i);
178   }
179 }
180 
181 void PSPromotionManager::reset_stats() {
182   claimed_stack_depth()->stats.reset();
183   _array_chunk_pushes = _array_chunk_steals = 0;
184   _arrays_chunked = _array_chunks_processed = 0;
185 }
186 #endif // TASKQUEUE_STATS
187 
188 // Most members are initialized either by initialize() or reset().
189 PSPromotionManager::PSPromotionManager()
190   : _partial_array_stepper(ParallelGCThreads, ParGCArrayScanChunk)
191 {
192   // We set the old lab's start array.
193   _old_lab.set_start_array(old_gen()->start_array());
194 
195   if (ParallelGCThreads == 1) {
196     _target_stack_size = 0;
197   } else {
198     _target_stack_size = GCDrainStackTargetSize;
199   }
200 
201   // Initialize to a bad value; fixed by initialize().
202   _partial_array_state_allocator_index = UINT_MAX;
203 
204   // let's choose 1.5x the chunk size
205   _min_array_size_for_chunking = (3 * ParGCArrayScanChunk / 2);
206 
207   _preserved_marks = nullptr;
208 
209   reset();
210 }
211 
212 void PSPromotionManager::reset() {
213   assert(stacks_empty(), "reset of non-empty stack");
214 
215   // We need to get an assert in here to make sure the labs are always flushed.
216 
217   // Do not prefill the LAB's, save heap wastage!
218   HeapWord* lab_base = young_space()->top();
219   _young_lab.initialize(MemRegion(lab_base, (size_t)0));
220   _young_gen_is_full = false;
221 
222   lab_base = old_gen()->object_space()->top();
223   _old_lab.initialize(MemRegion(lab_base, (size_t)0));
224   _old_gen_is_full = false;
225 
226   _promotion_failed_info.reset();
227 
228   TASKQUEUE_STATS_ONLY(reset_stats());
229 }
230 
231 void PSPromotionManager::register_preserved_marks(PreservedMarks* preserved_marks) {
232   assert(_preserved_marks == nullptr, "do not set it twice");
233   _preserved_marks = preserved_marks;
234 }
235 
236 void PSPromotionManager::restore_preserved_marks() {
237   _preserved_marks_set->restore(&ParallelScavengeHeap::heap()->workers());
238 }
239 
240 void PSPromotionManager::drain_stacks_depth(bool totally_drain) {
241   const uint threshold = totally_drain ? 0
242                                        : _target_stack_size;
243 
244   PSScannerTasksQueue* const tq = claimed_stack_depth();
245   do {
246     ScannerTask task;
247 
248     // Drain overflow stack first, so other threads can steal from
249     // claimed stack while we work.
250     while (tq->pop_overflow(task)) {
251       if (!tq->try_push_to_taskqueue(task)) {
252         process_popped_location_depth(task);
253       }
254     }
255 
256     while (tq->pop_local(task, threshold)) {
257       process_popped_location_depth(task);
258     }
259   } while (!tq->overflow_empty());
260 
261   assert(!totally_drain || tq->taskqueue_empty(), "Sanity");
262   assert(totally_drain || tq->size() <= _target_stack_size, "Sanity");
263   assert(tq->overflow_empty(), "Sanity");
264 }
265 
266 void PSPromotionManager::flush_labs() {
267   assert(stacks_empty(), "Attempt to flush lab with live stack");
268 
269   // If either promotion lab fills up, we can flush the
270   // lab but not refill it, so check first.
271   assert(!_young_lab.is_flushed() || _young_gen_is_full, "Sanity");
272   if (!_young_lab.is_flushed())
273     _young_lab.flush();
274 
275   assert(!_old_lab.is_flushed() || _old_gen_is_full, "Sanity");
276   if (!_old_lab.is_flushed())
277     _old_lab.flush();
278 
279   // Let PSScavenge know if we overflowed
280   if (_young_gen_is_full) {
281     PSScavenge::set_survivor_overflow(true);
282   }
283 }
284 
285 template <class T> void PSPromotionManager::process_array_chunk_work(
286                                                  oop obj,
287                                                  int start, int end) {
288   assert(start <= end, "invariant");
289   T* const base      = (T*)objArrayOop(obj)->base();
290   T* p               = base + start;
291   T* const chunk_end = base + end;
292   while (p < chunk_end) {
293     claim_or_forward_depth(p);
294     ++p;
295   }
296 }
297 
298 void PSPromotionManager::process_array_chunk(PartialArrayState* state) {
299   TASKQUEUE_STATS_ONLY(++_array_chunks_processed);
300 
301   // Claim a chunk.  Push additional tasks before processing the claimed
302   // chunk to allow other workers to steal while we're processing.
303   PartialArrayTaskStepper::Step step = _partial_array_stepper.next(state);
304   if (step._ncreate > 0) {
305     state->add_references(step._ncreate);
306     for (uint i = 0; i < step._ncreate; ++i) {
307       push_depth(ScannerTask(state));
308     }
309     TASKQUEUE_STATS_ONLY(_array_chunk_pushes += step._ncreate);
310   }
311   int start = checked_cast<int>(step._index);
312   int end = checked_cast<int>(step._index + _partial_array_stepper.chunk_size());
313   assert(start < end, "invariant");
314   if (UseCompressedOops) {
315     process_array_chunk_work<narrowOop>(state->destination(), start, end);
316   } else {
317     process_array_chunk_work<oop>(state->destination(), start, end);
318   }
319   // Release reference to state, now that we're done with it.
320   _partial_array_state_allocator->release(_partial_array_state_allocator_index, state);
321 }
322 
323 void PSPromotionManager::push_objArray(oop old_obj, oop new_obj) {
324   assert(old_obj->is_objArray(), "precondition");
325   assert(old_obj->is_forwarded(), "precondition");
326   assert(old_obj->forwardee() == new_obj, "precondition");
327   assert(new_obj->is_objArray(), "precondition");
328 
329   size_t array_length = objArrayOop(new_obj)->length();
330   PartialArrayTaskStepper::Step step = _partial_array_stepper.start(array_length);
331 
332   if (step._ncreate > 0) {
333     TASKQUEUE_STATS_ONLY(++_arrays_chunked);
334     PartialArrayState* state =
335       _partial_array_state_allocator->allocate(_partial_array_state_allocator_index,
336                                                old_obj, new_obj,
337                                                step._index,
338                                                array_length,
339                                                step._ncreate);
340     for (uint i = 0; i < step._ncreate; ++i) {
341       push_depth(ScannerTask(state));
342     }
343     TASKQUEUE_STATS_ONLY(_array_chunk_pushes += step._ncreate);
344   }
345   if (UseCompressedOops) {
346     process_array_chunk_work<narrowOop>(new_obj, 0, checked_cast<int>(step._index));
347   } else {
348     process_array_chunk_work<oop>(new_obj, 0, checked_cast<int>(step._index));
349   }
350 }
351 
352 oop PSPromotionManager::oop_promotion_failed(oop obj, markWord obj_mark) {
353   assert(_old_gen_is_full || PromotionFailureALot, "Sanity");
354 
355   // Attempt to CAS in the header.
356   // This tests if the header is still the same as when
357   // this started.  If it is the same (i.e., no forwarding
358   // pointer has been installed), then this thread owns
359   // it.
360   if (obj->forward_to_atomic(obj, obj_mark) == nullptr) {
361     // We won any races, we "own" this object.
362     assert(obj == obj->forwardee(), "Sanity");
363 
364     _promotion_failed_info.register_copy_failure(obj->size());
365 
366     ContinuationGCSupport::transform_stack_chunk(obj);
367 
368     push_contents(obj);
369 
370     // Save the markWord of promotion-failed objs in _preserved_marks for later
371     // restoration. This way we don't have to walk the young-gen to locate
372     // these promotion-failed objs.
373     _preserved_marks->push_always(obj, obj_mark);
374   }  else {
375     // We lost, someone else "owns" this object
376     guarantee(obj->is_forwarded(), "Object must be forwarded if the cas failed.");
377 
378     // No unallocation to worry about.
379     obj = obj->forwardee();
380   }
381 
382   return obj;
383 }