1 /* 2 * Copyright (c) 2001, 2016, 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/spaceDecorator.hpp" 28 #include "oops/oop.inline.hpp" 29 #include "runtime/atomic.hpp" 30 #include "runtime/safepoint.hpp" 31 #include "runtime/thread.hpp" 32 #include "utilities/macros.hpp" 33 34 MutableSpace::MutableSpace(size_t alignment): ImmutableSpace(), _top(NULL), _alignment(alignment) { 35 assert(MutableSpace::alignment() % os::vm_page_size() == 0, 36 "Space should be aligned"); 37 _mangler = new MutableSpaceMangler(this); 38 } 39 40 MutableSpace::~MutableSpace() { 41 delete _mangler; 42 } 43 44 void MutableSpace::numa_setup_pages(MemRegion mr, bool clear_space) { 45 if (!mr.is_empty()) { 46 size_t page_size = UseLargePages ? alignment() : os::vm_page_size(); 47 HeapWord *start = (HeapWord*)round_to((intptr_t) mr.start(), page_size); 48 HeapWord *end = (HeapWord*)round_down((intptr_t) mr.end(), page_size); 49 if (end > start) { 50 size_t size = pointer_delta(end, start, sizeof(char)); 51 if (clear_space) { 52 // Prefer page reallocation to migration. 53 os::free_memory((char*)start, size, page_size); 54 } 55 os::numa_make_global((char*)start, size); 56 } 57 } 58 } 59 60 void MutableSpace::pretouch_pages(MemRegion mr) { 61 os::pretouch_memory(mr.start(), mr.end()); 62 } 63 64 void MutableSpace::initialize(MemRegion mr, 65 bool clear_space, 66 bool mangle_space, 67 bool setup_pages) { 68 69 assert(Universe::on_page_boundary(mr.start()) && Universe::on_page_boundary(mr.end()), 70 "invalid space boundaries"); 71 72 if (setup_pages && (UseNUMA || AlwaysPreTouch)) { 73 // The space may move left and right or expand/shrink. 74 // We'd like to enforce the desired page placement. 75 MemRegion head, tail; 76 if (last_setup_region().is_empty()) { 77 // If it's the first initialization don't limit the amount of work. 78 head = mr; 79 tail = MemRegion(mr.end(), mr.end()); 80 } else { 81 // Is there an intersection with the address space? 82 MemRegion intersection = last_setup_region().intersection(mr); 83 if (intersection.is_empty()) { 84 intersection = MemRegion(mr.end(), mr.end()); 85 } 86 // All the sizes below are in words. 87 size_t head_size = 0, tail_size = 0; 88 if (mr.start() <= intersection.start()) { 89 head_size = pointer_delta(intersection.start(), mr.start()); 90 } 91 if(intersection.end() <= mr.end()) { 92 tail_size = pointer_delta(mr.end(), intersection.end()); 93 } 94 // Limit the amount of page manipulation if necessary. 95 if (NUMASpaceResizeRate > 0 && !AlwaysPreTouch) { 96 const size_t change_size = head_size + tail_size; 97 const float setup_rate_words = NUMASpaceResizeRate >> LogBytesPerWord; 98 head_size = MIN2((size_t)(setup_rate_words * head_size / change_size), 99 head_size); 100 tail_size = MIN2((size_t)(setup_rate_words * tail_size / change_size), 101 tail_size); 102 } 103 head = MemRegion(intersection.start() - head_size, intersection.start()); 104 tail = MemRegion(intersection.end(), intersection.end() + tail_size); 105 } 106 assert(mr.contains(head) && mr.contains(tail), "Sanity"); 107 108 if (UseNUMA) { 109 numa_setup_pages(head, clear_space); 110 numa_setup_pages(tail, clear_space); 111 } 112 113 if (AlwaysPreTouch) { 114 pretouch_pages(head); 115 pretouch_pages(tail); 116 } 117 118 // Remember where we stopped so that we can continue later. 119 set_last_setup_region(MemRegion(head.start(), tail.end())); 120 } 121 122 set_bottom(mr.start()); 123 set_end(mr.end()); 124 125 if (clear_space) { 126 clear(mangle_space); 127 } 128 } 129 130 void MutableSpace::clear(bool mangle_space) { 131 set_top(bottom()); 132 if (ZapUnusedHeapArea && mangle_space) { 133 mangle_unused_area(); 134 } 135 } 136 137 #ifndef PRODUCT 138 void MutableSpace::check_mangled_unused_area(HeapWord* limit) { 139 mangler()->check_mangled_unused_area(limit); 140 } 141 142 void MutableSpace::check_mangled_unused_area_complete() { 143 mangler()->check_mangled_unused_area_complete(); 144 } 145 146 // Mangle only the unused space that has not previously 147 // been mangled and that has not been allocated since being 148 // mangled. 149 void MutableSpace::mangle_unused_area() { 150 mangler()->mangle_unused_area(); 151 } 152 153 void MutableSpace::mangle_unused_area_complete() { 154 mangler()->mangle_unused_area_complete(); 155 } 156 157 void MutableSpace::mangle_region(MemRegion mr) { 158 SpaceMangler::mangle_region(mr); 159 } 160 161 void MutableSpace::set_top_for_allocations(HeapWord* v) { 162 mangler()->set_top_for_allocations(v); 163 } 164 165 void MutableSpace::set_top_for_allocations() { 166 mangler()->set_top_for_allocations(top()); 167 } 168 #endif 169 170 // This version requires locking. */ 171 HeapWord* MutableSpace::allocate(size_t size) { 172 assert(Heap_lock->owned_by_self() || 173 (SafepointSynchronize::is_at_safepoint() && 174 Thread::current()->is_VM_thread()), 175 "not locked"); 176 HeapWord* obj = top(); 177 if (pointer_delta(end(), obj) >= size) { 178 HeapWord* new_top = obj + size; 179 set_top(new_top); 180 assert(is_object_aligned(obj) && is_object_aligned(new_top), 181 "checking alignment"); 182 return obj; 183 } else { 184 return NULL; 185 } 186 } 187 188 // This version is lock-free. 189 HeapWord* MutableSpace::cas_allocate(size_t size) { 190 do { 191 HeapWord* obj = top(); 192 if (pointer_delta(end(), obj) >= size) { 193 HeapWord* new_top = obj + size; 194 HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj); 195 // result can be one of two: 196 // the old top value: the exchange succeeded 197 // otherwise: the new value of the top is returned. 198 if (result != obj) { 199 continue; // another thread beat us to the allocation, try again 200 } 201 assert(is_object_aligned(obj) && is_object_aligned(new_top), 202 "checking alignment"); 203 return obj; 204 } else { 205 return NULL; 206 } 207 } while (true); 208 } 209 210 // Try to deallocate previous allocation. Returns true upon success. 211 bool MutableSpace::cas_deallocate(HeapWord *obj, size_t size) { 212 HeapWord* expected_top = obj + size; 213 return (HeapWord*)Atomic::cmpxchg_ptr(obj, top_addr(), expected_top) == expected_top; 214 } 215 216 void MutableSpace::oop_iterate_no_header(OopClosure* cl) { 217 HeapWord* obj_addr = bottom(); 218 HeapWord* t = top(); 219 // Could call objects iterate, but this is easier. 220 while (obj_addr < t) { 221 obj_addr += oop(obj_addr)->oop_iterate_no_header(cl); 222 } 223 } 224 225 void MutableSpace::object_iterate(ObjectClosure* cl) { 226 HeapWord* p = bottom(); 227 while (p < top()) { 228 cl->do_object(oop(p)); 229 p += oop(p)->size(); 230 } 231 } 232 233 void MutableSpace::print_short() const { print_short_on(tty); } 234 void MutableSpace::print_short_on( outputStream* st) const { 235 st->print(" space " SIZE_FORMAT "K, %d%% used", capacity_in_bytes() / K, 236 (int) ((double) used_in_bytes() * 100 / capacity_in_bytes())); 237 } 238 239 void MutableSpace::print() const { print_on(tty); } 240 void MutableSpace::print_on(outputStream* st) const { 241 MutableSpace::print_short_on(st); 242 st->print_cr(" [" INTPTR_FORMAT "," INTPTR_FORMAT "," INTPTR_FORMAT ")", 243 p2i(bottom()), p2i(top()), p2i(end())); 244 } 245 246 void MutableSpace::verify() { 247 HeapWord* p = bottom(); 248 HeapWord* t = top(); 249 HeapWord* prev_p = NULL; 250 while (p < t) { 251 oop(p)->verify(); 252 prev_p = p; 253 p += oop(p)->size(); 254 } 255 guarantee(p == top(), "end of last object must match end of space"); 256 }