11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 */
26
27
28 #include "cds/aotMappedHeapWriter.hpp"
29 #include "classfile/systemDictionary.hpp"
30 #include "gc/shared/classUnloadingContext.hpp"
31 #include "gc/shared/fullGCForwarding.hpp"
32 #include "gc/shared/gc_globals.hpp"
33 #include "gc/shared/gcArguments.hpp"
34 #include "gc/shared/gcTimer.hpp"
35 #include "gc/shared/gcTraceTime.inline.hpp"
36 #include "gc/shared/locationPrinter.inline.hpp"
37 #include "gc/shared/memAllocator.hpp"
38 #include "gc/shared/plab.hpp"
39 #include "gc/shared/tlab_globals.hpp"
40 #include "gc/shenandoah/heuristics/shenandoahOldHeuristics.hpp"
41 #include "gc/shenandoah/heuristics/shenandoahYoungHeuristics.hpp"
42 #include "gc/shenandoah/mode/shenandoahGenerationalMode.hpp"
43 #include "gc/shenandoah/mode/shenandoahPassiveMode.hpp"
44 #include "gc/shenandoah/mode/shenandoahSATBMode.hpp"
45 #include "gc/shenandoah/shenandoahAllocRequest.hpp"
46 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
47 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
48 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
49 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
50 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
51 #include "gc/shenandoah/shenandoahConcurrentMark.hpp"
1273 // This thread went through the OOM during evac protocol. It is safe to return
1274 // the forward pointer. It must not attempt to evacuate any other objects.
1275 return ShenandoahBarrierSet::resolve_forwarded(p);
1276 }
1277
1278 assert(ShenandoahThreadLocalData::is_evac_allowed(thread), "must be enclosed in oom-evac scope");
1279
1280 ShenandoahHeapRegion* r = heap_region_containing(p);
1281 assert(!r->is_humongous(), "never evacuate humongous objects");
1282
1283 ShenandoahAffiliation target_gen = r->affiliation();
1284 return try_evacuate_object(p, thread, r, target_gen);
1285 }
1286
1287 oop ShenandoahHeap::try_evacuate_object(oop p, Thread* thread, ShenandoahHeapRegion* from_region,
1288 ShenandoahAffiliation target_gen) {
1289 assert(target_gen == YOUNG_GENERATION, "Only expect evacuations to young in this mode");
1290 assert(from_region->is_young(), "Only expect evacuations from young in this mode");
1291 bool alloc_from_lab = true;
1292 HeapWord* copy = nullptr;
1293 size_t size = ShenandoahForwarding::size(p);
1294
1295 #ifdef ASSERT
1296 if (ShenandoahOOMDuringEvacALot &&
1297 (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
1298 copy = nullptr;
1299 } else {
1300 #endif
1301 if (UseTLAB) {
1302 copy = allocate_from_gclab(thread, size);
1303 }
1304 if (copy == nullptr) {
1305 // If we failed to allocate in LAB, we'll try a shared allocation.
1306 ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size, target_gen);
1307 copy = allocate_memory(req);
1308 alloc_from_lab = false;
1309 }
1310 #ifdef ASSERT
1311 }
1312 #endif
1313
1314 if (copy == nullptr) {
1315 control_thread()->handle_alloc_failure_evac(size);
1316
1317 _oom_evac_handler.handle_out_of_memory_during_evacuation();
1318
1319 return ShenandoahBarrierSet::resolve_forwarded(p);
1320 }
1321
1322 if (ShenandoahEvacTracking) {
1323 evac_tracker()->begin_evacuation(thread, size * HeapWordSize, from_region->affiliation(), target_gen);
1324 }
1325
1326 // Copy the object:
1327 Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, size);
1328
1329 // Try to install the new forwarding pointer.
1330 oop copy_val = cast_to_oop(copy);
1331 oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);
1332 if (result == copy_val) {
1333 // Successfully evacuated. Our copy is now the public one!
1334 ContinuationGCSupport::relativize_stack_chunk(copy_val);
1335 shenandoah_assert_correct(nullptr, copy_val);
1336 if (ShenandoahEvacTracking) {
1337 evac_tracker()->end_evacuation(thread, size * HeapWordSize, from_region->affiliation(), target_gen);
1338 }
1339 return copy_val;
1340 } else {
1341 // Failed to evacuate. We need to deal with the object that is left behind. Since this
1342 // new allocation is certainly after TAMS, it will be considered live in the next cycle.
1343 // But if it happens to contain references to evacuated regions, those references would
1344 // not get updated for this stale copy during this cycle, and we will crash while scanning
1345 // it the next cycle.
1346 if (alloc_from_lab) {
1347 // For LAB allocations, it is enough to rollback the allocation ptr. Either the next
1348 // object will overwrite this stale copy, or the filler object on LAB retirement will
1349 // do this.
1350 ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size);
1351 } else {
1352 // For non-LAB allocations, we have no way to retract the allocation, and
1353 // have to explicitly overwrite the copy with the filler object. With that overwrite,
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 */
26
27
28 #include "cds/aotMappedHeapWriter.hpp"
29 #include "classfile/systemDictionary.hpp"
30 #include "gc/shared/classUnloadingContext.hpp"
31 #include "gc/shared/fullGCForwarding.inline.hpp"
32 #include "gc/shared/gc_globals.hpp"
33 #include "gc/shared/gcArguments.hpp"
34 #include "gc/shared/gcTimer.hpp"
35 #include "gc/shared/gcTraceTime.inline.hpp"
36 #include "gc/shared/locationPrinter.inline.hpp"
37 #include "gc/shared/memAllocator.hpp"
38 #include "gc/shared/plab.hpp"
39 #include "gc/shared/tlab_globals.hpp"
40 #include "gc/shenandoah/heuristics/shenandoahOldHeuristics.hpp"
41 #include "gc/shenandoah/heuristics/shenandoahYoungHeuristics.hpp"
42 #include "gc/shenandoah/mode/shenandoahGenerationalMode.hpp"
43 #include "gc/shenandoah/mode/shenandoahPassiveMode.hpp"
44 #include "gc/shenandoah/mode/shenandoahSATBMode.hpp"
45 #include "gc/shenandoah/shenandoahAllocRequest.hpp"
46 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
47 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
48 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
49 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
50 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
51 #include "gc/shenandoah/shenandoahConcurrentMark.hpp"
1273 // This thread went through the OOM during evac protocol. It is safe to return
1274 // the forward pointer. It must not attempt to evacuate any other objects.
1275 return ShenandoahBarrierSet::resolve_forwarded(p);
1276 }
1277
1278 assert(ShenandoahThreadLocalData::is_evac_allowed(thread), "must be enclosed in oom-evac scope");
1279
1280 ShenandoahHeapRegion* r = heap_region_containing(p);
1281 assert(!r->is_humongous(), "never evacuate humongous objects");
1282
1283 ShenandoahAffiliation target_gen = r->affiliation();
1284 return try_evacuate_object(p, thread, r, target_gen);
1285 }
1286
1287 oop ShenandoahHeap::try_evacuate_object(oop p, Thread* thread, ShenandoahHeapRegion* from_region,
1288 ShenandoahAffiliation target_gen) {
1289 assert(target_gen == YOUNG_GENERATION, "Only expect evacuations to young in this mode");
1290 assert(from_region->is_young(), "Only expect evacuations from young in this mode");
1291 bool alloc_from_lab = true;
1292 HeapWord* copy = nullptr;
1293
1294 markWord mark = p->mark();
1295 if (ShenandoahForwarding::is_forwarded(mark)) {
1296 return ShenandoahForwarding::get_forwardee(p);
1297 }
1298 size_t old_size = ShenandoahForwarding::size(p);
1299 size_t size = p->copy_size(old_size, mark);
1300
1301 #ifdef ASSERT
1302 if (ShenandoahOOMDuringEvacALot &&
1303 (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
1304 copy = nullptr;
1305 } else {
1306 #endif
1307 if (UseTLAB) {
1308 copy = allocate_from_gclab(thread, size);
1309 }
1310 if (copy == nullptr) {
1311 // If we failed to allocate in LAB, we'll try a shared allocation.
1312 ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size, target_gen);
1313 copy = allocate_memory(req);
1314 alloc_from_lab = false;
1315 }
1316 #ifdef ASSERT
1317 }
1318 #endif
1319
1320 if (copy == nullptr) {
1321 control_thread()->handle_alloc_failure_evac(size);
1322
1323 _oom_evac_handler.handle_out_of_memory_during_evacuation();
1324
1325 return ShenandoahBarrierSet::resolve_forwarded(p);
1326 }
1327
1328 if (ShenandoahEvacTracking) {
1329 evac_tracker()->begin_evacuation(thread, size * HeapWordSize, from_region->affiliation(), target_gen);
1330 }
1331
1332 // Copy the object:
1333 Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, old_size);
1334
1335 // Try to install the new forwarding pointer.
1336 oop copy_val = cast_to_oop(copy);
1337 oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);
1338 if (result == copy_val) {
1339 // Successfully evacuated. Our copy is now the public one!
1340 copy_val->initialize_hash_if_necessary(p);
1341 ContinuationGCSupport::relativize_stack_chunk(copy_val);
1342 shenandoah_assert_correct(nullptr, copy_val);
1343 if (ShenandoahEvacTracking) {
1344 evac_tracker()->end_evacuation(thread, size * HeapWordSize, from_region->affiliation(), target_gen);
1345 }
1346 return copy_val;
1347 } else {
1348 // Failed to evacuate. We need to deal with the object that is left behind. Since this
1349 // new allocation is certainly after TAMS, it will be considered live in the next cycle.
1350 // But if it happens to contain references to evacuated regions, those references would
1351 // not get updated for this stale copy during this cycle, and we will crash while scanning
1352 // it the next cycle.
1353 if (alloc_from_lab) {
1354 // For LAB allocations, it is enough to rollback the allocation ptr. Either the next
1355 // object will overwrite this stale copy, or the filler object on LAB retirement will
1356 // do this.
1357 ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size);
1358 } else {
1359 // For non-LAB allocations, we have no way to retract the allocation, and
1360 // have to explicitly overwrite the copy with the filler object. With that overwrite,
|