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