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