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