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