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 #define FOR_EACH_SUPPORTED_GC(var) \ 67 for (const SupportedGC* var = &SupportedGCs[0]; var < &SupportedGCs[ARRAY_SIZE(SupportedGCs)]; var++) 68 69 GCArguments* GCConfig::_arguments = NULL; 70 bool GCConfig::_gc_selected_ergonomically = false; 71 72 void GCConfig::select_gc_ergonomically() { 73 #if INCLUDE_ALL_GCS 74 if (os::is_server_class_machine()) { 75 FLAG_SET_ERGO_IF_DEFAULT(bool, UseG1GC, true); 76 } else { 77 FLAG_SET_ERGO_IF_DEFAULT(bool, UseSerialGC, true); 78 } 79 #else 80 UNSUPPORTED_OPTION(UseG1GC); 81 UNSUPPORTED_OPTION(UseParallelGC); 82 UNSUPPORTED_OPTION(UseParallelOldGC); 83 UNSUPPORTED_OPTION(UseConcMarkSweepGC); 84 FLAG_SET_ERGO_IF_DEFAULT(bool, UseSerialGC, true); 85 #endif // INCLUDE_ALL_GCS 86 } 87 88 bool GCConfig::is_no_gc_selected() { 89 FOR_EACH_SUPPORTED_GC(gc) { 90 if (gc->_flag) { 91 return false; 92 } 93 } 94 95 return true; 96 } 97 98 bool GCConfig::is_exactly_one_gc_selected() { 99 CollectedHeap::Name selected = CollectedHeap::None; 100 101 FOR_EACH_SUPPORTED_GC(gc) { 102 if (gc->_flag) { 103 if (gc->_name == selected || selected == CollectedHeap::None) { 104 // Selected 105 selected = gc->_name; 106 } else { 107 // More than one selected 108 return false; 109 } 110 } 111 } 112 113 return selected != CollectedHeap::None; 114 } 115 116 GCArguments* GCConfig::select_gc() { 117 if (is_no_gc_selected()) { 118 // Try select GC ergonomically 119 select_gc_ergonomically(); 120 121 if (is_no_gc_selected()) { 122 // Failed to select GC ergonomically 123 vm_exit_during_initialization("Garbage collector not selected " 124 "(default collector explicitly disabled)", NULL); 125 } 126 127 // Succeeded to select GC ergonomically 128 _gc_selected_ergonomically = true; 129 } 130 131 if (is_exactly_one_gc_selected()) { 132 // Exacly one GC selected 133 FOR_EACH_SUPPORTED_GC(gc) { 134 if (gc->_flag) { 135 return &gc->_arguments; 136 } 137 } 138 } 139 140 // More than one GC selected 141 vm_exit_during_initialization("Multiple garbage collectors selected", NULL); 142 143 return NULL; 144 } 145 146 void GCConfig::initialize() { 147 assert(_arguments == NULL, "Already initialized"); 148 _arguments = select_gc(); 149 } 150 151 bool GCConfig::is_gc_supported(CollectedHeap::Name name) { 152 FOR_EACH_SUPPORTED_GC(gc) { 153 if (gc->_name == name) { 154 // Supported 155 return true; 156 } 157 } 158 159 // Not supported 160 return false; 161 } 162 163 bool GCConfig::is_gc_selected(CollectedHeap::Name name) { 164 FOR_EACH_SUPPORTED_GC(gc) { 165 if (gc->_name == name && gc->_flag) { 166 // Selected 167 return true; 168 } 169 } 170 171 // Not selected 172 return false; 173 } 174 175 bool GCConfig::is_gc_selected_ergonomically() { 176 return _gc_selected_ergonomically; 177 } 178 179 const char* GCConfig::hs_err_name() { 180 if (is_exactly_one_gc_selected()) { 181 // Exacly one GC selected 182 FOR_EACH_SUPPORTED_GC(gc) { 183 if (gc->_flag) { 184 return gc->_hs_err_name; 185 } 186 } 187 } 188 189 // Zero or more than one GC selected 190 return "<unknown gc>"; 191 } 192 193 GCArguments* GCConfig::arguments() { 194 assert(_arguments != NULL, "Not initialized"); 195 return _arguments; 196 }