src/share/vm/gc_implementation/parallelScavenge/psMarkSweepDecorator.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6912
c49dcaf78a65
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/systemDictionary.hpp"
aoqi@0 27 #include "gc_implementation/parallelScavenge/objectStartArray.hpp"
aoqi@0 28 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
aoqi@0 29 #include "gc_implementation/parallelScavenge/psMarkSweep.hpp"
aoqi@0 30 #include "gc_implementation/parallelScavenge/psMarkSweepDecorator.hpp"
aoqi@0 31 #include "gc_implementation/shared/liveRange.hpp"
aoqi@0 32 #include "gc_implementation/shared/markSweep.inline.hpp"
aoqi@0 33 #include "gc_implementation/shared/spaceDecorator.hpp"
aoqi@0 34 #include "oops/oop.inline.hpp"
goetz@6912 35 #include "runtime/prefetch.inline.hpp"
aoqi@0 36
aoqi@0 37 PSMarkSweepDecorator* PSMarkSweepDecorator::_destination_decorator = NULL;
aoqi@0 38
aoqi@0 39
aoqi@0 40 void PSMarkSweepDecorator::set_destination_decorator_tenured() {
aoqi@0 41 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
aoqi@0 42 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
aoqi@0 43
aoqi@0 44 _destination_decorator = heap->old_gen()->object_mark_sweep();
aoqi@0 45 }
aoqi@0 46
aoqi@0 47 void PSMarkSweepDecorator::advance_destination_decorator() {
aoqi@0 48 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
aoqi@0 49 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
aoqi@0 50
aoqi@0 51 assert(_destination_decorator != NULL, "Sanity");
aoqi@0 52
aoqi@0 53 PSMarkSweepDecorator* first = heap->old_gen()->object_mark_sweep();
aoqi@0 54 PSMarkSweepDecorator* second = heap->young_gen()->eden_mark_sweep();
aoqi@0 55 PSMarkSweepDecorator* third = heap->young_gen()->from_mark_sweep();
aoqi@0 56 PSMarkSweepDecorator* fourth = heap->young_gen()->to_mark_sweep();
aoqi@0 57
aoqi@0 58 if ( _destination_decorator == first ) {
aoqi@0 59 _destination_decorator = second;
aoqi@0 60 } else if ( _destination_decorator == second ) {
aoqi@0 61 _destination_decorator = third;
aoqi@0 62 } else if ( _destination_decorator == third ) {
aoqi@0 63 _destination_decorator = fourth;
aoqi@0 64 } else {
aoqi@0 65 fatal("PSMarkSweep attempting to advance past last compaction area");
aoqi@0 66 }
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 PSMarkSweepDecorator* PSMarkSweepDecorator::destination_decorator() {
aoqi@0 70 assert(_destination_decorator != NULL, "Sanity");
aoqi@0 71
aoqi@0 72 return _destination_decorator;
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 // FIX ME FIX ME FIX ME FIX ME!!!!!!!!!
aoqi@0 76 // The object forwarding code is duplicated. Factor this out!!!!!
aoqi@0 77 //
aoqi@0 78 // This method "precompacts" objects inside its space to dest. It places forwarding
aoqi@0 79 // pointers into markOops for use by adjust_pointers. If "dest" should overflow, we
aoqi@0 80 // finish by compacting into our own space.
aoqi@0 81
aoqi@0 82 void PSMarkSweepDecorator::precompact() {
aoqi@0 83 // Reset our own compact top.
aoqi@0 84 set_compaction_top(space()->bottom());
aoqi@0 85
aoqi@0 86 /* We allow some amount of garbage towards the bottom of the space, so
aoqi@0 87 * we don't start compacting before there is a significant gain to be made.
aoqi@0 88 * Occasionally, we want to ensure a full compaction, which is determined
aoqi@0 89 * by the MarkSweepAlwaysCompactCount parameter. This is a significant
aoqi@0 90 * performance improvement!
aoqi@0 91 */
aoqi@0 92 bool skip_dead = ((PSMarkSweep::total_invocations() % MarkSweepAlwaysCompactCount) != 0);
aoqi@0 93
aoqi@0 94 size_t allowed_deadspace = 0;
aoqi@0 95 if (skip_dead) {
aoqi@0 96 const size_t ratio = allowed_dead_ratio();
aoqi@0 97 allowed_deadspace = space()->capacity_in_words() * ratio / 100;
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 // Fetch the current destination decorator
aoqi@0 101 PSMarkSweepDecorator* dest = destination_decorator();
aoqi@0 102 ObjectStartArray* start_array = dest->start_array();
aoqi@0 103
aoqi@0 104 HeapWord* compact_top = dest->compaction_top();
aoqi@0 105 HeapWord* compact_end = dest->space()->end();
aoqi@0 106
aoqi@0 107 HeapWord* q = space()->bottom();
aoqi@0 108 HeapWord* t = space()->top();
aoqi@0 109
aoqi@0 110 HeapWord* end_of_live= q; /* One byte beyond the last byte of the last
aoqi@0 111 live object. */
aoqi@0 112 HeapWord* first_dead = space()->end(); /* The first dead object. */
aoqi@0 113 LiveRange* liveRange = NULL; /* The current live range, recorded in the
aoqi@0 114 first header of preceding free area. */
aoqi@0 115 _first_dead = first_dead;
aoqi@0 116
aoqi@0 117 const intx interval = PrefetchScanIntervalInBytes;
aoqi@0 118
aoqi@0 119 while (q < t) {
aoqi@0 120 assert(oop(q)->mark()->is_marked() || oop(q)->mark()->is_unlocked() ||
aoqi@0 121 oop(q)->mark()->has_bias_pattern(),
aoqi@0 122 "these are the only valid states during a mark sweep");
aoqi@0 123 if (oop(q)->is_gc_marked()) {
aoqi@0 124 /* prefetch beyond q */
aoqi@0 125 Prefetch::write(q, interval);
aoqi@0 126 size_t size = oop(q)->size();
aoqi@0 127
aoqi@0 128 size_t compaction_max_size = pointer_delta(compact_end, compact_top);
aoqi@0 129
aoqi@0 130 // This should only happen if a space in the young gen overflows the
aoqi@0 131 // old gen. If that should happen, we null out the start_array, because
aoqi@0 132 // the young spaces are not covered by one.
aoqi@0 133 while(size > compaction_max_size) {
aoqi@0 134 // First record the last compact_top
aoqi@0 135 dest->set_compaction_top(compact_top);
aoqi@0 136
aoqi@0 137 // Advance to the next compaction decorator
aoqi@0 138 advance_destination_decorator();
aoqi@0 139 dest = destination_decorator();
aoqi@0 140
aoqi@0 141 // Update compaction info
aoqi@0 142 start_array = dest->start_array();
aoqi@0 143 compact_top = dest->compaction_top();
aoqi@0 144 compact_end = dest->space()->end();
aoqi@0 145 assert(compact_top == dest->space()->bottom(), "Advanced to space already in use");
aoqi@0 146 assert(compact_end > compact_top, "Must always be space remaining");
aoqi@0 147 compaction_max_size =
aoqi@0 148 pointer_delta(compact_end, compact_top);
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 // store the forwarding pointer into the mark word
aoqi@0 152 if (q != compact_top) {
aoqi@0 153 oop(q)->forward_to(oop(compact_top));
aoqi@0 154 assert(oop(q)->is_gc_marked(), "encoding the pointer should preserve the mark");
aoqi@0 155 } else {
aoqi@0 156 // if the object isn't moving we can just set the mark to the default
aoqi@0 157 // mark and handle it specially later on.
aoqi@0 158 oop(q)->init_mark();
aoqi@0 159 assert(oop(q)->forwardee() == NULL, "should be forwarded to NULL");
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 // Update object start array
aoqi@0 163 if (start_array) {
aoqi@0 164 start_array->allocate_block(compact_top);
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 compact_top += size;
aoqi@0 168 assert(compact_top <= dest->space()->end(),
aoqi@0 169 "Exceeding space in destination");
aoqi@0 170
aoqi@0 171 q += size;
aoqi@0 172 end_of_live = q;
aoqi@0 173 } else {
aoqi@0 174 /* run over all the contiguous dead objects */
aoqi@0 175 HeapWord* end = q;
aoqi@0 176 do {
aoqi@0 177 /* prefetch beyond end */
aoqi@0 178 Prefetch::write(end, interval);
aoqi@0 179 end += oop(end)->size();
aoqi@0 180 } while (end < t && (!oop(end)->is_gc_marked()));
aoqi@0 181
aoqi@0 182 /* see if we might want to pretend this object is alive so that
aoqi@0 183 * we don't have to compact quite as often.
aoqi@0 184 */
aoqi@0 185 if (allowed_deadspace > 0 && q == compact_top) {
aoqi@0 186 size_t sz = pointer_delta(end, q);
aoqi@0 187 if (insert_deadspace(allowed_deadspace, q, sz)) {
aoqi@0 188 size_t compaction_max_size = pointer_delta(compact_end, compact_top);
aoqi@0 189
aoqi@0 190 // This should only happen if a space in the young gen overflows the
aoqi@0 191 // old gen. If that should happen, we null out the start_array, because
aoqi@0 192 // the young spaces are not covered by one.
aoqi@0 193 while (sz > compaction_max_size) {
aoqi@0 194 // First record the last compact_top
aoqi@0 195 dest->set_compaction_top(compact_top);
aoqi@0 196
aoqi@0 197 // Advance to the next compaction decorator
aoqi@0 198 advance_destination_decorator();
aoqi@0 199 dest = destination_decorator();
aoqi@0 200
aoqi@0 201 // Update compaction info
aoqi@0 202 start_array = dest->start_array();
aoqi@0 203 compact_top = dest->compaction_top();
aoqi@0 204 compact_end = dest->space()->end();
aoqi@0 205 assert(compact_top == dest->space()->bottom(), "Advanced to space already in use");
aoqi@0 206 assert(compact_end > compact_top, "Must always be space remaining");
aoqi@0 207 compaction_max_size =
aoqi@0 208 pointer_delta(compact_end, compact_top);
aoqi@0 209 }
aoqi@0 210
aoqi@0 211 // store the forwarding pointer into the mark word
aoqi@0 212 if (q != compact_top) {
aoqi@0 213 oop(q)->forward_to(oop(compact_top));
aoqi@0 214 assert(oop(q)->is_gc_marked(), "encoding the pointer should preserve the mark");
aoqi@0 215 } else {
aoqi@0 216 // if the object isn't moving we can just set the mark to the default
aoqi@0 217 // mark and handle it specially later on.
aoqi@0 218 oop(q)->init_mark();
aoqi@0 219 assert(oop(q)->forwardee() == NULL, "should be forwarded to NULL");
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 // Update object start array
aoqi@0 223 if (start_array) {
aoqi@0 224 start_array->allocate_block(compact_top);
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 compact_top += sz;
aoqi@0 228 assert(compact_top <= dest->space()->end(),
aoqi@0 229 "Exceeding space in destination");
aoqi@0 230
aoqi@0 231 q = end;
aoqi@0 232 end_of_live = end;
aoqi@0 233 continue;
aoqi@0 234 }
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 /* for the previous LiveRange, record the end of the live objects. */
aoqi@0 238 if (liveRange) {
aoqi@0 239 liveRange->set_end(q);
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 /* record the current LiveRange object.
aoqi@0 243 * liveRange->start() is overlaid on the mark word.
aoqi@0 244 */
aoqi@0 245 liveRange = (LiveRange*)q;
aoqi@0 246 liveRange->set_start(end);
aoqi@0 247 liveRange->set_end(end);
aoqi@0 248
aoqi@0 249 /* see if this is the first dead region. */
aoqi@0 250 if (q < first_dead) {
aoqi@0 251 first_dead = q;
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 /* move on to the next object */
aoqi@0 255 q = end;
aoqi@0 256 }
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 assert(q == t, "just checking");
aoqi@0 260 if (liveRange != NULL) {
aoqi@0 261 liveRange->set_end(q);
aoqi@0 262 }
aoqi@0 263 _end_of_live = end_of_live;
aoqi@0 264 if (end_of_live < first_dead) {
aoqi@0 265 first_dead = end_of_live;
aoqi@0 266 }
aoqi@0 267 _first_dead = first_dead;
aoqi@0 268
aoqi@0 269 // Update compaction top
aoqi@0 270 dest->set_compaction_top(compact_top);
aoqi@0 271 }
aoqi@0 272
aoqi@0 273 bool PSMarkSweepDecorator::insert_deadspace(size_t& allowed_deadspace_words,
aoqi@0 274 HeapWord* q, size_t deadlength) {
aoqi@0 275 if (allowed_deadspace_words >= deadlength) {
aoqi@0 276 allowed_deadspace_words -= deadlength;
aoqi@0 277 CollectedHeap::fill_with_object(q, deadlength);
aoqi@0 278 oop(q)->set_mark(oop(q)->mark()->set_marked());
aoqi@0 279 assert((int) deadlength == oop(q)->size(), "bad filler object size");
aoqi@0 280 // Recall that we required "q == compaction_top".
aoqi@0 281 return true;
aoqi@0 282 } else {
aoqi@0 283 allowed_deadspace_words = 0;
aoqi@0 284 return false;
aoqi@0 285 }
aoqi@0 286 }
aoqi@0 287
aoqi@0 288 void PSMarkSweepDecorator::adjust_pointers() {
aoqi@0 289 // adjust all the interior pointers to point at the new locations of objects
aoqi@0 290 // Used by MarkSweep::mark_sweep_phase3()
aoqi@0 291
aoqi@0 292 HeapWord* q = space()->bottom();
aoqi@0 293 HeapWord* t = _end_of_live; // Established by "prepare_for_compaction".
aoqi@0 294
aoqi@0 295 assert(_first_dead <= _end_of_live, "Stands to reason, no?");
aoqi@0 296
aoqi@0 297 if (q < t && _first_dead > q &&
aoqi@0 298 !oop(q)->is_gc_marked()) {
aoqi@0 299 // we have a chunk of the space which hasn't moved and we've
aoqi@0 300 // reinitialized the mark word during the previous pass, so we can't
aoqi@0 301 // use is_gc_marked for the traversal.
aoqi@0 302 HeapWord* end = _first_dead;
aoqi@0 303
aoqi@0 304 while (q < end) {
aoqi@0 305 // point all the oops to the new location
aoqi@0 306 size_t size = oop(q)->adjust_pointers();
aoqi@0 307 q += size;
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 if (_first_dead == t) {
aoqi@0 311 q = t;
aoqi@0 312 } else {
aoqi@0 313 // $$$ This is funky. Using this to read the previously written
aoqi@0 314 // LiveRange. See also use below.
aoqi@0 315 q = (HeapWord*)oop(_first_dead)->mark()->decode_pointer();
aoqi@0 316 }
aoqi@0 317 }
aoqi@0 318 const intx interval = PrefetchScanIntervalInBytes;
aoqi@0 319
aoqi@0 320 debug_only(HeapWord* prev_q = NULL);
aoqi@0 321 while (q < t) {
aoqi@0 322 // prefetch beyond q
aoqi@0 323 Prefetch::write(q, interval);
aoqi@0 324 if (oop(q)->is_gc_marked()) {
aoqi@0 325 // q is alive
aoqi@0 326 // point all the oops to the new location
aoqi@0 327 size_t size = oop(q)->adjust_pointers();
aoqi@0 328 debug_only(prev_q = q);
aoqi@0 329 q += size;
aoqi@0 330 } else {
aoqi@0 331 // q is not a live object, so its mark should point at the next
aoqi@0 332 // live object
aoqi@0 333 debug_only(prev_q = q);
aoqi@0 334 q = (HeapWord*) oop(q)->mark()->decode_pointer();
aoqi@0 335 assert(q > prev_q, "we should be moving forward through memory");
aoqi@0 336 }
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 assert(q == t, "just checking");
aoqi@0 340 }
aoqi@0 341
aoqi@0 342 void PSMarkSweepDecorator::compact(bool mangle_free_space ) {
aoqi@0 343 // Copy all live objects to their new location
aoqi@0 344 // Used by MarkSweep::mark_sweep_phase4()
aoqi@0 345
aoqi@0 346 HeapWord* q = space()->bottom();
aoqi@0 347 HeapWord* const t = _end_of_live;
aoqi@0 348 debug_only(HeapWord* prev_q = NULL);
aoqi@0 349
aoqi@0 350 if (q < t && _first_dead > q &&
aoqi@0 351 !oop(q)->is_gc_marked()) {
aoqi@0 352 #ifdef ASSERT
aoqi@0 353 // we have a chunk of the space which hasn't moved and we've reinitialized the
aoqi@0 354 // mark word during the previous pass, so we can't use is_gc_marked for the
aoqi@0 355 // traversal.
aoqi@0 356 HeapWord* const end = _first_dead;
aoqi@0 357
aoqi@0 358 while (q < end) {
aoqi@0 359 size_t size = oop(q)->size();
aoqi@0 360 assert(!oop(q)->is_gc_marked(), "should be unmarked (special dense prefix handling)");
aoqi@0 361 debug_only(prev_q = q);
aoqi@0 362 q += size;
aoqi@0 363 }
aoqi@0 364 #endif
aoqi@0 365
aoqi@0 366 if (_first_dead == t) {
aoqi@0 367 q = t;
aoqi@0 368 } else {
aoqi@0 369 // $$$ Funky
aoqi@0 370 q = (HeapWord*) oop(_first_dead)->mark()->decode_pointer();
aoqi@0 371 }
aoqi@0 372 }
aoqi@0 373
aoqi@0 374 const intx scan_interval = PrefetchScanIntervalInBytes;
aoqi@0 375 const intx copy_interval = PrefetchCopyIntervalInBytes;
aoqi@0 376
aoqi@0 377 while (q < t) {
aoqi@0 378 if (!oop(q)->is_gc_marked()) {
aoqi@0 379 // mark is pointer to next marked oop
aoqi@0 380 debug_only(prev_q = q);
aoqi@0 381 q = (HeapWord*) oop(q)->mark()->decode_pointer();
aoqi@0 382 assert(q > prev_q, "we should be moving forward through memory");
aoqi@0 383 } else {
aoqi@0 384 // prefetch beyond q
aoqi@0 385 Prefetch::read(q, scan_interval);
aoqi@0 386
aoqi@0 387 // size and destination
aoqi@0 388 size_t size = oop(q)->size();
aoqi@0 389 HeapWord* compaction_top = (HeapWord*)oop(q)->forwardee();
aoqi@0 390
aoqi@0 391 // prefetch beyond compaction_top
aoqi@0 392 Prefetch::write(compaction_top, copy_interval);
aoqi@0 393
aoqi@0 394 // copy object and reinit its mark
aoqi@0 395 assert(q != compaction_top, "everything in this pass should be moving");
aoqi@0 396 Copy::aligned_conjoint_words(q, compaction_top, size);
aoqi@0 397 oop(compaction_top)->init_mark();
aoqi@0 398 assert(oop(compaction_top)->klass() != NULL, "should have a class");
aoqi@0 399
aoqi@0 400 debug_only(prev_q = q);
aoqi@0 401 q += size;
aoqi@0 402 }
aoqi@0 403 }
aoqi@0 404
aoqi@0 405 assert(compaction_top() >= space()->bottom() && compaction_top() <= space()->end(),
aoqi@0 406 "should point inside space");
aoqi@0 407 space()->set_top(compaction_top());
aoqi@0 408
aoqi@0 409 if (mangle_free_space) {
aoqi@0 410 space()->mangle_unused_area();
aoqi@0 411 }
aoqi@0 412 }

mercurial