1 /* 2 * Copyright (c) 2001, 2010, 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_PARNEW_PARGCALLOCBUFFER_HPP 26 #define SHARE_VM_GC_IMPLEMENTATION_PARNEW_PARGCALLOCBUFFER_HPP 27 28 #include "memory/allocation.hpp" 29 #include "memory/threadLocalAllocBuffer.hpp" 30 #include "utilities/globalDefinitions.hpp" 31 32 // Forward decl. 33 34 class PLABStats; 35 36 // A per-thread allocation buffer used during GC. 37 class ParGCAllocBuffer: public CHeapObj { 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 bool _retained; // whether we hold a _retained_filler 46 MemRegion _retained_filler; 47 // In support of ergonomic sizing of PLAB's 48 size_t _allocated; // in HeapWord units 49 size_t _wasted; // in HeapWord units 50 char tail[32]; 51 static size_t FillerHeaderSize; 52 static size_t AlignmentReserve; 53 54 public: 55 // Initializes the buffer to be empty, but with the given "word_sz". 56 // Must get initialized with "set_buf" for an allocation to succeed. 57 ParGCAllocBuffer(size_t word_sz); 58 59 static const size_t min_size() { 60 return ThreadLocalAllocBuffer::min_size(); 61 } 62 63 static const size_t max_size() { 64 return ThreadLocalAllocBuffer::max_size(); 65 } 66 67 // If an allocation of the given "word_sz" can be satisfied within the 68 // buffer, do the allocation, returning a pointer to the start of the 69 // allocated block. If the allocation request cannot be satisfied, 70 // return NULL. 71 HeapWord* allocate(size_t word_sz) { 72 HeapWord* res = _top; 73 if (pointer_delta(_end, _top) >= word_sz) { 74 _top = _top + word_sz; 75 return res; 76 } else { 77 return NULL; 78 } 79 } 80 81 // Undo the last allocation in the buffer, which is required to be of the 82 // "obj" of the given "word_sz". 83 void undo_allocation(HeapWord* obj, size_t word_sz) { 84 assert(pointer_delta(_top, _bottom) >= word_sz, "Bad undo"); 85 assert(pointer_delta(_top, obj) == word_sz, "Bad undo"); 86 _top = obj; 87 } 88 89 // The total (word) size of the buffer, including both allocated and 90 // unallocted space. 91 size_t word_sz() { return _word_sz; } 92 93 // Should only be done if we are about to reset with a new buffer of the 94 // given size. 95 void set_word_size(size_t new_word_sz) { 96 assert(new_word_sz > AlignmentReserve, "Too small"); 97 _word_sz = new_word_sz; 98 } 99 100 // The number of words of unallocated space remaining in the buffer. 101 size_t words_remaining() { 102 assert(_end >= _top, "Negative buffer"); 103 return pointer_delta(_end, _top, HeapWordSize); 104 } 105 106 bool contains(void* addr) { 107 return (void*)_bottom <= addr && addr < (void*)_hard_end; 108 } 109 110 // Sets the space of the buffer to be [buf, space+word_sz()). 111 void set_buf(HeapWord* buf) { 112 _bottom = buf; 113 _top = _bottom; 114 _hard_end = _bottom + word_sz(); 115 _end = _hard_end - AlignmentReserve; 116 assert(_end >= _top, "Negative buffer"); 117 // In support of ergonomic sizing 118 _allocated += word_sz(); 119 } 120 121 // Flush the stats supporting ergonomic sizing of PLAB's 122 void flush_stats(PLABStats* stats); 123 void flush_stats_and_retire(PLABStats* stats, bool retain) { 124 // We flush the stats first in order to get a reading of 125 // unused space in the last buffer. 126 if (ResizePLAB) { 127 flush_stats(stats); 128 } 129 // Retire the last allocation buffer. 130 retire(true, retain); 131 } 132 133 // Force future allocations to fail and queries for contains() 134 // to return false 135 void invalidate() { 136 assert(!_retained, "Shouldn't retain an invalidated buffer."); 137 _end = _hard_end; 138 _wasted += pointer_delta(_end, _top); // unused space 139 _top = _end; // force future allocations to fail 140 _bottom = _end; // force future contains() queries to return false 141 } 142 143 // Fills in the unallocated portion of the buffer with a garbage object. 144 // If "end_of_gc" is TRUE, is after the last use in the GC. IF "retain" 145 // is true, attempt to re-use the unused portion in the next GC. 146 void retire(bool end_of_gc, bool retain); 147 148 void print() PRODUCT_RETURN; 149 }; 150 151 // PLAB stats book-keeping 152 class PLABStats VALUE_OBJ_CLASS_SPEC { 153 size_t _allocated; // total allocated 154 size_t _wasted; // of which wasted (internal fragmentation) 155 size_t _unused; // Unused in last buffer 156 size_t _used; // derived = allocated - wasted - unused 157 size_t _desired_plab_sz;// output of filter (below), suitably trimmed and quantized 158 AdaptiveWeightedAverage 159 _filter; // integrator with decay 160 161 public: 162 PLABStats(size_t desired_plab_sz_, unsigned wt) : 163 _allocated(0), 164 _wasted(0), 165 _unused(0), 166 _used(0), 167 _desired_plab_sz(desired_plab_sz_), 168 _filter(wt) 169 { 170 size_t min_sz = min_size(); 171 size_t max_sz = max_size(); 172 size_t aligned_min_sz = align_object_size(min_sz); 173 size_t aligned_max_sz = align_object_size(max_sz); 174 assert(min_sz <= aligned_min_sz && max_sz >= aligned_max_sz && 175 min_sz <= max_sz, 176 "PLAB clipping computation in adjust_desired_plab_sz()" 177 " may be incorrect"); 178 } 179 180 static const size_t min_size() { 181 return ParGCAllocBuffer::min_size(); 182 } 183 184 static const size_t max_size() { 185 return ParGCAllocBuffer::max_size(); 186 } 187 188 size_t desired_plab_sz() { 189 return _desired_plab_sz; 190 } 191 192 void adjust_desired_plab_sz(); // filter computation, latches output to 193 // _desired_plab_sz, clears sensor accumulators 194 195 void add_allocated(size_t v) { 196 Atomic::add_ptr(v, &_allocated); 197 } 198 199 void add_unused(size_t v) { 200 Atomic::add_ptr(v, &_unused); 201 } 202 203 void add_wasted(size_t v) { 204 Atomic::add_ptr(v, &_wasted); 205 } 206 }; 207 208 class ParGCAllocBufferWithBOT: public ParGCAllocBuffer { 209 BlockOffsetArrayContigSpace _bt; 210 BlockOffsetSharedArray* _bsa; 211 HeapWord* _true_end; // end of the whole ParGCAllocBuffer 212 213 static const size_t ChunkSizeInWords; 214 static const size_t ChunkSizeInBytes; 215 HeapWord* allocate_slow(size_t word_sz); 216 217 void fill_region_with_block(MemRegion mr, bool contig); 218 219 public: 220 ParGCAllocBufferWithBOT(size_t word_sz, BlockOffsetSharedArray* bsa); 221 222 HeapWord* allocate(size_t word_sz) { 223 HeapWord* res = ParGCAllocBuffer::allocate(word_sz); 224 if (res != NULL) { 225 _bt.alloc_block(res, word_sz); 226 } else { 227 res = allocate_slow(word_sz); 228 } 229 return res; 230 } 231 232 void undo_allocation(HeapWord* obj, size_t word_sz); 233 234 void set_buf(HeapWord* buf_start) { 235 ParGCAllocBuffer::set_buf(buf_start); 236 _true_end = _hard_end; 237 _bt.set_region(MemRegion(buf_start, word_sz())); 238 _bt.initialize_threshold(); 239 } 240 241 void retire(bool end_of_gc, bool retain); 242 243 MemRegion range() { 244 return MemRegion(_top, _true_end); 245 } 246 }; 247 248 #endif // SHARE_VM_GC_IMPLEMENTATION_PARNEW_PARGCALLOCBUFFER_HPP