< prev index next >

src/hotspot/share/gc/parallel/psPromotionManager.inline.hpp

Print this page

 46 
 47 inline PSPromotionManager* PSPromotionManager::manager_array(uint index) {
 48   assert(_manager_array != nullptr, "access of null manager_array");
 49   assert(index < ParallelGCThreads, "out of range manager_array access");
 50   return &_manager_array[index];
 51 }
 52 
 53 inline void PSPromotionManager::push_depth(ScannerTask task) {
 54   claimed_stack_depth()->push(task);
 55 }
 56 
 57 template <class T>
 58 inline void PSPromotionManager::claim_or_forward_depth(T* p) {
 59   assert(should_scavenge(p, true), "revisiting object?");
 60   assert(ParallelScavengeHeap::heap()->is_in(p), "pointer outside heap");
 61   oop obj = RawAccess<IS_NOT_NULL>::oop_load(p);
 62   Prefetch::write(obj->mark_addr(), 0);
 63   push_depth(ScannerTask(p));
 64 }
 65 
 66 inline void PSPromotionManager::promotion_trace_event(oop new_obj, oop old_obj,
 67                                                       size_t obj_size,
 68                                                       uint age, bool tenured,
 69                                                       const PSPromotionLAB* lab) {
 70   // Skip if memory allocation failed
 71   if (new_obj != nullptr) {
 72     const ParallelScavengeTracer* gc_tracer = PSScavenge::gc_tracer();
 73 
 74     if (lab != nullptr) {
 75       // Promotion of object through newly allocated PLAB
 76       if (gc_tracer->should_report_promotion_in_new_plab_event()) {
 77         size_t obj_bytes = obj_size * HeapWordSize;
 78         size_t lab_size = lab->capacity();
 79         gc_tracer->report_promotion_in_new_plab_event(old_obj->klass(), obj_bytes,
 80                                                       age, tenured, lab_size);
 81       }
 82     } else {
 83       // Promotion of object directly to heap
 84       if (gc_tracer->should_report_promotion_outside_plab_event()) {
 85         size_t obj_bytes = obj_size * HeapWordSize;
 86         gc_tracer->report_promotion_outside_plab_event(old_obj->klass(), obj_bytes,
 87                                                        age, tenured);
 88       }
 89     }
 90   }
 91 }
 92 
 93 class PSPushContentsClosure: public BasicOopIterateClosure {
 94   PSPromotionManager* _pm;
 95  public:
 96   PSPushContentsClosure(PSPromotionManager* pm) : BasicOopIterateClosure(PSScavenge::reference_processor()), _pm(pm) {}
 97 
 98   template <typename T> void do_oop_nv(T* p) {
 99     if (PSScavenge::should_scavenge(p)) {
100       _pm->claim_or_forward_depth(p);
101     }
102   }
103 
104   virtual void do_oop(oop* p)       { do_oop_nv(p); }
105   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
106 };

130     obj->oop_iterate_backwards(&pcc);
131   }
132 }
133 
134 template<bool promote_immediately>
135 inline oop PSPromotionManager::copy_to_survivor_space(oop o) {
136   assert(should_scavenge(&o), "Sanity");
137 
138   // NOTE! We must be very careful with any methods that access the mark
139   // in o. There may be multiple threads racing on it, and it may be forwarded
140   // at any time.
141   markWord m = o->mark();
142   if (!m.is_marked()) {
143     return copy_unmarked_to_survivor_space<promote_immediately>(o, m);
144   } else {
145     // Ensure any loads from the forwardee follow all changes that precede
146     // the release-cmpxchg that performed the forwarding, possibly in some
147     // other thread.
148     OrderAccess::acquire();
149     // Return the already installed forwardee.
150     return cast_to_oop(m.decode_pointer());
151   }
152 }
153 
154 //
155 // This method is pretty bulky. It would be nice to split it up
156 // into smaller submethods, but we need to be careful not to hurt
157 // performance.
158 //
159 template<bool promote_immediately>
160 inline oop PSPromotionManager::copy_unmarked_to_survivor_space(oop o,
161                                                                markWord test_mark) {
162   assert(should_scavenge(&o), "Sanity");
163 
164   oop new_obj = nullptr;
165   bool new_obj_is_tenured = false;
166   size_t new_obj_size = o->size();







167 
168   // Find the objects age, MT safe.
169   uint age = (test_mark.has_displaced_mark_helper() /* o->has_displaced_mark() */) ?
170       test_mark.displaced_mark_helper().age() : test_mark.age();
171 
172   if (!promote_immediately) {
173     // Try allocating obj in to-space (unless too old)
174     if (age < PSScavenge::tenuring_threshold()) {
175       new_obj = cast_to_oop(_young_lab.allocate(new_obj_size));
176       if (new_obj == nullptr && !_young_gen_is_full) {
177         // Do we allocate directly, or flush and refill?
178         if (new_obj_size > (YoungPLABSize / 2)) {
179           // Allocate this object directly
180           new_obj = cast_to_oop(young_space()->cas_allocate(new_obj_size));
181           promotion_trace_event(new_obj, o, new_obj_size, age, false, nullptr);
182         } else {
183           // Flush and fill
184           _young_lab.flush();
185 
186           HeapWord* lab_base = young_space()->cas_allocate(YoungPLABSize);
187           if (lab_base != nullptr) {
188             _young_lab.initialize(MemRegion(lab_base, YoungPLABSize));
189             // Try the young lab allocation again.
190             new_obj = cast_to_oop(_young_lab.allocate(new_obj_size));
191             promotion_trace_event(new_obj, o, new_obj_size, age, false, &_young_lab);
192           } else {
193             _young_gen_is_full = true;
194           }
195         }
196       }
197     }
198   }
199 
200   // Otherwise try allocating obj tenured
201   if (new_obj == nullptr) {
202 #ifndef PRODUCT
203     if (ParallelScavengeHeap::heap()->promotion_should_fail()) {
204       return oop_promotion_failed(o, test_mark);
205     }
206 #endif  // #ifndef PRODUCT
207 
208     new_obj = cast_to_oop(_old_lab.allocate(new_obj_size));
209     new_obj_is_tenured = true;
210 
211     if (new_obj == nullptr) {
212       if (!_old_gen_is_full) {
213         // Do we allocate directly, or flush and refill?
214         if (new_obj_size > (OldPLABSize / 2)) {
215           // Allocate this object directly
216           new_obj = cast_to_oop(old_gen()->allocate(new_obj_size));
217           promotion_trace_event(new_obj, o, new_obj_size, age, true, nullptr);
218         } else {
219           // Flush and fill
220           _old_lab.flush();
221 
222           HeapWord* lab_base = old_gen()->allocate(OldPLABSize);
223           if(lab_base != nullptr) {
224             _old_lab.initialize(MemRegion(lab_base, OldPLABSize));
225             // Try the old lab allocation again.
226             new_obj = cast_to_oop(_old_lab.allocate(new_obj_size));
227             promotion_trace_event(new_obj, o, new_obj_size, age, true, &_old_lab);
228           }
229         }
230       }
231 
232       // This is the promotion failed test, and code handling.
233       // The code belongs here for two reasons. It is slightly
234       // different than the code below, and cannot share the
235       // CAS testing code. Keeping the code here also minimizes
236       // the impact on the common case fast path code.
237 
238       if (new_obj == nullptr) {
239         _old_gen_is_full = true;
240         return oop_promotion_failed(o, test_mark);
241       }
242     }
243   }
244 
245   assert(new_obj != nullptr, "allocation should have succeeded");
246 
247   // Copy obj
248   Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(o), cast_from_oop<HeapWord*>(new_obj), new_obj_size);
249 
250   // Parallel GC claims with a release - so other threads might access this object
251   // after claiming and they should see the "completed" object.
252   ContinuationGCSupport::transform_stack_chunk(new_obj);














253 
254   // Now we have to CAS in the header.
255   // Make copy visible to threads reading the forwardee.
256   oop forwardee = o->forward_to_atomic(new_obj, test_mark, memory_order_release);
257   if (forwardee == nullptr) {  // forwardee is null when forwarding is successful
258     // We won any races, we "own" this object.
259     assert(new_obj == o->forwardee(), "Sanity");
260 
261     // Increment age if obj still in new generation. Now that
262     // we're dealing with a markWord that cannot change, it is
263     // okay to use the non mt safe oop methods.
264     if (!new_obj_is_tenured) {
265       new_obj->incr_age();
266       assert(young_space()->contains(new_obj), "Attempt to push non-promoted obj");
267     }
268 
269     // Do the size comparison first with new_obj_size, which we
270     // already have. Hopefully, only a few objects are larger than
271     // _min_array_size_for_chunking, and most of them will be arrays.
272     // So, the is->objArray() test would be very infrequent.

 46 
 47 inline PSPromotionManager* PSPromotionManager::manager_array(uint index) {
 48   assert(_manager_array != nullptr, "access of null manager_array");
 49   assert(index < ParallelGCThreads, "out of range manager_array access");
 50   return &_manager_array[index];
 51 }
 52 
 53 inline void PSPromotionManager::push_depth(ScannerTask task) {
 54   claimed_stack_depth()->push(task);
 55 }
 56 
 57 template <class T>
 58 inline void PSPromotionManager::claim_or_forward_depth(T* p) {
 59   assert(should_scavenge(p, true), "revisiting object?");
 60   assert(ParallelScavengeHeap::heap()->is_in(p), "pointer outside heap");
 61   oop obj = RawAccess<IS_NOT_NULL>::oop_load(p);
 62   Prefetch::write(obj->mark_addr(), 0);
 63   push_depth(ScannerTask(p));
 64 }
 65 
 66 inline void PSPromotionManager::promotion_trace_event(oop new_obj, Klass* klass,
 67                                                       size_t obj_size,
 68                                                       uint age, bool tenured,
 69                                                       const PSPromotionLAB* lab) {
 70   // Skip if memory allocation failed
 71   if (new_obj != nullptr) {
 72     const ParallelScavengeTracer* gc_tracer = PSScavenge::gc_tracer();
 73 
 74     if (lab != nullptr) {
 75       // Promotion of object through newly allocated PLAB
 76       if (gc_tracer->should_report_promotion_in_new_plab_event()) {
 77         size_t obj_bytes = obj_size * HeapWordSize;
 78         size_t lab_size = lab->capacity();
 79         gc_tracer->report_promotion_in_new_plab_event(klass, obj_bytes,
 80                                                       age, tenured, lab_size);
 81       }
 82     } else {
 83       // Promotion of object directly to heap
 84       if (gc_tracer->should_report_promotion_outside_plab_event()) {
 85         size_t obj_bytes = obj_size * HeapWordSize;
 86         gc_tracer->report_promotion_outside_plab_event(klass, obj_bytes,
 87                                                        age, tenured);
 88       }
 89     }
 90   }
 91 }
 92 
 93 class PSPushContentsClosure: public BasicOopIterateClosure {
 94   PSPromotionManager* _pm;
 95  public:
 96   PSPushContentsClosure(PSPromotionManager* pm) : BasicOopIterateClosure(PSScavenge::reference_processor()), _pm(pm) {}
 97 
 98   template <typename T> void do_oop_nv(T* p) {
 99     if (PSScavenge::should_scavenge(p)) {
100       _pm->claim_or_forward_depth(p);
101     }
102   }
103 
104   virtual void do_oop(oop* p)       { do_oop_nv(p); }
105   virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
106 };

130     obj->oop_iterate_backwards(&pcc);
131   }
132 }
133 
134 template<bool promote_immediately>
135 inline oop PSPromotionManager::copy_to_survivor_space(oop o) {
136   assert(should_scavenge(&o), "Sanity");
137 
138   // NOTE! We must be very careful with any methods that access the mark
139   // in o. There may be multiple threads racing on it, and it may be forwarded
140   // at any time.
141   markWord m = o->mark();
142   if (!m.is_marked()) {
143     return copy_unmarked_to_survivor_space<promote_immediately>(o, m);
144   } else {
145     // Ensure any loads from the forwardee follow all changes that precede
146     // the release-cmpxchg that performed the forwarding, possibly in some
147     // other thread.
148     OrderAccess::acquire();
149     // Return the already installed forwardee.
150     return o->forwardee(m);
151   }
152 }
153 
154 //
155 // This method is pretty bulky. It would be nice to split it up
156 // into smaller submethods, but we need to be careful not to hurt
157 // performance.
158 //
159 template<bool promote_immediately>
160 inline oop PSPromotionManager::copy_unmarked_to_survivor_space(oop o,
161                                                                markWord test_mark) {
162   assert(should_scavenge(&o), "Sanity");
163 
164   oop new_obj = nullptr;
165   bool new_obj_is_tenured = false;
166   // NOTE: With compact headers, it is not safe to load the Klass* from o, because
167   // that would access the mark-word, and the mark-word might change at any time by
168   // concurrent promotion. The promoted mark-word would point to the forwardee, which
169   // may not yet have completed copying. Therefore we must load the Klass* from
170   // the mark-word that we have already loaded. This is safe, because we have checked
171   // that this is not yet forwarded in the caller.
172   Klass* klass = o->forward_safe_klass(test_mark);
173   size_t new_obj_size = o->size_given_klass(klass);
174 
175   // Find the objects age, MT safe.
176   uint age = (test_mark.has_displaced_mark_helper() /* o->has_displaced_mark() */) ?
177       test_mark.displaced_mark_helper().age() : test_mark.age();
178 
179   if (!promote_immediately) {
180     // Try allocating obj in to-space (unless too old)
181     if (age < PSScavenge::tenuring_threshold()) {
182       new_obj = cast_to_oop(_young_lab.allocate(new_obj_size));
183       if (new_obj == nullptr && !_young_gen_is_full) {
184         // Do we allocate directly, or flush and refill?
185         if (new_obj_size > (YoungPLABSize / 2)) {
186           // Allocate this object directly
187           new_obj = cast_to_oop(young_space()->cas_allocate(new_obj_size));
188           promotion_trace_event(new_obj, klass, new_obj_size, age, false, nullptr);
189         } else {
190           // Flush and fill
191           _young_lab.flush();
192 
193           HeapWord* lab_base = young_space()->cas_allocate(YoungPLABSize);
194           if (lab_base != nullptr) {
195             _young_lab.initialize(MemRegion(lab_base, YoungPLABSize));
196             // Try the young lab allocation again.
197             new_obj = cast_to_oop(_young_lab.allocate(new_obj_size));
198             promotion_trace_event(new_obj, klass, new_obj_size, age, false, &_young_lab);
199           } else {
200             _young_gen_is_full = true;
201           }
202         }
203       }
204     }
205   }
206 
207   // Otherwise try allocating obj tenured
208   if (new_obj == nullptr) {
209 #ifndef PRODUCT
210     if (ParallelScavengeHeap::heap()->promotion_should_fail()) {
211       return oop_promotion_failed(o, test_mark);
212     }
213 #endif  // #ifndef PRODUCT
214 
215     new_obj = cast_to_oop(_old_lab.allocate(new_obj_size));
216     new_obj_is_tenured = true;
217 
218     if (new_obj == nullptr) {
219       if (!_old_gen_is_full) {
220         // Do we allocate directly, or flush and refill?
221         if (new_obj_size > (OldPLABSize / 2)) {
222           // Allocate this object directly
223           new_obj = cast_to_oop(old_gen()->allocate(new_obj_size));
224           promotion_trace_event(new_obj, klass, new_obj_size, age, true, nullptr);
225         } else {
226           // Flush and fill
227           _old_lab.flush();
228 
229           HeapWord* lab_base = old_gen()->allocate(OldPLABSize);
230           if(lab_base != nullptr) {
231             _old_lab.initialize(MemRegion(lab_base, OldPLABSize));
232             // Try the old lab allocation again.
233             new_obj = cast_to_oop(_old_lab.allocate(new_obj_size));
234             promotion_trace_event(new_obj, klass, new_obj_size, age, true, &_old_lab);
235           }
236         }
237       }
238 
239       // This is the promotion failed test, and code handling.
240       // The code belongs here for two reasons. It is slightly
241       // different than the code below, and cannot share the
242       // CAS testing code. Keeping the code here also minimizes
243       // the impact on the common case fast path code.
244 
245       if (new_obj == nullptr) {
246         _old_gen_is_full = true;
247         return oop_promotion_failed(o, test_mark);
248       }
249     }
250   }
251 
252   assert(new_obj != nullptr, "allocation should have succeeded");
253 
254   // Copy obj
255   Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(o), cast_from_oop<HeapWord*>(new_obj), new_obj_size);
256 
257   // Parallel GC claims with a release - so other threads might access this object
258   // after claiming and they should see the "completed" object.
259   if (UseCompactObjectHeaders) {
260     // The copy above is not atomic. Make sure we have seen the proper mark
261     // and re-install it into the copy, so that Klass* is guaranteed to be correct.
262     markWord mark = o->mark();
263     if (!mark.is_marked()) {
264       new_obj->set_mark(mark);
265       ContinuationGCSupport::transform_stack_chunk(new_obj);
266     } else {
267       // If we copied a mark-word that indicates 'forwarded' state, the object
268       // installation would not succeed. We cannot access Klass* anymore either.
269       // Skip the transformation.
270     }
271   } else {
272     ContinuationGCSupport::transform_stack_chunk(new_obj);
273   }
274 
275   // Now we have to CAS in the header.
276   // Make copy visible to threads reading the forwardee.
277   oop forwardee = o->forward_to_atomic(new_obj, test_mark, memory_order_release);
278   if (forwardee == nullptr) {  // forwardee is null when forwarding is successful
279     // We won any races, we "own" this object.
280     assert(new_obj == o->forwardee(), "Sanity");
281 
282     // Increment age if obj still in new generation. Now that
283     // we're dealing with a markWord that cannot change, it is
284     // okay to use the non mt safe oop methods.
285     if (!new_obj_is_tenured) {
286       new_obj->incr_age();
287       assert(young_space()->contains(new_obj), "Attempt to push non-promoted obj");
288     }
289 
290     // Do the size comparison first with new_obj_size, which we
291     // already have. Hopefully, only a few objects are larger than
292     // _min_array_size_for_chunking, and most of them will be arrays.
293     // So, the is->objArray() test would be very infrequent.
< prev index next >