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