1 /*
   2  * Copyright (c) 2015, 2019, 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 #ifndef SHARE_GC_Z_ZLIVEMAP_INLINE_HPP
  25 #define SHARE_GC_Z_ZLIVEMAP_INLINE_HPP
  26 
  27 #include "gc/z/zBitMap.inline.hpp"
  28 #include "gc/z/zLiveMap.hpp"
  29 #include "gc/z/zMark.hpp"
  30 #include "gc/z/zOop.inline.hpp"
  31 #include "gc/z/zUtils.inline.hpp"
  32 #include "runtime/atomic.hpp"
  33 #include "runtime/orderAccess.hpp"
  34 #include "utilities/bitMap.inline.hpp"
  35 #include "utilities/debug.hpp"
  36 
  37 inline void ZLiveMap::reset() {
  38   _seqnum = 0;
  39 }
  40 
  41 inline bool ZLiveMap::is_marked() const {
  42   return Atomic::load_acquire(&_seqnum) == ZGlobalSeqNum;
  43 }
  44 
  45 inline uint32_t ZLiveMap::live_objects() const {
  46   assert(ZGlobalPhase != ZPhaseMark, "Invalid phase");
  47   return _live_objects;
  48 }
  49 
  50 inline size_t ZLiveMap::live_bytes() const {
  51   assert(ZGlobalPhase != ZPhaseMark, "Invalid phase");
  52   return _live_bytes;
  53 }
  54 
  55 inline const BitMapView ZLiveMap::segment_live_bits() const {
  56   return BitMapView(const_cast<BitMap::bm_word_t*>(&_segment_live_bits), nsegments);
  57 }
  58 
  59 inline const BitMapView ZLiveMap::segment_claim_bits() const {
  60   return BitMapView(const_cast<BitMap::bm_word_t*>(&_segment_claim_bits), nsegments);
  61 }
  62 
  63 inline BitMapView ZLiveMap::segment_live_bits() {
  64   return BitMapView(&_segment_live_bits, nsegments);
  65 }
  66 
  67 inline BitMapView ZLiveMap::segment_claim_bits() {
  68   return BitMapView(&_segment_claim_bits, nsegments);
  69 }
  70 
  71 inline bool ZLiveMap::is_segment_live(BitMap::idx_t segment) const {
  72   return segment_live_bits().par_at(segment);
  73 }
  74 
  75 inline bool ZLiveMap::set_segment_live(BitMap::idx_t segment) {
  76   return segment_live_bits().par_set_bit(segment, memory_order_release);
  77 }
  78 
  79 inline bool ZLiveMap::claim_segment(BitMap::idx_t segment) {
  80   return segment_claim_bits().par_set_bit(segment, memory_order_acq_rel);
  81 }
  82 
  83 inline BitMap::idx_t ZLiveMap::first_live_segment() const {
  84   return segment_live_bits().get_next_one_offset(0, nsegments);
  85 }
  86 
  87 inline BitMap::idx_t ZLiveMap::next_live_segment(BitMap::idx_t segment) const {
  88   return segment_live_bits().get_next_one_offset(segment + 1, nsegments);
  89 }
  90 
  91 inline BitMap::idx_t ZLiveMap::segment_size() const {
  92   return _bitmap.size() / nsegments;
  93 }
  94 
  95 inline BitMap::idx_t ZLiveMap::index_to_segment(BitMap::idx_t index) const {
  96   return index >> _segment_shift;
  97 }
  98 
  99 inline bool ZLiveMap::get(size_t index) const {
 100   BitMap::idx_t segment = index_to_segment(index);
 101   return is_marked() &&              // Page is marked
 102          is_segment_live(segment) && // Segment is marked
 103          _bitmap.at(index);          // Object is marked
 104 }
 105 
 106 inline bool ZLiveMap::set(size_t index, bool finalizable, bool& inc_live) {
 107   if (!is_marked()) {
 108     // First object to be marked during this
 109     // cycle, reset marking information.
 110     reset(index);
 111   }
 112 
 113   const BitMap::idx_t segment = index_to_segment(index);
 114   if (!is_segment_live(segment)) {
 115     // First object to be marked in this segment during
 116     // this cycle, reset segment bitmap.
 117     reset_segment(segment);
 118   }
 119 
 120   return _bitmap.par_set_bit_pair(index, finalizable, inc_live);
 121 }
 122 
 123 inline void ZLiveMap::inc_live(uint32_t objects, size_t bytes) {
 124   Atomic::add(objects, &_live_objects);
 125   Atomic::add(bytes, &_live_bytes);
 126 }
 127 
 128 inline BitMap::idx_t ZLiveMap::segment_start(BitMap::idx_t segment) const {
 129   return segment_size() * segment;
 130 }
 131 
 132 inline BitMap::idx_t ZLiveMap::segment_end(BitMap::idx_t segment) const {
 133   return segment_start(segment) + segment_size();
 134 }
 135 
 136 inline void ZLiveMap::iterate_segment(ObjectClosure* cl, BitMap::idx_t segment, uintptr_t page_start, size_t page_object_alignment_shift) {
 137   assert(is_segment_live(segment), "Must be");
 138 
 139   const BitMap::idx_t start_index = segment_start(segment);
 140   const BitMap::idx_t end_index   = segment_end(segment);
 141   BitMap::idx_t index = _bitmap.get_next_one_offset(start_index, end_index);
 142 
 143   while (index < end_index) {
 144     // Calculate object address
 145     const uintptr_t addr = page_start + ((index / 2) << page_object_alignment_shift);
 146 
 147     // Apply closure
 148     cl->do_object(ZOop::from_address(addr));
 149 
 150     // Find next bit after this object
 151     const size_t size = ZUtils::object_size(addr);
 152     const uintptr_t next_addr = align_up(addr + size, 1 << page_object_alignment_shift);
 153     const BitMap::idx_t next_index = ((next_addr - page_start) >> page_object_alignment_shift) * 2;
 154     if (next_index >= end_index) {
 155       // End of live map
 156       break;
 157     }
 158 
 159     index = _bitmap.get_next_one_offset(next_index, end_index);
 160   }
 161 }
 162 
 163 inline void ZLiveMap::iterate(ObjectClosure* cl, uintptr_t page_start, size_t page_object_alignment_shift) {
 164   if (is_marked()) {
 165     for (BitMap::idx_t segment = first_live_segment(); segment < nsegments; segment = next_live_segment(segment)) {
 166       // For each live segment
 167       iterate_segment(cl, segment, page_start, page_object_alignment_shift);
 168     }
 169   }
 170 }
 171 
 172 #endif // SHARE_GC_Z_ZLIVEMAP_INLINE_HPP