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            space->make_oop(q)->mark()->is_marked() ||
 125            space->make_oop(q)->mark()->is_unlocked() ||
 126            space->make_oop(q)->mark()->has_bias_pattern() ||
 127            oopDesc::bs()->read_barrier(space->make_oop(q)) != space->make_oop(q),
 128            "these are the only valid states during a mark sweep");
 129     if (space->scanned_block_is_obj(q) && space->make_oop(q)->is_gc_marked()) {
 130       // prefetch beyond q
 131       Prefetch::write(q, interval);
 132       size_t size = space->scanned_block_size(q);
 133       compact_top = cp->space->forward(space->make_oop(q), size, cp, compact_top);
 134       q += size;
 135       end_of_live = q;
 136     } else {
 137       // run over all the contiguous dead objects
 138       HeapWord* end = q;
 139       do {
 140         // prefetch beyond end
 141         Prefetch::write(end, interval);
 142         end += space->scanned_block_size(end);
 143       } while (end < t && (!space->scanned_block_is_obj(end) || !space->make_oop(end)->is_gc_marked()));
 144 
 145       // see if we might want to pretend this object is alive so that
 146       // we don't have to compact quite as often.
 147       if (allowed_deadspace > 0 && q == compact_top) {
 148         size_t sz = pointer_delta(end, q);
 149         if (space->insert_deadspace(allowed_deadspace, q, sz)) {
 150           compact_top = cp->space->forward(space->make_oop(q), sz, cp, compact_top);
 151           q = end;
 152           end_of_live = end;
 153           continue;
 154         }
 155       }
 156 
 157       // otherwise, it really is a free region.
 158 
 159       // for the previous LiveRange, record the end of the live objects.
 160       if (liveRange) {
 161         liveRange->set_end(q);
 162       }
 163 
 164       // record the current LiveRange object.
 165       // liveRange->start() is overlaid on the mark word.
 166       liveRange = (LiveRange*) (HeapWord*) space->make_oop(q);
 167       liveRange->set_start(end);
 168       liveRange->set_end(end);
 169 
 170       // see if this is the first dead region.
 171       if (q < first_dead) {
 172         first_dead = q;
 173       }
 174 
 175       // move on to the next object
 176       q = end;
 177     }
 178   }
 179 
 180   assert(q == t, "just checking");
 181   if (liveRange != NULL) {
 182     liveRange->set_end(q);
 183   }
 184   space->_end_of_live = end_of_live;
 185   if (end_of_live < first_dead) {
 186     first_dead = end_of_live;
 187   }
 188   space->_first_dead = first_dead;
 189 
 190   // save the compaction_top of the compaction space.
 191   cp->space->set_compaction_top(compact_top);
 192 }
 193 
 194 template <class SpaceType>
 195 inline void CompactibleSpace::scan_and_adjust_pointers(SpaceType* space) {
 196   // adjust all the interior pointers to point at the new locations of objects
 197   // Used by MarkSweep::mark_sweep_phase3()
 198 
 199   HeapWord* q = space->bottom();
 200   HeapWord* t = space->_end_of_live;  // Established by "prepare_for_compaction".
 201 
 202   assert(space->_first_dead <= space->_end_of_live, "Stands to reason, no?");
 203 
 204   if (q < t && space->_first_dead > q && !space->make_oop(q)->is_gc_marked()) {
 205     // we have a chunk of the space which hasn't moved and we've
 206     // reinitialized the mark word during the previous pass, so we can't
 207     // use is_gc_marked for the traversal.
 208     HeapWord* end = space->_first_dead;
 209 
 210     while (q < end) {
 211       // I originally tried to conjoin "block_start(q) == q" to the
 212       // assertion below, but that doesn't work, because you can't
 213       // accurately traverse previous objects to get to the current one
 214       // after their pointers have been
 215       // updated, until the actual compaction is done.  dld, 4/00
 216       assert(space->block_is_obj(q), "should be at block boundaries, and should be looking at objs");
 217 
 218       // point all the oops to the new location
 219       size_t size = MarkSweep::adjust_pointers(space->make_oop(q));
 220       size = space->adjust_obj_size(size);
 221 
 222       q += size;
 223     }
 224 
 225     if (space->_first_dead == t) {
 226       q = t;
 227     } else {
 228       // $$$ This is funky.  Using this to read the previously written
 229       // LiveRange.  See also use below.
 230       q = (HeapWord*)oop(space->_first_dead)->mark()->decode_pointer();
 231     }
 232   }
 233 
 234   const intx interval = PrefetchScanIntervalInBytes;
 235 
 236   debug_only(HeapWord* prev_q = NULL);
 237   while (q < t) {
 238     // prefetch beyond q
 239     Prefetch::write(q, interval);
 240     if (space->make_oop(q)->is_gc_marked()) {
 241       // q is alive
 242       // point all the oops to the new location
 243       size_t size = MarkSweep::adjust_pointers(space->make_oop(q));
 244       size = space->adjust_obj_size(size);
 245       debug_only(prev_q = q);
 246       q += size;
 247     } else {
 248       // q is not a live object, so its mark should point at the next
 249       // live object
 250       debug_only(prev_q = q);
 251       q = (HeapWord*) space->make_oop(q)->mark()->decode_pointer();
 252       assert(q > prev_q, "we should be moving forward through memory");
 253     }
 254   }
 255 
 256   assert(q == t, "just checking");
 257 }
 258 
 259 template <class SpaceType>
 260 inline void CompactibleSpace::scan_and_compact(SpaceType* space) {
 261   // Copy all live objects to their new location
 262   // Used by MarkSweep::mark_sweep_phase4()
 263 
 264   HeapWord*       q = space->bottom();
 265   HeapWord* const t = space->_end_of_live;
 266   debug_only(HeapWord* prev_q = NULL);
 267 
 268   if (q < t && space->_first_dead > q && !space->make_oop(q)->is_gc_marked()) {
 269     #ifdef ASSERT // Debug only
 270       // we have a chunk of the space which hasn't moved and we've reinitialized
 271       // the mark word during the previous pass, so we can't use is_gc_marked for
 272       // the traversal.
 273       HeapWord* const end = space->_first_dead;
 274 
 275       while (q < end) {
 276         size_t size = space->obj_size(q);
 277         assert(!space->make_oop(q)->is_gc_marked(), "should be unmarked (special dense prefix handling)");
 278         prev_q = q;
 279         q += size;
 280       }
 281     #endif
 282 
 283     if (space->_first_dead == t) {
 284       q = t;
 285     } else {
 286       // $$$ Funky
 287       q = (HeapWord*) oop(space->_first_dead)->mark()->decode_pointer();
 288     }
 289   }
 290 
 291   const intx scan_interval = PrefetchScanIntervalInBytes;
 292   const intx copy_interval = PrefetchCopyIntervalInBytes;
 293   while (q < t) {
 294     if (!space->make_oop(q)->is_gc_marked()) {
 295       // mark is pointer to next marked oop
 296       debug_only(prev_q = q);
 297       q = (HeapWord*) space->make_oop(q)->mark()->decode_pointer();
 298       assert(q > prev_q, "we should be moving forward through memory");
 299     } else {
 300       // prefetch beyond q
 301       Prefetch::read(q, scan_interval);
 302 
 303       // size and destination
 304       size_t size = space->obj_size(q);
 305       HeapWord* compaction_top = (HeapWord*)space->make_oop(q)->forwardee();
 306 
 307       // prefetch beyond compaction_top
 308       Prefetch::write(compaction_top, copy_interval);
 309 
 310       // copy object and reinit its mark
 311       assert(q != compaction_top, "everything in this pass should be moving");
 312       Copy::aligned_conjoint_words((HeapWord*) space->make_oop(q), compaction_top, size);
 313       oop(compaction_top)->init_mark();
 314       assert(oop(compaction_top)->klass() != NULL, "should have a class");
 315 
 316       debug_only(prev_q = q);
 317       q += size;
 318     }
 319   }
 320 
 321   // Let's remember if we were empty before we did the compaction.
 322   bool was_empty = space->used_region().is_empty();
 323   // Reset space after compaction is complete
 324   space->reset_after_compaction();
 325   // We do this clear, below, since it has overloaded meanings for some
 326   // space subtypes.  For example, OffsetTableContigSpace's that were
 327   // compacted into will have had their offset table thresholds updated
 328   // continuously, but those that weren't need to have their thresholds
 329   // re-initialized.  Also mangles unused area for debugging.
 330   if (space->used_region().is_empty()) {
 331     if (!was_empty) space->clear(SpaceDecorator::Mangle);
 332   } else {
 333     if (ZapUnusedHeapArea) space->mangle_unused_area();
 334   }
 335 }
 336 #endif // SHARE_VM_GC_SHARED_SPACE_INLINE_HPP