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