Thu, 23 Oct 2014 12:02:08 -0700
Merge
1 /*
2 * Copyright (c) 2001, 2013, 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 */
25 #ifndef SHARE_VM_GC_IMPLEMENTATION_PARNEW_PARGCALLOCBUFFER_HPP
26 #define SHARE_VM_GC_IMPLEMENTATION_PARNEW_PARGCALLOCBUFFER_HPP
27 #include "gc_interface/collectedHeap.hpp"
28 #include "memory/allocation.hpp"
29 #include "memory/blockOffsetTable.hpp"
30 #include "memory/threadLocalAllocBuffer.hpp"
31 #include "utilities/globalDefinitions.hpp"
33 // Forward decl.
35 class PLABStats;
37 // A per-thread allocation buffer used during GC.
38 class ParGCAllocBuffer: public CHeapObj<mtGC> {
39 protected:
40 char head[32];
41 size_t _word_sz; // in HeapWord units
42 HeapWord* _bottom;
43 HeapWord* _top;
44 HeapWord* _end; // last allocatable address + 1
45 HeapWord* _hard_end; // _end + AlignmentReserve
46 bool _retained; // whether we hold a _retained_filler
47 MemRegion _retained_filler;
48 // In support of ergonomic sizing of PLAB's
49 size_t _allocated; // in HeapWord units
50 size_t _wasted; // in HeapWord units
51 char tail[32];
52 static size_t FillerHeaderSize;
53 static size_t AlignmentReserve;
55 // Flush the stats supporting ergonomic sizing of PLAB's
56 // Should not be called directly
57 void flush_stats(PLABStats* stats);
59 public:
60 // Initializes the buffer to be empty, but with the given "word_sz".
61 // Must get initialized with "set_buf" for an allocation to succeed.
62 ParGCAllocBuffer(size_t word_sz);
63 virtual ~ParGCAllocBuffer() {}
65 static const size_t min_size() {
66 // Make sure that we return something that is larger than AlignmentReserve
67 return align_object_size(MAX2(MinTLABSize / HeapWordSize, (uintx)oopDesc::header_size())) + AlignmentReserve;
68 }
70 static const size_t max_size() {
71 return ThreadLocalAllocBuffer::max_size();
72 }
74 // If an allocation of the given "word_sz" can be satisfied within the
75 // buffer, do the allocation, returning a pointer to the start of the
76 // allocated block. If the allocation request cannot be satisfied,
77 // return NULL.
78 HeapWord* allocate(size_t word_sz) {
79 HeapWord* res = _top;
80 if (pointer_delta(_end, _top) >= word_sz) {
81 _top = _top + word_sz;
82 return res;
83 } else {
84 return NULL;
85 }
86 }
88 // Allocate the object aligned to "alignment_in_bytes".
89 HeapWord* allocate_aligned(size_t word_sz, unsigned short alignment_in_bytes);
91 // Undo the last allocation in the buffer, which is required to be of the
92 // "obj" of the given "word_sz".
93 void undo_allocation(HeapWord* obj, size_t word_sz) {
94 assert(pointer_delta(_top, _bottom) >= word_sz, "Bad undo");
95 assert(pointer_delta(_top, obj) == word_sz, "Bad undo");
96 _top = obj;
97 }
99 // The total (word) size of the buffer, including both allocated and
100 // unallocted space.
101 size_t word_sz() { return _word_sz; }
103 // Should only be done if we are about to reset with a new buffer of the
104 // given size.
105 void set_word_size(size_t new_word_sz) {
106 assert(new_word_sz > AlignmentReserve, "Too small");
107 _word_sz = new_word_sz;
108 }
110 // The number of words of unallocated space remaining in the buffer.
111 size_t words_remaining() {
112 assert(_end >= _top, "Negative buffer");
113 return pointer_delta(_end, _top, HeapWordSize);
114 }
116 bool contains(void* addr) {
117 return (void*)_bottom <= addr && addr < (void*)_hard_end;
118 }
120 // Sets the space of the buffer to be [buf, space+word_sz()).
121 virtual void set_buf(HeapWord* buf) {
122 _bottom = buf;
123 _top = _bottom;
124 _hard_end = _bottom + word_sz();
125 _end = _hard_end - AlignmentReserve;
126 assert(_end >= _top, "Negative buffer");
127 // In support of ergonomic sizing
128 _allocated += word_sz();
129 }
131 // Flush the stats supporting ergonomic sizing of PLAB's
132 // and retire the current buffer.
133 void flush_stats_and_retire(PLABStats* stats, bool end_of_gc, bool retain) {
134 // We flush the stats first in order to get a reading of
135 // unused space in the last buffer.
136 if (ResizePLAB) {
137 flush_stats(stats);
139 // Since we have flushed the stats we need to clear
140 // the _allocated and _wasted fields. Not doing so
141 // will artifically inflate the values in the stats
142 // to which we add them.
143 // The next time we flush these values, we will add
144 // what we have just flushed in addition to the size
145 // of the buffers allocated between now and then.
146 _allocated = 0;
147 _wasted = 0;
148 }
149 // Retire the last allocation buffer.
150 retire(end_of_gc, retain);
151 }
153 // Force future allocations to fail and queries for contains()
154 // to return false
155 void invalidate() {
156 assert(!_retained, "Shouldn't retain an invalidated buffer.");
157 _end = _hard_end;
158 _wasted += pointer_delta(_end, _top); // unused space
159 _top = _end; // force future allocations to fail
160 _bottom = _end; // force future contains() queries to return false
161 }
163 // Fills in the unallocated portion of the buffer with a garbage object.
164 // If "end_of_gc" is TRUE, is after the last use in the GC. IF "retain"
165 // is true, attempt to re-use the unused portion in the next GC.
166 virtual void retire(bool end_of_gc, bool retain);
168 void print() PRODUCT_RETURN;
169 };
171 // PLAB stats book-keeping
172 class PLABStats VALUE_OBJ_CLASS_SPEC {
173 size_t _allocated; // total allocated
174 size_t _wasted; // of which wasted (internal fragmentation)
175 size_t _unused; // Unused in last buffer
176 size_t _used; // derived = allocated - wasted - unused
177 size_t _desired_plab_sz;// output of filter (below), suitably trimmed and quantized
178 AdaptiveWeightedAverage
179 _filter; // integrator with decay
181 public:
182 PLABStats(size_t desired_plab_sz_, unsigned wt) :
183 _allocated(0),
184 _wasted(0),
185 _unused(0),
186 _used(0),
187 _desired_plab_sz(desired_plab_sz_),
188 _filter(wt)
189 { }
191 static const size_t min_size() {
192 return ParGCAllocBuffer::min_size();
193 }
195 static const size_t max_size() {
196 return ParGCAllocBuffer::max_size();
197 }
199 size_t desired_plab_sz() {
200 return _desired_plab_sz;
201 }
203 void adjust_desired_plab_sz(uint no_of_gc_workers);
204 // filter computation, latches output to
205 // _desired_plab_sz, clears sensor accumulators
207 void add_allocated(size_t v) {
208 Atomic::add_ptr(v, &_allocated);
209 }
211 void add_unused(size_t v) {
212 Atomic::add_ptr(v, &_unused);
213 }
215 void add_wasted(size_t v) {
216 Atomic::add_ptr(v, &_wasted);
217 }
218 };
220 class ParGCAllocBufferWithBOT: public ParGCAllocBuffer {
221 BlockOffsetArrayContigSpace _bt;
222 BlockOffsetSharedArray* _bsa;
223 HeapWord* _true_end; // end of the whole ParGCAllocBuffer
225 static const size_t ChunkSizeInWords;
226 static const size_t ChunkSizeInBytes;
227 HeapWord* allocate_slow(size_t word_sz);
229 void fill_region_with_block(MemRegion mr, bool contig);
231 public:
232 ParGCAllocBufferWithBOT(size_t word_sz, BlockOffsetSharedArray* bsa);
234 HeapWord* allocate(size_t word_sz) {
235 HeapWord* res = ParGCAllocBuffer::allocate(word_sz);
236 if (res != NULL) {
237 _bt.alloc_block(res, word_sz);
238 } else {
239 res = allocate_slow(word_sz);
240 }
241 return res;
242 }
244 void undo_allocation(HeapWord* obj, size_t word_sz);
246 virtual void set_buf(HeapWord* buf_start) {
247 ParGCAllocBuffer::set_buf(buf_start);
248 _true_end = _hard_end;
249 _bt.set_region(MemRegion(buf_start, word_sz()));
250 _bt.initialize_threshold();
251 }
253 virtual void retire(bool end_of_gc, bool retain);
255 MemRegion range() {
256 return MemRegion(_top, _true_end);
257 }
258 };
260 #endif // SHARE_VM_GC_IMPLEMENTATION_PARNEW_PARGCALLOCBUFFER_HPP