< prev index next >

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

Print this page

  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 #include "precompiled.hpp"
  26 #include "memory/allocation.hpp"
  27 #include "memory/universe.hpp"
  28 
  29 #include "gc/shared/gcArguments.hpp"
  30 #include "gc/shared/gcTimer.hpp"
  31 #include "gc/shared/gcTraceTime.inline.hpp"
  32 #include "gc/shared/locationPrinter.inline.hpp"
  33 #include "gc/shared/memAllocator.hpp"
  34 #include "gc/shared/plab.hpp"

  35 #include "gc/shared/tlab_globals.hpp"
  36 
  37 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  38 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
  39 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  40 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  41 #include "gc/shenandoah/shenandoahConcurrentMark.hpp"
  42 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  43 #include "gc/shenandoah/shenandoahControlThread.hpp"
  44 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  45 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  46 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  47 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  48 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  49 #include "gc/shenandoah/shenandoahInitLogger.hpp"
  50 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  51 #include "gc/shenandoah/shenandoahMemoryPool.hpp"
  52 #include "gc/shenandoah/shenandoahMetrics.hpp"
  53 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
  54 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"

 175   _soft_max_size = _num_regions * reg_size_bytes;
 176 
 177   _committed = _initial_size;
 178 
 179   size_t heap_page_size   = UseLargePages ? (size_t)os::large_page_size() : (size_t)os::vm_page_size();
 180   size_t bitmap_page_size = UseLargePages ? (size_t)os::large_page_size() : (size_t)os::vm_page_size();
 181   size_t region_page_size = UseLargePages ? (size_t)os::large_page_size() : (size_t)os::vm_page_size();
 182 
 183   //
 184   // Reserve and commit memory for heap
 185   //
 186 
 187   ReservedHeapSpace heap_rs = Universe::reserve_heap(max_byte_size, heap_alignment);
 188   initialize_reserved_region(heap_rs);
 189   _heap_region = MemRegion((HeapWord*)heap_rs.base(), heap_rs.size() / HeapWordSize);
 190   _heap_region_special = heap_rs.special();
 191 
 192   assert((((size_t) base()) & ShenandoahHeapRegion::region_size_bytes_mask()) == 0,
 193          "Misaligned heap: " PTR_FORMAT, p2i(base()));
 194 


 195 #if SHENANDOAH_OPTIMIZED_MARKTASK
 196   // The optimized ShenandoahMarkTask takes some bits away from the full object bits.
 197   // Fail if we ever attempt to address more than we can.
 198   if ((uintptr_t)heap_rs.end() >= ShenandoahMarkTask::max_addressable()) {
 199     FormatBuffer<512> buf("Shenandoah reserved [" PTR_FORMAT ", " PTR_FORMAT") for the heap, \n"
 200                           "but max object address is " PTR_FORMAT ". Try to reduce heap size, or try other \n"
 201                           "VM options that allocate heap at lower addresses (HeapBaseMinAddress, AllocateHeapAt, etc).",
 202                 p2i(heap_rs.base()), p2i(heap_rs.end()), ShenandoahMarkTask::max_addressable());
 203     vm_exit_during_initialization("Fatal Error", buf);
 204   }
 205 #endif
 206 
 207   ReservedSpace sh_rs = heap_rs.first_part(max_byte_size);
 208   if (!_heap_region_special) {
 209     os::commit_memory_or_exit(sh_rs.base(), _initial_size, heap_alignment, false,
 210                               "Cannot commit heap memory");
 211   }
 212 
 213   //
 214   // Reserve and commit memory for bitmap(s)

 931   // Expand and retry allocation
 932   result = loader_data->metaspace_non_null()->expand_and_allocate(size, mdtype);
 933   if (result != NULL) {
 934     return result;
 935   }
 936 
 937   // Out of memory
 938   return NULL;
 939 }
 940 
 941 class ShenandoahConcurrentEvacuateRegionObjectClosure : public ObjectClosure {
 942 private:
 943   ShenandoahHeap* const _heap;
 944   Thread* const _thread;
 945 public:
 946   ShenandoahConcurrentEvacuateRegionObjectClosure(ShenandoahHeap* heap) :
 947     _heap(heap), _thread(Thread::current()) {}
 948 
 949   void do_object(oop p) {
 950     shenandoah_assert_marked(NULL, p);
 951     if (!p->is_forwarded()) {
 952       _heap->evacuate_object(p, _thread);
 953     }
 954   }
 955 };
 956 
 957 class ShenandoahEvacuationTask : public WorkerTask {
 958 private:
 959   ShenandoahHeap* const _sh;
 960   ShenandoahCollectionSet* const _cs;
 961   bool _concurrent;
 962 public:
 963   ShenandoahEvacuationTask(ShenandoahHeap* sh,
 964                            ShenandoahCollectionSet* cs,
 965                            bool concurrent) :
 966     WorkerTask("Shenandoah Evacuation"),
 967     _sh(sh),
 968     _cs(cs),
 969     _concurrent(concurrent)
 970   {}
 971 

1219 size_t ShenandoahHeap::tlab_capacity(Thread *thr) const {
1220   return _free_set->capacity();
1221 }
1222 
1223 class ObjectIterateScanRootClosure : public BasicOopIterateClosure {
1224 private:
1225   MarkBitMap* _bitmap;
1226   ShenandoahScanObjectStack* _oop_stack;
1227   ShenandoahHeap* const _heap;
1228   ShenandoahMarkingContext* const _marking_context;
1229 
1230   template <class T>
1231   void do_oop_work(T* p) {
1232     T o = RawAccess<>::oop_load(p);
1233     if (!CompressedOops::is_null(o)) {
1234       oop obj = CompressedOops::decode_not_null(o);
1235       if (_heap->is_concurrent_weak_root_in_progress() && !_marking_context->is_marked(obj)) {
1236         // There may be dead oops in weak roots in concurrent root phase, do not touch them.
1237         return;
1238       }
1239       obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);



1240 
1241       assert(oopDesc::is_oop(obj), "must be a valid oop");
1242       if (!_bitmap->is_marked(obj)) {
1243         _bitmap->mark(obj);
1244         _oop_stack->push(obj);
1245       }
1246     }
1247   }
1248 public:
1249   ObjectIterateScanRootClosure(MarkBitMap* bitmap, ShenandoahScanObjectStack* oop_stack) :
1250     _bitmap(bitmap), _oop_stack(oop_stack), _heap(ShenandoahHeap::heap()),
1251     _marking_context(_heap->marking_context()) {}
1252   void do_oop(oop* p)       { do_oop_work(p); }
1253   void do_oop(narrowOop* p) { do_oop_work(p); }
1254 };
1255 
1256 /*
1257  * This is public API, used in preparation of object_iterate().
1258  * Since we don't do linear scan of heap in object_iterate() (see comment below), we don't
1259  * need to make the heap parsable. For Shenandoah-internal linear heap scans that we can

1275  * wiped the bitmap in preparation for next marking).
1276  *
1277  * For all those reasons, we implement object iteration as a single marking traversal, reporting
1278  * objects as we mark+traverse through the heap, starting from GC roots. JVMTI IterateThroughHeap
1279  * is allowed to report dead objects, but is not required to do so.
1280  */
1281 void ShenandoahHeap::object_iterate(ObjectClosure* cl) {
1282   // Reset bitmap
1283   if (!prepare_aux_bitmap_for_iteration())
1284     return;
1285 
1286   ShenandoahScanObjectStack oop_stack;
1287   ObjectIterateScanRootClosure oops(&_aux_bit_map, &oop_stack);
1288   // Seed the stack with root scan
1289   scan_roots_for_iteration(&oop_stack, &oops);
1290 
1291   // Work through the oop stack to traverse heap
1292   while (! oop_stack.is_empty()) {
1293     oop obj = oop_stack.pop();
1294     assert(oopDesc::is_oop(obj), "must be a valid oop");

1295     cl->do_object(obj);
1296     obj->oop_iterate(&oops);
1297   }
1298 
1299   assert(oop_stack.is_empty(), "should be empty");
1300   // Reclaim bitmap
1301   reclaim_aux_bitmap_for_iteration();
1302 }
1303 
1304 bool ShenandoahHeap::prepare_aux_bitmap_for_iteration() {
1305   assert(SafepointSynchronize::is_at_safepoint(), "safe iteration is only available during safepoints");
1306 
1307   if (!_aux_bitmap_region_special && !os::commit_memory((char*)_aux_bitmap_region.start(), _aux_bitmap_region.byte_size(), false)) {
1308     log_warning(gc)("Could not commit native memory for auxiliary marking bitmap for heap iteration");
1309     return false;
1310   }
1311   // Reset bitmap
1312   _aux_bit_map.clear();
1313   return true;
1314 }

  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 #include "precompiled.hpp"
  26 #include "memory/allocation.hpp"
  27 #include "memory/universe.hpp"
  28 
  29 #include "gc/shared/gcArguments.hpp"
  30 #include "gc/shared/gcTimer.hpp"
  31 #include "gc/shared/gcTraceTime.inline.hpp"
  32 #include "gc/shared/locationPrinter.inline.hpp"
  33 #include "gc/shared/memAllocator.hpp"
  34 #include "gc/shared/plab.hpp"
  35 #include "gc/shared/slidingForwarding.hpp"
  36 #include "gc/shared/tlab_globals.hpp"
  37 
  38 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  39 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
  40 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  41 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  42 #include "gc/shenandoah/shenandoahConcurrentMark.hpp"
  43 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  44 #include "gc/shenandoah/shenandoahControlThread.hpp"
  45 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  46 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  47 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  48 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  49 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  50 #include "gc/shenandoah/shenandoahInitLogger.hpp"
  51 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  52 #include "gc/shenandoah/shenandoahMemoryPool.hpp"
  53 #include "gc/shenandoah/shenandoahMetrics.hpp"
  54 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
  55 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"

 176   _soft_max_size = _num_regions * reg_size_bytes;
 177 
 178   _committed = _initial_size;
 179 
 180   size_t heap_page_size   = UseLargePages ? (size_t)os::large_page_size() : (size_t)os::vm_page_size();
 181   size_t bitmap_page_size = UseLargePages ? (size_t)os::large_page_size() : (size_t)os::vm_page_size();
 182   size_t region_page_size = UseLargePages ? (size_t)os::large_page_size() : (size_t)os::vm_page_size();
 183 
 184   //
 185   // Reserve and commit memory for heap
 186   //
 187 
 188   ReservedHeapSpace heap_rs = Universe::reserve_heap(max_byte_size, heap_alignment);
 189   initialize_reserved_region(heap_rs);
 190   _heap_region = MemRegion((HeapWord*)heap_rs.base(), heap_rs.size() / HeapWordSize);
 191   _heap_region_special = heap_rs.special();
 192 
 193   assert((((size_t) base()) & ShenandoahHeapRegion::region_size_bytes_mask()) == 0,
 194          "Misaligned heap: " PTR_FORMAT, p2i(base()));
 195 
 196   _forwarding = new SlidingForwarding(_heap_region, ShenandoahHeapRegion::region_size_words_shift());
 197 
 198 #if SHENANDOAH_OPTIMIZED_MARKTASK
 199   // The optimized ShenandoahMarkTask takes some bits away from the full object bits.
 200   // Fail if we ever attempt to address more than we can.
 201   if ((uintptr_t)heap_rs.end() >= ShenandoahMarkTask::max_addressable()) {
 202     FormatBuffer<512> buf("Shenandoah reserved [" PTR_FORMAT ", " PTR_FORMAT") for the heap, \n"
 203                           "but max object address is " PTR_FORMAT ". Try to reduce heap size, or try other \n"
 204                           "VM options that allocate heap at lower addresses (HeapBaseMinAddress, AllocateHeapAt, etc).",
 205                 p2i(heap_rs.base()), p2i(heap_rs.end()), ShenandoahMarkTask::max_addressable());
 206     vm_exit_during_initialization("Fatal Error", buf);
 207   }
 208 #endif
 209 
 210   ReservedSpace sh_rs = heap_rs.first_part(max_byte_size);
 211   if (!_heap_region_special) {
 212     os::commit_memory_or_exit(sh_rs.base(), _initial_size, heap_alignment, false,
 213                               "Cannot commit heap memory");
 214   }
 215 
 216   //
 217   // Reserve and commit memory for bitmap(s)

 934   // Expand and retry allocation
 935   result = loader_data->metaspace_non_null()->expand_and_allocate(size, mdtype);
 936   if (result != NULL) {
 937     return result;
 938   }
 939 
 940   // Out of memory
 941   return NULL;
 942 }
 943 
 944 class ShenandoahConcurrentEvacuateRegionObjectClosure : public ObjectClosure {
 945 private:
 946   ShenandoahHeap* const _heap;
 947   Thread* const _thread;
 948 public:
 949   ShenandoahConcurrentEvacuateRegionObjectClosure(ShenandoahHeap* heap) :
 950     _heap(heap), _thread(Thread::current()) {}
 951 
 952   void do_object(oop p) {
 953     shenandoah_assert_marked(NULL, p);
 954     if (!ShenandoahForwarding::is_forwarded(p)) {
 955       _heap->evacuate_object(p, _thread);
 956     }
 957   }
 958 };
 959 
 960 class ShenandoahEvacuationTask : public WorkerTask {
 961 private:
 962   ShenandoahHeap* const _sh;
 963   ShenandoahCollectionSet* const _cs;
 964   bool _concurrent;
 965 public:
 966   ShenandoahEvacuationTask(ShenandoahHeap* sh,
 967                            ShenandoahCollectionSet* cs,
 968                            bool concurrent) :
 969     WorkerTask("Shenandoah Evacuation"),
 970     _sh(sh),
 971     _cs(cs),
 972     _concurrent(concurrent)
 973   {}
 974 

1222 size_t ShenandoahHeap::tlab_capacity(Thread *thr) const {
1223   return _free_set->capacity();
1224 }
1225 
1226 class ObjectIterateScanRootClosure : public BasicOopIterateClosure {
1227 private:
1228   MarkBitMap* _bitmap;
1229   ShenandoahScanObjectStack* _oop_stack;
1230   ShenandoahHeap* const _heap;
1231   ShenandoahMarkingContext* const _marking_context;
1232 
1233   template <class T>
1234   void do_oop_work(T* p) {
1235     T o = RawAccess<>::oop_load(p);
1236     if (!CompressedOops::is_null(o)) {
1237       oop obj = CompressedOops::decode_not_null(o);
1238       if (_heap->is_concurrent_weak_root_in_progress() && !_marking_context->is_marked(obj)) {
1239         // There may be dead oops in weak roots in concurrent root phase, do not touch them.
1240         return;
1241       }
1242 
1243       // We must not expose from-space oops to the rest of runtime, or else it
1244       // will call klass() on it, which might fail because of unexpected header.
1245       obj = ShenandoahBarrierSet::barrier_set()->load_reference_barrier(obj);
1246 
1247       assert(oopDesc::is_oop(obj), "must be a valid oop");
1248       if (!_bitmap->is_marked(obj)) {
1249         _bitmap->mark(obj);
1250         _oop_stack->push(obj);
1251       }
1252     }
1253   }
1254 public:
1255   ObjectIterateScanRootClosure(MarkBitMap* bitmap, ShenandoahScanObjectStack* oop_stack) :
1256     _bitmap(bitmap), _oop_stack(oop_stack), _heap(ShenandoahHeap::heap()),
1257     _marking_context(_heap->marking_context()) {}
1258   void do_oop(oop* p)       { do_oop_work(p); }
1259   void do_oop(narrowOop* p) { do_oop_work(p); }
1260 };
1261 
1262 /*
1263  * This is public API, used in preparation of object_iterate().
1264  * Since we don't do linear scan of heap in object_iterate() (see comment below), we don't
1265  * need to make the heap parsable. For Shenandoah-internal linear heap scans that we can

1281  * wiped the bitmap in preparation for next marking).
1282  *
1283  * For all those reasons, we implement object iteration as a single marking traversal, reporting
1284  * objects as we mark+traverse through the heap, starting from GC roots. JVMTI IterateThroughHeap
1285  * is allowed to report dead objects, but is not required to do so.
1286  */
1287 void ShenandoahHeap::object_iterate(ObjectClosure* cl) {
1288   // Reset bitmap
1289   if (!prepare_aux_bitmap_for_iteration())
1290     return;
1291 
1292   ShenandoahScanObjectStack oop_stack;
1293   ObjectIterateScanRootClosure oops(&_aux_bit_map, &oop_stack);
1294   // Seed the stack with root scan
1295   scan_roots_for_iteration(&oop_stack, &oops);
1296 
1297   // Work through the oop stack to traverse heap
1298   while (! oop_stack.is_empty()) {
1299     oop obj = oop_stack.pop();
1300     assert(oopDesc::is_oop(obj), "must be a valid oop");
1301     shenandoah_assert_not_in_cset_except(NULL, obj, cancelled_gc());
1302     cl->do_object(obj);
1303     obj->oop_iterate(&oops);
1304   }
1305 
1306   assert(oop_stack.is_empty(), "should be empty");
1307   // Reclaim bitmap
1308   reclaim_aux_bitmap_for_iteration();
1309 }
1310 
1311 bool ShenandoahHeap::prepare_aux_bitmap_for_iteration() {
1312   assert(SafepointSynchronize::is_at_safepoint(), "safe iteration is only available during safepoints");
1313 
1314   if (!_aux_bitmap_region_special && !os::commit_memory((char*)_aux_bitmap_region.start(), _aux_bitmap_region.byte_size(), false)) {
1315     log_warning(gc)("Could not commit native memory for auxiliary marking bitmap for heap iteration");
1316     return false;
1317   }
1318   // Reset bitmap
1319   _aux_bit_map.clear();
1320   return true;
1321 }
< prev index next >