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