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/zUtils.inline.hpp"
  38 #include "memory/iterator.hpp"
  39 #include "memory/universe.hpp"
  40 #include "runtime/mutexLocker.hpp"
  41 #include "utilities/align.hpp"
  42 
  43 ZCollectedHeap* ZCollectedHeap::heap() {
  44   return named_heap<ZCollectedHeap>(CollectedHeap::Z);
  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     _stat(new ZStat()),
  55     _runtime_workers() {}
  56 
  57 CollectedHeap::Name ZCollectedHeap::kind() const {
  58   return CollectedHeap::Z;
  59 }
  60 
  61 const char* ZCollectedHeap::name() const {
  62   return ZName;
  63 }
  64 
  65 jint ZCollectedHeap::initialize() {
  66   if (!_heap.is_initialized()) {
  67     return JNI_ENOMEM;
  68   }
  69 
  70   Universe::calculate_verify_data((HeapWord*)0, (HeapWord*)UINTPTR_MAX);
  71 
  72   return JNI_OK;
  73 }
  74 
  75 void ZCollectedHeap::initialize_serviceability() {
  76   _heap.serviceability_initialize();
  77 }
  78 
  79 class ZStopConcurrentGCThreadClosure : public ThreadClosure {
  80 public:
  81   virtual void do_thread(Thread* thread) {
  82     if (thread->is_ConcurrentGC_thread() &&
  83         !thread->is_GC_task_thread()) {
  84       static_cast<ConcurrentGCThread*>(thread)->stop();
  85     }
  86   }
  87 };
  88 
  89 void ZCollectedHeap::stop() {
  90   ZStopConcurrentGCThreadClosure cl;
  91   gc_threads_do(&cl);
  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::run_task(AbstractGangTask* task) {
 257   return _heap.run_task(task);
 258 }
 259 
 260 void ZCollectedHeap::keep_alive(oop obj) {
 261   _heap.keep_alive(obj);
 262 }
 263 
 264 void ZCollectedHeap::register_nmethod(nmethod* nm) {
 265   ZNMethod::register_nmethod(nm);
 266 }
 267 
 268 void ZCollectedHeap::unregister_nmethod(nmethod* nm) {
 269   ZNMethod::unregister_nmethod(nm);
 270 }
 271 
 272 void ZCollectedHeap::flush_nmethod(nmethod* nm) {
 273   ZNMethod::flush_nmethod(nm);
 274 }
 275 
 276 void ZCollectedHeap::verify_nmethod(nmethod* nm) {
 277   // Does nothing
 278 }
 279 
 280 WorkGang* ZCollectedHeap::get_safepoint_workers() {
 281   return _runtime_workers.workers();
 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(_stat);
 288   _heap.threads_do(tc);
 289   _runtime_workers.threads_do(tc);
 290 }
 291 
 292 VirtualSpaceSummary ZCollectedHeap::create_heap_space_summary() {
 293   return VirtualSpaceSummary((HeapWord*)0, (HeapWord*)capacity(), (HeapWord*)max_capacity());
 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   st->print_cr("ZGC Globals:");
 314   st->print_cr(" GlobalPhase:       %u (%s)", ZGlobalPhase, ZGlobalPhaseToString());
 315   st->print_cr(" GlobalSeqNum:      %u", ZGlobalSeqNum);
 316   st->print_cr(" Offset Max:        " SIZE_FORMAT "%s (" PTR_FORMAT ")",
 317                byte_size_in_exact_unit(ZAddressOffsetMax),
 318                exact_unit_for_byte_size(ZAddressOffsetMax),
 319                ZAddressOffsetMax);
 320   st->print_cr(" Page Size Small:   " SIZE_FORMAT "M", ZPageSizeSmall / M);
 321   st->print_cr(" Page Size Medium:  " SIZE_FORMAT "M", ZPageSizeMedium / M);
 322   st->cr();
 323   st->print_cr("ZGC 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   st->cr();
 330   CollectedHeap::print_on_error(st);
 331 }
 332 
 333 void ZCollectedHeap::print_extended_on(outputStream* st) const {
 334   _heap.print_extended_on(st);
 335 }
 336 
 337 void ZCollectedHeap::print_tracing_info() const {
 338   // Does nothing
 339 }
 340 
 341 bool ZCollectedHeap::print_location(outputStream* st, void* addr) const {
 342   return _heap.print_location(st, (uintptr_t)addr);
 343 }
 344 
 345 void ZCollectedHeap::verify(VerifyOption option /* ignored */) {
 346   _heap.verify();
 347 }
 348 
 349 bool ZCollectedHeap::is_oop(oop object) const {
 350   return _heap.is_oop(ZOop::to_address(object));
 351 }
 352 
 353 bool ZCollectedHeap::supports_concurrent_gc_breakpoints() const {
 354   return true;
 355 }