1 /*
   2  * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "gc/shared/cardTableRS.hpp"
  28 #include "gc/shared/gcArguments.hpp"
  29 #include "logging/log.hpp"
  30 #include "runtime/arguments.hpp"
  31 #include "runtime/globals.hpp"

  32 #include "utilities/defaultStream.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 size_t HeapAlignment = 0;
  36 size_t SpaceAlignment = 0;
  37 
  38 void GCArguments::initialize() {
  39   if (FullGCALot && FLAG_IS_DEFAULT(MarkSweepAlwaysCompactCount)) {
  40     MarkSweepAlwaysCompactCount = 1;  // Move objects every gc.
  41   }
  42 
  43   if (!UseParallelGC && FLAG_IS_DEFAULT(ScavengeBeforeFullGC)) {
  44     FLAG_SET_DEFAULT(ScavengeBeforeFullGC, false);
  45   }
  46 
  47   if (GCTimeLimit == 100) {
  48     // Turn off gc-overhead-limit-exceeded checks
  49     FLAG_SET_DEFAULT(UseGCOverheadLimit, false);
  50   }
  51 
  52   if (MinHeapFreeRatio == 100) {
  53     // Keeping the heap 100% free is hard ;-) so limit it to 99%.
  54     FLAG_SET_ERGO(MinHeapFreeRatio, 99);
  55   }
  56 
  57   if (!ClassUnloading) {
  58     // If class unloading is disabled, also disable concurrent class unloading.
  59     FLAG_SET_CMDLINE(ClassUnloadingWithConcurrentMark, false);
  60   }
  61 
  62   if (!FLAG_IS_DEFAULT(AllocateOldGenAt)) {
  63     // CompressedOops not supported when AllocateOldGenAt is set.
  64     LP64_ONLY(FLAG_SET_DEFAULT(UseCompressedOops, false));
  65     LP64_ONLY(FLAG_SET_DEFAULT(UseCompressedClassPointers, false));
  66     // When AllocateOldGenAt is set, we cannot use largepages for entire heap memory.
  67     // Only young gen which is allocated in dram can use large pages, but we currently don't support that.
  68     FLAG_SET_DEFAULT(UseLargePages, false);
  69   }
  70 }
  71 
  72 void GCArguments::initialize_heap_sizes() {
  73   initialize_alignments();
  74   initialize_heap_flags_and_sizes();
  75   initialize_size_info();
  76 }
  77 
  78 size_t GCArguments::compute_heap_alignment() {
  79   // The card marking array and the offset arrays for old generations are
  80   // committed in os pages as well. Make sure they are entirely full (to
  81   // avoid partial page problems), e.g. if 512 bytes heap corresponds to 1
  82   // byte entry and the os page size is 4096, the maximum heap size should
  83   // be 512*4096 = 2MB aligned.
  84 
  85   size_t alignment = CardTableRS::ct_max_alignment_constraint();
  86 
  87   if (UseLargePages) {
  88       // In presence of large pages we have to make sure that our
  89       // alignment is large page aware.
  90       alignment = lcm(os::large_page_size(), alignment);
  91   }
  92 
  93   return alignment;
  94 }
  95 
  96 bool GCArguments::check_args_consistency() {
  97   bool status = true;
  98   if (!FLAG_IS_DEFAULT(AllocateHeapAt) && !FLAG_IS_DEFAULT(AllocateOldGenAt)) {
  99     jio_fprintf(defaultStream::error_stream(),
 100       "AllocateHeapAt and AllocateOldGenAt cannot be used together.\n");
 101     status = false;
 102   }
 103   if (!FLAG_IS_DEFAULT(AllocateOldGenAt) && (UseSerialGC || UseEpsilonGC || UseZGC)) {
 104     jio_fprintf(defaultStream::error_stream(),
 105       "AllocateOldGenAt is not supported for selected GC.\n");
 106     status = false;
 107   }
 108   return status;
 109 }
 110 
 111 #ifdef ASSERT
 112 void GCArguments::assert_flags() {
 113   assert(InitialHeapSize <= MaxHeapSize, "Ergonomics decided on incompatible initial and maximum heap sizes");
 114   assert(InitialHeapSize % HeapAlignment == 0, "InitialHeapSize alignment");
 115   assert(MaxHeapSize % HeapAlignment == 0, "MaxHeapSize alignment");
 116 }
 117 
 118 void GCArguments::assert_size_info() {
 119   assert(MaxHeapSize >= MinHeapSize, "Ergonomics decided on incompatible minimum and maximum heap sizes");
 120   assert(InitialHeapSize >= MinHeapSize, "Ergonomics decided on incompatible initial and minimum heap sizes");
 121   assert(MaxHeapSize >= InitialHeapSize, "Ergonomics decided on incompatible initial and maximum heap sizes");
 122   assert(MinHeapSize % HeapAlignment == 0, "MinHeapSize alignment");
 123   assert(InitialHeapSize % HeapAlignment == 0, "InitialHeapSize alignment");
 124   assert(MaxHeapSize % HeapAlignment == 0, "MaxHeapSize alignment");
 125 }
 126 #endif // ASSERT
 127 
 128 void GCArguments::initialize_size_info() {
 129   log_debug(gc, heap)("Minimum heap " SIZE_FORMAT "  Initial heap " SIZE_FORMAT "  Maximum heap " SIZE_FORMAT,
 130                       MinHeapSize, InitialHeapSize, MaxHeapSize);
 131 
 132   DEBUG_ONLY(assert_size_info();)
 133 }
 134 
 135 void GCArguments::initialize_heap_flags_and_sizes() {
 136   assert(SpaceAlignment != 0, "Space alignment not set up properly");
 137   assert(HeapAlignment != 0, "Heap alignment not set up properly");
 138   assert(HeapAlignment >= SpaceAlignment,
 139          "HeapAlignment: " SIZE_FORMAT " less than SpaceAlignment: " SIZE_FORMAT,
 140          HeapAlignment, SpaceAlignment);
 141   assert(HeapAlignment % SpaceAlignment == 0,
 142          "HeapAlignment: " SIZE_FORMAT " not aligned by SpaceAlignment: " SIZE_FORMAT,
 143          HeapAlignment, SpaceAlignment);
 144 
 145   if (FLAG_IS_CMDLINE(MaxHeapSize)) {
 146     if (FLAG_IS_CMDLINE(InitialHeapSize) && InitialHeapSize > MaxHeapSize) {
 147       vm_exit_during_initialization("Initial heap size set to a larger value than the maximum heap size");
 148     }
 149     if (FLAG_IS_CMDLINE(MinHeapSize) && MaxHeapSize < MinHeapSize) {
 150       vm_exit_during_initialization("Incompatible minimum and maximum heap sizes specified");
 151     }
 152   }
 153 
 154   // Check heap parameter properties
 155   if (MaxHeapSize < 2 * M) {
 156     vm_exit_during_initialization("Too small maximum heap");
 157   }
 158   if (InitialHeapSize < M) {
 159     vm_exit_during_initialization("Too small initial heap");
 160   }
 161   if (MinHeapSize < M) {
 162     vm_exit_during_initialization("Too small minimum heap");
 163   }
 164 
 165   // User inputs from -Xmx and -Xms must be aligned
 166   // Write back to flags if the values changed
 167   if (!is_aligned(MinHeapSize, HeapAlignment)) {
 168     FLAG_SET_ERGO(MinHeapSize, align_up(MinHeapSize, HeapAlignment));
 169   }
 170   if (!is_aligned(InitialHeapSize, HeapAlignment)) {
 171     FLAG_SET_ERGO(InitialHeapSize, align_up(InitialHeapSize, HeapAlignment));
 172   }
 173   if (!is_aligned(MaxHeapSize, HeapAlignment)) {
 174     FLAG_SET_ERGO(MaxHeapSize, align_up(MaxHeapSize, HeapAlignment));
 175   }
 176 
 177   if (FLAG_IS_CMDLINE(InitialHeapSize) && FLAG_IS_CMDLINE(MinHeapSize) &&
 178       InitialHeapSize < MinHeapSize) {
 179     vm_exit_during_initialization("Incompatible minimum and initial heap sizes specified");
 180   }
 181 
 182   if (!FLAG_IS_DEFAULT(InitialHeapSize) && InitialHeapSize > MaxHeapSize) {
 183     FLAG_SET_ERGO(MaxHeapSize, InitialHeapSize);
 184   } else if (!FLAG_IS_DEFAULT(MaxHeapSize) && InitialHeapSize > MaxHeapSize) {
 185     FLAG_SET_ERGO(InitialHeapSize, MaxHeapSize);
 186     if (InitialHeapSize < MinHeapSize) {
 187       FLAG_SET_ERGO(MinHeapSize, InitialHeapSize);
 188     }
 189   }
 190 
 191   if (FLAG_IS_DEFAULT(SoftMaxHeapSize)) {
 192     FLAG_SET_ERGO(SoftMaxHeapSize, MaxHeapSize);
 193   }
 194 
 195   FLAG_SET_ERGO(MinHeapDeltaBytes, align_up(MinHeapDeltaBytes, SpaceAlignment));
 196 
 197   DEBUG_ONLY(assert_flags();)
 198 }
--- EOF ---