1 /*
   2  * Copyright (c) 2017, 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 
  25 #include "precompiled.hpp"
  26 #include "gc/cms/compactibleFreeListSpace.hpp"
  27 #include "gc/cms/concurrentMarkSweepGeneration.hpp"
  28 #include "gc/cms/concurrentMarkSweepThread.hpp"
  29 #include "gc/cms/cmsHeap.hpp"
  30 #include "gc/cms/parNewGeneration.hpp"
  31 #include "gc/cms/vmCMSOperations.hpp"
  32 #include "gc/shared/genOopClosures.inline.hpp"
  33 #include "gc/shared/strongRootsScope.hpp"
  34 #include "gc/shared/workgroup.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "runtime/vmThread.hpp"
  37 #include "services/memoryManager.hpp"
  38 #include "services/memoryPool.hpp"
  39 #include "utilities/stack.inline.hpp"
  40 
  41 CMSHeap::CMSHeap(GenCollectorPolicy *policy) : GenCollectedHeap(policy) {
  42   _workers = new WorkGang("GC Thread", ParallelGCThreads,
  43                           /* are_GC_task_threads */true,
  44                           /* are_ConcurrentGC_threads */false);
  45   _workers->initialize_workers();
  46 }
  47 
  48 jint CMSHeap::initialize() {
  49   jint status = GenCollectedHeap::initialize();
  50   if (status != JNI_OK) return status;
  51 
  52   // If we are running CMS, create the collector responsible
  53   // for collecting the CMS generations.
  54   assert(collector_policy()->is_concurrent_mark_sweep_policy(), "must be CMS policy");
  55   if (!create_cms_collector()) {
  56     return JNI_ENOMEM;
  57   }
  58 
  59   return JNI_OK;
  60 }
  61 
  62 void CMSHeap::check_gen_kinds() {
  63   assert(young_gen()->kind() == Generation::ParNew,
  64          "Wrong youngest generation type");
  65   assert(old_gen()->kind() == Generation::ConcurrentMarkSweep,
  66          "Wrong generation kind");
  67 }
  68 
  69 CMSHeap* CMSHeap::heap() {
  70   CollectedHeap* heap = Universe::heap();
  71   assert(heap != NULL, "Uninitialized access to CMSHeap::heap()");
  72   assert(heap->kind() == CollectedHeap::CMSHeap, "Not a CMSHeap");
  73   return (CMSHeap*) heap;
  74 }
  75 
  76 void CMSHeap::gc_threads_do(ThreadClosure* tc) const {
  77   assert(workers() != NULL, "should have workers here");
  78   workers()->threads_do(tc);
  79   ConcurrentMarkSweepThread::threads_do(tc);
  80 }
  81 
  82 void CMSHeap::print_gc_threads_on(outputStream* st) const {
  83   assert(workers() != NULL, "should have workers here");
  84   workers()->print_worker_threads_on(st);
  85   ConcurrentMarkSweepThread::print_all_on(st);
  86 }
  87 
  88 void CMSHeap::print_on_error(outputStream* st) const {
  89   GenCollectedHeap::print_on_error(st);
  90   st->cr();
  91   CMSCollector::print_on_error(st);
  92 }
  93 
  94 bool CMSHeap::create_cms_collector() {
  95   assert(old_gen()->kind() == Generation::ConcurrentMarkSweep,
  96          "Unexpected generation kinds");
  97   assert(gen_policy()->is_concurrent_mark_sweep_policy(), "Unexpected policy type");
  98   CMSCollector* collector =
  99     new CMSCollector((ConcurrentMarkSweepGeneration*) old_gen(),
 100                      rem_set(),
 101                      gen_policy()->as_concurrent_mark_sweep_policy());
 102 
 103   if (collector == NULL || !collector->completed_initialization()) {
 104     if (collector) {
 105       delete collector; // Be nice in embedded situation
 106     }
 107     vm_shutdown_during_initialization("Could not create CMS collector");
 108     return false;
 109   }
 110   return true; // success
 111 }
 112 
 113 void CMSHeap::collect(GCCause::Cause cause) {
 114   if (should_do_concurrent_full_gc(cause)) {
 115     // Mostly concurrent full collection.
 116     collect_mostly_concurrent(cause);
 117   } else {
 118     GenCollectedHeap::collect(cause);
 119   }
 120 }
 121 
 122 bool CMSHeap::should_do_concurrent_full_gc(GCCause::Cause cause) {
 123   switch (cause) {
 124     case GCCause::_gc_locker:           return GCLockerInvokesConcurrent;
 125     case GCCause::_java_lang_system_gc:
 126     case GCCause::_dcmd_gc_run:         return ExplicitGCInvokesConcurrent;
 127     default:                            return false;
 128   }
 129 }
 130 
 131 void CMSHeap::collect_mostly_concurrent(GCCause::Cause cause) {
 132   assert(!Heap_lock->owned_by_self(), "Should not own Heap_lock");
 133 
 134   MutexLocker ml(Heap_lock);
 135   // Read the GC counts while holding the Heap_lock
 136   unsigned int full_gc_count_before = total_full_collections();
 137   unsigned int gc_count_before      = total_collections();
 138   {
 139     MutexUnlocker mu(Heap_lock);
 140     VM_GenCollectFullConcurrent op(gc_count_before, full_gc_count_before, cause);
 141     VMThread::execute(&op);
 142   }
 143 }
 144 
 145 void CMSHeap::stop() {
 146   ConcurrentMarkSweepThread::cmst()->stop();
 147 }
 148 
 149 void CMSHeap::safepoint_synchronize_begin() {
 150   ConcurrentMarkSweepThread::synchronize(false);
 151 }
 152 
 153 void CMSHeap::safepoint_synchronize_end() {
 154   ConcurrentMarkSweepThread::desynchronize(false);
 155 }
 156 
 157 void CMSHeap::cms_process_roots(StrongRootsScope* scope,
 158                                 bool young_gen_as_roots,
 159                                 ScanningOption so,
 160                                 bool only_strong_roots,
 161                                 OopsInGenClosure* root_closure,
 162                                 CLDClosure* cld_closure) {
 163   MarkingCodeBlobClosure mark_code_closure(root_closure, !CodeBlobToOopClosure::FixRelocations);
 164   OopsInGenClosure* weak_roots = only_strong_roots ? NULL : root_closure;
 165   CLDClosure* weak_cld_closure = only_strong_roots ? NULL : cld_closure;
 166 
 167   process_roots(scope, so, root_closure, weak_roots, cld_closure, weak_cld_closure, &mark_code_closure);
 168   if (!only_strong_roots) {
 169     process_string_table_roots(scope, root_closure);
 170   }
 171 
 172   if (young_gen_as_roots &&
 173       !_process_strong_tasks->is_task_claimed(GCH_PS_younger_gens)) {
 174     root_closure->set_generation(young_gen());
 175     young_gen()->oop_iterate(root_closure);
 176     root_closure->reset_generation();
 177   }
 178 
 179   _process_strong_tasks->all_tasks_completed(scope->n_threads());
 180 }
 181 
 182 void CMSHeap::gc_prologue(bool full) {
 183   always_do_update_barrier = false;
 184   GenCollectedHeap::gc_prologue(full);
 185 };
 186 
 187 void CMSHeap::gc_epilogue(bool full) {
 188   GenCollectedHeap::gc_epilogue(full);
 189   always_do_update_barrier = true;
 190 };
 191 
 192 class ParNewMemoryManager : public GCMemoryManager {
 193 private:
 194 public:
 195   ParNewMemoryManager() : GCMemoryManager() {}
 196 
 197   const char* name() { return "ParNew"; }
 198   virtual const char* gc_end_message() { return "end of major GC"; }
 199 };
 200 
 201 class CMSMemoryManager : public GCMemoryManager {
 202 private:
 203 public:
 204   CMSMemoryManager() : GCMemoryManager() {}
 205 
 206   const char* name() { return "ConcurrentMarkSweep";}
 207   virtual const char* gc_end_message() { return "end of minor GC"; }
 208 };
 209 
 210 class CompactibleFreeListSpacePool : public CollectedMemoryPool {
 211 private:
 212   CompactibleFreeListSpace* _space;
 213 public:
 214   CompactibleFreeListSpacePool(CompactibleFreeListSpace* space,
 215                                const char* name,
 216                                PoolType type,
 217                                size_t max_size,
 218                                bool support_usage_threshold) :
 219     CollectedMemoryPool(name, type, space->capacity(), max_size,
 220                         support_usage_threshold), _space(space) {
 221   }
 222 
 223   MemoryUsage get_memory_usage() {
 224     size_t maxSize   = (available_for_allocation() ? max_size() : 0);
 225     size_t used      = used_in_bytes();
 226     size_t committed = _space->capacity();
 227     
 228     return MemoryUsage(initial_size(), used, committed, maxSize);
 229   }
 230 
 231   size_t used_in_bytes() {
 232     return _space->used();
 233   }
 234 };
 235 
 236 void CMSHeap::init_memory_managers() {
 237   _minor_mgr = new ParNewMemoryManager();
 238   _major_mgr = new CMSMemoryManager();
 239 }
 240 
 241 GrowableArray<MemoryManager*> CMSHeap::memory_managers() {
 242   GrowableArray<MemoryManager*> cms_memory_managers;
 243   cms_memory_managers.append(_minor_mgr);
 244   cms_memory_managers.append(_major_mgr);
 245   return cms_memory_managers;
 246 }
 247 
 248 GrowableArray<MemoryPool*> CMSHeap::memory_pools() {
 249   GrowableArray<MemoryPool*> cms_memory_pools;
 250 
 251   ParNewGeneration* young = (ParNewGeneration*) young_gen();
 252   ContiguousSpacePool* eden = new ContiguousSpacePool(young->eden(), "Par Eden Space", MemoryPool::Heap, young->max_eden_size(), false);
 253   SurvivorContiguousSpacePool* survivor = new SurvivorContiguousSpacePool(young, "Par Survivor Space", MemoryPool::Heap, young->max_survivor_size(), false);
 254 
 255   ConcurrentMarkSweepGeneration* cms = (ConcurrentMarkSweepGeneration*) old_gen();
 256   CompactibleFreeListSpacePool* old = new CompactibleFreeListSpacePool(cms->cmsSpace(), "CMS Old Gen", MemoryPool::Heap, cms->reserved().byte_size(), true);
 257 
 258   cms_memory_pools.append(eden);
 259   cms_memory_pools.append(survivor);
 260   cms_memory_pools.append(old);
 261 
 262   _minor_mgr->add_pool(eden);
 263   _major_mgr->add_pool(eden);
 264   _minor_mgr->add_pool(survivor);
 265   _major_mgr->add_pool(survivor);
 266   _major_mgr->add_pool(old);
 267 
 268   return cms_memory_pools;
 269 }