1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   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 
  26 #include "precompiled.hpp"
  27 #include "logging/log.hpp"
  28 #include "logging/logStream.hpp"
  29 #include "memory/metaspace.hpp"
  30 #include "memory/metaspace/chunkManager.hpp"
  31 #include "memory/metaspace/metachunk.hpp"
  32 #include "memory/metaspace/metaspaceCommon.hpp"
  33 #include "memory/metaspace/virtualSpaceList.hpp"
  34 #include "memory/metaspace/virtualSpaceNode.hpp"
  35 #include "runtime/orderAccess.hpp"
  36 #include "runtime/mutexLocker.hpp"
  37 #include "runtime/safepoint.hpp"
  38 
  39 namespace metaspace {
  40 
  41 
  42 VirtualSpaceList::~VirtualSpaceList() {
  43   VirtualSpaceListIterator iter(virtual_space_list());
  44   while (iter.repeat()) {
  45     VirtualSpaceNode* vsl = iter.get_next();
  46     delete vsl;
  47   }
  48 }
  49 
  50 void VirtualSpaceList::inc_reserved_words(size_t v) {
  51   assert_lock_strong(MetaspaceExpand_lock);
  52   _reserved_words = _reserved_words + v;
  53 }
  54 void VirtualSpaceList::dec_reserved_words(size_t v) {
  55   assert_lock_strong(MetaspaceExpand_lock);
  56   _reserved_words = _reserved_words - v;
  57 }
  58 
  59 #define assert_committed_below_limit()                        \
  60   assert(MetaspaceUtils::committed_bytes() <= MaxMetaspaceSize, \
  61          "Too much committed memory. Committed: " SIZE_FORMAT \
  62          " limit (MaxMetaspaceSize): " SIZE_FORMAT,           \
  63           MetaspaceUtils::committed_bytes(), MaxMetaspaceSize);
  64 
  65 void VirtualSpaceList::inc_committed_words(size_t v) {
  66   assert_lock_strong(MetaspaceExpand_lock);
  67   _committed_words = _committed_words + v;
  68 
  69   assert_committed_below_limit();
  70 }
  71 void VirtualSpaceList::dec_committed_words(size_t v) {
  72   assert_lock_strong(MetaspaceExpand_lock);
  73   _committed_words = _committed_words - v;
  74 
  75   assert_committed_below_limit();
  76 }
  77 
  78 void VirtualSpaceList::inc_virtual_space_count() {
  79   assert_lock_strong(MetaspaceExpand_lock);
  80   _virtual_space_count++;
  81 }
  82 
  83 void VirtualSpaceList::dec_virtual_space_count() {
  84   assert_lock_strong(MetaspaceExpand_lock);
  85   _virtual_space_count--;
  86 }
  87 
  88 // Walk the list of VirtualSpaceNodes and delete
  89 // nodes with a 0 container_count.  Remove Metachunks in
  90 // the node from their respective freelists.
  91 void VirtualSpaceList::purge(ChunkManager* chunk_manager) {
  92   assert_lock_strong(MetaspaceExpand_lock);
  93   // Don't use a VirtualSpaceListIterator because this
  94   // list is being changed and a straightforward use of an iterator is not safe.
  95   VirtualSpaceNode* purged_vsl = NULL;
  96   VirtualSpaceNode* prev_vsl = virtual_space_list();
  97   VirtualSpaceNode* next_vsl = prev_vsl;
  98   while (next_vsl != NULL) {
  99     VirtualSpaceNode* vsl = next_vsl;
 100     DEBUG_ONLY(vsl->verify(false);)
 101     next_vsl = vsl->next();
 102     // Don't free the current virtual space since it will likely
 103     // be needed soon.
 104     if (vsl->container_count() == 0 && vsl != current_virtual_space()) {
 105       log_trace(gc, metaspace, freelist)("Purging VirtualSpaceNode " PTR_FORMAT " (capacity: " SIZE_FORMAT
 106                                          ", used: " SIZE_FORMAT ").", p2i(vsl), vsl->capacity_words_in_vs(), vsl->used_words_in_vs());
 107       DEBUG_ONLY(Atomic::inc(&g_internal_statistics.num_vsnodes_purged));
 108       // Unlink it from the list
 109       if (prev_vsl == vsl) {
 110         // This is the case of the current node being the first node.
 111         assert(vsl == virtual_space_list(), "Expected to be the first node");
 112         set_virtual_space_list(vsl->next());
 113       } else {
 114         prev_vsl->set_next(vsl->next());
 115       }
 116 
 117       vsl->purge(chunk_manager);
 118       dec_reserved_words(vsl->reserved_words());
 119       dec_committed_words(vsl->committed_words());
 120       dec_virtual_space_count();
 121       purged_vsl = vsl;
 122       delete vsl;
 123     } else {
 124       prev_vsl = vsl;
 125     }
 126   }
 127 #ifdef ASSERT
 128   if (purged_vsl != NULL) {
 129     // List should be stable enough to use an iterator here.
 130     VirtualSpaceListIterator iter(virtual_space_list());
 131     while (iter.repeat()) {
 132       VirtualSpaceNode* vsl = iter.get_next();
 133       assert(vsl != purged_vsl, "Purge of vsl failed");
 134     }
 135   }
 136 #endif
 137 }
 138 
 139 
 140 // This function looks at the mmap regions in the metaspace without locking.
 141 // The chunks are added with store ordering and not deleted except for at
 142 // unloading time during a safepoint.
 143 VirtualSpaceNode* VirtualSpaceList::find_enclosing_space(const void* ptr) {
 144   // List should be stable enough to use an iterator here because removing virtual
 145   // space nodes is only allowed at a safepoint.
 146   VirtualSpaceListIterator iter(virtual_space_list());
 147   while (iter.repeat()) {
 148     VirtualSpaceNode* vsn = iter.get_next();
 149     if (vsn->contains(ptr)) {
 150       return vsn;
 151     }
 152   }
 153   return NULL;
 154 }
 155 
 156 void VirtualSpaceList::retire_current_virtual_space() {
 157   assert_lock_strong(MetaspaceExpand_lock);
 158 
 159   VirtualSpaceNode* vsn = current_virtual_space();
 160 
 161   ChunkManager* cm = is_class() ? Metaspace::chunk_manager_class() :
 162                                   Metaspace::chunk_manager_metadata();
 163 
 164   vsn->retire(cm);
 165 }
 166 
 167 VirtualSpaceList::VirtualSpaceList(size_t word_size) :
 168                                    _virtual_space_list(NULL),
 169                                    _current_virtual_space(NULL),
 170                                    _is_class(false),
 171                                    _reserved_words(0),
 172                                    _committed_words(0),
 173                                    _virtual_space_count(0) {
 174   MutexLockerEx cl(MetaspaceExpand_lock,
 175                    Mutex::_no_safepoint_check_flag);
 176   create_new_virtual_space(word_size);
 177 }
 178 
 179 VirtualSpaceList::VirtualSpaceList(ReservedSpace rs) :
 180                                    _virtual_space_list(NULL),
 181                                    _current_virtual_space(NULL),
 182                                    _is_class(true),
 183                                    _reserved_words(0),
 184                                    _committed_words(0),
 185                                    _virtual_space_count(0) {
 186   MutexLockerEx cl(MetaspaceExpand_lock,
 187                    Mutex::_no_safepoint_check_flag);
 188   VirtualSpaceNode* class_entry = new VirtualSpaceNode(is_class(), rs);
 189   bool succeeded = class_entry->initialize();
 190   if (succeeded) {
 191     link_vs(class_entry);
 192   }
 193 }
 194 
 195 size_t VirtualSpaceList::free_bytes() {
 196   return current_virtual_space()->free_words_in_vs() * BytesPerWord;
 197 }
 198 
 199 // Allocate another meta virtual space and add it to the list.
 200 bool VirtualSpaceList::create_new_virtual_space(size_t vs_word_size) {
 201   assert_lock_strong(MetaspaceExpand_lock);
 202 
 203   if (is_class()) {
 204     assert(false, "We currently don't support more than one VirtualSpace for"
 205                   " the compressed class space. The initialization of the"
 206                   " CCS uses another code path and should not hit this path.");
 207     return false;
 208   }
 209 
 210   if (vs_word_size == 0) {
 211     assert(false, "vs_word_size should always be at least _reserve_alignment large.");
 212     return false;
 213   }
 214 
 215   // Reserve the space
 216   size_t vs_byte_size = vs_word_size * BytesPerWord;
 217   assert_is_aligned(vs_byte_size, Metaspace::reserve_alignment());
 218 
 219   // Allocate the meta virtual space and initialize it.
 220   VirtualSpaceNode* new_entry = new VirtualSpaceNode(is_class(), vs_byte_size);
 221   if (!new_entry->initialize()) {
 222     delete new_entry;
 223     return false;
 224   } else {
 225     assert(new_entry->reserved_words() == vs_word_size,
 226         "Reserved memory size differs from requested memory size");
 227     // ensure lock-free iteration sees fully initialized node
 228     OrderAccess::storestore();
 229     link_vs(new_entry);
 230     DEBUG_ONLY(Atomic::inc(&g_internal_statistics.num_vsnodes_created));
 231     return true;
 232   }
 233 }
 234 
 235 void VirtualSpaceList::link_vs(VirtualSpaceNode* new_entry) {
 236   if (virtual_space_list() == NULL) {
 237       set_virtual_space_list(new_entry);
 238   } else {
 239     current_virtual_space()->set_next(new_entry);
 240   }
 241   set_current_virtual_space(new_entry);
 242   inc_reserved_words(new_entry->reserved_words());
 243   inc_committed_words(new_entry->committed_words());
 244   inc_virtual_space_count();
 245 #ifdef ASSERT
 246   new_entry->mangle();
 247 #endif
 248   LogTarget(Trace, gc, metaspace) lt;
 249   if (lt.is_enabled()) {
 250     LogStream ls(lt);
 251     VirtualSpaceNode* vsl = current_virtual_space();
 252     ResourceMark rm;
 253     vsl->print_on(&ls);
 254   }
 255 }
 256 
 257 bool VirtualSpaceList::expand_node_by(VirtualSpaceNode* node,
 258                                       size_t min_words,
 259                                       size_t preferred_words) {
 260   size_t before = node->committed_words();
 261 
 262   bool result = node->expand_by(min_words, preferred_words);
 263 
 264   size_t after = node->committed_words();
 265 
 266   // after and before can be the same if the memory was pre-committed.
 267   assert(after >= before, "Inconsistency");
 268   inc_committed_words(after - before);
 269 
 270   return result;
 271 }
 272 
 273 bool VirtualSpaceList::expand_by(size_t min_words, size_t preferred_words) {
 274   assert_is_aligned(min_words,       Metaspace::commit_alignment_words());
 275   assert_is_aligned(preferred_words, Metaspace::commit_alignment_words());
 276   assert(min_words <= preferred_words, "Invalid arguments");
 277 
 278   const char* const class_or_not = (is_class() ? "class" : "non-class");
 279 
 280   if (!MetaspaceGC::can_expand(min_words, this->is_class())) {
 281     log_trace(gc, metaspace, freelist)("Cannot expand %s virtual space list.",
 282               class_or_not);
 283     return  false;
 284   }
 285 
 286   size_t allowed_expansion_words = MetaspaceGC::allowed_expansion();
 287   if (allowed_expansion_words < min_words) {
 288     log_trace(gc, metaspace, freelist)("Cannot expand %s virtual space list (must try gc first).",
 289               class_or_not);
 290     return false;
 291   }
 292 
 293   size_t max_expansion_words = MIN2(preferred_words, allowed_expansion_words);
 294 
 295   // Commit more memory from the the current virtual space.
 296   bool vs_expanded = expand_node_by(current_virtual_space(),
 297                                     min_words,
 298                                     max_expansion_words);
 299   if (vs_expanded) {
 300      log_trace(gc, metaspace, freelist)("Expanded %s virtual space list.",
 301                class_or_not);
 302      return true;
 303   }
 304   log_trace(gc, metaspace, freelist)("%s virtual space list: retire current node.",
 305             class_or_not);
 306   retire_current_virtual_space();
 307 
 308   // Get another virtual space.
 309   size_t grow_vs_words = MAX2((size_t)VirtualSpaceSize, preferred_words);
 310   grow_vs_words = align_up(grow_vs_words, Metaspace::reserve_alignment_words());
 311 
 312   if (create_new_virtual_space(grow_vs_words)) {
 313     if (current_virtual_space()->is_pre_committed()) {
 314       // The memory was pre-committed, so we are done here.
 315       assert(min_words <= current_virtual_space()->committed_words(),
 316           "The new VirtualSpace was pre-committed, so it"
 317           "should be large enough to fit the alloc request.");
 318       return true;
 319     }
 320 
 321     return expand_node_by(current_virtual_space(),
 322                           min_words,
 323                           max_expansion_words);
 324   }
 325 
 326   return false;
 327 }
 328 
 329 // Given a chunk, calculate the largest possible padding space which
 330 // could be required when allocating it.
 331 static size_t largest_possible_padding_size_for_chunk(size_t chunk_word_size, bool is_class) {
 332   const ChunkIndex chunk_type = get_chunk_type_by_size(chunk_word_size, is_class);
 333   if (chunk_type != HumongousIndex) {
 334     // Normal, non-humongous chunks are allocated at chunk size
 335     // boundaries, so the largest padding space required would be that
 336     // minus the smallest chunk size.
 337     const size_t smallest_chunk_size = is_class ? ClassSpecializedChunk : SpecializedChunk;
 338     return chunk_word_size - smallest_chunk_size;
 339   } else {
 340     // Humongous chunks are allocated at smallest-chunksize
 341     // boundaries, so there is no padding required.
 342     return 0;
 343   }
 344 }
 345 
 346 
 347 Metachunk* VirtualSpaceList::get_new_chunk(size_t chunk_word_size, size_t suggested_commit_granularity) {
 348 
 349   // Allocate a chunk out of the current virtual space.
 350   Metachunk* next = current_virtual_space()->get_chunk_vs(chunk_word_size);
 351 
 352   if (next != NULL) {
 353     return next;
 354   }
 355 
 356   // The expand amount is currently only determined by the requested sizes
 357   // and not how much committed memory is left in the current virtual space.
 358 
 359   // We must have enough space for the requested size and any
 360   // additional reqired padding chunks.
 361   const size_t size_for_padding = largest_possible_padding_size_for_chunk(chunk_word_size, this->is_class());
 362 
 363   size_t min_word_size       = align_up(chunk_word_size + size_for_padding, Metaspace::commit_alignment_words());
 364   size_t preferred_word_size = align_up(suggested_commit_granularity, Metaspace::commit_alignment_words());
 365   if (min_word_size >= preferred_word_size) {
 366     // Can happen when humongous chunks are allocated.
 367     preferred_word_size = min_word_size;
 368   }
 369 
 370   bool expanded = expand_by(min_word_size, preferred_word_size);
 371   if (expanded) {
 372     next = current_virtual_space()->get_chunk_vs(chunk_word_size);
 373     assert(next != NULL, "The allocation was expected to succeed after the expansion");
 374   }
 375 
 376    return next;
 377 }
 378 
 379 void VirtualSpaceList::print_on(outputStream* st, size_t scale) const {
 380   st->print_cr(SIZE_FORMAT " nodes, current node: " PTR_FORMAT,
 381       _virtual_space_count, p2i(_current_virtual_space));
 382   VirtualSpaceListIterator iter(virtual_space_list());
 383   while (iter.repeat()) {
 384     st->cr();
 385     VirtualSpaceNode* node = iter.get_next();
 386     node->print_on(st, scale);
 387   }
 388 }
 389 
 390 void VirtualSpaceList::print_map(outputStream* st) const {
 391   VirtualSpaceNode* list = virtual_space_list();
 392   VirtualSpaceListIterator iter(list);
 393   unsigned i = 0;
 394   while (iter.repeat()) {
 395     st->print_cr("Node %u:", i);
 396     VirtualSpaceNode* node = iter.get_next();
 397     node->print_map(st, this->is_class());
 398     i ++;
 399   }
 400 }
 401 
 402 } // namespace metaspace
 403