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