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