1 /*
   2  * Copyright (c) 2015, 2019, 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 #include "precompiled.hpp"
  25 #include "gc/shared/gcHeapSummary.hpp"
  26 #include "gc/shared/locationPrinter.hpp"
  27 #include "gc/shared/suspendibleThreadSet.hpp"
  28 #include "gc/z/zCollectedHeap.hpp"
  29 #include "gc/z/zGlobals.hpp"
  30 #include "gc/z/zHeap.inline.hpp"
  31 #include "gc/z/zNMethod.hpp"
  32 #include "gc/z/zObjArrayAllocator.hpp"
  33 #include "gc/z/zServiceability.hpp"
  34 #include "gc/z/zStat.hpp"
  35 #include "gc/z/zUtils.inline.hpp"
  36 #include "memory/universe.hpp"
  37 #include "runtime/mutexLocker.hpp"
  38 #include "utilities/align.hpp"
  39 
  40 ZCollectedHeap* ZCollectedHeap::heap() {
  41   CollectedHeap* heap = Universe::heap();
  42   assert(heap != NULL, "Uninitialized access to ZCollectedHeap::heap()");
  43   assert(heap->kind() == CollectedHeap::Z, "Invalid name");
  44   return (ZCollectedHeap*)heap;
  45 }
  46 
  47 ZCollectedHeap::ZCollectedHeap() :
  48     _soft_ref_policy(),
  49     _barrier_set(),
  50     _initialize(&_barrier_set),
  51     _heap(),
  52     _director(new ZDirector()),
  53     _driver(new ZDriver()),
  54     _uncommitter(new ZUncommitter()),
  55     _stat(new ZStat()),
  56     _runtime_workers() {}
  57 
  58 CollectedHeap::Name ZCollectedHeap::kind() const {
  59   return CollectedHeap::Z;
  60 }
  61 
  62 const char* ZCollectedHeap::name() const {
  63   return ZName;
  64 }
  65 
  66 jint ZCollectedHeap::initialize() {
  67   if (!_heap.is_initialized()) {
  68     return JNI_ENOMEM;
  69   }
  70 
  71   Universe::calculate_verify_data((HeapWord*)ZAddressReservedStart,
  72                                   (HeapWord*)ZAddressReservedEnd);
  73 
  74   return JNI_OK;
  75 }
  76 
  77 void ZCollectedHeap::initialize_serviceability() {
  78   _heap.serviceability_initialize();
  79 }
  80 
  81 void ZCollectedHeap::stop() {
  82   _director->stop();
  83   _driver->stop();
  84   _uncommitter->stop();
  85   _stat->stop();
  86 }
  87 
  88 SoftRefPolicy* ZCollectedHeap::soft_ref_policy() {
  89   return &_soft_ref_policy;
  90 }
  91 
  92 size_t ZCollectedHeap::max_capacity() const {
  93   return _heap.max_capacity();
  94 }
  95 
  96 size_t ZCollectedHeap::capacity() const {
  97   return _heap.capacity();
  98 }
  99 
 100 size_t ZCollectedHeap::used() const {
 101   return _heap.used();
 102 }
 103 
 104 size_t ZCollectedHeap::unused() const {
 105   return _heap.unused();
 106 }
 107 
 108 bool ZCollectedHeap::is_maximal_no_gc() const {
 109   // Not supported
 110   ShouldNotReachHere();
 111   return false;
 112 }
 113 
 114 bool ZCollectedHeap::is_in(const void* p) const {
 115   return _heap.is_in((uintptr_t)p);
 116 }
 117 
 118 uint32_t ZCollectedHeap::hash_oop(oop obj) const {
 119   return _heap.hash_oop(obj);
 120 }
 121 
 122 HeapWord* ZCollectedHeap::allocate_new_tlab(size_t min_size, size_t requested_size, size_t* actual_size) {
 123   const size_t size_in_bytes = ZUtils::words_to_bytes(align_object_size(requested_size));
 124   const uintptr_t addr = _heap.alloc_tlab(size_in_bytes);
 125 
 126   if (addr != 0) {
 127     *actual_size = requested_size;
 128   }
 129 
 130   return (HeapWord*)addr;
 131 }
 132 
 133 oop ZCollectedHeap::array_allocate(Klass* klass, int size, int length, bool do_zero, TRAPS) {
 134   if (!do_zero) {
 135     return CollectedHeap::array_allocate(klass, size, length, false /* do_zero */, THREAD);
 136   }
 137 
 138   ZObjArrayAllocator allocator(klass, size, length, THREAD);
 139   return allocator.allocate();
 140 }
 141 
 142 HeapWord* ZCollectedHeap::mem_allocate(size_t size, bool* gc_overhead_limit_was_exceeded) {
 143   const size_t size_in_bytes = ZUtils::words_to_bytes(align_object_size(size));
 144   return (HeapWord*)_heap.alloc_object(size_in_bytes);
 145 }
 146 
 147 MetaWord* ZCollectedHeap::satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
 148                                                              size_t size,
 149                                                              Metaspace::MetadataType mdtype) {
 150   MetaWord* result;
 151 
 152   // Start asynchronous GC
 153   collect(GCCause::_metadata_GC_threshold);
 154 
 155   // Expand and retry allocation
 156   result = loader_data->metaspace_non_null()->expand_and_allocate(size, mdtype);
 157   if (result != NULL) {
 158     return result;
 159   }
 160 
 161   // Start synchronous GC
 162   collect(GCCause::_metadata_GC_clear_soft_refs);
 163 
 164   // Retry allocation
 165   result = loader_data->metaspace_non_null()->allocate(size, mdtype);
 166   if (result != NULL) {
 167     return result;
 168   }
 169 
 170   // Expand and retry allocation
 171   result = loader_data->metaspace_non_null()->expand_and_allocate(size, mdtype);
 172   if (result != NULL) {
 173     return result;
 174   }
 175 
 176   // Out of memory
 177   return NULL;
 178 }
 179 
 180 void ZCollectedHeap::collect(GCCause::Cause cause) {
 181   _driver->collect(cause);
 182 }
 183 
 184 void ZCollectedHeap::collect_as_vm_thread(GCCause::Cause cause) {
 185   // These collection requests are ignored since ZGC can't run a synchronous
 186   // GC cycle from within the VM thread. This is considered benign, since the
 187   // only GC causes coming in here should be heap dumper and heap inspector.
 188   // However, neither the heap dumper nor the heap inspector really need a GC
 189   // to happen, but the result of their heap iterations might in that case be
 190   // less accurate since they might include objects that would otherwise have
 191   // been collected by a GC.
 192   assert(Thread::current()->is_VM_thread(), "Should be the VM thread");
 193   guarantee(cause == GCCause::_heap_dump ||
 194             cause == GCCause::_heap_inspection, "Invalid cause");
 195 }
 196 
 197 void ZCollectedHeap::do_full_collection(bool clear_all_soft_refs) {
 198   // Not supported
 199   ShouldNotReachHere();
 200 }
 201 
 202 bool ZCollectedHeap::supports_tlab_allocation() const {
 203   return true;
 204 }
 205 
 206 size_t ZCollectedHeap::tlab_capacity(Thread* ignored) const {
 207   return _heap.tlab_capacity();
 208 }
 209 
 210 size_t ZCollectedHeap::tlab_used(Thread* ignored) const {
 211   return _heap.tlab_used();
 212 }
 213 
 214 size_t ZCollectedHeap::max_tlab_size() const {
 215   return _heap.max_tlab_size();
 216 }
 217 
 218 size_t ZCollectedHeap::unsafe_max_tlab_alloc(Thread* ignored) const {
 219   return _heap.unsafe_max_tlab_alloc();
 220 }
 221 
 222 bool ZCollectedHeap::can_elide_tlab_store_barriers() const {
 223   return false;
 224 }
 225 
 226 bool ZCollectedHeap::can_elide_initializing_store_barrier(oop new_obj) {
 227   // Not supported
 228   ShouldNotReachHere();
 229   return true;
 230 }
 231 
 232 bool ZCollectedHeap::card_mark_must_follow_store() const {
 233   // Not supported
 234   ShouldNotReachHere();
 235   return false;
 236 }
 237 
 238 GrowableArray<GCMemoryManager*> ZCollectedHeap::memory_managers() {
 239   return GrowableArray<GCMemoryManager*>(1, 1, _heap.serviceability_memory_manager());
 240 }
 241 
 242 GrowableArray<MemoryPool*> ZCollectedHeap::memory_pools() {
 243   return GrowableArray<MemoryPool*>(1, 1, _heap.serviceability_memory_pool());
 244 }
 245 
 246 void ZCollectedHeap::object_iterate(ObjectClosure* cl) {
 247   _heap.object_iterate(cl, true /* visit_weaks */);
 248 }
 249 
 250 void ZCollectedHeap::safe_object_iterate(ObjectClosure* cl) {
 251   _heap.object_iterate(cl, true /* visit_weaks */);
 252 }
 253 
 254 void ZCollectedHeap::register_nmethod(nmethod* nm) {
 255   ZNMethod::register_nmethod(nm);
 256 }
 257 
 258 void ZCollectedHeap::unregister_nmethod(nmethod* nm) {
 259   ZNMethod::unregister_nmethod(nm);
 260 }
 261 
 262 void ZCollectedHeap::flush_nmethod(nmethod* nm) {
 263   ZNMethod::flush_nmethod(nm);
 264 }
 265 
 266 void ZCollectedHeap::verify_nmethod(nmethod* nm) {
 267   // Does nothing
 268 }
 269 
 270 WorkGang* ZCollectedHeap::get_safepoint_workers() {
 271   return _runtime_workers.workers();
 272 }
 273 
 274 jlong ZCollectedHeap::millis_since_last_gc() {
 275   return ZStatCycle::time_since_last() / MILLIUNITS;
 276 }
 277 
 278 void ZCollectedHeap::gc_threads_do(ThreadClosure* tc) const {
 279   tc->do_thread(_director);
 280   tc->do_thread(_driver);
 281   tc->do_thread(_uncommitter);
 282   tc->do_thread(_stat);
 283   _heap.worker_threads_do(tc);
 284   _runtime_workers.threads_do(tc);
 285 }
 286 
 287 VirtualSpaceSummary ZCollectedHeap::create_heap_space_summary() {
 288   const size_t capacity_in_words = capacity() / HeapWordSize;
 289   const size_t max_capacity_in_words = max_capacity() / HeapWordSize;
 290   HeapWord* const heap_start = (HeapWord*)ZAddressReservedStart;
 291   return VirtualSpaceSummary(heap_start,
 292                              heap_start + capacity_in_words,
 293                              heap_start + max_capacity_in_words);
 294 }
 295 
 296 void ZCollectedHeap::safepoint_synchronize_begin() {
 297   SuspendibleThreadSet::synchronize();
 298 }
 299 
 300 void ZCollectedHeap::safepoint_synchronize_end() {
 301   SuspendibleThreadSet::desynchronize();
 302 }
 303 
 304 void ZCollectedHeap::prepare_for_verify() {
 305   // Does nothing
 306 }
 307 
 308 void ZCollectedHeap::print_on(outputStream* st) const {
 309   _heap.print_on(st);
 310 }
 311 
 312 void ZCollectedHeap::print_on_error(outputStream* st) const {
 313   CollectedHeap::print_on_error(st);
 314 
 315   st->print_cr("Address Space");
 316   st->print_cr( "     Start:             " PTR_FORMAT, ZAddressSpaceStart);
 317   st->print_cr( "     End:               " PTR_FORMAT, ZAddressSpaceEnd);
 318   st->print_cr( "     Size:              " SIZE_FORMAT_W(-15) " (" PTR_FORMAT ")", ZAddressSpaceSize, ZAddressSpaceSize);
 319   st->print_cr( "Heap");
 320   st->print_cr( "     GlobalPhase:       %u", ZGlobalPhase);
 321   st->print_cr( "     GlobalSeqNum:      %u", ZGlobalSeqNum);
 322   st->print_cr( "     Offset Max:        " SIZE_FORMAT_W(-15) " (" PTR_FORMAT ")", ZAddressOffsetMax, ZAddressOffsetMax);
 323   st->print_cr( "     Page Size Small:   " SIZE_FORMAT_W(-15) " (" PTR_FORMAT ")", ZPageSizeSmall, ZPageSizeSmall);
 324   st->print_cr( "     Page Size Medium:  " SIZE_FORMAT_W(-15) " (" PTR_FORMAT ")", ZPageSizeMedium, ZPageSizeMedium);
 325   st->print_cr( "Metadata Bits");
 326   st->print_cr( "     Good:              " PTR_FORMAT, ZAddressGoodMask);
 327   st->print_cr( "     Bad:               " PTR_FORMAT, ZAddressBadMask);
 328   st->print_cr( "     WeakBad:           " PTR_FORMAT, ZAddressWeakBadMask);
 329   st->print_cr( "     Marked:            " PTR_FORMAT, ZAddressMetadataMarked);
 330   st->print_cr( "     Remapped:          " PTR_FORMAT, ZAddressMetadataRemapped);
 331 }
 332 
 333 void ZCollectedHeap::print_extended_on(outputStream* st) const {
 334   _heap.print_extended_on(st);
 335 }
 336 
 337 void ZCollectedHeap::print_gc_threads_on(outputStream* st) const {
 338   _director->print_on(st);
 339   st->cr();
 340   _driver->print_on(st);
 341   st->cr();
 342   _uncommitter->print_on(st);
 343   st->cr();
 344   _stat->print_on(st);
 345   st->cr();
 346   _heap.print_worker_threads_on(st);
 347   _runtime_workers.print_threads_on(st);
 348 }
 349 
 350 void ZCollectedHeap::print_tracing_info() const {
 351   // Does nothing
 352 }
 353 
 354 bool ZCollectedHeap::print_location(outputStream* st, void* addr) const {
 355   if (LocationPrinter::is_valid_obj(addr)) {
 356     st->print(INTPTR_FORMAT " is a %s oop: ", p2i(addr),
 357               ZAddress::is_good(reinterpret_cast<uintptr_t>(addr)) ? "good" : "bad");
 358     cast_to_oop(addr)->print_on(st);
 359     return true;
 360   }
 361   return false;
 362 }
 363 
 364 void ZCollectedHeap::verify(VerifyOption option /* ignored */) {
 365   _heap.verify();
 366 }
 367 
 368 bool ZCollectedHeap::is_oop(oop object) const {
 369   return CollectedHeap::is_oop(object) && _heap.is_oop(object);
 370 }
 371 
 372 void ZCollectedHeap::check_oop_location(void* addr) const {
 373   assert(is_object_aligned(addr), "address is not aligned");
 374 
 375   const uintptr_t addr_int = reinterpret_cast<uintptr_t>(addr);
 376   assert(addr_int >= ZAddressSpaceStart, "address is outside of the heap");
 377   assert(addr_int < ZAddressSpaceEnd,    "address is outside of the heap");
 378 }