1 /*
   2  * Copyright (c) 2015, 2017, 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 #include "precompiled.hpp"
  25 #include "gc/z/zHeap.inline.hpp"
  26 #include "gc/z/zLiveMap.inline.hpp"
  27 #include "gc/z/zStat.hpp"
  28 #include "gc/z/zThread.hpp"
  29 #include "logging/log.hpp"
  30 #include "runtime/atomic.hpp"
  31 #include "runtime/orderAccess.inline.hpp"
  32 #include "utilities/debug.hpp"
  33 
  34 static const ZStatCounter ZCounterMarkSeqNumResetContention("Contention", "Mark SeqNum Reset Contention", ZStatUnitOpsPerSecond);
  35 static const ZStatCounter ZCounterMarkSegmentResetContention("Contention", "Mark Segment Reset Contention", ZStatUnitOpsPerSecond);
  36 
  37 ZLiveMap::ZLiveMap(uint32_t size) :
  38     _seqnum(0),
  39     _live_objects(0),
  40     _live_bytes(0),
  41     _segment_live_bits(0),
  42     _segment_claim_bits(0),
  43     // We need at least one bit per segment.
  44     _bitmap(MAX2<size_t>(size, nsegments) * 2),
  45     _shift(exact_log2(segment_size())) {}
  46 
  47 void ZLiveMap::reset(size_t index) {
  48   const uint32_t seqnum_initializing = (uint32_t)-1;
  49   bool contention = false;
  50 
  51   // Multiple threads can enter here, make sure only one of them
  52   // resets the marking information while the others busy wait.
  53   for (uint32_t seqnum = _seqnum; seqnum != ZGlobalSeqNum; seqnum = _seqnum) {
  54     if ((seqnum != seqnum_initializing) &&
  55         (Atomic::cmpxchg(seqnum_initializing, &_seqnum, seqnum) == seqnum)) {
  56       // Reset marking information
  57       _live_bytes = 0;
  58       _live_objects = 0;
  59 
  60       // Clear segment claimed/live bits
  61       segment_live_bits().clear();
  62       segment_claim_bits().clear();
  63 
  64       // Make sure the newly reset marking information is
  65       // globally visible before updating the page seqnum.
  66       OrderAccess::storestore();
  67 
  68       // Update seqnum
  69       assert(_seqnum == seqnum_initializing, "Invalid");
  70       _seqnum = ZGlobalSeqNum;
  71       break;
  72     }
  73 
  74     // Mark reset contention
  75     if (!contention) {
  76       // Count contention once, not every loop
  77       ZStatInc(ZCounterMarkSeqNumResetContention);
  78       contention = true;
  79 
  80       log_trace(gc)("Mark seqnum reset contention, thread: " PTR_FORMAT " (%s), map: " PTR_FORMAT ", bit: " SIZE_FORMAT,
  81                     ZThread::id(), ZThread::name(), p2i(this), index);
  82     }
  83   }
  84 }
  85 
  86 void ZLiveMap::reset_segment(BitMap::idx_t segment) {
  87   bool contention = false;
  88 
  89   if (!claim_segment(segment)) {
  90     // Already claimed, wait for live bit to be set
  91     while (!is_segment_live(segment)) {
  92       // Busy wait. The loadload barrier is needed to make
  93       // sure we re-read the live bit every time we loop.
  94       OrderAccess::loadload();
  95 
  96       // Mark reset contention
  97       if (!contention) {
  98         // Count contention once, not every loop
  99         ZStatInc(ZCounterMarkSegmentResetContention);
 100         contention = true;
 101 
 102         log_trace(gc)("Mark segment reset contention, thread: " PTR_FORMAT " (%s), map: " PTR_FORMAT ", segment: " SIZE_FORMAT,
 103                       ZThread::id(), ZThread::name(), p2i(this), segment);
 104       }
 105     }
 106 
 107     // Segment is live
 108     return;
 109   }
 110 
 111   // Segment claimed, clear it
 112   const BitMap::idx_t start_index = segment_start(segment);
 113   const BitMap::idx_t end_index   = segment_end(segment);
 114   if (segment_size() / BitsPerWord >= 32) {
 115     _bitmap.clear_large_range(start_index, end_index);
 116   } else {
 117     _bitmap.clear_range(start_index, end_index);
 118   }
 119 
 120   // Set live bit
 121   const bool success = set_segment_live_atomic(segment);
 122   assert(success, "Should never fail");
 123 }