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/concurrentMarkSweepThread.hpp"
  27 #include "gc/cms/cmsGCServicabilitySupport.hpp"
  28 #include "gc/cms/cmsHeap.hpp"
  29 #include "gc/cms/vmCMSOperations.hpp"
  30 #include "gc/shared/genOopClosures.inline.hpp"
  31 #include "gc/shared/strongRootsScope.hpp"
  32 #include "gc/shared/workgroup.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "runtime/vmThread.hpp"
  35 #include "utilities/stack.inline.hpp"
  36 
  37 CMSHeap::CMSHeap(GenCollectorPolicy *policy) : GenCollectedHeap(policy) {
  38   _workers = new WorkGang("GC Thread", ParallelGCThreads,
  39                           /* are_GC_task_threads */true,
  40                           /* are_ConcurrentGC_threads */false);
  41   _workers->initialize_workers();
  42 }
  43 
  44 jint CMSHeap::initialize() {
  45   jint status = GenCollectedHeap::initialize();
  46   if (status != JNI_OK) return status;
  47 
  48   // If we are running CMS, create the collector responsible
  49   // for collecting the CMS generations.
  50   assert(collector_policy()->is_concurrent_mark_sweep_policy(), "must be CMS policy");
  51   if (!create_cms_collector()) {
  52     return JNI_ENOMEM;
  53   }
  54 
  55   return JNI_OK;
  56 }
  57 
  58 void CMSHeap::check_gen_kinds() {
  59   assert(young_gen()->kind() == Generation::ParNew,
  60          "Wrong youngest generation type");
  61   assert(old_gen()->kind() == Generation::ConcurrentMarkSweep,
  62          "Wrong generation kind");
  63 }
  64 
  65 CMSHeap* CMSHeap::heap() {
  66   CollectedHeap* heap = Universe::heap();
  67   assert(heap != NULL, "Uninitialized access to CMSHeap::heap()");
  68   assert(heap->kind() == CollectedHeap::CMSHeap, "Not a CMSHeap");
  69   return (CMSHeap*) heap;
  70 }
  71 
  72 void CMSHeap::gc_threads_do(ThreadClosure* tc) const {
  73   assert(workers() != NULL, "should have workers here");
  74   workers()->threads_do(tc);
  75   ConcurrentMarkSweepThread::threads_do(tc);
  76 }
  77 
  78 void CMSHeap::print_gc_threads_on(outputStream* st) const {
  79   assert(workers() != NULL, "should have workers here");
  80   workers()->print_worker_threads_on(st);
  81   ConcurrentMarkSweepThread::print_all_on(st);
  82 }
  83 
  84 void CMSHeap::print_on_error(outputStream* st) const {
  85   GenCollectedHeap::print_on_error(st);
  86   st->cr();
  87   CMSCollector::print_on_error(st);
  88 }
  89 
  90 bool CMSHeap::create_cms_collector() {
  91   assert(old_gen()->kind() == Generation::ConcurrentMarkSweep,
  92          "Unexpected generation kinds");
  93   assert(gen_policy()->is_concurrent_mark_sweep_policy(), "Unexpected policy type");
  94   CMSCollector* collector =
  95     new CMSCollector((ConcurrentMarkSweepGeneration*) old_gen(),
  96                      rem_set(),
  97                      gen_policy()->as_concurrent_mark_sweep_policy());
  98 
  99   if (collector == NULL || !collector->completed_initialization()) {
 100     if (collector) {
 101       delete collector; // Be nice in embedded situation
 102     }
 103     vm_shutdown_during_initialization("Could not create CMS collector");
 104     return false;
 105   }
 106   return true; // success
 107 }
 108 
 109 void CMSHeap::collect(GCCause::Cause cause) {
 110   if (should_do_concurrent_full_gc(cause)) {
 111     // Mostly concurrent full collection.
 112     collect_mostly_concurrent(cause);
 113   } else {
 114     GenCollectedHeap::collect(cause);
 115   }
 116 }
 117 
 118 bool CMSHeap::should_do_concurrent_full_gc(GCCause::Cause cause) {
 119   switch (cause) {
 120     case GCCause::_gc_locker:           return GCLockerInvokesConcurrent;
 121     case GCCause::_java_lang_system_gc:
 122     case GCCause::_dcmd_gc_run:         return ExplicitGCInvokesConcurrent;
 123     default:                            return false;
 124   }
 125 }
 126 
 127 void CMSHeap::collect_mostly_concurrent(GCCause::Cause cause) {
 128   assert(!Heap_lock->owned_by_self(), "Should not own Heap_lock");
 129 
 130   MutexLocker ml(Heap_lock);
 131   // Read the GC counts while holding the Heap_lock
 132   unsigned int full_gc_count_before = total_full_collections();
 133   unsigned int gc_count_before      = total_collections();
 134   {
 135     MutexUnlocker mu(Heap_lock);
 136     VM_GenCollectFullConcurrent op(gc_count_before, full_gc_count_before, cause);
 137     VMThread::execute(&op);
 138   }
 139 }
 140 
 141 void CMSHeap::stop() {
 142   ConcurrentMarkSweepThread::cmst()->stop();
 143 }
 144 
 145 void CMSHeap::safepoint_synchronize_begin() {
 146   ConcurrentMarkSweepThread::synchronize(false);
 147 }
 148 
 149 void CMSHeap::safepoint_synchronize_end() {
 150   ConcurrentMarkSweepThread::desynchronize(false);
 151 }
 152 
 153 void CMSHeap::cms_process_roots(StrongRootsScope* scope,
 154                                 bool young_gen_as_roots,
 155                                 ScanningOption so,
 156                                 bool only_strong_roots,
 157                                 OopsInGenClosure* root_closure,
 158                                 CLDClosure* cld_closure) {
 159   MarkingCodeBlobClosure mark_code_closure(root_closure, !CodeBlobToOopClosure::FixRelocations);
 160   OopsInGenClosure* weak_roots = only_strong_roots ? NULL : root_closure;
 161   CLDClosure* weak_cld_closure = only_strong_roots ? NULL : cld_closure;
 162 
 163   process_roots(scope, so, root_closure, weak_roots, cld_closure, weak_cld_closure, &mark_code_closure);
 164   if (!only_strong_roots) {
 165     process_string_table_roots(scope, root_closure);
 166   }
 167 
 168   if (young_gen_as_roots &&
 169       !_process_strong_tasks->is_task_claimed(GCH_PS_younger_gens)) {
 170     root_closure->set_generation(young_gen());
 171     young_gen()->oop_iterate(root_closure);
 172     root_closure->reset_generation();
 173   }
 174 
 175   _process_strong_tasks->all_tasks_completed(scope->n_threads());
 176 }
 177 
 178 void CMSHeap::gc_prologue(bool full) {
 179   always_do_update_barrier = false;
 180   GenCollectedHeap::gc_prologue(full);
 181 };
 182 
 183 void CMSHeap::gc_epilogue(bool full) {
 184   GenCollectedHeap::gc_epilogue(full);
 185   always_do_update_barrier = true;
 186 };
 187 
 188 GCServicabilitySupport* CMSHeap::create_servicability_support() {
 189   return new CMSGCServicabilitySupport();
 190 }