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/gc.hpp"
  27 #include "gc/serial/serialGC.hpp"
  28 #include "runtime/globals.hpp"
  29 #include "runtime/globals_extension.hpp"
  30 #include "runtime/os.hpp"
  31 #include "utilities/macros.hpp"
  32 
  33 #if INCLUDE_ALL_GCS
  34 #include "gc/parallel/parallelGC.hpp"
  35 #include "gc/cms/cmsGC.hpp"
  36 #include "gc/g1/g1GC.hpp"
  37 #endif
  38 
  39 GC* GC::_gc = NULL;
  40 
  41 bool GC::gc_selected() {
  42 #if INCLUDE_ALL_GCS
  43   return UseSerialGC || UseParallelGC || UseParallelOldGC || UseConcMarkSweepGC || UseG1GC;
  44 #else
  45   return UseSerialGC;
  46 #endif // INCLUDE_ALL_GCS
  47 }
  48 
  49 void GC::select_gc_ergonomically() {
  50 #if INCLUDE_ALL_GCS
  51   if (os::is_server_class_machine()) {
  52     FLAG_SET_ERGO_IF_DEFAULT(bool, UseG1GC, true);
  53   } else {
  54     FLAG_SET_ERGO_IF_DEFAULT(bool, UseSerialGC, true);
  55   }
  56 #else
  57   UNSUPPORTED_OPTION(UseG1GC);
  58   UNSUPPORTED_OPTION(UseParallelGC);
  59   UNSUPPORTED_OPTION(UseParallelOldGC);
  60   UNSUPPORTED_OPTION(UseConcMarkSweepGC);
  61   FLAG_SET_ERGO_IF_DEFAULT(bool, UseSerialGC, true);
  62 #endif // INCLUDE_ALL_GCS
  63 }
  64 
  65 void GC::initialize_flags_global() {
  66 #if INCLUDE_ALL_GCS
  67   if (AssumeMP && !UseSerialGC) {
  68     if (FLAG_IS_DEFAULT(ParallelGCThreads) && ParallelGCThreads == 1) {
  69       warning("If the number of processors is expected to increase from one, then"
  70               " you should configure the number of parallel GC threads appropriately"
  71               " using -XX:ParallelGCThreads=N");
  72     }
  73   }
  74   if (MinHeapFreeRatio == 100) {
  75     // Keeping the heap 100% free is hard ;-) so limit it to 99%.
  76     FLAG_SET_ERGO(uintx, MinHeapFreeRatio, 99);
  77   }
  78 
  79   // If class unloading is disabled, also disable concurrent class unloading.
  80   if (!ClassUnloading) {
  81     FLAG_SET_CMDLINE(bool, CMSClassUnloadingEnabled, false);
  82     FLAG_SET_CMDLINE(bool, ClassUnloadingWithConcurrentMark, false);
  83   }
  84 #endif // INCLUDE_ALL_GCS
  85 }
  86 
  87 jint GC::initialize() {
  88   assert(_gc == NULL, "GC already created");
  89 
  90   select_gc();
  91 
  92 #if !INCLUDE_ALL_GCS
  93   if (UseParallelGC || UseParallelOldGC) {
  94     fatal("UseParallelGC not supported in this VM.");
  95   } else if (UseG1GC) {
  96     fatal("UseG1GC not supported in this VM.");
  97   } else if (UseConcMarkSweepGC) {
  98     fatal("UseConcMarkSweepGC not supported in this VM.");
  99 #else
 100   if (UseParallelGC || UseParallelOldGC) {
 101     _gc = new ParallelGC();
 102   } else if (UseG1GC) {
 103     _gc = new G1GC();
 104   } else if (UseConcMarkSweepGC) {
 105     _gc = new CMSGC();
 106 #endif
 107   } else if (UseSerialGC) {
 108     _gc = new SerialGC();
 109   } else {
 110     ShouldNotReachHere();
 111   }
 112   return JNI_OK;
 113 }