changeset 9663:91bd6ad35898

8067339: PLAB reallocation might result in failure to allocate object in that recently allocated PLAB Summary: Properly size the PLAB to make sure that the object to allocate always has enough space in it. Reviewed-by: mgerdin, brutisso
author tschatzl
date Thu, 20 Aug 2015 15:17:43 +0200
parents 57093b085a8f
children da41f75db57a
files src/share/vm/gc/cms/parNewGeneration.cpp src/share/vm/gc/g1/g1Allocator.cpp src/share/vm/gc/g1/g1Allocator.hpp src/share/vm/gc/shared/plab.hpp
diffstat 4 files changed, 27 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/vm/gc/cms/parNewGeneration.cpp	Thu Aug 20 15:17:43 2015 +0200
+++ b/src/share/vm/gc/cms/parNewGeneration.cpp	Thu Aug 20 15:17:43 2015 +0200
@@ -248,8 +248,7 @@
         }
       }
       if (buf_space != NULL) {
-        plab->set_word_size(buf_size);
-        plab->set_buf(buf_space);
+        plab->set_buf(buf_space, buf_size);
         record_survivor_plab(buf_space, buf_size);
         obj = plab->allocate_aligned(word_sz, SurvivorAlignmentInBytes);
         // Note that we cannot compare buf_size < word_sz below
--- a/src/share/vm/gc/g1/g1Allocator.cpp	Thu Aug 20 15:17:43 2015 +0200
+++ b/src/share/vm/gc/g1/g1Allocator.cpp	Thu Aug 20 15:17:43 2015 +0200
@@ -223,23 +223,34 @@
   }
 }
 
+bool G1PLABAllocator::may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const {
+  return (allocation_word_sz * 100 < buffer_size * ParallelGCBufferWastePct);
+}
+
 HeapWord* G1PLABAllocator::allocate_direct_or_new_plab(InCSetState dest,
                                                        size_t word_sz,
                                                        AllocationContext_t context,
                                                        bool* plab_refill_failed) {
-  size_t gclab_word_size = _g1h->desired_plab_sz(dest);
-  if (word_sz * 100 < gclab_word_size * ParallelGCBufferWastePct) {
+  size_t plab_word_size = G1CollectedHeap::heap()->desired_plab_sz(dest);
+  size_t required_in_plab = PLAB::size_required_for_allocation(word_sz);
+
+  // Only get a new PLAB if the allocation fits and it would not waste more than
+  // ParallelGCBufferWastePct in the existing buffer.
+  if ((required_in_plab <= plab_word_size) &&
+    may_throw_away_buffer(required_in_plab, plab_word_size)) {
+
     G1PLAB* alloc_buf = alloc_buffer(dest, context);
     alloc_buf->retire();
 
-    HeapWord* buf = _allocator->par_allocate_during_gc(dest, gclab_word_size, context);
+    HeapWord* buf = _allocator->par_allocate_during_gc(dest, plab_word_size, context);
     if (buf != NULL) {
       // Otherwise.
-      alloc_buf->set_word_size(gclab_word_size);
-      alloc_buf->set_buf(buf);
+      alloc_buf->set_buf(buf, plab_word_size);
 
       HeapWord* const obj = alloc_buf->allocate(word_sz);
-      assert(obj != NULL, "buffer was definitely big enough...");
+      assert(obj != NULL, err_msg("PLAB should have been big enough, tried to allocate "
+                                  SIZE_FORMAT " requiring " SIZE_FORMAT " PLAB size " SIZE_FORMAT,
+                                  word_sz, required_in_plab, plab_word_size));
       return obj;
     }
     // Otherwise.
--- a/src/share/vm/gc/g1/g1Allocator.hpp	Thu Aug 20 15:17:43 2015 +0200
+++ b/src/share/vm/gc/g1/g1Allocator.hpp	Thu Aug 20 15:17:43 2015 +0200
@@ -176,8 +176,8 @@
   // waste due to refills and alignment.
   size_t wasted() const { return _wasted; }
 
-  virtual void set_buf(HeapWord* buf) {
-    PLAB::set_buf(buf);
+  virtual void set_buf(HeapWord* buf, size_t word_size) {
+    PLAB::set_buf(buf, word_size);
     _retired = false;
   }
 
@@ -235,6 +235,7 @@
                               size_t word_sz,
                               AllocationContext_t context);
 
+  bool may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const;
 public:
   G1PLABAllocator(G1Allocator* allocator);
   virtual ~G1PLABAllocator() { }
--- a/src/share/vm/gc/shared/plab.hpp	Thu Aug 20 15:17:43 2015 +0200
+++ b/src/share/vm/gc/shared/plab.hpp	Thu Aug 20 15:17:43 2015 +0200
@@ -74,6 +74,8 @@
   PLAB(size_t word_sz);
   virtual ~PLAB() {}
 
+  static size_t size_required_for_allocation(size_t word_size) { return word_size + AlignmentReserve; }
+
   // Minimum PLAB size.
   static size_t min_size();
   // Maximum PLAB size.
@@ -107,13 +109,6 @@
   size_t waste() { return _wasted; }
   size_t undo_waste() { return _undo_wasted; }
 
-  // Should only be done if we are about to reset with a new buffer of the
-  // given size.
-  void set_word_size(size_t new_word_sz) {
-    assert(new_word_sz > AlignmentReserve, "Too small");
-    _word_sz = new_word_sz;
-  }
-
   // The number of words of unallocated space remaining in the buffer.
   size_t words_remaining() {
     assert(_end >= _top, "Negative buffer");
@@ -125,7 +120,10 @@
   }
 
   // Sets the space of the buffer to be [buf, space+word_sz()).
-  virtual void set_buf(HeapWord* buf) {
+  virtual void set_buf(HeapWord* buf, size_t new_word_sz) {
+    assert(new_word_sz > AlignmentReserve, "Too small");
+    _word_sz = new_word_sz;
+
     _bottom   = buf;
     _top      = _bottom;
     _hard_end = _bottom + word_sz();