< prev index next >

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

Print this page
rev 57380 : [mq]: metaspace-improvement

@@ -20,153 +20,113 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  *
  */
 
+
 #include "precompiled.hpp"
-#include "memory/allocation.hpp"
+
+#include "memory/metaspace/chunkLevel.hpp"
+#include "memory/metaspace/chunkTree.hpp"
+#include "memory/metaspace/constants.hpp"
 #include "memory/metaspace/metachunk.hpp"
-#include "memory/metaspace/occupancyMap.hpp"
 #include "memory/metaspace/virtualSpaceNode.hpp"
+
 #include "utilities/align.hpp"
 #include "utilities/copy.hpp"
 #include "utilities/debug.hpp"
 
 namespace metaspace {
 
-size_t Metachunk::object_alignment() {
-  // Must align pointers and sizes to 8,
-  // so that 64 bit types get correctly aligned.
-  const size_t alignment = 8;
+// Must align pointers and sizes to 8 so that 64 bit types get correctly aligned.
+static const size_t allocation_alignment = 8;
 
-  // Make sure that the Klass alignment also agree.
-  STATIC_ASSERT(alignment == (size_t)KlassAlignmentInBytes);
+// Make sure that the Klass alignment also agree.
+STATIC_ASSERT(allocation_alignment == (size_t)KlassAlignmentInBytes);
 
-  return alignment;
-}
-
-size_t Metachunk::overhead() {
-  return align_up(sizeof(Metachunk), object_alignment()) / BytesPerWord;
+void Metachunk::remove_from_list() {
+  if (_prev != NULL) {
+    _prev->set_next(_next);
+  }
+  if (_next != NULL) {
+    _next->set_prev(_prev);
+  }
+  _prev = _next = NULL;
 }
 
-// Metachunk methods
+// Allocate word_size words from this chunk.
+//
+// May cause memory to be committed. That may fail if we hit a commit limit. In that case,
+//  NULL is returned and p_did_hit_commit_limit will be set to true.
+// If the remainder portion of the chunk was too small to hold the allocation,
+//  NULL is returned and p_did_hit_commit_limit will be set to false.
+MetaWord* Metachunk::allocate(size_t word_size, bool* p_did_hit_commit_limit) {
+
+  size_t request_word_size = align_up(word_size, allocation_alignment);
+
+  // Space enough left?
+  if (free_words() < request_word_size) {
+    *p_did_hit_commit_limit = false;
+    return NULL;
+  }
 
-Metachunk::Metachunk(ChunkIndex chunktype, bool is_class, size_t word_size,
-                     VirtualSpaceNode* container)
-    : Metabase<Metachunk>(word_size),
-    _container(container),
-    _top(NULL),
-    _sentinel(CHUNK_SENTINEL),
-    _chunk_type(chunktype),
-    _is_class(is_class),
-    _origin(origin_normal),
-    _use_count(0)
-{
-  _top = initial_top();
-  set_is_tagged_free(false);
-#ifdef ASSERT
-  mangle(uninitMetaWordVal);
-  verify();
-#endif
-}
+  // Will bring this allocation us over the border of the next commit granule?
+  MetaWord* const top = top();
+  MetaWord* const next_top = top() + request_word_size;
+
+  MetaWord* const commit_granule_border = align_up(top, constants::commit_granule_words);
+
+  if (next_top > commit_granule_border) {
+    // We may need to commit.
+
+    // Calc the extend of the area we may need to commit.
+    MetaWord* const commit_granule_border_2 = align_up(next_top, constants::commit_granule_words);
+
+    // and attempt to commit.
+    const size_t word_size_to_commit = commit_granule_border_2 - commit_granule_border;
+    if (_vsnode->ensure_range_is_committed(commit_granule_border, word_size_to_commit) == false) {
+      // ... which failed because we hit a limit.
+      *p_did_hit_commit_limit = true;
+      return NULL;
+    }
 
-MetaWord* Metachunk::allocate(size_t word_size) {
-  MetaWord* result = NULL;
-  // If available, bump the pointer to allocate.
-  if (free_word_size() >= word_size) {
-    result = _top;
-    _top = _top + word_size;
   }
-  return result;
-}
 
-// _bottom points to the start of the chunk including the overhead.
-size_t Metachunk::used_word_size() const {
-  return pointer_delta(_top, bottom(), sizeof(MetaWord));
-}
+  // All is well.
+  _used_words += request_word_size;
 
-size_t Metachunk::free_word_size() const {
-  return pointer_delta(end(), _top, sizeof(MetaWord));
-}
+  return top;
 
-void Metachunk::print_on(outputStream* st) const {
-  st->print_cr("Metachunk:"
-               " bottom " PTR_FORMAT " top " PTR_FORMAT
-               " end " PTR_FORMAT " size " SIZE_FORMAT " (%s)",
-               p2i(bottom()), p2i(_top), p2i(end()), word_size(),
-               chunk_size_name(get_chunk_type()));
-  if (Verbose) {
-    st->print_cr("    used " SIZE_FORMAT " free " SIZE_FORMAT,
-                 used_word_size(), free_word_size());
-  }
 }
 
 #ifdef ASSERT
-void Metachunk::mangle(juint word_value) {
-  // Overwrite the payload of the chunk and not the links that
-  // maintain list of chunks.
-  HeapWord* start = (HeapWord*)initial_top();
-  size_t size = word_size() - overhead();
-  Copy::fill_to_words(start, size, word_value);
-}
 
-void Metachunk::verify() const {
-  assert(is_valid_sentinel(), "Chunk " PTR_FORMAT ": sentinel invalid", p2i(this));
-  const ChunkIndex chunk_type = get_chunk_type();
-  assert(is_valid_chunktype(chunk_type), "Chunk " PTR_FORMAT ": Invalid chunk type.", p2i(this));
-  if (chunk_type != HumongousIndex) {
-    assert(word_size() == get_size_for_nonhumongous_chunktype(chunk_type, is_class()),
-           "Chunk " PTR_FORMAT ": wordsize " SIZE_FORMAT " does not fit chunk type %s.",
-           p2i(this), word_size(), chunk_size_name(chunk_type));
-  }
-  assert(is_valid_chunkorigin(get_origin()), "Chunk " PTR_FORMAT ": Invalid chunk origin.", p2i(this));
-  assert(bottom() <= _top && _top <= (MetaWord*)end(),
-         "Chunk " PTR_FORMAT ": Chunk top out of chunk bounds.", p2i(this));
-
-  // For non-humongous chunks, starting address shall be aligned
-  // to its chunk size. Humongous chunks start address is
-  // aligned to specialized chunk size.
-  const size_t required_alignment =
-    (chunk_type != HumongousIndex ? word_size() : get_size_for_nonhumongous_chunktype(SpecializedIndex, is_class())) * sizeof(MetaWord);
-  assert(is_aligned((address)this, required_alignment),
-         "Chunk " PTR_FORMAT ": (size " SIZE_FORMAT ") not aligned to " SIZE_FORMAT ".",
+void Metachunk::verify(bool slow) const {
+
+  // Note: only call this on a life Metachunk.
+
+  chklvl::check_valid_level(level());
+
+  // Test base pointer
+  assert(vsnode() != NULL, "No space");
+  vsnode()->check_pointer(base());
+  assert(base() != NULL, "Base pointer NULL");
+
+  // Starting address shall be aligned to chunk size.
+  const size_t required_alignment = word_size() * sizeof(MetaWord);
+  assert(is_aligned(base(), required_alignment),
+         "Chunk " PTR_FORMAT ": (size " SIZE_FORMAT ") not aligned correctly to " SIZE_FORMAT ".",
          p2i(this), word_size() * sizeof(MetaWord), required_alignment);
-}
 
-#endif // ASSERT
+  // Used words
+  assert(used_words() < word_size(), "oob");
 
-// Helper, returns a descriptive name for the given index.
-const char* chunk_size_name(ChunkIndex index) {
-  switch (index) {
-    case SpecializedIndex:
-      return "specialized";
-    case SmallIndex:
-      return "small";
-    case MediumIndex:
-      return "medium";
-    case HumongousIndex:
-      return "humongous";
-    default:
-      return "Invalid index";
-  }
-}
+  // We shall have a reference to a tree node and that node shall refer back to us
+  assert(tree_node() != NULL &&
+         tree_node()->check_is_child(this), "Invalid tree node relationship");
 
-#ifdef ASSERT
-void do_verify_chunk(Metachunk* chunk) {
-  guarantee(chunk != NULL, "Sanity");
-  // Verify chunk itself; then verify that it is consistent with the
-  // occupany map of its containing node.
-  chunk->verify();
-  VirtualSpaceNode* const vsn = chunk->container();
-  OccupancyMap* const ocmap = vsn->occupancy_map();
-  ocmap->verify_for_chunk(chunk);
-}
-#endif
 
-void do_update_in_use_info_for_chunk(Metachunk* chunk, bool inuse) {
-  chunk->set_is_tagged_free(!inuse);
-  OccupancyMap* const ocmap = chunk->container()->occupancy_map();
-  ocmap->set_region_in_use((MetaWord*)chunk, chunk->word_size(), inuse);
 }
+#endif // ASSERT
 
 } // namespace metaspace
 
< prev index next >