1 /*
   2  * Copyright (c) 2018, 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/shared/gcConfig.hpp"
  27 #include "runtime/globals_extension.hpp"
  28 #include "runtime/java.hpp"
  29 #include "runtime/os.hpp"
  30 #include "utilities/macros.hpp"
  31 #if INCLUDE_CMSGC
  32 #include "gc/cms/cmsArguments.hpp"
  33 #endif
  34 #if INCLUDE_EPSILONGC
  35 #include "gc/epsilon/epsilonArguments.hpp"
  36 #endif
  37 #if INCLUDE_G1GC
  38 #include "gc/g1/g1Arguments.hpp"
  39 #endif
  40 #if INCLUDE_PARALLELGC
  41 #include "gc/parallel/parallelArguments.hpp"
  42 #endif
  43 #if INCLUDE_SERIALGC
  44 #include "gc/serial/serialArguments.hpp"
  45 #endif
  46 
  47 struct SupportedGC {
  48   bool&               _flag;
  49   CollectedHeap::Name _name;
  50   GCArguments&        _arguments;
  51   const char*         _hs_err_name;
  52 
  53   SupportedGC(bool& flag, CollectedHeap::Name name, GCArguments& arguments, const char* hs_err_name) :
  54       _flag(flag), _name(name), _arguments(arguments), _hs_err_name(hs_err_name) {}
  55 };
  56 
  57      CMSGC_ONLY(static CMSArguments      cmsArguments;)
  58  EPSILONGC_ONLY(static EpsilonArguments  epsilonArguments;)
  59       G1GC_ONLY(static G1Arguments       g1Arguments;)
  60 PARALLELGC_ONLY(static ParallelArguments parallelArguments;)
  61   SERIALGC_ONLY(static SerialArguments   serialArguments;)
  62 
  63 // Table of supported GCs, for translating between command
  64 // line flag, CollectedHeap::Name and GCArguments instance.
  65 static const SupportedGC SupportedGCs[] = {
  66        CMSGC_ONLY_ARG(SupportedGC(UseConcMarkSweepGC, CollectedHeap::CMS,      cmsArguments,      "concurrent mark sweep gc"))
  67    EPSILONGC_ONLY_ARG(SupportedGC(UseEpsilonGC,       CollectedHeap::Epsilon,  epsilonArguments,  "epsilon gc"))
  68         G1GC_ONLY_ARG(SupportedGC(UseG1GC,            CollectedHeap::G1,       g1Arguments,       "g1 gc"))
  69   PARALLELGC_ONLY_ARG(SupportedGC(UseParallelGC,      CollectedHeap::Parallel, parallelArguments, "parallel gc"))
  70   PARALLELGC_ONLY_ARG(SupportedGC(UseParallelOldGC,   CollectedHeap::Parallel, parallelArguments, "parallel gc"))
  71     SERIALGC_ONLY_ARG(SupportedGC(UseSerialGC,        CollectedHeap::Serial,   serialArguments,   "serial gc"))
  72 };
  73 
  74 #define FOR_EACH_SUPPORTED_GC(var) \
  75   for (const SupportedGC* var = &SupportedGCs[0]; var < &SupportedGCs[ARRAY_SIZE(SupportedGCs)]; var++)
  76 
  77 GCArguments* GCConfig::_arguments = NULL;
  78 bool GCConfig::_gc_selected_ergonomically = false;
  79 
  80 void GCConfig::select_gc_ergonomically() {
  81   if (os::is_server_class_machine()) {
  82 #if INCLUDE_G1GC
  83     FLAG_SET_ERGO_IF_DEFAULT(bool, UseG1GC, true);
  84 #elif INCLUDE_PARALLELGC
  85     FLAG_SET_ERGO_IF_DEFAULT(bool, UseParallelGC, true);
  86 #elif INCLUDE_SERIALGC
  87     FLAG_SET_ERGO_IF_DEFAULT(bool, UseSerialGC, true);
  88 #endif
  89   } else {
  90 #if INCLUDE_SERIALGC
  91     FLAG_SET_ERGO_IF_DEFAULT(bool, UseSerialGC, true);
  92 #endif
  93   }
  94 
  95   NOT_CMSGC(     UNSUPPORTED_OPTION(UseConcMarkSweepGC));
  96   NOT_EPSILONGC( UNSUPPORTED_OPTION(UseEpsilonGC);)
  97   NOT_G1GC(      UNSUPPORTED_OPTION(UseG1GC);)
  98   NOT_PARALLELGC(UNSUPPORTED_OPTION(UseParallelGC);)
  99   NOT_PARALLELGC(UNSUPPORTED_OPTION(UseParallelOldGC));
 100   NOT_SERIALGC(  UNSUPPORTED_OPTION(UseSerialGC);)
 101 }
 102 
 103 bool GCConfig::is_no_gc_selected() {
 104   FOR_EACH_SUPPORTED_GC(gc) {
 105     if (gc->_flag) {
 106       return false;
 107     }
 108   }
 109 
 110   return true;
 111 }
 112 
 113 bool GCConfig::is_exactly_one_gc_selected() {
 114   CollectedHeap::Name selected = CollectedHeap::None;
 115 
 116   FOR_EACH_SUPPORTED_GC(gc) {
 117     if (gc->_flag) {
 118       if (gc->_name == selected || selected == CollectedHeap::None) {
 119         // Selected
 120         selected = gc->_name;
 121       } else {
 122         // More than one selected
 123         return false;
 124       }
 125     }
 126   }
 127 
 128   return selected != CollectedHeap::None;
 129 }
 130 
 131 GCArguments* GCConfig::select_gc() {
 132   if (is_no_gc_selected()) {
 133     // Try select GC ergonomically
 134     select_gc_ergonomically();
 135 
 136     if (is_no_gc_selected()) {
 137       // Failed to select GC ergonomically
 138       vm_exit_during_initialization("Garbage collector not selected "
 139                                     "(default collector explicitly disabled)", NULL);
 140     }
 141 
 142     // Succeeded to select GC ergonomically
 143     _gc_selected_ergonomically = true;
 144   }
 145 
 146   if (!is_exactly_one_gc_selected()) {
 147     // More than one GC selected
 148     vm_exit_during_initialization("Multiple garbage collectors selected", NULL);
 149   }
 150 
 151 #if INCLUDE_PARALLELGC && !INCLUDE_SERIALGC
 152   if (FLAG_IS_CMDLINE(UseParallelOldGC) && !UseParallelOldGC) {
 153     vm_exit_during_initialization("This JVM build only supports UseParallelOldGC as the full GC");
 154   }
 155 #endif
 156 
 157   // Exactly one GC selected
 158   FOR_EACH_SUPPORTED_GC(gc) {
 159     if (gc->_flag) {
 160       return &gc->_arguments;
 161     }
 162   }
 163 
 164   fatal("Should have found the selected GC");
 165 
 166   return NULL;
 167 }
 168 
 169 void GCConfig::initialize() {
 170   assert(_arguments == NULL, "Already initialized");
 171   _arguments = select_gc();
 172 }
 173 
 174 bool GCConfig::is_gc_supported(CollectedHeap::Name name) {
 175   FOR_EACH_SUPPORTED_GC(gc) {
 176     if (gc->_name == name) {
 177       // Supported
 178       return true;
 179     }
 180   }
 181 
 182   // Not supported
 183   return false;
 184 }
 185 
 186 bool GCConfig::is_gc_selected(CollectedHeap::Name name) {
 187   FOR_EACH_SUPPORTED_GC(gc) {
 188     if (gc->_name == name && gc->_flag) {
 189       // Selected
 190       return true;
 191     }
 192   }
 193 
 194   // Not selected
 195   return false;
 196 }
 197 
 198 bool GCConfig::is_gc_selected_ergonomically() {
 199   return _gc_selected_ergonomically;
 200 }
 201 
 202 const char* GCConfig::hs_err_name() {
 203   if (is_exactly_one_gc_selected()) {
 204     // Exacly one GC selected
 205     FOR_EACH_SUPPORTED_GC(gc) {
 206       if (gc->_flag) {
 207         return gc->_hs_err_name;
 208       }
 209     }
 210   }
 211 
 212   // Zero or more than one GC selected
 213   return "unknown gc";
 214 }
 215 
 216 GCArguments* GCConfig::arguments() {
 217   assert(_arguments != NULL, "Not initialized");
 218   return _arguments;
 219 }