1 /* 2 * Copyright (c) 2001, 2015, 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 #ifndef SHARE_VM_GC_IMPLEMENTATION_SHARED_PLAB_HPP 26 #define SHARE_VM_GC_IMPLEMENTATION_SHARED_PLAB_HPP 27 28 #include "gc_implementation/shared/gcUtil.hpp" 29 #include "memory/allocation.hpp" 30 #include "runtime/atomic.hpp" 31 #include "utilities/globalDefinitions.hpp" 32 33 // Forward declarations. 34 class PLABStats; 35 36 // A per-thread allocation buffer used during GC. 37 class PLAB: public CHeapObj<mtGC> { 38 protected: 39 char head[32]; 40 size_t _word_sz; // In HeapWord units 41 HeapWord* _bottom; 42 HeapWord* _top; 43 HeapWord* _end; // Last allocatable address + 1 44 HeapWord* _hard_end; // _end + AlignmentReserve 45 // In support of ergonomic sizing of PLAB's 46 size_t _allocated; // in HeapWord units 47 size_t _wasted; // in HeapWord units 48 size_t _undo_wasted; 49 char tail[32]; 50 static size_t AlignmentReserve; 51 52 // Force future allocations to fail and queries for contains() 53 // to return false. Returns the amount of unused space in this PLAB. 54 size_t invalidate() { 55 _end = _hard_end; 56 size_t remaining = pointer_delta(_end, _top); // Calculate remaining space. 57 _top = _end; // Force future allocations to fail. 58 _bottom = _end; // Force future contains() queries to return false. 59 return remaining; 60 } 61 62 // Fill in remaining space with a dummy object and invalidate the PLAB. Returns 63 // the amount of remaining space. 64 size_t retire_internal(); 65 66 void add_undo_waste(HeapWord* obj, size_t word_sz) { 67 CollectedHeap::fill_with_object(obj, word_sz); 68 _undo_wasted += word_sz; 69 } 70 71 // Undo the last allocation in the buffer, which is required to be of the 72 // "obj" of the given "word_sz". 73 void undo_last_allocation(HeapWord* obj, size_t word_sz) { 74 assert(pointer_delta(_top, _bottom) >= word_sz, "Bad undo"); 75 assert(pointer_delta(_top, obj) == word_sz, "Bad undo"); 76 _top = obj; 77 } 78 79 public: 80 // Initializes the buffer to be empty, but with the given "word_sz". 81 // Must get initialized with "set_buf" for an allocation to succeed. 82 PLAB(size_t word_sz); 83 virtual ~PLAB() {} 84 85 // Minimum PLAB size. 86 static size_t min_size(); 87 // Maximum PLAB size. 88 static size_t max_size(); 89 90 // If an allocation of the given "word_sz" can be satisfied within the 91 // buffer, do the allocation, returning a pointer to the start of the 92 // allocated block. If the allocation request cannot be satisfied, 93 // return NULL. 94 HeapWord* allocate(size_t word_sz) { 95 HeapWord* res = _top; 96 if (pointer_delta(_end, _top) >= word_sz) { 97 _top = _top + word_sz; 98 return res; 99 } else { 100 return NULL; 101 } 102 } 103 104 // Allocate the object aligned to "alignment_in_bytes". 105 HeapWord* allocate_aligned(size_t word_sz, unsigned short alignment_in_bytes); 106 107 // Undo any allocation in the buffer, which is required to be of the 108 // "obj" of the given "word_sz". 109 void undo_allocation(HeapWord* obj, size_t word_sz) { 110 // Is the alloc in the current alloc buffer? 111 if (contains(obj)) { 112 assert(contains(obj + word_sz - 1), 113 "should contain whole object"); 114 undo_last_allocation(obj, word_sz); 115 } else { 116 add_undo_waste(obj, word_sz); 117 } 118 } 119 120 // The total (word) size of the buffer, including both allocated and 121 // unallocated space. 122 size_t word_sz() { return _word_sz; } 123 124 size_t waste() { return _wasted; } 125 size_t undo_waste() { return _undo_wasted; } 126 127 // Should only be done if we are about to reset with a new buffer of the 128 // given size. 129 void set_word_size(size_t new_word_sz) { 130 assert(new_word_sz > AlignmentReserve, "Too small"); 131 _word_sz = new_word_sz; 132 } 133 134 // The number of words of unallocated space remaining in the buffer. 135 size_t words_remaining() { 136 assert(_end >= _top, "Negative buffer"); 137 return pointer_delta(_end, _top, HeapWordSize); 138 } 139 140 bool contains(void* addr) { 141 return (void*)_bottom <= addr && addr < (void*)_hard_end; 142 } 143 144 // Sets the space of the buffer to be [buf, space+word_sz()). 145 virtual void set_buf(HeapWord* buf) { 146 _bottom = buf; 147 _top = _bottom; 148 _hard_end = _bottom + word_sz(); 149 _end = _hard_end - AlignmentReserve; 150 assert(_end >= _top, "Negative buffer"); 151 // In support of ergonomic sizing 152 _allocated += word_sz(); 153 } 154 155 // Flush allocation statistics into the given PLABStats supporting ergonomic 156 // sizing of PLAB's and retire the current buffer. To be called at the end of 157 // GC. 158 void flush_and_retire_stats(PLABStats* stats); 159 160 // Fills in the unallocated portion of the buffer with a garbage object and updates 161 // statistics. To be called during GC. 162 virtual void retire(); 163 164 void print() PRODUCT_RETURN; 165 }; 166 167 // PLAB book-keeping. 168 class PLABStats VALUE_OBJ_CLASS_SPEC { 169 size_t _allocated; // Total allocated 170 size_t _wasted; // of which wasted (internal fragmentation) 171 size_t _undo_wasted; // of which wasted on undo (is not used for calculation of PLAB size) 172 size_t _unused; // Unused in last buffer 173 size_t _desired_plab_sz;// Output of filter (below), suitably trimmed and quantized 174 AdaptiveWeightedAverage 175 _filter; // Integrator with decay 176 177 void reset() { 178 _allocated = 0; 179 _wasted = 0; 180 _undo_wasted = 0; 181 _unused = 0; 182 } 183 public: 184 PLABStats(size_t desired_plab_sz_, unsigned wt) : 185 _allocated(0), 186 _wasted(0), 187 _undo_wasted(0), 188 _unused(0), 189 _desired_plab_sz(desired_plab_sz_), 190 _filter(wt) 191 { } 192 193 static const size_t min_size() { 194 return PLAB::min_size(); 195 } 196 197 static const size_t max_size() { 198 return PLAB::max_size(); 199 } 200 201 size_t desired_plab_sz() { 202 return _desired_plab_sz; 203 } 204 205 // Updates the current desired PLAB size. Computes the new desired PLAB size, 206 // updates _desired_plab_sz and clears sensor accumulators. 207 void adjust_desired_plab_sz(uint no_of_gc_workers); 208 209 void add_allocated(size_t v) { 210 Atomic::add_ptr(v, &_allocated); 211 } 212 213 void add_unused(size_t v) { 214 Atomic::add_ptr(v, &_unused); 215 } 216 217 void add_wasted(size_t v) { 218 Atomic::add_ptr(v, &_wasted); 219 } 220 221 void add_undo_wasted(size_t v) { 222 Atomic::add_ptr(v, &_undo_wasted); 223 } 224 }; 225 226 #endif // SHARE_VM_GC_IMPLEMENTATION_SHARED_PLAB_HPP