< prev index next >

src/hotspot/share/gc/shenandoah/shenandoahHeap.cpp

Print this page

  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/shenandoahAllocRate.inline.hpp"
  46 #include "gc/shenandoah/shenandoahAllocRequest.hpp"
  47 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  48 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
  49 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
  50 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  51 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"

1284     Handshake::execute(&composite);
1285   }
1286 }
1287 
1288 oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) {
1289   assert(thread == Thread::current(), "Expected thread parameter to be current thread.");
1290 
1291   ShenandoahHeapRegion* r = heap_region_containing(p);
1292   assert(!r->is_humongous(), "never evacuate humongous objects");
1293 
1294   ShenandoahAffiliation target_gen = r->affiliation();
1295   return try_evacuate_object(p, thread, r, target_gen);
1296 }
1297 
1298 oop ShenandoahHeap::try_evacuate_object(oop p, Thread* thread, ShenandoahHeapRegion* from_region,
1299                                                ShenandoahAffiliation target_gen) {
1300   assert(target_gen == YOUNG_GENERATION, "Only expect evacuations to young in this mode");
1301   assert(from_region->is_young(), "Only expect evacuations from young in this mode");
1302   bool alloc_from_lab = true;
1303   HeapWord* copy = nullptr;
1304   size_t size = ShenandoahForwarding::size(p);






1305 
1306 #ifdef ASSERT
1307   if (ShenandoahOOMDuringEvacALot &&
1308       (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
1309     copy = nullptr;
1310   } else {
1311 #endif
1312     if (UseTLAB) {
1313       copy = allocate_from_gclab(thread, size);
1314     }
1315     if (copy == nullptr) {
1316       // If we failed to allocate in LAB, we'll try a shared allocation.
1317       ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size, target_gen);
1318       copy = allocate_memory(req);
1319       alloc_from_lab = false;
1320     }
1321 #ifdef ASSERT
1322   }
1323 #endif
1324 

1331     // (they succeeded where we failed) or self-forwarded first.
1332     markWord old_mark = p->mark();
1333     if (old_mark.is_forwarded()) {
1334       return ShenandoahForwarding::get_forwardee(p);
1335     }
1336     oop winner = ShenandoahForwarding::try_forward_to_self(p, old_mark);
1337     if (winner == nullptr) {
1338       // We own the self-forwarding. Flag the region so the degen/full GC
1339       // entry drain knows to scan it for self_fwd bits to clear.
1340       from_region->set_has_self_forwards();
1341       return p;
1342     }
1343     return winner;
1344   }
1345 
1346   if (ShenandoahEvacTracking) {
1347     evac_tracker()->begin_evacuation(thread, size * HeapWordSize, from_region->affiliation(), target_gen);
1348   }
1349 
1350   // Copy the object:
1351   Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, size);
1352 
1353   oop copy_val = cast_to_oop(copy);
1354 










1355   // Relativize stack chunks before publishing the copy. After the forwarding CAS,
1356   // mutators can see the copy and thaw it via the fast path if flags == 0. We must
1357   // relativize derived pointers and set gc_mode before that happens. Skip if the
1358   // copy's mark word is already a forwarding pointer (another thread won the race
1359   // and overwrote the original's header before we copied it).
1360   if (!ShenandoahForwarding::is_forwarded(copy_val)) {
1361     ContinuationGCSupport::relativize_stack_chunk(copy_val);
1362   }
1363 
1364   // Try to install the new forwarding pointer.
1365   oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);
1366   if (result == copy_val) {
1367     // Successfully evacuated. Our copy is now the public one!
1368     shenandoah_assert_correct(nullptr, copy_val);
1369     if (ShenandoahEvacTracking) {
1370       evac_tracker()->end_evacuation(thread, size * HeapWordSize, from_region->affiliation(), target_gen);
1371     }
1372     return copy_val;
1373   }  else {
1374     // Failed to evacuate. We need to deal with the object that is left behind. Since this

  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/shenandoahAllocRate.inline.hpp"
  46 #include "gc/shenandoah/shenandoahAllocRequest.hpp"
  47 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  48 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
  49 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
  50 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  51 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"

1284     Handshake::execute(&composite);
1285   }
1286 }
1287 
1288 oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) {
1289   assert(thread == Thread::current(), "Expected thread parameter to be current thread.");
1290 
1291   ShenandoahHeapRegion* r = heap_region_containing(p);
1292   assert(!r->is_humongous(), "never evacuate humongous objects");
1293 
1294   ShenandoahAffiliation target_gen = r->affiliation();
1295   return try_evacuate_object(p, thread, r, target_gen);
1296 }
1297 
1298 oop ShenandoahHeap::try_evacuate_object(oop p, Thread* thread, ShenandoahHeapRegion* from_region,
1299                                                ShenandoahAffiliation target_gen) {
1300   assert(target_gen == YOUNG_GENERATION, "Only expect evacuations to young in this mode");
1301   assert(from_region->is_young(), "Only expect evacuations from young in this mode");
1302   bool alloc_from_lab = true;
1303   HeapWord* copy = nullptr;
1304 
1305   markWord mark = p->mark();
1306   if (mark.is_forwarded()) {
1307     return ShenandoahForwarding::get_forwardee(p);
1308   }
1309   size_t old_size = ShenandoahForwarding::size(p);
1310   size_t size = p->copy_size(old_size, mark);
1311 
1312 #ifdef ASSERT
1313   if (ShenandoahOOMDuringEvacALot &&
1314       (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
1315     copy = nullptr;
1316   } else {
1317 #endif
1318     if (UseTLAB) {
1319       copy = allocate_from_gclab(thread, size);
1320     }
1321     if (copy == nullptr) {
1322       // If we failed to allocate in LAB, we'll try a shared allocation.
1323       ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size, target_gen);
1324       copy = allocate_memory(req);
1325       alloc_from_lab = false;
1326     }
1327 #ifdef ASSERT
1328   }
1329 #endif
1330 

1337     // (they succeeded where we failed) or self-forwarded first.
1338     markWord old_mark = p->mark();
1339     if (old_mark.is_forwarded()) {
1340       return ShenandoahForwarding::get_forwardee(p);
1341     }
1342     oop winner = ShenandoahForwarding::try_forward_to_self(p, old_mark);
1343     if (winner == nullptr) {
1344       // We own the self-forwarding. Flag the region so the degen/full GC
1345       // entry drain knows to scan it for self_fwd bits to clear.
1346       from_region->set_has_self_forwards();
1347       return p;
1348     }
1349     return winner;
1350   }
1351 
1352   if (ShenandoahEvacTracking) {
1353     evac_tracker()->begin_evacuation(thread, size * HeapWordSize, from_region->affiliation(), target_gen);
1354   }
1355 
1356   // Copy the object:
1357   Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, old_size);

1358   oop copy_val = cast_to_oop(copy);
1359 
1360   // Initialize the identity hash on the copy before installing the forwarding
1361   // pointer, using the mark word we captured earlier. We must do this before
1362   // the CAS so that the copy is fully initialized when it becomes visible to
1363   // other threads. Using the captured mark (rather than re-reading the copy's
1364   // mark) avoids races with other threads that may have evacuated p and
1365   // installed a forwarding pointer in the meantime.
1366   if (UseCompactObjectHeaders && mark.is_hashed_not_expanded()) {
1367     copy_val->set_mark(copy_val->initialize_hash_if_necessary(p, mark.klass(), mark));
1368   }
1369 
1370   // Relativize stack chunks before publishing the copy. After the forwarding CAS,
1371   // mutators can see the copy and thaw it via the fast path if flags == 0. We must
1372   // relativize derived pointers and set gc_mode before that happens. Skip if the
1373   // copy's mark word is already a forwarding pointer (another thread won the race
1374   // and overwrote the original's header before we copied it).
1375   if (!ShenandoahForwarding::is_forwarded(copy_val)) {
1376     ContinuationGCSupport::relativize_stack_chunk(copy_val);
1377   }
1378 
1379   // Try to install the new forwarding pointer.
1380   oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);
1381   if (result == copy_val) {
1382     // Successfully evacuated. Our copy is now the public one!
1383     shenandoah_assert_correct(nullptr, copy_val);
1384     if (ShenandoahEvacTracking) {
1385       evac_tracker()->end_evacuation(thread, size * HeapWordSize, from_region->affiliation(), target_gen);
1386     }
1387     return copy_val;
1388   }  else {
1389     // Failed to evacuate. We need to deal with the object that is left behind. Since this
< prev index next >