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