1 /*
   2  * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
   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/shared/gc.hpp"
  27 #include "gc/shared/gcFactory.hpp"
  28 #include "gc/serial/serialGC.hpp"
  29 #include "runtime/globals.hpp"
  30 #include "runtime/globals_extension.hpp"
  31 #include "runtime/java.hpp"
  32 #include "runtime/os.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 #if INCLUDE_ALL_GCS
  36 #include "gc/parallel/parallelGC.hpp"
  37 #include "gc/cms/cmsGC.hpp"
  38 #include "gc/g1/g1GC.hpp"
  39 #endif
  40 
  41 GC* GCFactory::_gc = NULL;
  42 
  43 GC* GCFactory::gc() {
  44   assert(_gc != NULL, "GC not yet created");
  45   return _gc;
  46 }
  47 
  48 bool GCFactory::is_initialized() {
  49   return _gc != NULL;
  50 }
  51 
  52 bool GCFactory::gc_selected() {
  53 #if INCLUDE_ALL_GCS
  54   return UseSerialGC || UseParallelGC || UseParallelOldGC || UseConcMarkSweepGC || UseG1GC;
  55 #else
  56   return UseSerialGC;
  57 #endif // INCLUDE_ALL_GCS
  58 }
  59 
  60 void GCFactory::select_gc() {
  61   if (!gc_selected()) {
  62     select_gc_ergonomically();
  63     if (!gc_selected()) {
  64       vm_exit_during_initialization("Garbage collector not selected (default collector explicitly disabled)", NULL);
  65     }
  66   }
  67 }
  68 
  69 void GCFactory::select_gc_ergonomically() {
  70 #if INCLUDE_ALL_GCS
  71   if (os::is_server_class_machine()) {
  72     FLAG_SET_ERGO_IF_DEFAULT(bool, UseG1GC, true);
  73   } else {
  74     FLAG_SET_ERGO_IF_DEFAULT(bool, UseSerialGC, true);
  75   }
  76 #else
  77   UNSUPPORTED_OPTION(UseG1GC);
  78   UNSUPPORTED_OPTION(UseParallelGC);
  79   UNSUPPORTED_OPTION(UseParallelOldGC);
  80   UNSUPPORTED_OPTION(UseConcMarkSweepGC);
  81   FLAG_SET_ERGO_IF_DEFAULT(bool, UseSerialGC, true);
  82 #endif // INCLUDE_ALL_GCS
  83 }
  84 
  85 void GCFactory::initialize_flags_global() {
  86 #if INCLUDE_ALL_GCS
  87   if (AssumeMP && !UseSerialGC) {
  88     if (FLAG_IS_DEFAULT(ParallelGCThreads) && ParallelGCThreads == 1) {
  89       warning("If the number of processors is expected to increase from one, then"
  90               " you should configure the number of parallel GC threads appropriately"
  91               " using -XX:ParallelGCThreads=N");
  92     }
  93   }
  94   if (MinHeapFreeRatio == 100) {
  95     // Keeping the heap 100% free is hard ;-) so limit it to 99%.
  96     FLAG_SET_ERGO(uintx, MinHeapFreeRatio, 99);
  97   }
  98 
  99   // If class unloading is disabled, also disable concurrent class unloading.
 100   if (!ClassUnloading) {
 101     FLAG_SET_CMDLINE(bool, CMSClassUnloadingEnabled, false);
 102     FLAG_SET_CMDLINE(bool, ClassUnloadingWithConcurrentMark, false);
 103   }
 104 #endif // INCLUDE_ALL_GCS
 105 }
 106 
 107 jint GCFactory::initialize() {
 108   assert(_gc == NULL, "GC already created");
 109 
 110   select_gc();
 111 
 112 #if !INCLUDE_ALL_GCS
 113   if (UseParallelGC || UseParallelOldGC) {
 114     fatal("UseParallelGC not supported in this VM.");
 115   } else if (UseG1GC) {
 116     fatal("UseG1GC not supported in this VM.");
 117   } else if (UseConcMarkSweepGC) {
 118     fatal("UseConcMarkSweepGC not supported in this VM.");
 119 #else
 120   if (UseParallelGC || UseParallelOldGC) {
 121     _gc = new ParallelGC();
 122   } else if (UseG1GC) {
 123     _gc = new G1GC();
 124   } else if (UseConcMarkSweepGC) {
 125     _gc = new CMSGC();
 126 #endif
 127   } else if (UseSerialGC) {
 128     _gc = new SerialGC();
 129   } else {
 130     ShouldNotReachHere();
 131   }
 132   return JNI_OK;
 133 }