1 /* 2 * Copyright (c) 2000, 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 #ifndef SHARE_VM_GC_SHARED_SPACE_INLINE_HPP 26 #define SHARE_VM_GC_SHARED_SPACE_INLINE_HPP 27 28 #include "gc/serial/markSweep.inline.hpp" 29 #include "gc/shared/collectedHeap.hpp" 30 #include "gc/shared/generation.hpp" 31 #include "gc/shared/liveRange.hpp" 32 #include "gc/shared/space.hpp" 33 #include "gc/shared/spaceDecorator.hpp" 34 #include "memory/universe.hpp" 35 #include "runtime/prefetch.inline.hpp" 36 #include "runtime/safepoint.hpp" 37 38 inline HeapWord* Space::block_start(const void* p) { 39 return block_start_const(p); 40 } 41 42 inline HeapWord* OffsetTableContigSpace::allocate(size_t size) { 43 HeapWord* res = ContiguousSpace::allocate(size); 44 if (res != NULL) { 45 _offsets.alloc_block(res, size); 46 } 47 return res; 48 } 49 50 // Because of the requirement of keeping "_offsets" up to date with the 51 // allocations, we sequentialize these with a lock. Therefore, best if 52 // this is used for larger LAB allocations only. 53 inline HeapWord* OffsetTableContigSpace::par_allocate(size_t size) { 54 MutexLocker x(&_par_alloc_lock); 55 // This ought to be just "allocate", because of the lock above, but that 56 // ContiguousSpace::allocate asserts that either the allocating thread 57 // holds the heap lock or it is the VM thread and we're at a safepoint. 58 // The best I (dld) could figure was to put a field in ContiguousSpace 59 // meaning "locking at safepoint taken care of", and set/reset that 60 // here. But this will do for now, especially in light of the comment 61 // above. Perhaps in the future some lock-free manner of keeping the 62 // coordination. 63 HeapWord* res = ContiguousSpace::par_allocate(size); 64 if (res != NULL) { 65 _offsets.alloc_block(res, size); 66 } 67 return res; 68 } 69 70 inline HeapWord* 71 OffsetTableContigSpace::block_start_const(const void* p) const { 72 return _offsets.block_start(p); 73 } 74 75 template <class SpaceType> 76 inline void CompactibleSpace::scan_and_forward(SpaceType* space, CompactPoint* cp) { 77 // Compute the new addresses for the live objects and store it in the mark 78 // Used by universe::mark_sweep_phase2() 79 HeapWord* compact_top; // This is where we are currently compacting to. 80 81 // We're sure to be here before any objects are compacted into this 82 // space, so this is a good time to initialize this: 83 space->set_compaction_top(space->bottom()); 84 85 if (cp->space == NULL) { 86 assert(cp->gen != NULL, "need a generation"); 87 assert(cp->threshold == NULL, "just checking"); 88 assert(cp->gen->first_compaction_space() == space, "just checking"); 89 cp->space = cp->gen->first_compaction_space(); 90 compact_top = cp->space->bottom(); 91 cp->space->set_compaction_top(compact_top); 92 cp->threshold = cp->space->initialize_threshold(); 93 } else { 94 compact_top = cp->space->compaction_top(); 95 } 96 97 // We allow some amount of garbage towards the bottom of the space, so 98 // we don't start compacting before there is a significant gain to be made. 99 // Occasionally, we want to ensure a full compaction, which is determined 100 // by the MarkSweepAlwaysCompactCount parameter. 101 uint invocations = MarkSweep::total_invocations(); 102 bool skip_dead = ((invocations % MarkSweepAlwaysCompactCount) != 0); 103 104 size_t allowed_deadspace = 0; 105 if (skip_dead) { 106 const size_t ratio = space->allowed_dead_ratio(); 107 allowed_deadspace = (space->capacity() * ratio / 100) / HeapWordSize; 108 } 109 110 HeapWord* q = space->bottom(); 111 HeapWord* t = space->scan_limit(); 112 113 HeapWord* end_of_live= q; // One byte beyond the last byte of the last 114 // live object. 115 HeapWord* first_dead = space->end(); // The first dead object. 116 LiveRange* liveRange = NULL; // The current live range, recorded in the 117 // first header of preceding free area. 118 space->_first_dead = first_dead; 119 120 const intx interval = PrefetchScanIntervalInBytes; 121 122 while (q < t) { 123 assert(!space->scanned_block_is_obj(q) || 124 oop(q)->mark()->is_marked() || oop(q)->mark()->is_unlocked() || 125 oop(q)->mark()->has_bias_pattern(), 126 "these are the only valid states during a mark sweep"); 127 if (space->scanned_block_is_obj(q) && oop(q)->is_gc_marked()) { 128 // prefetch beyond q 129 Prefetch::write(q, interval); 130 size_t size = space->scanned_block_size(q); 131 compact_top = cp->space->forward(oop(q), size, cp, compact_top); 132 q += size; 133 end_of_live = q; 134 } else { 135 // run over all the contiguous dead objects 136 HeapWord* end = q; 137 do { 138 // prefetch beyond end 139 Prefetch::write(end, interval); 140 end += space->scanned_block_size(end); 141 } while (end < t && (!space->scanned_block_is_obj(end) || !oop(end)->is_gc_marked())); 142 143 // see if we might want to pretend this object is alive so that 144 // we don't have to compact quite as often. 145 if (allowed_deadspace > 0 && q == compact_top) { 146 size_t sz = pointer_delta(end, q); 147 if (space->insert_deadspace(allowed_deadspace, q, sz)) { 148 compact_top = cp->space->forward(oop(q), sz, cp, compact_top); 149 q = end; 150 end_of_live = end; 151 continue; 152 } 153 } 154 155 // otherwise, it really is a free region. 156 157 // for the previous LiveRange, record the end of the live objects. 158 if (liveRange) { 159 liveRange->set_end(q); 160 } 161 162 // record the current LiveRange object. 163 // liveRange->start() is overlaid on the mark word. 164 liveRange = (LiveRange*)q; 165 liveRange->set_start(end); 166 liveRange->set_end(end); 167 168 // see if this is the first dead region. 169 if (q < first_dead) { 170 first_dead = q; 171 } 172 173 // move on to the next object 174 q = end; 175 } 176 } 177 178 assert(q == t, "just checking"); 179 if (liveRange != NULL) { 180 liveRange->set_end(q); 181 } 182 space->_end_of_live = end_of_live; 183 if (end_of_live < first_dead) { 184 first_dead = end_of_live; 185 } 186 space->_first_dead = first_dead; 187 188 // save the compaction_top of the compaction space. 189 cp->space->set_compaction_top(compact_top); 190 } 191 192 template <class SpaceType> 193 inline void CompactibleSpace::scan_and_adjust_pointers(SpaceType* space) { 194 // adjust all the interior pointers to point at the new locations of objects 195 // Used by MarkSweep::mark_sweep_phase3() 196 197 HeapWord* q = space->bottom(); 198 HeapWord* t = space->_end_of_live; // Established by "prepare_for_compaction". 199 200 assert(space->_first_dead <= space->_end_of_live, "Stands to reason, no?"); 201 202 if (q < t && space->_first_dead > q && !oop(q)->is_gc_marked()) { 203 // we have a chunk of the space which hasn't moved and we've 204 // reinitialized the mark word during the previous pass, so we can't 205 // use is_gc_marked for the traversal. 206 HeapWord* end = space->_first_dead; 207 208 while (q < end) { 209 // I originally tried to conjoin "block_start(q) == q" to the 210 // assertion below, but that doesn't work, because you can't 211 // accurately traverse previous objects to get to the current one 212 // after their pointers have been 213 // updated, until the actual compaction is done. dld, 4/00 214 assert(space->block_is_obj(q), "should be at block boundaries, and should be looking at objs"); 215 216 // point all the oops to the new location 217 size_t size = MarkSweep::adjust_pointers(oop(q)); 218 size = space->adjust_obj_size(size); 219 220 q += size; 221 } 222 223 if (space->_first_dead == t) { 224 q = t; 225 } else { 226 // $$$ This is funky. Using this to read the previously written 227 // LiveRange. See also use below. 228 q = (HeapWord*)oop(space->_first_dead)->mark()->decode_pointer(); 229 } 230 } 231 232 const intx interval = PrefetchScanIntervalInBytes; 233 234 debug_only(HeapWord* prev_q = NULL); 235 while (q < t) { 236 // prefetch beyond q 237 Prefetch::write(q, interval); 238 if (oop(q)->is_gc_marked()) { 239 // q is alive 240 // point all the oops to the new location 241 size_t size = MarkSweep::adjust_pointers(oop(q)); 242 size = space->adjust_obj_size(size); 243 debug_only(prev_q = q); 244 q += size; 245 } else { 246 // q is not a live object, so its mark should point at the next 247 // live object 248 debug_only(prev_q = q); 249 q = (HeapWord*) oop(q)->mark()->decode_pointer(); 250 assert(q > prev_q, "we should be moving forward through memory"); 251 } 252 } 253 254 assert(q == t, "just checking"); 255 } 256 257 template <class SpaceType> 258 inline void CompactibleSpace::scan_and_compact(SpaceType* space) { 259 // Copy all live objects to their new location 260 // Used by MarkSweep::mark_sweep_phase4() 261 262 HeapWord* q = space->bottom(); 263 HeapWord* const t = space->_end_of_live; 264 debug_only(HeapWord* prev_q = NULL); 265 266 if (q < t && space->_first_dead > q && !oop(q)->is_gc_marked()) { 267 #ifdef ASSERT // Debug only 268 // we have a chunk of the space which hasn't moved and we've reinitialized 269 // the mark word during the previous pass, so we can't use is_gc_marked for 270 // the traversal. 271 HeapWord* const end = space->_first_dead; 272 273 while (q < end) { 274 size_t size = space->obj_size(q); 275 assert(!oop(q)->is_gc_marked(), "should be unmarked (special dense prefix handling)"); 276 prev_q = q; 277 q += size; 278 } 279 #endif 280 281 if (space->_first_dead == t) { 282 q = t; 283 } else { 284 // $$$ Funky 285 q = (HeapWord*) oop(space->_first_dead)->mark()->decode_pointer(); 286 } 287 } 288 289 const intx scan_interval = PrefetchScanIntervalInBytes; 290 const intx copy_interval = PrefetchCopyIntervalInBytes; 291 while (q < t) { 292 if (!oop(q)->is_gc_marked()) { 293 // mark is pointer to next marked oop 294 debug_only(prev_q = q); 295 q = (HeapWord*) oop(q)->mark()->decode_pointer(); 296 assert(q > prev_q, "we should be moving forward through memory"); 297 } else { 298 // prefetch beyond q 299 Prefetch::read(q, scan_interval); 300 301 // size and destination 302 size_t size = space->obj_size(q); 303 HeapWord* compaction_top = (HeapWord*)oop(q)->forwardee(); 304 305 // prefetch beyond compaction_top 306 Prefetch::write(compaction_top, copy_interval); 307 308 // copy object and reinit its mark 309 assert(q != compaction_top, "everything in this pass should be moving"); 310 Copy::aligned_conjoint_words(q, compaction_top, size); 311 oop(compaction_top)->init_mark(); 312 assert(oop(compaction_top)->klass() != NULL, "should have a class"); 313 314 debug_only(prev_q = q); 315 q += size; 316 } 317 } 318 319 // Let's remember if we were empty before we did the compaction. 320 bool was_empty = space->used_region().is_empty(); 321 // Reset space after compaction is complete 322 space->reset_after_compaction(); 323 // We do this clear, below, since it has overloaded meanings for some 324 // space subtypes. For example, OffsetTableContigSpace's that were 325 // compacted into will have had their offset table thresholds updated 326 // continuously, but those that weren't need to have their thresholds 327 // re-initialized. Also mangles unused area for debugging. 328 if (space->used_region().is_empty()) { 329 if (!was_empty) space->clear(SpaceDecorator::Mangle); 330 } else { 331 if (ZapUnusedHeapArea) space->mangle_unused_area(); 332 } 333 } 334 #endif // SHARE_VM_GC_SHARED_SPACE_INLINE_HPP