Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/gc/parallel/psPromotionManager.inline.hpp
41149 views
1
/*
2
* Copyright (c) 2002, 2021, 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
#ifndef SHARE_GC_PARALLEL_PSPROMOTIONMANAGER_INLINE_HPP
26
#define SHARE_GC_PARALLEL_PSPROMOTIONMANAGER_INLINE_HPP
27
28
#include "gc/parallel/psPromotionManager.hpp"
29
30
#include "gc/parallel/parallelScavengeHeap.hpp"
31
#include "gc/parallel/parMarkBitMap.inline.hpp"
32
#include "gc/parallel/psOldGen.hpp"
33
#include "gc/parallel/psPromotionLAB.inline.hpp"
34
#include "gc/parallel/psScavenge.inline.hpp"
35
#include "gc/shared/taskqueue.inline.hpp"
36
#include "gc/shared/tlab_globals.hpp"
37
#include "logging/log.hpp"
38
#include "memory/iterator.inline.hpp"
39
#include "oops/access.inline.hpp"
40
#include "oops/oop.inline.hpp"
41
#include "runtime/prefetch.inline.hpp"
42
43
inline PSPromotionManager* PSPromotionManager::manager_array(uint index) {
44
assert(_manager_array != NULL, "access of NULL manager_array");
45
assert(index <= ParallelGCThreads, "out of range manager_array access");
46
return &_manager_array[index];
47
}
48
49
inline void PSPromotionManager::push_depth(ScannerTask task) {
50
claimed_stack_depth()->push(task);
51
}
52
53
template <class T>
54
inline void PSPromotionManager::claim_or_forward_depth(T* p) {
55
assert(should_scavenge(p, true), "revisiting object?");
56
assert(ParallelScavengeHeap::heap()->is_in(p), "pointer outside heap");
57
oop obj = RawAccess<IS_NOT_NULL>::oop_load(p);
58
Prefetch::write(obj->mark_addr(), 0);
59
push_depth(ScannerTask(p));
60
}
61
62
inline void PSPromotionManager::promotion_trace_event(oop new_obj, oop old_obj,
63
size_t obj_size,
64
uint age, bool tenured,
65
const PSPromotionLAB* lab) {
66
// Skip if memory allocation failed
67
if (new_obj != NULL) {
68
const ParallelScavengeTracer* gc_tracer = PSScavenge::gc_tracer();
69
70
if (lab != NULL) {
71
// Promotion of object through newly allocated PLAB
72
if (gc_tracer->should_report_promotion_in_new_plab_event()) {
73
size_t obj_bytes = obj_size * HeapWordSize;
74
size_t lab_size = lab->capacity();
75
gc_tracer->report_promotion_in_new_plab_event(old_obj->klass(), obj_bytes,
76
age, tenured, lab_size);
77
}
78
} else {
79
// Promotion of object directly to heap
80
if (gc_tracer->should_report_promotion_outside_plab_event()) {
81
size_t obj_bytes = obj_size * HeapWordSize;
82
gc_tracer->report_promotion_outside_plab_event(old_obj->klass(), obj_bytes,
83
age, tenured);
84
}
85
}
86
}
87
}
88
89
class PSPushContentsClosure: public BasicOopIterateClosure {
90
PSPromotionManager* _pm;
91
public:
92
PSPushContentsClosure(PSPromotionManager* pm) : BasicOopIterateClosure(PSScavenge::reference_processor()), _pm(pm) {}
93
94
template <typename T> void do_oop_nv(T* p) {
95
if (PSScavenge::should_scavenge(p)) {
96
_pm->claim_or_forward_depth(p);
97
}
98
}
99
100
virtual void do_oop(oop* p) { do_oop_nv(p); }
101
virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
102
};
103
104
//
105
// This closure specialization will override the one that is defined in
106
// instanceRefKlass.inline.cpp. It swaps the order of oop_oop_iterate and
107
// oop_oop_iterate_ref_processing. Unfortunately G1 and Parallel behaves
108
// significantly better (especially in the Derby benchmark) using opposite
109
// order of these function calls.
110
//
111
template <>
112
inline void InstanceRefKlass::oop_oop_iterate_reverse<oop, PSPushContentsClosure>(oop obj, PSPushContentsClosure* closure) {
113
oop_oop_iterate_ref_processing<oop>(obj, closure);
114
InstanceKlass::oop_oop_iterate_reverse<oop>(obj, closure);
115
}
116
117
template <>
118
inline void InstanceRefKlass::oop_oop_iterate_reverse<narrowOop, PSPushContentsClosure>(oop obj, PSPushContentsClosure* closure) {
119
oop_oop_iterate_ref_processing<narrowOop>(obj, closure);
120
InstanceKlass::oop_oop_iterate_reverse<narrowOop>(obj, closure);
121
}
122
123
inline void PSPromotionManager::push_contents(oop obj) {
124
if (!obj->klass()->is_typeArray_klass()) {
125
PSPushContentsClosure pcc(this);
126
obj->oop_iterate_backwards(&pcc);
127
}
128
}
129
//
130
// This method is pretty bulky. It would be nice to split it up
131
// into smaller submethods, but we need to be careful not to hurt
132
// performance.
133
//
134
template<bool promote_immediately>
135
inline oop PSPromotionManager::copy_to_survivor_space(oop o) {
136
assert(should_scavenge(&o), "Sanity");
137
138
oop new_obj = NULL;
139
140
// NOTE! We must be very careful with any methods that access the mark
141
// in o. There may be multiple threads racing on it, and it may be forwarded
142
// at any time. Do not use oop methods for accessing the mark!
143
markWord test_mark = o->mark();
144
145
// The same test as "o->is_forwarded()"
146
if (!test_mark.is_marked()) {
147
bool new_obj_is_tenured = false;
148
size_t new_obj_size = o->size();
149
150
// Find the objects age, MT safe.
151
uint age = (test_mark.has_displaced_mark_helper() /* o->has_displaced_mark() */) ?
152
test_mark.displaced_mark_helper().age() : test_mark.age();
153
154
if (!promote_immediately) {
155
// Try allocating obj in to-space (unless too old)
156
if (age < PSScavenge::tenuring_threshold()) {
157
new_obj = cast_to_oop(_young_lab.allocate(new_obj_size));
158
if (new_obj == NULL && !_young_gen_is_full) {
159
// Do we allocate directly, or flush and refill?
160
if (new_obj_size > (YoungPLABSize / 2)) {
161
// Allocate this object directly
162
new_obj = cast_to_oop(young_space()->cas_allocate(new_obj_size));
163
promotion_trace_event(new_obj, o, new_obj_size, age, false, NULL);
164
} else {
165
// Flush and fill
166
_young_lab.flush();
167
168
HeapWord* lab_base = young_space()->cas_allocate(YoungPLABSize);
169
if (lab_base != NULL) {
170
_young_lab.initialize(MemRegion(lab_base, YoungPLABSize));
171
// Try the young lab allocation again.
172
new_obj = cast_to_oop(_young_lab.allocate(new_obj_size));
173
promotion_trace_event(new_obj, o, new_obj_size, age, false, &_young_lab);
174
} else {
175
_young_gen_is_full = true;
176
}
177
}
178
}
179
}
180
}
181
182
// Otherwise try allocating obj tenured
183
if (new_obj == NULL) {
184
#ifndef PRODUCT
185
if (ParallelScavengeHeap::heap()->promotion_should_fail()) {
186
return oop_promotion_failed(o, test_mark);
187
}
188
#endif // #ifndef PRODUCT
189
190
new_obj = cast_to_oop(_old_lab.allocate(new_obj_size));
191
new_obj_is_tenured = true;
192
193
if (new_obj == NULL) {
194
if (!_old_gen_is_full) {
195
// Do we allocate directly, or flush and refill?
196
if (new_obj_size > (OldPLABSize / 2)) {
197
// Allocate this object directly
198
new_obj = cast_to_oop(old_gen()->allocate(new_obj_size));
199
promotion_trace_event(new_obj, o, new_obj_size, age, true, NULL);
200
} else {
201
// Flush and fill
202
_old_lab.flush();
203
204
HeapWord* lab_base = old_gen()->allocate(OldPLABSize);
205
if(lab_base != NULL) {
206
#ifdef ASSERT
207
// Delay the initialization of the promotion lab (plab).
208
// This exposes uninitialized plabs to card table processing.
209
if (GCWorkerDelayMillis > 0) {
210
os::naked_sleep(GCWorkerDelayMillis);
211
}
212
#endif
213
_old_lab.initialize(MemRegion(lab_base, OldPLABSize));
214
// Try the old lab allocation again.
215
new_obj = cast_to_oop(_old_lab.allocate(new_obj_size));
216
promotion_trace_event(new_obj, o, new_obj_size, age, true, &_old_lab);
217
}
218
}
219
}
220
221
// This is the promotion failed test, and code handling.
222
// The code belongs here for two reasons. It is slightly
223
// different than the code below, and cannot share the
224
// CAS testing code. Keeping the code here also minimizes
225
// the impact on the common case fast path code.
226
227
if (new_obj == NULL) {
228
_old_gen_is_full = true;
229
return oop_promotion_failed(o, test_mark);
230
}
231
}
232
}
233
234
assert(new_obj != NULL, "allocation should have succeeded");
235
236
// Copy obj
237
Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(o), cast_from_oop<HeapWord*>(new_obj), new_obj_size);
238
239
// Now we have to CAS in the header.
240
// Make copy visible to threads reading the forwardee.
241
if (o->cas_forward_to(new_obj, test_mark, memory_order_release)) {
242
// We won any races, we "own" this object.
243
assert(new_obj == o->forwardee(), "Sanity");
244
245
// Increment age if obj still in new generation. Now that
246
// we're dealing with a markWord that cannot change, it is
247
// okay to use the non mt safe oop methods.
248
if (!new_obj_is_tenured) {
249
new_obj->incr_age();
250
assert(young_space()->contains(new_obj), "Attempt to push non-promoted obj");
251
}
252
253
// Do the size comparison first with new_obj_size, which we
254
// already have. Hopefully, only a few objects are larger than
255
// _min_array_size_for_chunking, and most of them will be arrays.
256
// So, the is->objArray() test would be very infrequent.
257
if (new_obj_size > _min_array_size_for_chunking &&
258
new_obj->is_objArray() &&
259
PSChunkLargeArrays) {
260
// we'll chunk it
261
push_depth(ScannerTask(PartialArrayScanTask(o)));
262
TASKQUEUE_STATS_ONLY(++_arrays_chunked; ++_array_chunk_pushes);
263
} else {
264
// we'll just push its contents
265
push_contents(new_obj);
266
}
267
} else {
268
// We lost, someone else "owns" this object
269
guarantee(o->is_forwarded(), "Object must be forwarded if the cas failed.");
270
271
// Try to deallocate the space. If it was directly allocated we cannot
272
// deallocate it, so we have to test. If the deallocation fails,
273
// overwrite with a filler object.
274
if (new_obj_is_tenured) {
275
if (!_old_lab.unallocate_object(cast_from_oop<HeapWord*>(new_obj), new_obj_size)) {
276
CollectedHeap::fill_with_object(cast_from_oop<HeapWord*>(new_obj), new_obj_size);
277
}
278
} else if (!_young_lab.unallocate_object(cast_from_oop<HeapWord*>(new_obj), new_obj_size)) {
279
CollectedHeap::fill_with_object(cast_from_oop<HeapWord*>(new_obj), new_obj_size);
280
}
281
282
// don't update this before the unallocation!
283
// Using acquire though consume would be accurate for accessing new_obj.
284
new_obj = o->forwardee_acquire();
285
}
286
} else {
287
assert(o->is_forwarded(), "Sanity");
288
new_obj = o->forwardee_acquire();
289
}
290
291
// This code must come after the CAS test, or it will print incorrect
292
// information.
293
log_develop_trace(gc, scavenge)("{%s %s " PTR_FORMAT " -> " PTR_FORMAT " (%d)}",
294
should_scavenge(&new_obj) ? "copying" : "tenuring",
295
new_obj->klass()->internal_name(), p2i((void *)o), p2i((void *)new_obj), new_obj->size());
296
297
return new_obj;
298
}
299
300
// Attempt to "claim" oop at p via CAS, push the new obj if successful
301
// This version tests the oop* to make sure it is within the heap before
302
// attempting marking.
303
template <bool promote_immediately, class T>
304
inline void PSPromotionManager::copy_and_push_safe_barrier(T* p) {
305
assert(should_scavenge(p, true), "revisiting object?");
306
307
oop o = RawAccess<IS_NOT_NULL>::oop_load(p);
308
oop new_obj = o->is_forwarded()
309
? o->forwardee()
310
: copy_to_survivor_space<promote_immediately>(o);
311
312
// This code must come after the CAS test, or it will print incorrect
313
// information.
314
if (log_develop_is_enabled(Trace, gc, scavenge) && o->is_forwarded()) {
315
log_develop_trace(gc, scavenge)("{%s %s " PTR_FORMAT " -> " PTR_FORMAT " (%d)}",
316
"forwarding",
317
new_obj->klass()->internal_name(), p2i((void *)o), p2i((void *)new_obj), new_obj->size());
318
}
319
320
RawAccess<IS_NOT_NULL>::oop_store(p, new_obj);
321
322
// We cannot mark without test, as some code passes us pointers
323
// that are outside the heap. These pointers are either from roots
324
// or from metadata.
325
if ((!PSScavenge::is_obj_in_young((HeapWord*)p)) &&
326
ParallelScavengeHeap::heap()->is_in_reserved(p)) {
327
if (PSScavenge::is_obj_in_young(new_obj)) {
328
PSScavenge::card_table()->inline_write_ref_field_gc(p, new_obj);
329
}
330
}
331
}
332
333
inline void PSPromotionManager::process_popped_location_depth(ScannerTask task) {
334
if (task.is_partial_array_task()) {
335
assert(PSChunkLargeArrays, "invariant");
336
process_array_chunk(task.to_partial_array_task());
337
} else {
338
if (task.is_narrow_oop_ptr()) {
339
assert(UseCompressedOops, "Error");
340
copy_and_push_safe_barrier</*promote_immediately=*/false>(task.to_narrow_oop_ptr());
341
} else {
342
copy_and_push_safe_barrier</*promote_immediately=*/false>(task.to_oop_ptr());
343
}
344
}
345
}
346
347
inline bool PSPromotionManager::steal_depth(int queue_num, ScannerTask& t) {
348
return stack_array_depth()->steal(queue_num, t);
349
}
350
351
#if TASKQUEUE_STATS
352
void PSPromotionManager::record_steal(ScannerTask task) {
353
if (task.is_partial_array_task()) {
354
++_array_chunk_steals;
355
}
356
}
357
#endif // TASKQUEUE_STATS
358
359
#endif // SHARE_GC_PARALLEL_PSPROMOTIONMANAGER_INLINE_HPP
360
361