Wed, 23 Jan 2013 13:02:39 -0500
8005915: Unify SERIALGC and INCLUDE_ALTERNATE_GCS
Summary: Rename INCLUDE_ALTERNATE_GCS to INCLUDE_ALL_GCS and replace SERIALGC with INCLUDE_ALL_GCS.
Reviewed-by: coleenp, stefank
1 /*
2 * Copyright (c) 2001, 2012, 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 #include "precompiled.hpp"
26 #include "memory/freeBlockDictionary.hpp"
27 #include "memory/freeList.hpp"
28 #include "memory/metablock.hpp"
29 #include "memory/metachunk.hpp"
30 #include "memory/sharedHeap.hpp"
31 #include "runtime/globals.hpp"
32 #include "runtime/mutex.hpp"
33 #include "runtime/vmThread.hpp"
34 #include "utilities/macros.hpp"
36 #if INCLUDE_ALL_GCS
37 #include "gc_implementation/concurrentMarkSweep/freeChunk.hpp"
38 #endif // INCLUDE_ALL_GCS
40 // Free list. A FreeList is used to access a linked list of chunks
41 // of space in the heap. The head and tail are maintained so that
42 // items can be (as in the current implementation) added at the
43 // at the tail of the list and removed from the head of the list to
44 // maintain a FIFO queue.
46 template <class Chunk>
47 FreeList<Chunk>::FreeList() :
48 _head(NULL), _tail(NULL)
49 #ifdef ASSERT
50 , _protecting_lock(NULL)
51 #endif
52 {
53 _size = 0;
54 _count = 0;
55 }
57 template <class Chunk>
58 FreeList<Chunk>::FreeList(Chunk* fc) :
59 _head(fc), _tail(fc)
60 #ifdef ASSERT
61 , _protecting_lock(NULL)
62 #endif
63 {
64 _size = fc->size();
65 _count = 1;
66 }
68 template <class Chunk>
69 void FreeList<Chunk>::link_head(Chunk* v) {
70 assert_proper_lock_protection();
71 set_head(v);
72 // If this method is not used (just set the head instead),
73 // this check can be avoided.
74 if (v != NULL) {
75 v->link_prev(NULL);
76 }
77 }
81 template <class Chunk>
82 void FreeList<Chunk>::reset() {
83 // Don't set the _size to 0 because this method is
84 // used with a existing list that has a size but which has
85 // been emptied.
86 // Don't clear the _protecting_lock of an existing list.
87 set_count(0);
88 set_head(NULL);
89 set_tail(NULL);
90 }
92 template <class Chunk>
93 void FreeList<Chunk>::initialize() {
94 #ifdef ASSERT
95 // Needed early because it might be checked in other initializing code.
96 set_protecting_lock(NULL);
97 #endif
98 reset();
99 set_size(0);
100 }
102 template <class Chunk_t>
103 Chunk_t* FreeList<Chunk_t>::get_chunk_at_head() {
104 assert_proper_lock_protection();
105 assert(head() == NULL || head()->prev() == NULL, "list invariant");
106 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
107 Chunk_t* fc = head();
108 if (fc != NULL) {
109 Chunk_t* nextFC = fc->next();
110 if (nextFC != NULL) {
111 // The chunk fc being removed has a "next". Set the "next" to the
112 // "prev" of fc.
113 nextFC->link_prev(NULL);
114 } else { // removed tail of list
115 link_tail(NULL);
116 }
117 link_head(nextFC);
118 decrement_count();
119 }
120 assert(head() == NULL || head()->prev() == NULL, "list invariant");
121 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
122 return fc;
123 }
126 template <class Chunk>
127 void FreeList<Chunk>::getFirstNChunksFromList(size_t n, FreeList<Chunk>* fl) {
128 assert_proper_lock_protection();
129 assert(fl->count() == 0, "Precondition");
130 if (count() > 0) {
131 int k = 1;
132 fl->set_head(head()); n--;
133 Chunk* tl = head();
134 while (tl->next() != NULL && n > 0) {
135 tl = tl->next(); n--; k++;
136 }
137 assert(tl != NULL, "Loop Inv.");
139 // First, fix up the list we took from.
140 Chunk* new_head = tl->next();
141 set_head(new_head);
142 set_count(count() - k);
143 if (new_head == NULL) {
144 set_tail(NULL);
145 } else {
146 new_head->link_prev(NULL);
147 }
148 // Now we can fix up the tail.
149 tl->link_next(NULL);
150 // And return the result.
151 fl->set_tail(tl);
152 fl->set_count(k);
153 }
154 }
156 // Remove this chunk from the list
157 template <class Chunk>
158 void FreeList<Chunk>::remove_chunk(Chunk*fc) {
159 assert_proper_lock_protection();
160 assert(head() != NULL, "Remove from empty list");
161 assert(fc != NULL, "Remove a NULL chunk");
162 assert(size() == fc->size(), "Wrong list");
163 assert(head() == NULL || head()->prev() == NULL, "list invariant");
164 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
166 Chunk* prevFC = fc->prev();
167 Chunk* nextFC = fc->next();
168 if (nextFC != NULL) {
169 // The chunk fc being removed has a "next". Set the "next" to the
170 // "prev" of fc.
171 nextFC->link_prev(prevFC);
172 } else { // removed tail of list
173 link_tail(prevFC);
174 }
175 if (prevFC == NULL) { // removed head of list
176 link_head(nextFC);
177 assert(nextFC == NULL || nextFC->prev() == NULL,
178 "Prev of head should be NULL");
179 } else {
180 prevFC->link_next(nextFC);
181 assert(tail() != prevFC || prevFC->next() == NULL,
182 "Next of tail should be NULL");
183 }
184 decrement_count();
185 assert(((head() == NULL) + (tail() == NULL) + (count() == 0)) % 3 == 0,
186 "H/T/C Inconsistency");
187 // clear next and prev fields of fc, debug only
188 NOT_PRODUCT(
189 fc->link_prev(NULL);
190 fc->link_next(NULL);
191 )
192 assert(fc->is_free(), "Should still be a free chunk");
193 assert(head() == NULL || head()->prev() == NULL, "list invariant");
194 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
195 assert(head() == NULL || head()->size() == size(), "wrong item on list");
196 assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
197 }
199 // Add this chunk at the head of the list.
200 template <class Chunk>
201 void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk, bool record_return) {
202 assert_proper_lock_protection();
203 assert(chunk != NULL, "insert a NULL chunk");
204 assert(size() == chunk->size(), "Wrong size");
205 assert(head() == NULL || head()->prev() == NULL, "list invariant");
206 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
208 Chunk* oldHead = head();
209 assert(chunk != oldHead, "double insertion");
210 chunk->link_after(oldHead);
211 link_head(chunk);
212 if (oldHead == NULL) { // only chunk in list
213 assert(tail() == NULL, "inconsistent FreeList");
214 link_tail(chunk);
215 }
216 increment_count(); // of # of chunks in list
217 assert(head() == NULL || head()->prev() == NULL, "list invariant");
218 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
219 assert(head() == NULL || head()->size() == size(), "wrong item on list");
220 assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
221 }
223 template <class Chunk>
224 void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk) {
225 assert_proper_lock_protection();
226 return_chunk_at_head(chunk, true);
227 }
229 // Add this chunk at the tail of the list.
230 template <class Chunk>
231 void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk, bool record_return) {
232 assert_proper_lock_protection();
233 assert(head() == NULL || head()->prev() == NULL, "list invariant");
234 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
235 assert(chunk != NULL, "insert a NULL chunk");
236 assert(size() == chunk->size(), "wrong size");
238 Chunk* oldTail = tail();
239 assert(chunk != oldTail, "double insertion");
240 if (oldTail != NULL) {
241 oldTail->link_after(chunk);
242 } else { // only chunk in list
243 assert(head() == NULL, "inconsistent FreeList");
244 link_head(chunk);
245 }
246 link_tail(chunk);
247 increment_count(); // of # of chunks in list
248 assert(head() == NULL || head()->prev() == NULL, "list invariant");
249 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
250 assert(head() == NULL || head()->size() == size(), "wrong item on list");
251 assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
252 }
254 template <class Chunk>
255 void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk) {
256 return_chunk_at_tail(chunk, true);
257 }
259 template <class Chunk>
260 void FreeList<Chunk>::prepend(FreeList<Chunk>* fl) {
261 assert_proper_lock_protection();
262 if (fl->count() > 0) {
263 if (count() == 0) {
264 set_head(fl->head());
265 set_tail(fl->tail());
266 set_count(fl->count());
267 } else {
268 // Both are non-empty.
269 Chunk* fl_tail = fl->tail();
270 Chunk* this_head = head();
271 assert(fl_tail->next() == NULL, "Well-formedness of fl");
272 fl_tail->link_next(this_head);
273 this_head->link_prev(fl_tail);
274 set_head(fl->head());
275 set_count(count() + fl->count());
276 }
277 fl->set_head(NULL);
278 fl->set_tail(NULL);
279 fl->set_count(0);
280 }
281 }
283 // verify_chunk_in_free_lists() is used to verify that an item is in this free list.
284 // It is used as a debugging aid.
285 template <class Chunk>
286 bool FreeList<Chunk>::verify_chunk_in_free_list(Chunk* fc) const {
287 // This is an internal consistency check, not part of the check that the
288 // chunk is in the free lists.
289 guarantee(fc->size() == size(), "Wrong list is being searched");
290 Chunk* curFC = head();
291 while (curFC) {
292 // This is an internal consistency check.
293 guarantee(size() == curFC->size(), "Chunk is in wrong list.");
294 if (fc == curFC) {
295 return true;
296 }
297 curFC = curFC->next();
298 }
299 return false;
300 }
302 #ifndef PRODUCT
303 template <class Chunk>
304 void FreeList<Chunk>::assert_proper_lock_protection_work() const {
305 assert(protecting_lock() != NULL, "Don't call this directly");
306 assert(ParallelGCThreads > 0, "Don't call this directly");
307 Thread* thr = Thread::current();
308 if (thr->is_VM_thread() || thr->is_ConcurrentGC_thread()) {
309 // assert that we are holding the freelist lock
310 } else if (thr->is_GC_task_thread()) {
311 assert(protecting_lock()->owned_by_self(), "FreeList RACE DETECTED");
312 } else if (thr->is_Java_thread()) {
313 assert(!SafepointSynchronize::is_at_safepoint(), "Should not be executing");
314 } else {
315 ShouldNotReachHere(); // unaccounted thread type?
316 }
317 }
318 #endif
320 // Print the "label line" for free list stats.
321 template <class Chunk>
322 void FreeList<Chunk>::print_labels_on(outputStream* st, const char* c) {
323 st->print("%16s\t", c);
324 st->print("%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t"
325 "%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t" "\n",
326 "bfrsurp", "surplus", "desired", "prvSwep", "bfrSwep",
327 "count", "cBirths", "cDeaths", "sBirths", "sDeaths");
328 }
330 // Print the AllocationStats for the given free list. If the second argument
331 // to the call is a non-null string, it is printed in the first column;
332 // otherwise, if the argument is null (the default), then the size of the
333 // (free list) block is printed in the first column.
334 template <class Chunk_t>
335 void FreeList<Chunk_t>::print_on(outputStream* st, const char* c) const {
336 if (c != NULL) {
337 st->print("%16s", c);
338 } else {
339 st->print(SIZE_FORMAT_W(16), size());
340 }
341 }
343 template class FreeList<Metablock>;
344 template class FreeList<Metachunk>;
345 #if INCLUDE_ALL_GCS
346 template class FreeList<FreeChunk>;
347 #endif // INCLUDE_ALL_GCS