1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2020 SAP SE. All rights reserved.
   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 
  28 #include "logging/log.hpp"
  29 #include "logging/logStream.hpp"
  30 
  31 #include "memory/metaspace/msSettings.hpp"
  32 
  33 #include "runtime/java.hpp"
  34 #include "utilities/globalDefinitions.hpp"
  35 #include "utilities/debug.hpp"
  36 
  37 namespace metaspace {
  38 
  39 size_t Settings::_commit_granule_bytes = 0;
  40 size_t Settings::_commit_granule_words = 0;
  41 
  42 bool Settings::_new_chunks_are_fully_committed = false;
  43 bool Settings::_uncommit_free_chunks = false;
  44 
  45 DEBUG_ONLY(bool Settings::_use_allocation_guard = false;)
  46 DEBUG_ONLY(bool Settings::_handle_deallocations = true;)
  47 
  48 void Settings::ergo_initialize() {
  49 
  50   if (strcmp(MetaspaceReclaimPolicy, "none") == 0) {
  51 
  52     log_info(metaspace)("Initialized with strategy: no reclaim.");
  53 
  54     _commit_granule_bytes = MAX2((size_t)os::vm_page_size(), 64 * K);
  55     _commit_granule_words = _commit_granule_bytes / BytesPerWord;
  56 
  57     // In "none" reclamation mode, we do not uncommit, and we commit new chunks fully;
  58     // that very closely mimicks the behaviour of old Metaspace.
  59     _new_chunks_are_fully_committed = true;
  60     _uncommit_free_chunks = false;
  61 
  62   } else if (strcmp(MetaspaceReclaimPolicy, "aggressive") == 0) {
  63 
  64     log_info(metaspace)("Initialized with strategy: aggressive reclaim.");
  65 
  66     // Set the granule size rather small; may increase
  67     // mapping fragmentation but also increase chance to uncommit.
  68     _commit_granule_bytes = MAX2((size_t)os::vm_page_size(), 16 * K);
  69     _commit_granule_words = _commit_granule_bytes / BytesPerWord;
  70 
  71     _new_chunks_are_fully_committed = false;
  72     _uncommit_free_chunks = true;
  73 
  74   } else if (strcmp(MetaspaceReclaimPolicy, "balanced") == 0) {
  75 
  76     log_info(metaspace)("Initialized with strategy: balanced reclaim.");
  77 
  78     _commit_granule_bytes = MAX2((size_t)os::vm_page_size(), 64 * K);
  79     _commit_granule_words = _commit_granule_bytes / BytesPerWord;
  80 
  81     _new_chunks_are_fully_committed = false;
  82     _uncommit_free_chunks = true;
  83 
  84   } else {
  85 
  86     vm_exit_during_initialization("Invalid value for MetaspaceReclaimPolicy: \"%s\".", MetaspaceReclaimPolicy);
  87 
  88   }
  89 
  90   // Sanity checks.
  91   assert(commit_granule_words() <= chunklevel::MAX_CHUNK_WORD_SIZE, "Too large granule size");
  92   assert(is_power_of_2(commit_granule_words()), "granule size must be a power of 2");
  93 
  94   // Should always be true since root chunk size should be much larger than alloc granularity
  95   assert(is_aligned(VirtualSpaceNodeReserveAlignmentWordSize * BytesPerWord,
  96                     os::vm_allocation_granularity()), "Sanity");
  97 
  98 #ifdef ASSERT
  99   // Off for release builds, and by default for debug builds, but can be switched on manually to aid
 100   // error analysis.
 101   _use_allocation_guard = MetaspaceGuardAllocations;
 102 
 103   // Deallocations can be manually switched off to aid error analysis, since this removes one layer of complexity
 104   //  from allocation.
 105   _handle_deallocations = MetaspaceHandleDeallocations;
 106 
 107   // We also switch it off automatically if we use allocation guards. This is to keep prefix handling in MetaspaceArena simple.
 108   if (_use_allocation_guard) {
 109     _handle_deallocations = false;
 110   }
 111 #endif
 112 
 113   LogStream ls(Log(metaspace)::info());
 114   Settings::print_on(&ls);
 115 
 116 }
 117 
 118 void Settings::print_on(outputStream* st) {
 119 
 120   st->print_cr(" - commit_granule_bytes: " SIZE_FORMAT ".", commit_granule_bytes());
 121   st->print_cr(" - commit_granule_words: " SIZE_FORMAT ".", commit_granule_words());
 122 
 123   st->print_cr(" - virtual_space_node_default_size: " SIZE_FORMAT ".", virtual_space_node_default_word_size());
 124 
 125   st->print_cr(" - enlarge_chunks_in_place: %d.", (int)enlarge_chunks_in_place());
 126 
 127   st->print_cr(" - new_chunks_are_fully_committed: %d.", (int)new_chunks_are_fully_committed());
 128   st->print_cr(" - uncommit_free_chunks: %d.", (int)uncommit_free_chunks());
 129 
 130   st->print_cr(" - use_allocation_guard: %d.", (int)use_allocation_guard());
 131   st->print_cr(" - handle_deallocations: %d.", (int)handle_deallocations());
 132 
 133 }
 134 
 135 } // namespace metaspace
 136