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