Print this page
rev 3122 : 7133038: G1: Some small profile based optimizations
Summary: Some minor profile based optimizations. Reduce the number of branches and branch mispredicts by removing some virtual calls, through closure specalization, and refactoring some conditional statements.
Reviewed-by: brutisso, tonyp
Split |
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/gc_implementation/g1/heapRegion.cpp
+++ new/src/share/vm/gc_implementation/g1/heapRegion.cpp
1 1 /*
2 2 * Copyright (c) 2001, 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/g1BlockOffsetTable.inline.hpp"
27 27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
28 28 #include "gc_implementation/g1/g1OopClosures.inline.hpp"
29 29 #include "gc_implementation/g1/heapRegion.inline.hpp"
30 30 #include "gc_implementation/g1/heapRegionRemSet.hpp"
31 31 #include "gc_implementation/g1/heapRegionSeq.inline.hpp"
32 32 #include "memory/genOopClosures.inline.hpp"
33 33 #include "memory/iterator.hpp"
34 34 #include "oops/oop.inline.hpp"
35 35
36 36 int HeapRegion::LogOfHRGrainBytes = 0;
37 37 int HeapRegion::LogOfHRGrainWords = 0;
38 38 size_t HeapRegion::GrainBytes = 0;
39 39 size_t HeapRegion::GrainWords = 0;
40 40 size_t HeapRegion::CardsPerRegion = 0;
41 41
42 42 HeapRegionDCTOC::HeapRegionDCTOC(G1CollectedHeap* g1,
43 43 HeapRegion* hr, OopClosure* cl,
44 44 CardTableModRefBS::PrecisionStyle precision,
45 45 FilterKind fk) :
46 46 ContiguousSpaceDCTOC(hr, cl, precision, NULL),
47 47 _hr(hr), _fk(fk), _g1(g1)
48 48 { }
49 49
50 50 FilterOutOfRegionClosure::FilterOutOfRegionClosure(HeapRegion* r,
51 51 OopClosure* oc) :
52 52 _r_bottom(r->bottom()), _r_end(r->end()),
53 53 _oc(oc), _out_of_region(0)
54 54 {}
55 55
56 56 class VerifyLiveClosure: public OopClosure {
57 57 private:
58 58 G1CollectedHeap* _g1h;
59 59 CardTableModRefBS* _bs;
60 60 oop _containing_obj;
61 61 bool _failures;
62 62 int _n_failures;
63 63 VerifyOption _vo;
64 64 public:
65 65 // _vo == UsePrevMarking -> use "prev" marking information,
66 66 // _vo == UseNextMarking -> use "next" marking information,
67 67 // _vo == UseMarkWord -> use mark word from object header.
68 68 VerifyLiveClosure(G1CollectedHeap* g1h, VerifyOption vo) :
69 69 _g1h(g1h), _bs(NULL), _containing_obj(NULL),
70 70 _failures(false), _n_failures(0), _vo(vo)
71 71 {
72 72 BarrierSet* bs = _g1h->barrier_set();
73 73 if (bs->is_a(BarrierSet::CardTableModRef))
74 74 _bs = (CardTableModRefBS*)bs;
75 75 }
76 76
77 77 void set_containing_obj(oop obj) {
78 78 _containing_obj = obj;
79 79 }
80 80
81 81 bool failures() { return _failures; }
82 82 int n_failures() { return _n_failures; }
83 83
84 84 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
85 85 virtual void do_oop( oop* p) { do_oop_work(p); }
86 86
87 87 void print_object(outputStream* out, oop obj) {
88 88 #ifdef PRODUCT
89 89 klassOop k = obj->klass();
90 90 const char* class_name = instanceKlass::cast(k)->external_name();
91 91 out->print_cr("class name %s", class_name);
92 92 #else // PRODUCT
93 93 obj->print_on(out);
94 94 #endif // PRODUCT
95 95 }
96 96
97 97 template <class T>
98 98 void do_oop_work(T* p) {
99 99 assert(_containing_obj != NULL, "Precondition");
100 100 assert(!_g1h->is_obj_dead_cond(_containing_obj, _vo),
101 101 "Precondition");
102 102 T heap_oop = oopDesc::load_heap_oop(p);
103 103 if (!oopDesc::is_null(heap_oop)) {
104 104 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
105 105 bool failed = false;
106 106 if (!_g1h->is_in_closed_subset(obj) || _g1h->is_obj_dead_cond(obj, _vo)) {
107 107 MutexLockerEx x(ParGCRareEvent_lock,
108 108 Mutex::_no_safepoint_check_flag);
109 109
110 110 if (!_failures) {
111 111 gclog_or_tty->print_cr("");
112 112 gclog_or_tty->print_cr("----------");
113 113 }
114 114 if (!_g1h->is_in_closed_subset(obj)) {
115 115 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
116 116 gclog_or_tty->print_cr("Field "PTR_FORMAT
117 117 " of live obj "PTR_FORMAT" in region "
118 118 "["PTR_FORMAT", "PTR_FORMAT")",
119 119 p, (void*) _containing_obj,
120 120 from->bottom(), from->end());
121 121 print_object(gclog_or_tty, _containing_obj);
122 122 gclog_or_tty->print_cr("points to obj "PTR_FORMAT" not in the heap",
123 123 (void*) obj);
124 124 } else {
125 125 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
126 126 HeapRegion* to = _g1h->heap_region_containing((HeapWord*)obj);
127 127 gclog_or_tty->print_cr("Field "PTR_FORMAT
128 128 " of live obj "PTR_FORMAT" in region "
129 129 "["PTR_FORMAT", "PTR_FORMAT")",
130 130 p, (void*) _containing_obj,
131 131 from->bottom(), from->end());
132 132 print_object(gclog_or_tty, _containing_obj);
133 133 gclog_or_tty->print_cr("points to dead obj "PTR_FORMAT" in region "
134 134 "["PTR_FORMAT", "PTR_FORMAT")",
135 135 (void*) obj, to->bottom(), to->end());
136 136 print_object(gclog_or_tty, obj);
137 137 }
138 138 gclog_or_tty->print_cr("----------");
139 139 gclog_or_tty->flush();
140 140 _failures = true;
141 141 failed = true;
142 142 _n_failures++;
143 143 }
144 144
145 145 if (!_g1h->full_collection()) {
146 146 HeapRegion* from = _g1h->heap_region_containing((HeapWord*)p);
147 147 HeapRegion* to = _g1h->heap_region_containing(obj);
148 148 if (from != NULL && to != NULL &&
149 149 from != to &&
150 150 !to->isHumongous()) {
151 151 jbyte cv_obj = *_bs->byte_for_const(_containing_obj);
152 152 jbyte cv_field = *_bs->byte_for_const(p);
153 153 const jbyte dirty = CardTableModRefBS::dirty_card_val();
154 154
155 155 bool is_bad = !(from->is_young()
156 156 || to->rem_set()->contains_reference(p)
157 157 || !G1HRRSFlushLogBuffersOnVerify && // buffers were not flushed
158 158 (_containing_obj->is_objArray() ?
159 159 cv_field == dirty
160 160 : cv_obj == dirty || cv_field == dirty));
161 161 if (is_bad) {
162 162 MutexLockerEx x(ParGCRareEvent_lock,
163 163 Mutex::_no_safepoint_check_flag);
164 164
165 165 if (!_failures) {
166 166 gclog_or_tty->print_cr("");
167 167 gclog_or_tty->print_cr("----------");
168 168 }
169 169 gclog_or_tty->print_cr("Missing rem set entry:");
170 170 gclog_or_tty->print_cr("Field "PTR_FORMAT" "
171 171 "of obj "PTR_FORMAT", "
172 172 "in region "HR_FORMAT,
173 173 p, (void*) _containing_obj,
174 174 HR_FORMAT_PARAMS(from));
175 175 _containing_obj->print_on(gclog_or_tty);
176 176 gclog_or_tty->print_cr("points to obj "PTR_FORMAT" "
177 177 "in region "HR_FORMAT,
178 178 (void*) obj,
179 179 HR_FORMAT_PARAMS(to));
180 180 obj->print_on(gclog_or_tty);
181 181 gclog_or_tty->print_cr("Obj head CTE = %d, field CTE = %d.",
182 182 cv_obj, cv_field);
183 183 gclog_or_tty->print_cr("----------");
184 184 gclog_or_tty->flush();
185 185 _failures = true;
186 186 if (!failed) _n_failures++;
187 187 }
188 188 }
189 189 }
190 190 }
191 191 }
192 192 };
193 193
194 194 template<class ClosureType>
195 195 HeapWord* walk_mem_region_loop(ClosureType* cl, G1CollectedHeap* g1h,
196 196 HeapRegion* hr,
197 197 HeapWord* cur, HeapWord* top) {
198 198 oop cur_oop = oop(cur);
199 199 int oop_size = cur_oop->size();
200 200 HeapWord* next_obj = cur + oop_size;
201 201 while (next_obj < top) {
202 202 // Keep filtering the remembered set.
203 203 if (!g1h->is_obj_dead(cur_oop, hr)) {
204 204 // Bottom lies entirely below top, so we can call the
205 205 // non-memRegion version of oop_iterate below.
206 206 cur_oop->oop_iterate(cl);
207 207 }
208 208 cur = next_obj;
209 209 cur_oop = oop(cur);
210 210 oop_size = cur_oop->size();
211 211 next_obj = cur + oop_size;
212 212 }
213 213 return cur;
214 214 }
215 215
216 216 void HeapRegionDCTOC::walk_mem_region_with_cl(MemRegion mr,
217 217 HeapWord* bottom,
218 218 HeapWord* top,
219 219 OopClosure* cl) {
220 220 G1CollectedHeap* g1h = _g1;
221 221 int oop_size;
222 222 OopClosure* cl2 = NULL;
223 223
224 224 FilterIntoCSClosure intoCSFilt(this, g1h, cl);
225 225 FilterOutOfRegionClosure outOfRegionFilt(_hr, cl);
226 226
227 227 switch (_fk) {
228 228 case NoFilterKind: cl2 = cl; break;
229 229 case IntoCSFilterKind: cl2 = &intoCSFilt; break;
230 230 case OutOfRegionFilterKind: cl2 = &outOfRegionFilt; break;
231 231 default: ShouldNotReachHere();
232 232 }
233 233
234 234 // Start filtering what we add to the remembered set. If the object is
235 235 // not considered dead, either because it is marked (in the mark bitmap)
236 236 // or it was allocated after marking finished, then we add it. Otherwise
237 237 // we can safely ignore the object.
238 238 if (!g1h->is_obj_dead(oop(bottom), _hr)) {
239 239 oop_size = oop(bottom)->oop_iterate(cl2, mr);
240 240 } else {
241 241 oop_size = oop(bottom)->size();
242 242 }
243 243
244 244 bottom += oop_size;
245 245
246 246 if (bottom < top) {
247 247 // We replicate the loop below for several kinds of possible filters.
248 248 switch (_fk) {
249 249 case NoFilterKind:
250 250 bottom = walk_mem_region_loop(cl, g1h, _hr, bottom, top);
251 251 break;
252 252
253 253 case IntoCSFilterKind: {
254 254 FilterIntoCSClosure filt(this, g1h, cl);
255 255 bottom = walk_mem_region_loop(&filt, g1h, _hr, bottom, top);
256 256 break;
257 257 }
258 258
259 259 case OutOfRegionFilterKind: {
260 260 FilterOutOfRegionClosure filt(_hr, cl);
261 261 bottom = walk_mem_region_loop(&filt, g1h, _hr, bottom, top);
262 262 break;
263 263 }
264 264
265 265 default:
266 266 ShouldNotReachHere();
267 267 }
268 268
269 269 // Last object. Need to do dead-obj filtering here too.
270 270 if (!g1h->is_obj_dead(oop(bottom), _hr)) {
271 271 oop(bottom)->oop_iterate(cl2, mr);
272 272 }
273 273 }
274 274 }
275 275
276 276 // Minimum region size; we won't go lower than that.
277 277 // We might want to decrease this in the future, to deal with small
278 278 // heaps a bit more efficiently.
279 279 #define MIN_REGION_SIZE ( 1024 * 1024 )
280 280
281 281 // Maximum region size; we don't go higher than that. There's a good
282 282 // reason for having an upper bound. We don't want regions to get too
283 283 // large, otherwise cleanup's effectiveness would decrease as there
284 284 // will be fewer opportunities to find totally empty regions after
285 285 // marking.
286 286 #define MAX_REGION_SIZE ( 32 * 1024 * 1024 )
287 287
288 288 // The automatic region size calculation will try to have around this
289 289 // many regions in the heap (based on the min heap size).
290 290 #define TARGET_REGION_NUMBER 2048
291 291
292 292 void HeapRegion::setup_heap_region_size(uintx min_heap_size) {
293 293 // region_size in bytes
294 294 uintx region_size = G1HeapRegionSize;
295 295 if (FLAG_IS_DEFAULT(G1HeapRegionSize)) {
296 296 // We base the automatic calculation on the min heap size. This
297 297 // can be problematic if the spread between min and max is quite
298 298 // wide, imagine -Xms128m -Xmx32g. But, if we decided it based on
299 299 // the max size, the region size might be way too large for the
300 300 // min size. Either way, some users might have to set the region
301 301 // size manually for some -Xms / -Xmx combos.
302 302
303 303 region_size = MAX2(min_heap_size / TARGET_REGION_NUMBER,
304 304 (uintx) MIN_REGION_SIZE);
305 305 }
306 306
307 307 int region_size_log = log2_long((jlong) region_size);
308 308 // Recalculate the region size to make sure it's a power of
309 309 // 2. This means that region_size is the largest power of 2 that's
310 310 // <= what we've calculated so far.
311 311 region_size = ((uintx)1 << region_size_log);
312 312
313 313 // Now make sure that we don't go over or under our limits.
314 314 if (region_size < MIN_REGION_SIZE) {
315 315 region_size = MIN_REGION_SIZE;
316 316 } else if (region_size > MAX_REGION_SIZE) {
317 317 region_size = MAX_REGION_SIZE;
318 318 }
319 319
320 320 // And recalculate the log.
321 321 region_size_log = log2_long((jlong) region_size);
322 322
323 323 // Now, set up the globals.
324 324 guarantee(LogOfHRGrainBytes == 0, "we should only set it once");
325 325 LogOfHRGrainBytes = region_size_log;
326 326
327 327 guarantee(LogOfHRGrainWords == 0, "we should only set it once");
328 328 LogOfHRGrainWords = LogOfHRGrainBytes - LogHeapWordSize;
329 329
330 330 guarantee(GrainBytes == 0, "we should only set it once");
331 331 // The cast to int is safe, given that we've bounded region_size by
332 332 // MIN_REGION_SIZE and MAX_REGION_SIZE.
333 333 GrainBytes = (size_t)region_size;
334 334
335 335 guarantee(GrainWords == 0, "we should only set it once");
336 336 GrainWords = GrainBytes >> LogHeapWordSize;
337 337 guarantee((size_t)(1 << LogOfHRGrainWords) == GrainWords, "sanity");
338 338
339 339 guarantee(CardsPerRegion == 0, "we should only set it once");
340 340 CardsPerRegion = GrainBytes >> CardTableModRefBS::card_shift;
341 341 }
342 342
343 343 void HeapRegion::reset_after_compaction() {
344 344 G1OffsetTableContigSpace::reset_after_compaction();
345 345 // After a compaction the mark bitmap is invalid, so we must
346 346 // treat all objects as being inside the unmarked area.
347 347 zero_marked_bytes();
348 348 init_top_at_mark_start();
349 349 }
350 350
351 351 void HeapRegion::hr_clear(bool par, bool clear_space) {
352 352 assert(_humongous_type == NotHumongous,
353 353 "we should have already filtered out humongous regions");
354 354 assert(_humongous_start_region == NULL,
355 355 "we should have already filtered out humongous regions");
356 356 assert(_end == _orig_end,
357 357 "we should have already filtered out humongous regions");
358 358
359 359 _in_collection_set = false;
360 360
361 361 set_young_index_in_cset(-1);
362 362 uninstall_surv_rate_group();
363 363 set_young_type(NotYoung);
364 364 reset_pre_dummy_top();
365 365
366 366 if (!par) {
367 367 // If this is parallel, this will be done later.
368 368 HeapRegionRemSet* hrrs = rem_set();
369 369 if (hrrs != NULL) hrrs->clear();
370 370 _claimed = InitialClaimValue;
371 371 }
372 372 zero_marked_bytes();
373 373 set_sort_index(-1);
374 374
375 375 _offsets.resize(HeapRegion::GrainWords);
376 376 init_top_at_mark_start();
377 377 if (clear_space) clear(SpaceDecorator::Mangle);
378 378 }
379 379
380 380 void HeapRegion::par_clear() {
381 381 assert(used() == 0, "the region should have been already cleared");
382 382 assert(capacity() == HeapRegion::GrainBytes, "should be back to normal");
383 383 HeapRegionRemSet* hrrs = rem_set();
384 384 hrrs->clear();
385 385 CardTableModRefBS* ct_bs =
386 386 (CardTableModRefBS*)G1CollectedHeap::heap()->barrier_set();
387 387 ct_bs->clear(MemRegion(bottom(), end()));
388 388 }
389 389
390 390 // <PREDICTION>
391 391 void HeapRegion::calc_gc_efficiency() {
392 392 G1CollectedHeap* g1h = G1CollectedHeap::heap();
393 393 _gc_efficiency = (double) garbage_bytes() /
394 394 g1h->predict_region_elapsed_time_ms(this, false);
395 395 }
396 396 // </PREDICTION>
397 397
398 398 void HeapRegion::set_startsHumongous(HeapWord* new_top, HeapWord* new_end) {
399 399 assert(!isHumongous(), "sanity / pre-condition");
400 400 assert(end() == _orig_end,
401 401 "Should be normal before the humongous object allocation");
402 402 assert(top() == bottom(), "should be empty");
403 403 assert(bottom() <= new_top && new_top <= new_end, "pre-condition");
404 404
405 405 _humongous_type = StartsHumongous;
406 406 _humongous_start_region = this;
407 407
408 408 set_end(new_end);
409 409 _offsets.set_for_starts_humongous(new_top);
410 410 }
411 411
412 412 void HeapRegion::set_continuesHumongous(HeapRegion* first_hr) {
413 413 assert(!isHumongous(), "sanity / pre-condition");
414 414 assert(end() == _orig_end,
415 415 "Should be normal before the humongous object allocation");
416 416 assert(top() == bottom(), "should be empty");
417 417 assert(first_hr->startsHumongous(), "pre-condition");
418 418
419 419 _humongous_type = ContinuesHumongous;
420 420 _humongous_start_region = first_hr;
421 421 }
422 422
423 423 void HeapRegion::set_notHumongous() {
424 424 assert(isHumongous(), "pre-condition");
425 425
426 426 if (startsHumongous()) {
427 427 assert(top() <= end(), "pre-condition");
428 428 set_end(_orig_end);
429 429 if (top() > end()) {
430 430 // at least one "continues humongous" region after it
431 431 set_top(end());
432 432 }
433 433 } else {
434 434 // continues humongous
435 435 assert(end() == _orig_end, "sanity");
436 436 }
437 437
438 438 assert(capacity() == HeapRegion::GrainBytes, "pre-condition");
439 439 _humongous_type = NotHumongous;
440 440 _humongous_start_region = NULL;
441 441 }
442 442
443 443 bool HeapRegion::claimHeapRegion(jint claimValue) {
444 444 jint current = _claimed;
445 445 if (current != claimValue) {
446 446 jint res = Atomic::cmpxchg(claimValue, &_claimed, current);
447 447 if (res == current) {
448 448 return true;
449 449 }
450 450 }
451 451 return false;
452 452 }
453 453
454 454 HeapWord* HeapRegion::next_block_start_careful(HeapWord* addr) {
455 455 HeapWord* low = addr;
456 456 HeapWord* high = end();
457 457 while (low < high) {
458 458 size_t diff = pointer_delta(high, low);
459 459 // Must add one below to bias toward the high amount. Otherwise, if
460 460 // "high" were at the desired value, and "low" were one less, we
461 461 // would not converge on "high". This is not symmetric, because
462 462 // we set "high" to a block start, which might be the right one,
463 463 // which we don't do for "low".
464 464 HeapWord* middle = low + (diff+1)/2;
465 465 if (middle == high) return high;
466 466 HeapWord* mid_bs = block_start_careful(middle);
467 467 if (mid_bs < addr) {
468 468 low = middle;
469 469 } else {
470 470 high = mid_bs;
471 471 }
472 472 }
473 473 assert(low == high && low >= addr, "Didn't work.");
474 474 return low;
475 475 }
476 476
477 477 void HeapRegion::initialize(MemRegion mr, bool clear_space, bool mangle_space) {
478 478 G1OffsetTableContigSpace::initialize(mr, false, mangle_space);
479 479 hr_clear(false/*par*/, clear_space);
480 480 }
481 481 #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
482 482 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
483 483 #endif // _MSC_VER
484 484
485 485
486 486 HeapRegion::
487 487 HeapRegion(size_t hrs_index, G1BlockOffsetSharedArray* sharedOffsetArray,
488 488 MemRegion mr, bool is_zeroed)
489 489 : G1OffsetTableContigSpace(sharedOffsetArray, mr, is_zeroed),
490 490 _hrs_index(hrs_index),
491 491 _humongous_type(NotHumongous), _humongous_start_region(NULL),
492 492 _in_collection_set(false),
493 493 _next_in_special_set(NULL), _orig_end(NULL),
494 494 _claimed(InitialClaimValue), _evacuation_failed(false),
495 495 _prev_marked_bytes(0), _next_marked_bytes(0), _sort_index(-1),
496 496 _gc_efficiency(0.0),
497 497 _young_type(NotYoung), _next_young_region(NULL),
498 498 _next_dirty_cards_region(NULL), _next(NULL), _pending_removal(false),
499 499 #ifdef ASSERT
500 500 _containing_set(NULL),
501 501 #endif // ASSERT
502 502 _young_index_in_cset(-1), _surv_rate_group(NULL), _age_index(-1),
503 503 _rem_set(NULL), _recorded_rs_length(0), _predicted_elapsed_time_ms(0),
504 504 _predicted_bytes_to_copy(0)
505 505 {
506 506 _orig_end = mr.end();
507 507 // Note that initialize() will set the start of the unmarked area of the
508 508 // region.
509 509 this->initialize(mr, !is_zeroed, SpaceDecorator::Mangle);
510 510 set_top(bottom());
511 511 set_saved_mark();
512 512
513 513 _rem_set = new HeapRegionRemSet(sharedOffsetArray, this);
514 514
515 515 assert(HeapRegionRemSet::num_par_rem_sets() > 0, "Invariant.");
516 516 // In case the region is allocated during a pause, note the top.
517 517 // We haven't done any counting on a brand new region.
518 518 _top_at_conc_mark_count = bottom();
519 519 }
520 520
521 521 class NextCompactionHeapRegionClosure: public HeapRegionClosure {
522 522 const HeapRegion* _target;
523 523 bool _target_seen;
524 524 HeapRegion* _last;
525 525 CompactibleSpace* _res;
526 526 public:
527 527 NextCompactionHeapRegionClosure(const HeapRegion* target) :
528 528 _target(target), _target_seen(false), _res(NULL) {}
529 529 bool doHeapRegion(HeapRegion* cur) {
530 530 if (_target_seen) {
531 531 if (!cur->isHumongous()) {
532 532 _res = cur;
533 533 return true;
534 534 }
535 535 } else if (cur == _target) {
536 536 _target_seen = true;
537 537 }
538 538 return false;
539 539 }
540 540 CompactibleSpace* result() { return _res; }
541 541 };
542 542
543 543 CompactibleSpace* HeapRegion::next_compaction_space() const {
544 544 G1CollectedHeap* g1h = G1CollectedHeap::heap();
545 545 // cast away const-ness
546 546 HeapRegion* r = (HeapRegion*) this;
547 547 NextCompactionHeapRegionClosure blk(r);
548 548 g1h->heap_region_iterate_from(r, &blk);
549 549 return blk.result();
550 550 }
551 551
552 552 void HeapRegion::save_marks() {
553 553 set_saved_mark();
554 554 }
555 555
556 556 void HeapRegion::oops_in_mr_iterate(MemRegion mr, OopClosure* cl) {
557 557 HeapWord* p = mr.start();
558 558 HeapWord* e = mr.end();
559 559 oop obj;
560 560 while (p < e) {
561 561 obj = oop(p);
562 562 p += obj->oop_iterate(cl);
563 563 }
564 564 assert(p == e, "bad memregion: doesn't end on obj boundary");
565 565 }
566 566
567 567 #define HeapRegion_OOP_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \
568 568 void HeapRegion::oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl) { \
569 569 ContiguousSpace::oop_since_save_marks_iterate##nv_suffix(cl); \
570 570 }
571 571 SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(HeapRegion_OOP_SINCE_SAVE_MARKS_DEFN)
572 572
573 573
574 574 void HeapRegion::oop_before_save_marks_iterate(OopClosure* cl) {
575 575 oops_in_mr_iterate(MemRegion(bottom(), saved_mark_word()), cl);
576 576 }
577 577
578 578 void HeapRegion::note_self_forwarding_removal_start(bool during_initial_mark,
579 579 bool during_conc_mark) {
580 580 // We always recreate the prev marking info and we'll explicitly
581 581 // mark all objects we find to be self-forwarded on the prev
582 582 // bitmap. So all objects need to be below PTAMS.
583 583 _prev_top_at_mark_start = top();
584 584 _prev_marked_bytes = 0;
585 585
586 586 if (during_initial_mark) {
587 587 // During initial-mark, we'll also explicitly mark all objects
588 588 // we find to be self-forwarded on the next bitmap. So all
589 589 // objects need to be below NTAMS.
590 590 _next_top_at_mark_start = top();
591 591 set_top_at_conc_mark_count(bottom());
592 592 _next_marked_bytes = 0;
593 593 } else if (during_conc_mark) {
594 594 // During concurrent mark, all objects in the CSet (including
595 595 // the ones we find to be self-forwarded) are implicitly live.
596 596 // So all objects need to be above NTAMS.
597 597 _next_top_at_mark_start = bottom();
598 598 set_top_at_conc_mark_count(bottom());
599 599 _next_marked_bytes = 0;
600 600 }
601 601 }
602 602
603 603 void HeapRegion::note_self_forwarding_removal_end(bool during_initial_mark,
604 604 bool during_conc_mark,
605 605 size_t marked_bytes) {
606 606 assert(0 <= marked_bytes && marked_bytes <= used(),
607 607 err_msg("marked: "SIZE_FORMAT" used: "SIZE_FORMAT,
608 608 marked_bytes, used()));
609 609 _prev_marked_bytes = marked_bytes;
610 610 }
611 611
612 612 HeapWord*
613 613 HeapRegion::object_iterate_mem_careful(MemRegion mr,
614 614 ObjectClosure* cl) {
615 615 G1CollectedHeap* g1h = G1CollectedHeap::heap();
616 616 // We used to use "block_start_careful" here. But we're actually happy
617 617 // to update the BOT while we do this...
618 618 HeapWord* cur = block_start(mr.start());
619 619 mr = mr.intersection(used_region());
620 620 if (mr.is_empty()) return NULL;
621 621 // Otherwise, find the obj that extends onto mr.start().
622 622
623 623 assert(cur <= mr.start()
624 624 && (oop(cur)->klass_or_null() == NULL ||
625 625 cur + oop(cur)->size() > mr.start()),
626 626 "postcondition of block_start");
627 627 oop obj;
628 628 while (cur < mr.end()) {
629 629 obj = oop(cur);
630 630 if (obj->klass_or_null() == NULL) {
631 631 // Ran into an unparseable point.
632 632 return cur;
633 633 } else if (!g1h->is_obj_dead(obj)) {
634 634 cl->do_object(obj);
635 635 }
636 636 if (cl->abort()) return cur;
637 637 // The check above must occur before the operation below, since an
638 638 // abort might invalidate the "size" operation.
639 639 cur += obj->size();
640 640 }
641 641 return NULL;
642 642 }
643 643
644 644 HeapWord*
645 645 HeapRegion::
646 646 oops_on_card_seq_iterate_careful(MemRegion mr,
647 647 FilterOutOfRegionClosure* cl,
648 648 bool filter_young,
649 649 jbyte* card_ptr) {
650 650 // Currently, we should only have to clean the card if filter_young
651 651 // is true and vice versa.
↓ open down ↓ |
651 lines elided |
↑ open up ↑ |
652 652 if (filter_young) {
653 653 assert(card_ptr != NULL, "pre-condition");
654 654 } else {
655 655 assert(card_ptr == NULL, "pre-condition");
656 656 }
657 657 G1CollectedHeap* g1h = G1CollectedHeap::heap();
658 658
659 659 // If we're within a stop-world GC, then we might look at a card in a
660 660 // GC alloc region that extends onto a GC LAB, which may not be
661 661 // parseable. Stop such at the "saved_mark" of the region.
662 - if (G1CollectedHeap::heap()->is_gc_active()) {
662 + if (g1h->is_gc_active()) {
663 663 mr = mr.intersection(used_region_at_save_marks());
664 664 } else {
665 665 mr = mr.intersection(used_region());
666 666 }
667 667 if (mr.is_empty()) return NULL;
668 668 // Otherwise, find the obj that extends onto mr.start().
669 669
670 670 // The intersection of the incoming mr (for the card) and the
671 671 // allocated part of the region is non-empty. This implies that
672 672 // we have actually allocated into this region. The code in
673 673 // G1CollectedHeap.cpp that allocates a new region sets the
674 674 // is_young tag on the region before allocating. Thus we
675 675 // safely know if this region is young.
676 676 if (is_young() && filter_young) {
677 677 return NULL;
678 678 }
679 679
680 680 assert(!is_young(), "check value of filter_young");
↓ open down ↓ |
8 lines elided |
↑ open up ↑ |
681 681
682 682 // We can only clean the card here, after we make the decision that
683 683 // the card is not young. And we only clean the card if we have been
684 684 // asked to (i.e., card_ptr != NULL).
685 685 if (card_ptr != NULL) {
686 686 *card_ptr = CardTableModRefBS::clean_card_val();
687 687 // We must complete this write before we do any of the reads below.
688 688 OrderAccess::storeload();
689 689 }
690 690
691 + // Cache the boundaries of the memory region in some const locals
692 + HeapWord* const start = mr.start();
693 + HeapWord* const end = mr.end();
694 +
691 695 // We used to use "block_start_careful" here. But we're actually happy
692 696 // to update the BOT while we do this...
693 - HeapWord* cur = block_start(mr.start());
694 - assert(cur <= mr.start(), "Postcondition");
697 + HeapWord* cur = block_start(start);
698 + assert(cur <= start, "Postcondition");
695 699
696 - while (cur <= mr.start()) {
697 - if (oop(cur)->klass_or_null() == NULL) {
700 + oop obj;
701 +
702 + HeapWord* next = cur;
703 + while (next <= start) {
704 + cur = next;
705 + obj = oop(cur);
706 + if (obj->klass_or_null() == NULL) {
698 707 // Ran into an unparseable point.
699 708 return cur;
700 709 }
701 710 // Otherwise...
702 - int sz = oop(cur)->size();
703 - if (cur + sz > mr.start()) break;
704 - // Otherwise, go on.
705 - cur = cur + sz;
711 + next = (cur + obj->size());
706 712 }
707 - oop obj;
708 - obj = oop(cur);
709 - // If we finish this loop...
710 - assert(cur <= mr.start()
711 - && obj->klass_or_null() != NULL
712 - && cur + obj->size() > mr.start(),
713 +
714 + // If we finish the above loop...We have a parseable object that
715 + // begins on or before the start of the memory region, and ends
716 + // inside or spans the entire region.
717 +
718 + assert(obj == oop(cur), "sanity");
719 + assert(cur <= start &&
720 + obj->klass_or_null() != NULL &&
721 + (cur + obj->size()) > start,
713 722 "Loop postcondition");
723 +
714 724 if (!g1h->is_obj_dead(obj)) {
715 725 obj->oop_iterate(cl, mr);
716 726 }
717 727
718 - HeapWord* next;
719 - while (cur < mr.end()) {
728 + while (cur < end) {
720 729 obj = oop(cur);
721 730 if (obj->klass_or_null() == NULL) {
722 731 // Ran into an unparseable point.
723 732 return cur;
724 733 };
734 +
725 735 // Otherwise:
726 736 next = (cur + obj->size());
737 +
727 738 if (!g1h->is_obj_dead(obj)) {
728 - if (next < mr.end()) {
739 + if (next < end || !obj->is_objArray()) {
740 + // This object either does not span the MemRegion
741 + // boundary, or if it does it's not an array.
742 + // Apply closure to whole object.
729 743 obj->oop_iterate(cl);
730 744 } else {
731 - // this obj spans the boundary. If it's an array, stop at the
732 - // boundary.
733 - if (obj->is_objArray()) {
734 - obj->oop_iterate(cl, mr);
735 - } else {
736 - obj->oop_iterate(cl);
737 - }
745 + // This obj is an array that spans the boundary.
746 + // Stop at the boundary.
747 + obj->oop_iterate(cl, mr);
738 748 }
739 749 }
740 750 cur = next;
741 751 }
742 752 return NULL;
743 753 }
744 754
745 755 void HeapRegion::print() const { print_on(gclog_or_tty); }
746 756 void HeapRegion::print_on(outputStream* st) const {
747 757 if (isHumongous()) {
748 758 if (startsHumongous())
749 759 st->print(" HS");
750 760 else
751 761 st->print(" HC");
752 762 } else {
753 763 st->print(" ");
754 764 }
755 765 if (in_collection_set())
756 766 st->print(" CS");
757 767 else
758 768 st->print(" ");
759 769 if (is_young())
760 770 st->print(is_survivor() ? " SU" : " Y ");
761 771 else
762 772 st->print(" ");
763 773 if (is_empty())
764 774 st->print(" F");
765 775 else
766 776 st->print(" ");
767 777 st->print(" TS %5d", _gc_time_stamp);
768 778 st->print(" PTAMS "PTR_FORMAT" NTAMS "PTR_FORMAT,
769 779 prev_top_at_mark_start(), next_top_at_mark_start());
770 780 G1OffsetTableContigSpace::print_on(st);
771 781 }
772 782
773 783 void HeapRegion::verify(bool allow_dirty) const {
774 784 bool dummy = false;
775 785 verify(allow_dirty, VerifyOption_G1UsePrevMarking, /* failures */ &dummy);
776 786 }
777 787
778 788 // This really ought to be commoned up into OffsetTableContigSpace somehow.
779 789 // We would need a mechanism to make that code skip dead objects.
780 790
781 791 void HeapRegion::verify(bool allow_dirty,
782 792 VerifyOption vo,
783 793 bool* failures) const {
784 794 G1CollectedHeap* g1 = G1CollectedHeap::heap();
785 795 *failures = false;
786 796 HeapWord* p = bottom();
787 797 HeapWord* prev_p = NULL;
788 798 VerifyLiveClosure vl_cl(g1, vo);
789 799 bool is_humongous = isHumongous();
790 800 bool do_bot_verify = !is_young();
791 801 size_t object_num = 0;
792 802 while (p < top()) {
793 803 oop obj = oop(p);
794 804 size_t obj_size = obj->size();
795 805 object_num += 1;
796 806
797 807 if (is_humongous != g1->isHumongous(obj_size)) {
798 808 gclog_or_tty->print_cr("obj "PTR_FORMAT" is of %shumongous size ("
799 809 SIZE_FORMAT" words) in a %shumongous region",
800 810 p, g1->isHumongous(obj_size) ? "" : "non-",
801 811 obj_size, is_humongous ? "" : "non-");
802 812 *failures = true;
803 813 return;
804 814 }
805 815
806 816 // If it returns false, verify_for_object() will output the
807 817 // appropriate messasge.
808 818 if (do_bot_verify && !_offsets.verify_for_object(p, obj_size)) {
809 819 *failures = true;
810 820 return;
811 821 }
812 822
813 823 if (!g1->is_obj_dead_cond(obj, this, vo)) {
814 824 if (obj->is_oop()) {
815 825 klassOop klass = obj->klass();
816 826 if (!klass->is_perm()) {
817 827 gclog_or_tty->print_cr("klass "PTR_FORMAT" of object "PTR_FORMAT" "
818 828 "not in perm", klass, obj);
819 829 *failures = true;
820 830 return;
821 831 } else if (!klass->is_klass()) {
822 832 gclog_or_tty->print_cr("klass "PTR_FORMAT" of object "PTR_FORMAT" "
823 833 "not a klass", klass, obj);
824 834 *failures = true;
825 835 return;
826 836 } else {
827 837 vl_cl.set_containing_obj(obj);
828 838 obj->oop_iterate(&vl_cl);
829 839 if (vl_cl.failures()) {
830 840 *failures = true;
831 841 }
832 842 if (G1MaxVerifyFailures >= 0 &&
833 843 vl_cl.n_failures() >= G1MaxVerifyFailures) {
834 844 return;
835 845 }
836 846 }
837 847 } else {
838 848 gclog_or_tty->print_cr(PTR_FORMAT" no an oop", obj);
839 849 *failures = true;
840 850 return;
841 851 }
842 852 }
843 853 prev_p = p;
844 854 p += obj_size;
845 855 }
846 856
847 857 if (p != top()) {
848 858 gclog_or_tty->print_cr("end of last object "PTR_FORMAT" "
849 859 "does not match top "PTR_FORMAT, p, top());
850 860 *failures = true;
851 861 return;
852 862 }
853 863
854 864 HeapWord* the_end = end();
855 865 assert(p == top(), "it should still hold");
856 866 // Do some extra BOT consistency checking for addresses in the
857 867 // range [top, end). BOT look-ups in this range should yield
858 868 // top. No point in doing that if top == end (there's nothing there).
859 869 if (p < the_end) {
860 870 // Look up top
861 871 HeapWord* addr_1 = p;
862 872 HeapWord* b_start_1 = _offsets.block_start_const(addr_1);
863 873 if (b_start_1 != p) {
864 874 gclog_or_tty->print_cr("BOT look up for top: "PTR_FORMAT" "
865 875 " yielded "PTR_FORMAT", expecting "PTR_FORMAT,
866 876 addr_1, b_start_1, p);
867 877 *failures = true;
868 878 return;
869 879 }
870 880
871 881 // Look up top + 1
872 882 HeapWord* addr_2 = p + 1;
873 883 if (addr_2 < the_end) {
874 884 HeapWord* b_start_2 = _offsets.block_start_const(addr_2);
875 885 if (b_start_2 != p) {
876 886 gclog_or_tty->print_cr("BOT look up for top + 1: "PTR_FORMAT" "
877 887 " yielded "PTR_FORMAT", expecting "PTR_FORMAT,
878 888 addr_2, b_start_2, p);
879 889 *failures = true;
880 890 return;
881 891 }
882 892 }
883 893
884 894 // Look up an address between top and end
885 895 size_t diff = pointer_delta(the_end, p) / 2;
886 896 HeapWord* addr_3 = p + diff;
887 897 if (addr_3 < the_end) {
888 898 HeapWord* b_start_3 = _offsets.block_start_const(addr_3);
889 899 if (b_start_3 != p) {
890 900 gclog_or_tty->print_cr("BOT look up for top + diff: "PTR_FORMAT" "
891 901 " yielded "PTR_FORMAT", expecting "PTR_FORMAT,
892 902 addr_3, b_start_3, p);
893 903 *failures = true;
894 904 return;
895 905 }
896 906 }
897 907
898 908 // Loook up end - 1
899 909 HeapWord* addr_4 = the_end - 1;
900 910 HeapWord* b_start_4 = _offsets.block_start_const(addr_4);
901 911 if (b_start_4 != p) {
902 912 gclog_or_tty->print_cr("BOT look up for end - 1: "PTR_FORMAT" "
903 913 " yielded "PTR_FORMAT", expecting "PTR_FORMAT,
904 914 addr_4, b_start_4, p);
905 915 *failures = true;
906 916 return;
907 917 }
908 918 }
909 919
910 920 if (is_humongous && object_num > 1) {
911 921 gclog_or_tty->print_cr("region ["PTR_FORMAT","PTR_FORMAT"] is humongous "
912 922 "but has "SIZE_FORMAT", objects",
913 923 bottom(), end(), object_num);
914 924 *failures = true;
915 925 return;
916 926 }
917 927 }
918 928
919 929 // G1OffsetTableContigSpace code; copied from space.cpp. Hope this can go
920 930 // away eventually.
921 931
922 932 void G1OffsetTableContigSpace::initialize(MemRegion mr, bool clear_space, bool mangle_space) {
923 933 // false ==> we'll do the clearing if there's clearing to be done.
924 934 ContiguousSpace::initialize(mr, false, mangle_space);
925 935 _offsets.zero_bottom_entry();
926 936 _offsets.initialize_threshold();
927 937 if (clear_space) clear(mangle_space);
928 938 }
929 939
930 940 void G1OffsetTableContigSpace::clear(bool mangle_space) {
931 941 ContiguousSpace::clear(mangle_space);
932 942 _offsets.zero_bottom_entry();
933 943 _offsets.initialize_threshold();
934 944 }
935 945
936 946 void G1OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) {
937 947 Space::set_bottom(new_bottom);
938 948 _offsets.set_bottom(new_bottom);
939 949 }
940 950
941 951 void G1OffsetTableContigSpace::set_end(HeapWord* new_end) {
942 952 Space::set_end(new_end);
943 953 _offsets.resize(new_end - bottom());
944 954 }
945 955
946 956 void G1OffsetTableContigSpace::print() const {
947 957 print_short();
948 958 gclog_or_tty->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", "
949 959 INTPTR_FORMAT ", " INTPTR_FORMAT ")",
950 960 bottom(), top(), _offsets.threshold(), end());
951 961 }
952 962
953 963 HeapWord* G1OffsetTableContigSpace::initialize_threshold() {
954 964 return _offsets.initialize_threshold();
955 965 }
956 966
957 967 HeapWord* G1OffsetTableContigSpace::cross_threshold(HeapWord* start,
958 968 HeapWord* end) {
959 969 _offsets.alloc_block(start, end);
960 970 return _offsets.threshold();
961 971 }
962 972
963 973 HeapWord* G1OffsetTableContigSpace::saved_mark_word() const {
964 974 G1CollectedHeap* g1h = G1CollectedHeap::heap();
965 975 assert( _gc_time_stamp <= g1h->get_gc_time_stamp(), "invariant" );
966 976 if (_gc_time_stamp < g1h->get_gc_time_stamp())
967 977 return top();
968 978 else
969 979 return ContiguousSpace::saved_mark_word();
970 980 }
971 981
972 982 void G1OffsetTableContigSpace::set_saved_mark() {
973 983 G1CollectedHeap* g1h = G1CollectedHeap::heap();
974 984 unsigned curr_gc_time_stamp = g1h->get_gc_time_stamp();
975 985
976 986 if (_gc_time_stamp < curr_gc_time_stamp) {
977 987 // The order of these is important, as another thread might be
978 988 // about to start scanning this region. If it does so after
979 989 // set_saved_mark and before _gc_time_stamp = ..., then the latter
980 990 // will be false, and it will pick up top() as the high water mark
981 991 // of region. If it does so after _gc_time_stamp = ..., then it
982 992 // will pick up the right saved_mark_word() as the high water mark
983 993 // of the region. Either way, the behaviour will be correct.
984 994 ContiguousSpace::set_saved_mark();
985 995 OrderAccess::storestore();
986 996 _gc_time_stamp = curr_gc_time_stamp;
987 997 // No need to do another barrier to flush the writes above. If
988 998 // this is called in parallel with other threads trying to
989 999 // allocate into the region, the caller should call this while
990 1000 // holding a lock and when the lock is released the writes will be
991 1001 // flushed.
992 1002 }
993 1003 }
994 1004
995 1005 G1OffsetTableContigSpace::
996 1006 G1OffsetTableContigSpace(G1BlockOffsetSharedArray* sharedOffsetArray,
997 1007 MemRegion mr, bool is_zeroed) :
998 1008 _offsets(sharedOffsetArray, mr),
999 1009 _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true),
1000 1010 _gc_time_stamp(0)
1001 1011 {
1002 1012 _offsets.set_space(this);
1003 1013 initialize(mr, !is_zeroed, SpaceDecorator::Mangle);
1004 1014 }
↓ open down ↓ |
257 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX