1 /* 2 * Copyright (c) 2001, 2023, 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 #include "precompiled.hpp" 26 #include "gc/parallel/mutableSpace.hpp" 27 #include "gc/shared/pretouchTask.hpp" 28 #include "gc/shared/spaceDecorator.inline.hpp" 29 #include "memory/iterator.inline.hpp" 30 #include "memory/universe.hpp" 31 #include "oops/oop.inline.hpp" 32 #include "runtime/atomic.hpp" 33 #include "runtime/javaThread.hpp" 34 #include "runtime/safepoint.hpp" 35 #include "utilities/align.hpp" 36 #include "utilities/macros.hpp" 37 38 MutableSpace::MutableSpace(size_t alignment) : 39 _mangler(nullptr), 40 _last_setup_region(), 41 _alignment(alignment), 42 _bottom(nullptr), 43 _top(nullptr), 44 _end(nullptr) 45 { 46 assert(MutableSpace::alignment() % os::vm_page_size() == 0, 47 "Space should be aligned"); 48 _mangler = new MutableSpaceMangler(this); 49 } 50 51 MutableSpace::~MutableSpace() { 52 delete _mangler; 53 } 54 55 void MutableSpace::numa_setup_pages(MemRegion mr, size_t page_size, bool clear_space) { 56 if (!mr.is_empty()) { 57 HeapWord *start = align_up(mr.start(), page_size); 58 HeapWord *end = align_down(mr.end(), page_size); 59 if (end > start) { 60 size_t size = pointer_delta(end, start, sizeof(char)); 61 if (clear_space) { 62 // Prefer page reallocation to migration. 63 os::free_memory((char*)start, size, page_size); 64 } 65 os::numa_make_global((char*)start, size); 66 } 67 } 68 } 69 70 void MutableSpace::initialize(MemRegion mr, 71 bool clear_space, 72 bool mangle_space, 73 bool setup_pages, 74 WorkerThreads* pretouch_workers) { 75 76 assert(Universe::on_page_boundary(mr.start()) && Universe::on_page_boundary(mr.end()), 77 "invalid space boundaries"); 78 79 if (setup_pages && (UseNUMA || AlwaysPreTouch)) { 80 // The space may move left and right or expand/shrink. 81 // We'd like to enforce the desired page placement. 82 MemRegion head, tail; 83 if (last_setup_region().is_empty()) { 84 // If it's the first initialization don't limit the amount of work. 85 head = mr; 86 tail = MemRegion(mr.end(), mr.end()); 87 } else { 88 // Is there an intersection with the address space? 89 MemRegion intersection = last_setup_region().intersection(mr); 90 if (intersection.is_empty()) { 91 intersection = MemRegion(mr.end(), mr.end()); 92 } 93 // All the sizes below are in words. 94 size_t head_size = 0, tail_size = 0; 95 if (mr.start() <= intersection.start()) { 96 head_size = pointer_delta(intersection.start(), mr.start()); 97 } 98 if(intersection.end() <= mr.end()) { 99 tail_size = pointer_delta(mr.end(), intersection.end()); 100 } 101 // Limit the amount of page manipulation if necessary. 102 if (NUMASpaceResizeRate > 0 && !AlwaysPreTouch) { 103 const size_t change_size = head_size + tail_size; 104 const float setup_rate_words = NUMASpaceResizeRate >> LogBytesPerWord; 105 head_size = MIN2((size_t)(setup_rate_words * head_size / change_size), 106 head_size); 107 tail_size = MIN2((size_t)(setup_rate_words * tail_size / change_size), 108 tail_size); 109 } 110 head = MemRegion(intersection.start() - head_size, intersection.start()); 111 tail = MemRegion(intersection.end(), intersection.end() + tail_size); 112 } 113 assert(mr.contains(head) && mr.contains(tail), "Sanity"); 114 115 size_t page_size = alignment(); 116 117 if (UseNUMA) { 118 numa_setup_pages(head, page_size, clear_space); 119 numa_setup_pages(tail, page_size, clear_space); 120 } 121 122 if (AlwaysPreTouch) { 123 PretouchTask::pretouch("ParallelGC PreTouch head", (char*)head.start(), (char*)head.end(), 124 page_size, pretouch_workers); 125 126 PretouchTask::pretouch("ParallelGC PreTouch tail", (char*)tail.start(), (char*)tail.end(), 127 page_size, pretouch_workers); 128 } 129 130 // Remember where we stopped so that we can continue later. 131 set_last_setup_region(MemRegion(head.start(), tail.end())); 132 } 133 134 set_bottom(mr.start()); 135 // When expanding concurrently with callers of cas_allocate, setting end 136 // makes the new space available for allocation by other threads. So this 137 // assignment must follow all other configuration and initialization that 138 // might be done for expansion. 139 Atomic::release_store(end_addr(), mr.end()); 140 141 if (clear_space) { 142 clear(mangle_space); 143 } 144 } 145 146 void MutableSpace::clear(bool mangle_space) { 147 set_top(bottom()); 148 if (ZapUnusedHeapArea && mangle_space) { 149 mangle_unused_area(); 150 } 151 } 152 153 #ifndef PRODUCT 154 void MutableSpace::check_mangled_unused_area(HeapWord* limit) { 155 mangler()->check_mangled_unused_area(limit); 156 } 157 158 void MutableSpace::check_mangled_unused_area_complete() { 159 mangler()->check_mangled_unused_area_complete(); 160 } 161 162 // Mangle only the unused space that has not previously 163 // been mangled and that has not been allocated since being 164 // mangled. 165 void MutableSpace::mangle_unused_area() { 166 mangler()->mangle_unused_area(); 167 } 168 169 void MutableSpace::mangle_unused_area_complete() { 170 mangler()->mangle_unused_area_complete(); 171 } 172 173 void MutableSpace::mangle_region(MemRegion mr) { 174 SpaceMangler::mangle_region(mr); 175 } 176 177 void MutableSpace::set_top_for_allocations(HeapWord* v) { 178 mangler()->set_top_for_allocations(v); 179 } 180 181 void MutableSpace::set_top_for_allocations() { 182 mangler()->set_top_for_allocations(top()); 183 } 184 #endif 185 186 HeapWord* MutableSpace::cas_allocate(size_t size) { 187 do { 188 // Read top before end, else the range check may pass when it shouldn't. 189 // If end is read first, other threads may advance end and top such that 190 // current top > old end and current top + size > current end. Then 191 // pointer_delta underflows, allowing installation of top > current end. 192 HeapWord* obj = Atomic::load_acquire(top_addr()); 193 if (pointer_delta(end(), obj) >= size) { 194 HeapWord* new_top = obj + size; 195 HeapWord* result = Atomic::cmpxchg(top_addr(), obj, new_top); 196 // result can be one of two: 197 // the old top value: the exchange succeeded 198 // otherwise: the new value of the top is returned. 199 if (result != obj) { 200 continue; // another thread beat us to the allocation, try again 201 } 202 assert(is_object_aligned(obj) && is_object_aligned(new_top), 203 "checking alignment"); 204 return obj; 205 } else { 206 return nullptr; 207 } 208 } while (true); 209 } 210 211 // Try to deallocate previous allocation. Returns true upon success. 212 bool MutableSpace::cas_deallocate(HeapWord *obj, size_t size) { 213 HeapWord* expected_top = obj + size; 214 return Atomic::cmpxchg(top_addr(), expected_top, obj) == expected_top; 215 } 216 217 // Only used by oldgen allocation. 218 bool MutableSpace::needs_expand(size_t word_size) const { 219 assert_lock_strong(PSOldGenExpand_lock); 220 // Holding the lock means end is stable. So while top may be advancing 221 // via concurrent allocations, there is no need to order the reads of top 222 // and end here, unlike in cas_allocate. 223 return pointer_delta(end(), top()) < word_size; 224 } 225 226 void MutableSpace::oop_iterate(OopIterateClosure* cl) { 227 HeapWord* obj_addr = bottom(); 228 HeapWord* t = top(); 229 // Could call objects iterate, but this is easier. 230 while (obj_addr < t) { 231 obj_addr += cast_to_oop(obj_addr)->oop_iterate_size(cl); 232 } 233 } 234 235 void MutableSpace::object_iterate(ObjectClosure* cl) { 236 HeapWord* p = bottom(); 237 while (p < top()) { 238 oop obj = cast_to_oop(p); 239 // When promotion-failure occurs during Young GC, eden/from space is not cleared, 240 // so we can encounter objects with "forwarded" markword. 241 // They are essentially dead, so skipping them 242 if (!obj->is_forwarded()) { 243 cl->do_object(obj); 244 } 245 #ifdef ASSERT 246 else { 247 assert(obj->forwardee() != obj, "must not be self-forwarded"); 248 } 249 #endif 250 p += obj->size(); 251 } 252 } 253 254 void MutableSpace::print_short() const { print_short_on(tty); } 255 void MutableSpace::print_short_on( outputStream* st) const { 256 st->print(" space " SIZE_FORMAT "K, %d%% used", capacity_in_bytes() / K, 257 (int) ((double) used_in_bytes() * 100 / capacity_in_bytes())); 258 } 259 260 void MutableSpace::print() const { print_on(tty); } 261 void MutableSpace::print_on(outputStream* st) const { 262 MutableSpace::print_short_on(st); 263 st->print_cr(" [" PTR_FORMAT "," PTR_FORMAT "," PTR_FORMAT ")", 264 p2i(bottom()), p2i(top()), p2i(end())); 265 } 266 267 void MutableSpace::verify() { 268 HeapWord* p = bottom(); 269 HeapWord* t = top(); 270 HeapWord* prev_p = nullptr; 271 while (p < t) { 272 oopDesc::verify(cast_to_oop(p)); 273 prev_p = p; 274 p += cast_to_oop(p)->size(); 275 } 276 guarantee(p == top(), "end of last object must match end of space"); 277 }