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 "gc/cms/concurrentMarkSweepThread.hpp"
  26 #include "gc/cms/cmsHeap.hpp"
  27 #include "gc/cms/vmCMSOperations.hpp"
  28 #include "gc/shared/genOopClosures.inline.hpp"
  29 #include "gc/shared/strongRootsScope.hpp"
  30 #include "gc/shared/workgroup.hpp"
  31 #include "oops/oop.inline.hpp"
  32 #include "runtime/vmThread.hpp"
  33 #include "utilities/stack.inline.hpp"
  34 
  35 CMSHeap::CMSHeap(GenCollectorPolicy *policy) : GenCollectedHeap(policy) {
  36 
  37   _workers = new WorkGang("GC Thread", ParallelGCThreads,
  38                           /* are_GC_task_threads */true,
  39                           /* are_ConcurrentGC_threads */false);
  40   _workers->initialize_workers();
  41 }
  42 
  43 jint CMSHeap::initialize() {
  44   jint status = GenCollectedHeap::initialize();
  45   if (status != JNI_OK) return status;
  46 
  47   // If we are running CMS, create the collector responsible
  48   // for collecting the CMS generations.
  49   assert(collector_policy()->is_concurrent_mark_sweep_policy(), "must be CMS policy");
  50   bool success = create_cms_collector();
  51   if (!success) return JNI_ENOMEM;
  52 
  53   return JNI_OK;
  54 }
  55 
  56 #ifdef ASSERT
  57 void CMSHeap::check_gen_kinds() {
  58   assert(young_gen()->kind() == Generation::ParNew,
  59          "Wrong youngest generation type");
  60   assert(old_gen()->kind() == Generation::ConcurrentMarkSweep,
  61          "Wrong generation kind");
  62 }
  63 #endif
  64 
  65 
  66 CMSHeap* CMSHeap::heap() {
  67   CollectedHeap* heap = Universe::heap();
  68   assert(heap != NULL, "Uninitialized access to CMSHeap::heap()");
  69   assert(heap->kind() == CollectedHeap::CMSHeap, "Not a CMSHeap");
  70   return (CMSHeap*) heap;
  71 }
  72 
  73 void CMSHeap::gc_threads_do(ThreadClosure* tc) const {
  74   assert(workers() != NULL, "should have workers here");
  75   workers()->threads_do(tc);
  76   ConcurrentMarkSweepThread::threads_do(tc);
  77 }
  78 
  79 void CMSHeap::print_gc_threads_on(outputStream* st) const {
  80   assert(workers() != NULL, "should have workers here");
  81   workers()->print_worker_threads_on(st);
  82   ConcurrentMarkSweepThread::print_all_on(st);
  83 }
  84 
  85 void CMSHeap::print_on_error(outputStream* st) const {
  86   this->GenCollectedHeap::print_on_error(st);
  87   st->cr();
  88   CMSCollector::print_on_error(st);
  89 }
  90 
  91 bool CMSHeap::create_cms_collector() {
  92 
  93   assert(old_gen()->kind() == Generation::ConcurrentMarkSweep,
  94          "Unexpected generation kinds");
  95   // Skip two header words in the block content verification
  96   NOT_PRODUCT(_skip_header_HeapWords = CMSCollector::skip_header_HeapWords();)
  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   if (!UseConcMarkSweepGC) {
 124     return false;
 125   }
 126 
 127   switch (cause) {
 128     case GCCause::_gc_locker:           return GCLockerInvokesConcurrent;
 129     case GCCause::_java_lang_system_gc:
 130     case GCCause::_dcmd_gc_run:         return ExplicitGCInvokesConcurrent;
 131     default:                            return false;
 132   }
 133 }
 134 
 135 
 136 void CMSHeap::collect_mostly_concurrent(GCCause::Cause cause) {
 137   assert(!Heap_lock->owned_by_self(), "Should not own Heap_lock");
 138 
 139   MutexLocker ml(Heap_lock);
 140   // Read the GC counts while holding the Heap_lock
 141   unsigned int full_gc_count_before = total_full_collections();
 142   unsigned int gc_count_before      = total_collections();
 143   {
 144     MutexUnlocker mu(Heap_lock);
 145     VM_GenCollectFullConcurrent op(gc_count_before, full_gc_count_before, cause);
 146     VMThread::execute(&op);
 147   }
 148 }
 149 
 150 void CMSHeap::stop() {
 151   ConcurrentMarkSweepThread::cmst()->stop();
 152 }
 153 
 154 void CMSHeap::cms_process_roots(StrongRootsScope* scope,
 155                                 bool young_gen_as_roots,
 156                                 ScanningOption so,
 157                                 bool only_strong_roots,
 158                                 OopsInGenClosure* root_closure,
 159                                 CLDClosure* cld_closure) {
 160   MarkingCodeBlobClosure mark_code_closure(root_closure, !CodeBlobToOopClosure::FixRelocations);
 161   OopsInGenClosure* weak_roots = only_strong_roots ? NULL : root_closure;
 162   CLDClosure* weak_cld_closure = only_strong_roots ? NULL : cld_closure;
 163 
 164   process_roots(scope, so, root_closure, weak_roots, cld_closure, weak_cld_closure, &mark_code_closure);
 165   if (!only_strong_roots) {
 166     process_string_table_roots(scope, root_closure);
 167   }
 168 
 169   if (young_gen_as_roots &&
 170       !_process_strong_tasks->is_task_claimed(GCH_PS_younger_gens)) {
 171     root_closure->set_generation(young_gen());
 172     young_gen()->oop_iterate(root_closure);
 173     root_closure->reset_generation();
 174   }
 175 
 176   _process_strong_tasks->all_tasks_completed(scope->n_threads());
 177 }
 178