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/zAddress.inline.hpp" 26 #include "gc/z/zBarrier.inline.hpp" 27 #include "gc/z/zForwarding.inline.hpp" 28 #include "gc/z/zHeap.hpp" 29 #include "gc/z/zOopClosures.inline.hpp" 30 #include "gc/z/zPage.hpp" 31 #include "gc/z/zRelocate.hpp" 32 #include "gc/z/zRelocationSet.inline.hpp" 33 #include "gc/z/zRootsIterator.hpp" 34 #include "gc/z/zStat.hpp" 35 #include "gc/z/zTask.hpp" 36 #include "gc/z/zThreadLocalAllocBuffer.hpp" 37 #include "gc/z/zWorkers.hpp" 38 #include "logging/log.hpp" 39 40 static const ZStatCounter ZCounterRelocationContention("Contention", "Relocation Contention", ZStatUnitOpsPerSecond); 41 42 ZRelocate::ZRelocate(ZWorkers* workers) : 43 _workers(workers) {} 44 45 class ZRelocateRootsIteratorClosure : public ZRootsIteratorClosure { 46 public: 47 virtual void do_thread(Thread* thread) { 48 // Update thread local address bad mask 49 ZThreadLocalData::set_address_bad_mask(thread, ZAddressBadMask); 50 51 // Relocate invisible root 52 ZThreadLocalData::do_invisible_root(thread, ZBarrier::relocate_barrier_on_root_oop_field); 53 54 // Remap TLAB 55 ZThreadLocalAllocBuffer::remap(thread); 56 } 57 58 virtual void do_oop(oop* p) { 59 ZBarrier::relocate_barrier_on_root_oop_field(p); 60 } 61 62 virtual void do_oop(narrowOop* p) { 63 ShouldNotReachHere(); 64 } 65 }; 66 67 class ZRelocateRootsTask : public ZTask { 68 private: 69 ZRootsIterator _roots; 70 ZRelocateRootsIteratorClosure _cl; 71 72 public: 73 ZRelocateRootsTask() : 74 ZTask("ZRelocateRootsTask"), 75 _roots(true /* visit_jvmti_weak_export */) {} 76 77 virtual void work() { 78 // During relocation we need to visit the JVMTI 79 // export weak roots to rehash the JVMTI tag map 80 _roots.oops_do(&_cl); 81 } 82 }; 83 84 void ZRelocate::start() { 85 ZRelocateRootsTask task; 86 _workers->run_parallel(&task); 87 } 88 89 uintptr_t ZRelocate::relocate_object_inner(ZForwarding* forwarding, uintptr_t from_index, uintptr_t from_offset) const { 90 ZForwardingCursor cursor; 91 92 // Lookup forwarding entry 93 const ZForwardingEntry entry = forwarding->find(from_index, &cursor); 94 if (entry.populated() && entry.from_index() == from_index) { 95 // Already relocated, return new address 96 return entry.to_offset(); 97 } 98 99 assert(ZHeap::heap()->is_object_live(ZAddress::good(from_offset)), "Should be live"); 100 101 if (forwarding->is_pinned()) { 102 // In-place forward 103 return forwarding->insert(from_index, from_offset, &cursor); 104 } 105 106 // Allocate object 107 const uintptr_t from_good = ZAddress::good(from_offset); 108 const size_t size = ZUtils::object_size(from_good); 109 const uintptr_t to_good = ZHeap::heap()->alloc_object_for_relocation(size); 110 if (to_good == 0) { 111 // Failed, in-place forward 112 return forwarding->insert(from_index, from_offset, &cursor); 113 } 114 115 // Copy object 116 ZUtils::object_copy(from_good, to_good, size); 117 118 // Insert forwarding entry 119 const uintptr_t to_offset = ZAddress::offset(to_good); 120 const uintptr_t to_offset_final = forwarding->insert(from_index, to_offset, &cursor); 121 if (to_offset_final == to_offset) { 122 // Relocation succeeded 123 return to_offset; 124 } 125 126 // Relocation contention 127 ZStatInc(ZCounterRelocationContention); 128 log_trace(gc)("Relocation contention, thread: " PTR_FORMAT " (%s), forwarding: " PTR_FORMAT 129 ", entry: " UINT32_FORMAT ", oop: " PTR_FORMAT ", size: " SIZE_FORMAT, 130 ZThread::id(), ZThread::name(), p2i(forwarding), cursor, from_good, size); 131 132 // Try undo allocation 133 ZHeap::heap()->undo_alloc_object_for_relocation(to_good, size); 134 135 return to_offset_final; 136 } 137 138 uintptr_t ZRelocate::relocate_object(ZForwarding* forwarding, uintptr_t from_addr) const { 139 const uintptr_t from_offset = ZAddress::offset(from_addr); 140 const uintptr_t from_index = (from_offset - forwarding->start()) >> forwarding->object_alignment_shift(); 141 const uintptr_t to_offset = relocate_object_inner(forwarding, from_index, from_offset); 142 143 if (from_offset == to_offset) { 144 // In-place forwarding, pin page 145 forwarding->set_pinned(); 146 } 147 148 return ZAddress::good(to_offset); 149 } 150 151 uintptr_t ZRelocate::forward_object(ZForwarding* forwarding, uintptr_t from_addr) const { 152 const uintptr_t from_offset = ZAddress::offset(from_addr); 153 const uintptr_t from_index = (from_offset - forwarding->start()) >> forwarding->object_alignment_shift(); 154 const ZForwardingEntry entry = forwarding->find(from_index); 155 156 assert(entry.populated(), "Should be forwarded"); 157 assert(entry.from_index() == from_index, "Should be forwarded"); 158 159 return ZAddress::good(entry.to_offset()); 160 } 161 162 class ZRelocateObjectClosure : public ObjectClosure { 163 private: 164 ZRelocate* const _relocate; 165 ZForwarding* const _forwarding; 166 167 public: 168 ZRelocateObjectClosure(ZRelocate* relocate, ZForwarding* forwarding) : 169 _relocate(relocate), 170 _forwarding(forwarding) {} 171 172 virtual void do_object(oop o) { 173 _relocate->relocate_object(_forwarding, ZOop::to_address(o)); 174 } 175 }; 176 177 bool ZRelocate::work(ZRelocationSetParallelIterator* iter) { 178 bool success = true; 179 180 // Relocate pages in the relocation set 181 for (ZForwarding* forwarding; iter->next(&forwarding);) { 182 // Relocate objects in page 183 ZRelocateObjectClosure cl(this, forwarding); 184 forwarding->page()->object_iterate(&cl); 185 186 if (ZVerifyForwarding) { 187 forwarding->verify(); 188 } 189 190 if (forwarding->is_pinned()) { 191 // Relocation failed, page is now pinned 192 success = false; 193 } else { 194 // Relocation succeeded, release page 195 forwarding->release_page(); 196 } 197 } 198 199 return success; 200 } 201 202 class ZRelocateTask : public ZTask { 203 private: 204 ZRelocate* const _relocate; 205 ZRelocationSetParallelIterator _iter; 206 bool _failed; 207 208 public: 209 ZRelocateTask(ZRelocate* relocate, ZRelocationSet* relocation_set) : 210 ZTask("ZRelocateTask"), 211 _relocate(relocate), 212 _iter(relocation_set), 213 _failed(false) {} 214 215 virtual void work() { 216 if (!_relocate->work(&_iter)) { 217 _failed = true; 218 } 219 } 220 221 bool failed() const { 222 return _failed; 223 } 224 }; 225 226 bool ZRelocate::relocate(ZRelocationSet* relocation_set) { 227 ZRelocateTask task(this, relocation_set); 228 _workers->run_concurrent(&task); 229 return !task.failed(); 230 }