< prev index next >

src/hotspot/share/memory/metaspace/virtualSpaceList.cpp

Print this page


   1 /*
   2  * Copyright (c) 2018, 2019, 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  *


  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* prev_vsl = virtual_space_list();
  96   VirtualSpaceNode* next_vsl = prev_vsl;
  97   int num_purged_nodes = 0;
  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       delete vsl;
 122       num_purged_nodes ++;
 123     } else {
 124       prev_vsl = vsl;
 125     }
 126   }
 127 
 128   // Verify list
 129 #ifdef ASSERT
 130   if (num_purged_nodes > 0) {
 131     verify(false);





 132   }
 133 #endif
 134 }
 135 
 136 
 137 // This function looks at the mmap regions in the metaspace without locking.
 138 // The chunks are added with store ordering and not deleted except for at
 139 // unloading time during a safepoint.
 140 VirtualSpaceNode* VirtualSpaceList::find_enclosing_space(const void* ptr) {
 141   // List should be stable enough to use an iterator here because removing virtual
 142   // space nodes is only allowed at a safepoint.
 143   if (is_within_envelope((address)ptr)) {
 144     VirtualSpaceListIterator iter(virtual_space_list());
 145     while (iter.repeat()) {
 146       VirtualSpaceNode* vsn = iter.get_next();
 147       if (vsn->contains(ptr)) {
 148         return vsn;
 149       }
 150     }
 151   }
 152   return NULL;
 153 }
 154 
 155 void VirtualSpaceList::retire_current_virtual_space() {
 156   assert_lock_strong(MetaspaceExpand_lock);
 157 
 158   VirtualSpaceNode* vsn = current_virtual_space();
 159 
 160   ChunkManager* cm = is_class() ? Metaspace::chunk_manager_class() :
 161                                   Metaspace::chunk_manager_metadata();
 162 
 163   vsn->retire(cm);
 164 }
 165 
 166 VirtualSpaceList::VirtualSpaceList(size_t word_size) :
 167                                    _virtual_space_list(NULL),
 168                                    _current_virtual_space(NULL),
 169                                    _is_class(false),
 170                                    _reserved_words(0),
 171                                    _committed_words(0),
 172                                    _virtual_space_count(0),
 173                                    _envelope_lo((address)max_uintx),
 174                                    _envelope_hi(NULL) {
 175   MutexLocker cl(MetaspaceExpand_lock, 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                                    _envelope_lo((address)max_uintx),
 187                                    _envelope_hi(NULL) {
 188   MutexLocker cl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
 189   VirtualSpaceNode* class_entry = new VirtualSpaceNode(is_class(), rs);
 190   bool succeeded = class_entry->initialize();
 191   if (succeeded) {
 192     expand_envelope_to_include_node(class_entry);
 193     // ensure lock-free iteration sees fully initialized node
 194     OrderAccess::storestore();
 195     link_vs(class_entry);
 196   }
 197 }
 198 
 199 size_t VirtualSpaceList::free_bytes() {
 200   return current_virtual_space()->free_words_in_vs() * BytesPerWord;
 201 }
 202 
 203 // Allocate another meta virtual space and add it to the list.
 204 bool VirtualSpaceList::create_new_virtual_space(size_t vs_word_size) {
 205   assert_lock_strong(MetaspaceExpand_lock);
 206 
 207   if (is_class()) {
 208     assert(false, "We currently don't support more than one VirtualSpace for"
 209                   " the compressed class space. The initialization of the"
 210                   " CCS uses another code path and should not hit this path.");
 211     return false;
 212   }
 213 
 214   if (vs_word_size == 0) {
 215     assert(false, "vs_word_size should always be at least _reserve_alignment large.");
 216     return false;
 217   }
 218 
 219   // Reserve the space
 220   size_t vs_byte_size = vs_word_size * BytesPerWord;
 221   assert_is_aligned(vs_byte_size, Metaspace::reserve_alignment());
 222 
 223   // Allocate the meta virtual space and initialize it.
 224   VirtualSpaceNode* new_entry = new VirtualSpaceNode(is_class(), vs_byte_size);
 225   if (!new_entry->initialize()) {
 226     delete new_entry;
 227     return false;
 228   } else {
 229     assert(new_entry->reserved_words() == vs_word_size,
 230         "Reserved memory size differs from requested memory size");
 231     expand_envelope_to_include_node(new_entry);
 232     // ensure lock-free iteration sees fully initialized node
 233     OrderAccess::storestore();
 234     link_vs(new_entry);
 235     DEBUG_ONLY(Atomic::inc(&g_internal_statistics.num_vsnodes_created));
 236     return true;
 237   }
 238 
 239   DEBUG_ONLY(verify(false);)
 240 
 241 }
 242 
 243 void VirtualSpaceList::link_vs(VirtualSpaceNode* new_entry) {
 244   if (virtual_space_list() == NULL) {
 245       set_virtual_space_list(new_entry);
 246   } else {
 247     current_virtual_space()->set_next(new_entry);
 248   }
 249   set_current_virtual_space(new_entry);
 250   inc_reserved_words(new_entry->reserved_words());
 251   inc_committed_words(new_entry->committed_words());
 252   inc_virtual_space_count();
 253 #ifdef ASSERT
 254   new_entry->mangle();
 255 #endif
 256   LogTarget(Trace, gc, metaspace) lt;
 257   if (lt.is_enabled()) {
 258     LogStream ls(lt);
 259     VirtualSpaceNode* vsl = current_virtual_space();
 260     ResourceMark rm;


 389       _virtual_space_count, p2i(_current_virtual_space));
 390   VirtualSpaceListIterator iter(virtual_space_list());
 391   while (iter.repeat()) {
 392     st->cr();
 393     VirtualSpaceNode* node = iter.get_next();
 394     node->print_on(st, scale);
 395   }
 396 }
 397 
 398 void VirtualSpaceList::print_map(outputStream* st) const {
 399   VirtualSpaceNode* list = virtual_space_list();
 400   VirtualSpaceListIterator iter(list);
 401   unsigned i = 0;
 402   while (iter.repeat()) {
 403     st->print_cr("Node %u:", i);
 404     VirtualSpaceNode* node = iter.get_next();
 405     node->print_map(st, this->is_class());
 406     i ++;
 407   }
 408 }
 409 
 410 // Given a node, expand range such that it includes the node.
 411 void VirtualSpaceList::expand_envelope_to_include_node(const VirtualSpaceNode* node) {
 412   _envelope_lo = MIN2(_envelope_lo, (address)node->low_boundary());
 413   _envelope_hi = MAX2(_envelope_hi, (address)node->high_boundary());
 414 }
 415 
 416 
 417 #ifdef ASSERT
 418 void VirtualSpaceList::verify(bool slow) {
 419   VirtualSpaceNode* list = virtual_space_list();
 420   VirtualSpaceListIterator iter(list);
 421   size_t reserved = 0;
 422   size_t committed = 0;
 423   size_t node_count = 0;
 424   while (iter.repeat()) {
 425     VirtualSpaceNode* node = iter.get_next();
 426     if (slow) {
 427       node->verify(true);
 428     }
 429     // Check that the node resides fully within our envelope.
 430     assert((address)node->low_boundary() >= _envelope_lo && (address)node->high_boundary() <= _envelope_hi,
 431            "Node " SIZE_FORMAT " [" PTR_FORMAT ", " PTR_FORMAT ") outside envelope [" PTR_FORMAT ", " PTR_FORMAT ").",
 432            node_count, p2i(node->low_boundary()), p2i(node->high_boundary()), p2i(_envelope_lo), p2i(_envelope_hi));
 433     reserved += node->reserved_words();
 434     committed += node->committed_words();
 435     node_count ++;
 436   }
 437   assert(reserved == reserved_words() && committed == committed_words() && node_count == _virtual_space_count,
 438       "Mismatch: reserved real: " SIZE_FORMAT " expected: " SIZE_FORMAT
 439       ", committed real: " SIZE_FORMAT " expected: " SIZE_FORMAT
 440       ", node count real: " SIZE_FORMAT " expected: " SIZE_FORMAT ".",
 441       reserved, reserved_words(), committed, committed_words(),
 442       node_count, _virtual_space_count);
 443 }
 444 #endif // ASSERT
 445 
 446 } // namespace metaspace
 447 
   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  *


  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;


 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 
< prev index next >