1 /*
   2  * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
   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/gcArguments.hpp"
  27 #include "gc/serial/serialArguments.hpp"
  28 #include "runtime/globals.hpp"
  29 #include "runtime/globals_extension.hpp"
  30 #include "runtime/java.hpp"
  31 #include "runtime/os.hpp"
  32 #include "utilities/defaultStream.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 #if INCLUDE_ALL_GCS
  36 #include "gc/parallel/parallelArguments.hpp"
  37 #include "gc/cms/cmsArguments.hpp"
  38 #include "gc/g1/g1Arguments.hpp"
  39 #endif
  40 
  41 GCArguments* GCArguments::_instance = NULL;
  42 
  43 GCArguments* GCArguments::arguments() {
  44   assert(is_initialized(), "Heap factory not yet created");
  45   return _instance;
  46 }
  47 
  48 bool GCArguments::is_initialized() {
  49   return _instance != NULL;
  50 }
  51 
  52 bool GCArguments::gc_selected() {
  53 #if INCLUDE_ALL_GCS
  54   return UseSerialGC || UseParallelGC || UseParallelOldGC || UseConcMarkSweepGC || UseG1GC;
  55 #else
  56   return UseSerialGC;
  57 #endif // INCLUDE_ALL_GCS
  58 }
  59 
  60 void GCArguments::select_gc() {
  61   if (!gc_selected()) {
  62     select_gc_ergonomically();
  63     if (!gc_selected()) {
  64       vm_exit_during_initialization("Garbage collector not selected (default collector explicitly disabled)", NULL);
  65     }
  66   }
  67 }
  68 
  69 void GCArguments::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 void GCArguments::initialize_flags() {
  86 #if INCLUDE_ALL_GCS
  87   if (AssumeMP && !UseSerialGC) {
  88     if (FLAG_IS_DEFAULT(ParallelGCThreads) && ParallelGCThreads == 1) {
  89       warning("If the number of processors is expected to increase from one, then"
  90               " you should configure the number of parallel GC threads appropriately"
  91               " using -XX:ParallelGCThreads=N");
  92     }
  93   }
  94   if (MinHeapFreeRatio == 100) {
  95     // Keeping the heap 100% free is hard ;-) so limit it to 99%.
  96     FLAG_SET_ERGO(uintx, MinHeapFreeRatio, 99);
  97   }
  98 
  99   // If class unloading is disabled, also disable concurrent class unloading.
 100   if (!ClassUnloading) {
 101     FLAG_SET_CMDLINE(bool, CMSClassUnloadingEnabled, false);
 102     FLAG_SET_CMDLINE(bool, ClassUnloadingWithConcurrentMark, false);
 103   }
 104 #endif // INCLUDE_ALL_GCS
 105 }
 106 
 107 jint GCArguments::initialize() {
 108   assert(!is_initialized(), "GC arguments already initialized");
 109 
 110   select_gc();
 111 
 112 #if !INCLUDE_ALL_GCS
 113   if (UseParallelGC || UseParallelOldGC) {
 114     jio_fprintf(defaultStream::error_stream(), "UseParallelGC not supported in this VM.\n");
 115     return JNI_ERR;
 116   } else if (UseG1GC) {
 117     jio_fprintf(defaultStream::error_stream(), "UseG1GC not supported in this VM.\n");
 118     return JNI_ERR;
 119   } else if (UseConcMarkSweepGC) {
 120     jio_fprintf(defaultStream::error_stream(), "UseConcMarkSweepGC not supported in this VM.\n");
 121     return JNI_ERR;
 122 #else
 123   if (UseParallelGC || UseParallelOldGC) {
 124     _instance = new ParallelArguments();
 125   } else if (UseG1GC) {
 126     _instance = new G1Arguments();
 127   } else if (UseConcMarkSweepGC) {
 128     _instance = new CMSArguments();
 129 #endif
 130   } else if (UseSerialGC) {
 131     _instance = new SerialArguments();
 132   } else {
 133     ShouldNotReachHere();
 134   }
 135   return JNI_OK;
 136 }