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