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