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