Print this page
rev 3463 : 7114678: G1: various small fixes, code cleanup, and refactoring
Summary: Various cleanups as a prelude to introducing iterators for HeapRegions.
Reviewed-by: johnc
Contributed-by: tonyp
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/gc_implementation/g1/heapRegionSet.cpp
+++ new/src/share/vm/gc_implementation/g1/heapRegionSet.cpp
1 1 /*
2 2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 *
23 23 */
24 24
25 25 #include "precompiled.hpp"
26 26 #include "gc_implementation/g1/heapRegionSet.inline.hpp"
27 27
↓ open down ↓ |
27 lines elided |
↑ open up ↑ |
28 28 uint HeapRegionSetBase::_unrealistically_long_length = 0;
29 29 HRSPhase HeapRegionSetBase::_phase = HRSPhaseNone;
30 30
31 31 //////////////////// HeapRegionSetBase ////////////////////
32 32
33 33 void HeapRegionSetBase::set_unrealistically_long_length(uint len) {
34 34 guarantee(_unrealistically_long_length == 0, "should only be set once");
35 35 _unrealistically_long_length = len;
36 36 }
37 37
38 -uint HeapRegionSetBase::calculate_region_num(HeapRegion* hr) {
39 - assert(hr->startsHumongous(), "pre-condition");
40 - assert(hr->capacity() % HeapRegion::GrainBytes == 0, "invariant");
41 - uint region_num = (uint) (hr->capacity() >> HeapRegion::LogOfHRGrainBytes);
42 - assert(region_num > 0, "sanity");
43 - return region_num;
44 -}
45 -
46 38 void HeapRegionSetBase::fill_in_ext_msg(hrs_ext_msg* msg, const char* message) {
47 39 msg->append("[%s] %s ln: %u rn: %u cy: "SIZE_FORMAT" ud: "SIZE_FORMAT,
48 40 name(), message, length(), region_num(),
49 41 total_capacity_bytes(), total_used_bytes());
50 42 fill_in_ext_msg_extra(msg);
51 43 }
52 44
53 45 bool HeapRegionSetBase::verify_region(HeapRegion* hr,
54 46 HeapRegionSetBase* expected_containing_set) {
55 47 const char* error_message = NULL;
56 48
57 49 if (!regions_humongous()) {
58 50 if (hr->isHumongous()) {
59 51 error_message = "the region should not be humongous";
60 52 }
61 53 } else {
62 54 if (!hr->isHumongous() || !hr->startsHumongous()) {
63 55 error_message = "the region should be 'starts humongous'";
64 56 }
65 57 }
66 58
67 59 if (!regions_empty()) {
68 60 if (hr->is_empty()) {
69 61 error_message = "the region should not be empty";
70 62 }
71 63 } else {
72 64 if (!hr->is_empty()) {
73 65 error_message = "the region should be empty";
74 66 }
75 67 }
76 68
77 69 #ifdef ASSERT
78 70 // The _containing_set field is only available when ASSERT is defined.
79 71 if (hr->containing_set() != expected_containing_set) {
80 72 error_message = "inconsistent containing set found";
81 73 }
82 74 #endif // ASSERT
83 75
84 76 const char* extra_error_message = verify_region_extra(hr);
85 77 if (extra_error_message != NULL) {
86 78 error_message = extra_error_message;
87 79 }
88 80
89 81 if (error_message != NULL) {
90 82 outputStream* out = tty;
91 83 out->cr();
92 84 out->print_cr("## [%s] %s", name(), error_message);
93 85 out->print_cr("## Offending Region: "PTR_FORMAT, hr);
94 86 out->print_cr(" "HR_FORMAT, HR_FORMAT_PARAMS(hr));
95 87 #ifdef ASSERT
96 88 out->print_cr(" containing set: "PTR_FORMAT, hr->containing_set());
97 89 #endif // ASSERT
98 90 out->print_cr("## Offending Region Set: "PTR_FORMAT, this);
99 91 print_on(out);
100 92 return false;
101 93 } else {
102 94 return true;
103 95 }
104 96 }
105 97
106 98 void HeapRegionSetBase::verify() {
107 99 // It's important that we also observe the MT safety protocol even
108 100 // for the verification calls. If we do verification without the
109 101 // appropriate locks and the set changes underneath our feet
110 102 // verification might fail and send us on a wild goose chase.
111 103 hrs_assert_mt_safety_ok(this);
112 104
113 105 guarantee(( is_empty() && length() == 0 && region_num() == 0 &&
114 106 total_used_bytes() == 0 && total_capacity_bytes() == 0) ||
115 107 (!is_empty() && length() >= 0 && region_num() >= 0 &&
116 108 total_used_bytes() >= 0 && total_capacity_bytes() >= 0),
117 109 hrs_ext_msg(this, "invariant"));
118 110
119 111 guarantee((!regions_humongous() && region_num() == length()) ||
120 112 ( regions_humongous() && region_num() >= length()),
121 113 hrs_ext_msg(this, "invariant"));
122 114
123 115 guarantee(!regions_empty() || total_used_bytes() == 0,
124 116 hrs_ext_msg(this, "invariant"));
125 117
126 118 guarantee(total_used_bytes() <= total_capacity_bytes(),
127 119 hrs_ext_msg(this, "invariant"));
128 120 }
129 121
130 122 void HeapRegionSetBase::verify_start() {
131 123 // See comment in verify() about MT safety and verification.
132 124 hrs_assert_mt_safety_ok(this);
133 125 assert(!_verify_in_progress,
134 126 hrs_ext_msg(this, "verification should not be in progress"));
135 127
136 128 // Do the basic verification first before we do the checks over the regions.
137 129 HeapRegionSetBase::verify();
138 130
139 131 _calc_length = 0;
140 132 _calc_region_num = 0;
141 133 _calc_total_capacity_bytes = 0;
142 134 _calc_total_used_bytes = 0;
143 135 _verify_in_progress = true;
144 136 }
↓ open down ↓ |
89 lines elided |
↑ open up ↑ |
145 137
146 138 void HeapRegionSetBase::verify_next_region(HeapRegion* hr) {
147 139 // See comment in verify() about MT safety and verification.
148 140 hrs_assert_mt_safety_ok(this);
149 141 assert(_verify_in_progress,
150 142 hrs_ext_msg(this, "verification should be in progress"));
151 143
152 144 guarantee(verify_region(hr, this), hrs_ext_msg(this, "region verification"));
153 145
154 146 _calc_length += 1;
155 - if (!hr->isHumongous()) {
156 - _calc_region_num += 1;
157 - } else {
158 - _calc_region_num += calculate_region_num(hr);
159 - }
147 + _calc_region_num += hr->region_num();
160 148 _calc_total_capacity_bytes += hr->capacity();
161 149 _calc_total_used_bytes += hr->used();
162 150 }
163 151
164 152 void HeapRegionSetBase::verify_end() {
165 153 // See comment in verify() about MT safety and verification.
166 154 hrs_assert_mt_safety_ok(this);
167 155 assert(_verify_in_progress,
168 156 hrs_ext_msg(this, "verification should be in progress"));
169 157
170 158 guarantee(length() == _calc_length,
171 159 hrs_err_msg("[%s] length: %u should be == calc length: %u",
172 160 name(), length(), _calc_length));
173 161
174 162 guarantee(region_num() == _calc_region_num,
175 163 hrs_err_msg("[%s] region num: %u should be == calc region num: %u",
176 164 name(), region_num(), _calc_region_num));
177 165
178 166 guarantee(total_capacity_bytes() == _calc_total_capacity_bytes,
179 167 hrs_err_msg("[%s] capacity bytes: "SIZE_FORMAT" should be == "
180 168 "calc capacity bytes: "SIZE_FORMAT,
181 169 name(),
182 170 total_capacity_bytes(), _calc_total_capacity_bytes));
183 171
184 172 guarantee(total_used_bytes() == _calc_total_used_bytes,
185 173 hrs_err_msg("[%s] used bytes: "SIZE_FORMAT" should be == "
186 174 "calc used bytes: "SIZE_FORMAT,
187 175 name(), total_used_bytes(), _calc_total_used_bytes));
188 176
189 177 _verify_in_progress = false;
190 178 }
191 179
192 180 void HeapRegionSetBase::clear_phase() {
193 181 assert(_phase != HRSPhaseNone, "pre-condition");
194 182 _phase = HRSPhaseNone;
195 183 }
196 184
197 185 void HeapRegionSetBase::set_phase(HRSPhase phase) {
198 186 assert(_phase == HRSPhaseNone, "pre-condition");
199 187 assert(phase != HRSPhaseNone, "pre-condition");
200 188 _phase = phase;
201 189 }
202 190
203 191 void HeapRegionSetBase::print_on(outputStream* out, bool print_contents) {
204 192 out->cr();
205 193 out->print_cr("Set: %s ("PTR_FORMAT")", name(), this);
206 194 out->print_cr(" Region Assumptions");
207 195 out->print_cr(" humongous : %s", BOOL_TO_STR(regions_humongous()));
208 196 out->print_cr(" empty : %s", BOOL_TO_STR(regions_empty()));
209 197 out->print_cr(" Attributes");
210 198 out->print_cr(" length : %14u", length());
211 199 out->print_cr(" region num : %14u", region_num());
212 200 out->print_cr(" total capacity : "SIZE_FORMAT_W(14)" bytes",
213 201 total_capacity_bytes());
214 202 out->print_cr(" total used : "SIZE_FORMAT_W(14)" bytes",
215 203 total_used_bytes());
216 204 }
217 205
218 206 void HeapRegionSetBase::clear() {
219 207 _length = 0;
220 208 _region_num = 0;
221 209 _total_used_bytes = 0;
222 210 }
223 211
224 212 HeapRegionSetBase::HeapRegionSetBase(const char* name)
225 213 : _name(name), _verify_in_progress(false),
226 214 _calc_length(0), _calc_region_num(0),
227 215 _calc_total_capacity_bytes(0), _calc_total_used_bytes(0) { }
228 216
229 217 //////////////////// HeapRegionSet ////////////////////
230 218
231 219 void HeapRegionSet::update_from_proxy(HeapRegionSet* proxy_set) {
232 220 hrs_assert_mt_safety_ok(this);
233 221 hrs_assert_mt_safety_ok(proxy_set);
234 222 hrs_assert_sets_match(this, proxy_set);
235 223
236 224 verify_optional();
237 225 proxy_set->verify_optional();
238 226
239 227 if (proxy_set->is_empty()) return;
240 228
241 229 assert(proxy_set->length() <= _length,
242 230 hrs_err_msg("[%s] proxy set length: %u should be <= length: %u",
243 231 name(), proxy_set->length(), _length));
244 232 _length -= proxy_set->length();
245 233
246 234 assert(proxy_set->region_num() <= _region_num,
247 235 hrs_err_msg("[%s] proxy set region num: %u should be <= region num: %u",
248 236 name(), proxy_set->region_num(), _region_num));
249 237 _region_num -= proxy_set->region_num();
250 238
251 239 assert(proxy_set->total_used_bytes() <= _total_used_bytes,
252 240 hrs_err_msg("[%s] proxy set used bytes: "SIZE_FORMAT" "
253 241 "should be <= used bytes: "SIZE_FORMAT,
254 242 name(), proxy_set->total_used_bytes(),
255 243 _total_used_bytes));
256 244 _total_used_bytes -= proxy_set->total_used_bytes();
257 245
258 246 proxy_set->clear();
259 247
260 248 verify_optional();
261 249 proxy_set->verify_optional();
262 250 }
263 251
264 252 //////////////////// HeapRegionLinkedList ////////////////////
265 253
266 254 void HeapRegionLinkedList::fill_in_ext_msg_extra(hrs_ext_msg* msg) {
267 255 msg->append(" hd: "PTR_FORMAT" tl: "PTR_FORMAT, head(), tail());
268 256 }
269 257
270 258 void HeapRegionLinkedList::add_as_head(HeapRegionLinkedList* from_list) {
271 259 hrs_assert_mt_safety_ok(this);
272 260 hrs_assert_mt_safety_ok(from_list);
273 261
274 262 verify_optional();
275 263 from_list->verify_optional();
276 264
277 265 if (from_list->is_empty()) return;
278 266
279 267 #ifdef ASSERT
280 268 HeapRegionLinkedListIterator iter(from_list);
281 269 while (iter.more_available()) {
282 270 HeapRegion* hr = iter.get_next();
283 271 // In set_containing_set() we check that we either set the value
284 272 // from NULL to non-NULL or vice versa to catch bugs. So, we have
285 273 // to NULL it first before setting it to the value.
286 274 hr->set_containing_set(NULL);
287 275 hr->set_containing_set(this);
288 276 }
289 277 #endif // ASSERT
290 278
291 279 if (_head != NULL) {
292 280 assert(length() > 0 && _tail != NULL, hrs_ext_msg(this, "invariant"));
293 281 from_list->_tail->set_next(_head);
294 282 } else {
295 283 assert(length() == 0 && _head == NULL, hrs_ext_msg(this, "invariant"));
296 284 _tail = from_list->_tail;
297 285 }
298 286 _head = from_list->_head;
299 287
300 288 _length += from_list->length();
301 289 _region_num += from_list->region_num();
302 290 _total_used_bytes += from_list->total_used_bytes();
303 291 from_list->clear();
304 292
305 293 verify_optional();
306 294 from_list->verify_optional();
307 295 }
308 296
309 297 void HeapRegionLinkedList::add_as_tail(HeapRegionLinkedList* from_list) {
310 298 hrs_assert_mt_safety_ok(this);
311 299 hrs_assert_mt_safety_ok(from_list);
312 300
313 301 verify_optional();
314 302 from_list->verify_optional();
315 303
316 304 if (from_list->is_empty()) return;
317 305
318 306 #ifdef ASSERT
319 307 HeapRegionLinkedListIterator iter(from_list);
320 308 while (iter.more_available()) {
321 309 HeapRegion* hr = iter.get_next();
322 310 // In set_containing_set() we check that we either set the value
323 311 // from NULL to non-NULL or vice versa to catch bugs. So, we have
324 312 // to NULL it first before setting it to the value.
325 313 hr->set_containing_set(NULL);
326 314 hr->set_containing_set(this);
327 315 }
328 316 #endif // ASSERT
329 317
330 318 if (_tail != NULL) {
331 319 assert(length() > 0 && _head != NULL, hrs_ext_msg(this, "invariant"));
332 320 _tail->set_next(from_list->_head);
333 321 } else {
334 322 assert(length() == 0 && _head == NULL, hrs_ext_msg(this, "invariant"));
335 323 _head = from_list->_head;
336 324 }
337 325 _tail = from_list->_tail;
338 326
339 327 _length += from_list->length();
340 328 _region_num += from_list->region_num();
341 329 _total_used_bytes += from_list->total_used_bytes();
342 330 from_list->clear();
343 331
344 332 verify_optional();
345 333 from_list->verify_optional();
346 334 }
347 335
348 336 void HeapRegionLinkedList::remove_all() {
349 337 hrs_assert_mt_safety_ok(this);
350 338 verify_optional();
351 339
352 340 HeapRegion* curr = _head;
353 341 while (curr != NULL) {
354 342 hrs_assert_region_ok(this, curr, this);
355 343
356 344 HeapRegion* next = curr->next();
357 345 curr->set_next(NULL);
358 346 curr->set_containing_set(NULL);
359 347 curr = next;
360 348 }
361 349 clear();
362 350
363 351 verify_optional();
364 352 }
365 353
366 354 void HeapRegionLinkedList::remove_all_pending(uint target_count) {
367 355 hrs_assert_mt_safety_ok(this);
368 356 assert(target_count > 1, hrs_ext_msg(this, "pre-condition"));
369 357 assert(!is_empty(), hrs_ext_msg(this, "pre-condition"));
370 358
371 359 verify_optional();
372 360 DEBUG_ONLY(uint old_length = length();)
373 361
374 362 HeapRegion* curr = _head;
375 363 HeapRegion* prev = NULL;
376 364 uint count = 0;
377 365 while (curr != NULL) {
378 366 hrs_assert_region_ok(this, curr, this);
379 367 HeapRegion* next = curr->next();
380 368
381 369 if (curr->pending_removal()) {
382 370 assert(count < target_count,
383 371 hrs_err_msg("[%s] should not come across more regions "
384 372 "pending for removal than target_count: %u",
385 373 name(), target_count));
386 374
387 375 if (prev == NULL) {
388 376 assert(_head == curr, hrs_ext_msg(this, "invariant"));
389 377 _head = next;
390 378 } else {
391 379 assert(_head != curr, hrs_ext_msg(this, "invariant"));
392 380 prev->set_next(next);
393 381 }
394 382 if (next == NULL) {
395 383 assert(_tail == curr, hrs_ext_msg(this, "invariant"));
396 384 _tail = prev;
397 385 } else {
398 386 assert(_tail != curr, hrs_ext_msg(this, "invariant"));
399 387 }
400 388
401 389 curr->set_next(NULL);
402 390 remove_internal(curr);
403 391 curr->set_pending_removal(false);
404 392
405 393 count += 1;
406 394
407 395 // If we have come across the target number of regions we can
408 396 // just bail out. However, for debugging purposes, we can just
409 397 // carry on iterating to make sure there are not more regions
410 398 // tagged with pending removal.
411 399 DEBUG_ONLY(if (count == target_count) break;)
412 400 } else {
413 401 prev = curr;
414 402 }
415 403 curr = next;
416 404 }
417 405
418 406 assert(count == target_count,
419 407 hrs_err_msg("[%s] count: %u should be == target_count: %u",
420 408 name(), count, target_count));
421 409 assert(length() + target_count == old_length,
422 410 hrs_err_msg("[%s] new length should be consistent "
423 411 "new length: %u old length: %u target_count: %u",
424 412 name(), length(), old_length, target_count));
425 413
426 414 verify_optional();
427 415 }
428 416
429 417 void HeapRegionLinkedList::verify() {
430 418 // See comment in HeapRegionSetBase::verify() about MT safety and
431 419 // verification.
432 420 hrs_assert_mt_safety_ok(this);
433 421
434 422 // This will also do the basic verification too.
435 423 verify_start();
436 424
437 425 HeapRegion* curr = _head;
438 426 HeapRegion* prev1 = NULL;
439 427 HeapRegion* prev0 = NULL;
440 428 uint count = 0;
441 429 while (curr != NULL) {
442 430 verify_next_region(curr);
443 431
444 432 count += 1;
445 433 guarantee(count < _unrealistically_long_length,
446 434 hrs_err_msg("[%s] the calculated length: %u "
447 435 "seems very long, is there maybe a cycle? "
448 436 "curr: "PTR_FORMAT" prev0: "PTR_FORMAT" "
449 437 "prev1: "PTR_FORMAT" length: %u",
450 438 name(), count, curr, prev0, prev1, length()));
451 439
452 440 prev1 = prev0;
453 441 prev0 = curr;
454 442 curr = curr->next();
455 443 }
456 444
457 445 guarantee(_tail == prev0, hrs_ext_msg(this, "post-condition"));
458 446
459 447 verify_end();
460 448 }
461 449
462 450 void HeapRegionLinkedList::clear() {
463 451 HeapRegionSetBase::clear();
464 452 _head = NULL;
465 453 _tail = NULL;
466 454 }
467 455
468 456 void HeapRegionLinkedList::print_on(outputStream* out, bool print_contents) {
469 457 HeapRegionSetBase::print_on(out, print_contents);
470 458 out->print_cr(" Linking");
471 459 out->print_cr(" head : "PTR_FORMAT, _head);
472 460 out->print_cr(" tail : "PTR_FORMAT, _tail);
473 461
474 462 if (print_contents) {
475 463 out->print_cr(" Contents");
476 464 HeapRegionLinkedListIterator iter(this);
477 465 while (iter.more_available()) {
478 466 HeapRegion* hr = iter.get_next();
479 467 hr->print_on(out);
480 468 }
481 469 }
482 470 }
↓ open down ↓ |
313 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX