Print this page
Split |
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/gc_implementation/concurrentMarkSweep/freeList.cpp
+++ new/src/share/vm/gc_implementation/concurrentMarkSweep/freeList.cpp
1 1 /*
2 2 * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 21 * have any questions.
22 22 *
23 23 */
24 24
25 25 # include "incls/_precompiled.incl"
26 26 # include "incls/_freeList.cpp.incl"
27 27
28 28 // Free list. A FreeList is used to access a linked list of chunks
29 29 // of space in the heap. The head and tail are maintained so that
30 30 // items can be (as in the current implementation) added at the
31 31 // at the tail of the list and removed from the head of the list to
32 32 // maintain a FIFO queue.
33 33
34 34 FreeList::FreeList() :
35 35 _head(NULL), _tail(NULL)
36 36 #ifdef ASSERT
37 37 , _protecting_lock(NULL)
38 38 #endif
39 39 {
40 40 _size = 0;
41 41 _count = 0;
42 42 _hint = 0;
43 43 init_statistics();
44 44 }
45 45
46 46 FreeList::FreeList(FreeChunk* fc) :
47 47 _head(fc), _tail(fc)
48 48 #ifdef ASSERT
49 49 , _protecting_lock(NULL)
50 50 #endif
51 51 {
52 52 _size = fc->size();
53 53 _count = 1;
54 54 _hint = 0;
55 55 init_statistics();
56 56 #ifndef PRODUCT
57 57 _allocation_stats.set_returnedBytes(size() * HeapWordSize);
58 58 #endif
59 59 }
60 60
61 61 FreeList::FreeList(HeapWord* addr, size_t size) :
62 62 _head((FreeChunk*) addr), _tail((FreeChunk*) addr)
63 63 #ifdef ASSERT
64 64 , _protecting_lock(NULL)
65 65 #endif
66 66 {
67 67 assert(size > sizeof(FreeChunk), "size is too small");
68 68 head()->setSize(size);
69 69 _size = size;
70 70 _count = 1;
71 71 init_statistics();
72 72 #ifndef PRODUCT
73 73 _allocation_stats.set_returnedBytes(_size * HeapWordSize);
74 74 #endif
75 75 }
76 76
77 77 void FreeList::reset(size_t hint) {
78 78 set_count(0);
79 79 set_head(NULL);
80 80 set_tail(NULL);
81 81 set_hint(hint);
82 82 }
83 83
84 84 void FreeList::init_statistics() {
85 85 _allocation_stats.initialize();
86 86 }
87 87
88 88 FreeChunk* FreeList::getChunkAtHead() {
89 89 assert_proper_lock_protection();
90 90 assert(head() == NULL || head()->prev() == NULL, "list invariant");
91 91 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
92 92 FreeChunk* fc = head();
93 93 if (fc != NULL) {
94 94 FreeChunk* nextFC = fc->next();
95 95 if (nextFC != NULL) {
96 96 // The chunk fc being removed has a "next". Set the "next" to the
97 97 // "prev" of fc.
98 98 nextFC->linkPrev(NULL);
99 99 } else { // removed tail of list
100 100 link_tail(NULL);
101 101 }
102 102 link_head(nextFC);
103 103 decrement_count();
104 104 }
105 105 assert(head() == NULL || head()->prev() == NULL, "list invariant");
106 106 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
107 107 return fc;
108 108 }
109 109
110 110
111 111 void FreeList::getFirstNChunksFromList(size_t n, FreeList* fl) {
112 112 assert_proper_lock_protection();
113 113 assert(fl->count() == 0, "Precondition");
114 114 if (count() > 0) {
115 115 int k = 1;
116 116 fl->set_head(head()); n--;
117 117 FreeChunk* tl = head();
118 118 while (tl->next() != NULL && n > 0) {
119 119 tl = tl->next(); n--; k++;
120 120 }
121 121 assert(tl != NULL, "Loop Inv.");
122 122
123 123 // First, fix up the list we took from.
124 124 FreeChunk* new_head = tl->next();
125 125 set_head(new_head);
126 126 set_count(count() - k);
127 127 if (new_head == NULL) {
128 128 set_tail(NULL);
129 129 } else {
130 130 new_head->linkPrev(NULL);
131 131 }
132 132 // Now we can fix up the tail.
133 133 tl->linkNext(NULL);
134 134 // And return the result.
135 135 fl->set_tail(tl);
136 136 fl->set_count(k);
137 137 }
138 138 }
139 139
140 140 // Remove this chunk from the list
141 141 void FreeList::removeChunk(FreeChunk*fc) {
142 142 assert_proper_lock_protection();
143 143 assert(head() != NULL, "Remove from empty list");
144 144 assert(fc != NULL, "Remove a NULL chunk");
145 145 assert(size() == fc->size(), "Wrong list");
146 146 assert(head() == NULL || head()->prev() == NULL, "list invariant");
147 147 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
148 148
149 149 FreeChunk* prevFC = fc->prev();
150 150 FreeChunk* nextFC = fc->next();
151 151 if (nextFC != NULL) {
152 152 // The chunk fc being removed has a "next". Set the "next" to the
153 153 // "prev" of fc.
154 154 nextFC->linkPrev(prevFC);
155 155 } else { // removed tail of list
156 156 link_tail(prevFC);
157 157 }
158 158 if (prevFC == NULL) { // removed head of list
159 159 link_head(nextFC);
160 160 assert(nextFC == NULL || nextFC->prev() == NULL,
161 161 "Prev of head should be NULL");
162 162 } else {
163 163 prevFC->linkNext(nextFC);
164 164 assert(tail() != prevFC || prevFC->next() == NULL,
165 165 "Next of tail should be NULL");
166 166 }
167 167 decrement_count();
168 168 #define TRAP_CODE 1
169 169 #if TRAP_CODE
170 170 if (head() == NULL) {
171 171 guarantee(tail() == NULL, "INVARIANT");
172 172 guarantee(count() == 0, "INVARIANT");
173 173 }
174 174 #endif
175 175 // clear next and prev fields of fc, debug only
176 176 NOT_PRODUCT(
177 177 fc->linkPrev(NULL);
178 178 fc->linkNext(NULL);
179 179 )
180 180 assert(fc->isFree(), "Should still be a free chunk");
181 181 assert(head() == NULL || head()->prev() == NULL, "list invariant");
182 182 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
183 183 assert(head() == NULL || head()->size() == size(), "wrong item on list");
184 184 assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
185 185 }
186 186
187 187 // Add this chunk at the head of the list.
188 188 void FreeList::returnChunkAtHead(FreeChunk* chunk, bool record_return) {
189 189 assert_proper_lock_protection();
190 190 assert(chunk != NULL, "insert a NULL chunk");
191 191 assert(size() == chunk->size(), "Wrong size");
192 192 assert(head() == NULL || head()->prev() == NULL, "list invariant");
193 193 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
194 194
195 195 FreeChunk* oldHead = head();
196 196 assert(chunk != oldHead, "double insertion");
197 197 chunk->linkAfter(oldHead);
198 198 link_head(chunk);
199 199 if (oldHead == NULL) { // only chunk in list
200 200 assert(tail() == NULL, "inconsistent FreeList");
201 201 link_tail(chunk);
202 202 }
203 203 increment_count(); // of # of chunks in list
204 204 DEBUG_ONLY(
205 205 if (record_return) {
206 206 increment_returnedBytes_by(size()*HeapWordSize);
207 207 }
208 208 )
209 209 assert(head() == NULL || head()->prev() == NULL, "list invariant");
210 210 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
211 211 assert(head() == NULL || head()->size() == size(), "wrong item on list");
212 212 assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
213 213 }
214 214
215 215 void FreeList::returnChunkAtHead(FreeChunk* chunk) {
216 216 assert_proper_lock_protection();
217 217 returnChunkAtHead(chunk, true);
218 218 }
219 219
220 220 // Add this chunk at the tail of the list.
221 221 void FreeList::returnChunkAtTail(FreeChunk* chunk, bool record_return) {
222 222 assert_proper_lock_protection();
223 223 assert(head() == NULL || head()->prev() == NULL, "list invariant");
224 224 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
225 225 assert(chunk != NULL, "insert a NULL chunk");
226 226 assert(size() == chunk->size(), "wrong size");
227 227
228 228 FreeChunk* oldTail = tail();
229 229 assert(chunk != oldTail, "double insertion");
230 230 if (oldTail != NULL) {
231 231 oldTail->linkAfter(chunk);
232 232 } else { // only chunk in list
233 233 assert(head() == NULL, "inconsistent FreeList");
234 234 link_head(chunk);
235 235 }
236 236 link_tail(chunk);
237 237 increment_count(); // of # of chunks in list
238 238 DEBUG_ONLY(
239 239 if (record_return) {
240 240 increment_returnedBytes_by(size()*HeapWordSize);
241 241 }
242 242 )
243 243 assert(head() == NULL || head()->prev() == NULL, "list invariant");
244 244 assert(tail() == NULL || tail()->next() == NULL, "list invariant");
245 245 assert(head() == NULL || head()->size() == size(), "wrong item on list");
246 246 assert(tail() == NULL || tail()->size() == size(), "wrong item on list");
247 247 }
248 248
249 249 void FreeList::returnChunkAtTail(FreeChunk* chunk) {
250 250 returnChunkAtTail(chunk, true);
251 251 }
252 252
253 253 void FreeList::prepend(FreeList* fl) {
254 254 assert_proper_lock_protection();
255 255 if (fl->count() > 0) {
256 256 if (count() == 0) {
257 257 set_head(fl->head());
258 258 set_tail(fl->tail());
259 259 set_count(fl->count());
260 260 } else {
261 261 // Both are non-empty.
262 262 FreeChunk* fl_tail = fl->tail();
263 263 FreeChunk* this_head = head();
264 264 assert(fl_tail->next() == NULL, "Well-formedness of fl");
265 265 fl_tail->linkNext(this_head);
266 266 this_head->linkPrev(fl_tail);
267 267 set_head(fl->head());
268 268 set_count(count() + fl->count());
269 269 }
270 270 fl->set_head(NULL);
271 271 fl->set_tail(NULL);
272 272 fl->set_count(0);
273 273 }
274 274 }
275 275
276 276 // verifyChunkInFreeLists() is used to verify that an item is in this free list.
277 277 // It is used as a debugging aid.
278 278 bool FreeList::verifyChunkInFreeLists(FreeChunk* fc) const {
279 279 // This is an internal consistency check, not part of the check that the
280 280 // chunk is in the free lists.
281 281 guarantee(fc->size() == size(), "Wrong list is being searched");
282 282 FreeChunk* curFC = head();
283 283 while (curFC) {
284 284 // This is an internal consistency check.
285 285 guarantee(size() == curFC->size(), "Chunk is in wrong list.");
286 286 if (fc == curFC) {
287 287 return true;
288 288 }
289 289 curFC = curFC->next();
290 290 }
291 291 return false;
292 292 }
293 293
294 294 #ifndef PRODUCT
↓ open down ↓ |
294 lines elided |
↑ open up ↑ |
295 295 void FreeList::assert_proper_lock_protection_work() const {
296 296 #ifdef ASSERT
297 297 if (_protecting_lock != NULL &&
298 298 SharedHeap::heap()->n_par_threads() > 0) {
299 299 // Should become an assert.
300 300 guarantee(_protecting_lock->owned_by_self(), "FreeList RACE DETECTED");
301 301 }
302 302 #endif
303 303 }
304 304 #endif
305 +
306 +// Print the "label line" for free list stats.
307 +void FreeList::print_labels_on(outputStream* st, const char* c) {
308 + st->print("%16s\t", c);
309 + st->print("%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t"
310 + "%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t" "\n",
311 + "bfrsurp", "surplus", "desired", "prvSwep", "bfrSwep",
312 + "count", "cBirths", "cDeaths", "sBirths", "sDeaths");
313 +}
314 +
315 +// Print the AllocationStats for the given free list. If the second argument
316 +// to the call is a non-null string, it is printed in the first column;
317 +// otherwise, if the argument is null (the default), then the size of the
318 +// (free list) block is printed in the first column.
319 +void FreeList::print_on(outputStream* st, const char* c) const {
320 + if (c != NULL) {
321 + st->print("%16s", c);
322 + } else {
323 + st->print(SIZE_FORMAT_W(16), size());
324 + }
325 + st->print("\t"
326 + SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t"
327 + SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\t" SSIZE_FORMAT_W(14) "\n",
328 + bfrSurp(), surplus(), desired(), prevSweep(), beforeSweep(),
329 + count(), coalBirths(), coalDeaths(), splitBirths(), splitDeaths());
330 +}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX