src/share/vm/gc_implementation/shared/mutableSpace.cpp

Wed, 23 Jan 2013 13:02:39 -0500

author
jprovino
date
Wed, 23 Jan 2013 13:02:39 -0500
changeset 4542
db9981fd3124
parent 4037
da91efe96a93
child 6198
55fb97c4c58d
permissions
-rw-r--r--

8005915: Unify SERIALGC and INCLUDE_ALTERNATE_GCS
Summary: Rename INCLUDE_ALTERNATE_GCS to INCLUDE_ALL_GCS and replace SERIALGC with INCLUDE_ALL_GCS.
Reviewed-by: coleenp, stefank

duke@435 1 /*
brutisso@3711 2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
jprovino@4542 26 #include "utilities/macros.hpp"
jprovino@4542 27 #if INCLUDE_ALL_GCS
stefank@2314 28 #include "gc_implementation/shared/mutableSpace.hpp"
stefank@2314 29 #include "gc_implementation/shared/spaceDecorator.hpp"
stefank@2314 30 #include "oops/oop.inline.hpp"
stefank@2314 31 #include "runtime/safepoint.hpp"
stefank@2314 32 #include "runtime/thread.hpp"
jprovino@4542 33 #endif // INCLUDE_ALL_GCS
duke@435 34
iveresov@970 35 MutableSpace::MutableSpace(size_t alignment): ImmutableSpace(), _top(NULL), _alignment(alignment) {
iveresov@970 36 assert(MutableSpace::alignment() >= 0 &&
iveresov@970 37 MutableSpace::alignment() % os::vm_page_size() == 0,
iveresov@970 38 "Space should be aligned");
jmasa@698 39 _mangler = new MutableSpaceMangler(this);
jmasa@698 40 }
jmasa@698 41
jmasa@698 42 MutableSpace::~MutableSpace() {
jmasa@698 43 delete _mangler;
jmasa@698 44 }
jmasa@698 45
iveresov@970 46 void MutableSpace::numa_setup_pages(MemRegion mr, bool clear_space) {
iveresov@970 47 if (!mr.is_empty()) {
iveresov@970 48 size_t page_size = UseLargePages ? alignment() : os::vm_page_size();
iveresov@970 49 HeapWord *start = (HeapWord*)round_to((intptr_t) mr.start(), page_size);
iveresov@970 50 HeapWord *end = (HeapWord*)round_down((intptr_t) mr.end(), page_size);
iveresov@970 51 if (end > start) {
iveresov@970 52 size_t size = pointer_delta(end, start, sizeof(char));
iveresov@970 53 if (clear_space) {
iveresov@970 54 // Prefer page reallocation to migration.
iveresov@3363 55 os::free_memory((char*)start, size, page_size);
iveresov@970 56 }
iveresov@970 57 os::numa_make_global((char*)start, size);
iveresov@970 58 }
iveresov@970 59 }
iveresov@970 60 }
iveresov@970 61
iveresov@970 62 void MutableSpace::pretouch_pages(MemRegion mr) {
iveresov@970 63 for (volatile char *p = (char*)mr.start(); p < (char*)mr.end(); p += os::vm_page_size()) {
iveresov@970 64 char t = *p; *p = t;
iveresov@970 65 }
iveresov@970 66 }
iveresov@970 67
jmasa@698 68 void MutableSpace::initialize(MemRegion mr,
jmasa@698 69 bool clear_space,
iveresov@970 70 bool mangle_space,
iveresov@970 71 bool setup_pages) {
duke@435 72
iveresov@970 73 assert(Universe::on_page_boundary(mr.start()) && Universe::on_page_boundary(mr.end()),
duke@435 74 "invalid space boundaries");
iveresov@970 75
iveresov@970 76 if (setup_pages && (UseNUMA || AlwaysPreTouch)) {
iveresov@970 77 // The space may move left and right or expand/shrink.
iveresov@970 78 // We'd like to enforce the desired page placement.
iveresov@970 79 MemRegion head, tail;
iveresov@970 80 if (last_setup_region().is_empty()) {
iveresov@970 81 // If it's the first initialization don't limit the amount of work.
iveresov@970 82 head = mr;
iveresov@970 83 tail = MemRegion(mr.end(), mr.end());
iveresov@970 84 } else {
iveresov@970 85 // Is there an intersection with the address space?
iveresov@970 86 MemRegion intersection = last_setup_region().intersection(mr);
iveresov@970 87 if (intersection.is_empty()) {
iveresov@970 88 intersection = MemRegion(mr.end(), mr.end());
iveresov@970 89 }
iveresov@970 90 // All the sizes below are in words.
iveresov@970 91 size_t head_size = 0, tail_size = 0;
iveresov@970 92 if (mr.start() <= intersection.start()) {
iveresov@970 93 head_size = pointer_delta(intersection.start(), mr.start());
iveresov@970 94 }
iveresov@970 95 if(intersection.end() <= mr.end()) {
iveresov@970 96 tail_size = pointer_delta(mr.end(), intersection.end());
iveresov@970 97 }
iveresov@970 98 // Limit the amount of page manipulation if necessary.
iveresov@970 99 if (NUMASpaceResizeRate > 0 && !AlwaysPreTouch) {
iveresov@970 100 const size_t change_size = head_size + tail_size;
iveresov@970 101 const float setup_rate_words = NUMASpaceResizeRate >> LogBytesPerWord;
iveresov@970 102 head_size = MIN2((size_t)(setup_rate_words * head_size / change_size),
iveresov@970 103 head_size);
iveresov@970 104 tail_size = MIN2((size_t)(setup_rate_words * tail_size / change_size),
iveresov@970 105 tail_size);
iveresov@970 106 }
iveresov@970 107 head = MemRegion(intersection.start() - head_size, intersection.start());
iveresov@970 108 tail = MemRegion(intersection.end(), intersection.end() + tail_size);
iveresov@970 109 }
iveresov@970 110 assert(mr.contains(head) && mr.contains(tail), "Sanity");
iveresov@970 111
iveresov@970 112 if (UseNUMA) {
iveresov@970 113 numa_setup_pages(head, clear_space);
iveresov@970 114 numa_setup_pages(tail, clear_space);
iveresov@970 115 }
iveresov@970 116
iveresov@970 117 if (AlwaysPreTouch) {
iveresov@970 118 pretouch_pages(head);
iveresov@970 119 pretouch_pages(tail);
iveresov@970 120 }
iveresov@970 121
iveresov@970 122 // Remember where we stopped so that we can continue later.
iveresov@970 123 set_last_setup_region(MemRegion(head.start(), tail.end()));
iveresov@970 124 }
iveresov@970 125
iveresov@970 126 set_bottom(mr.start());
iveresov@970 127 set_end(mr.end());
duke@435 128
jmasa@698 129 if (clear_space) {
jmasa@698 130 clear(mangle_space);
jmasa@698 131 }
duke@435 132 }
duke@435 133
jmasa@698 134 void MutableSpace::clear(bool mangle_space) {
duke@435 135 set_top(bottom());
jmasa@698 136 if (ZapUnusedHeapArea && mangle_space) {
jmasa@698 137 mangle_unused_area();
jmasa@698 138 }
duke@435 139 }
duke@435 140
jmasa@698 141 #ifndef PRODUCT
jmasa@698 142 void MutableSpace::check_mangled_unused_area(HeapWord* limit) {
jmasa@698 143 mangler()->check_mangled_unused_area(limit);
jmasa@698 144 }
jmasa@698 145
jmasa@698 146 void MutableSpace::check_mangled_unused_area_complete() {
jmasa@698 147 mangler()->check_mangled_unused_area_complete();
jmasa@698 148 }
jmasa@698 149
jmasa@698 150 // Mangle only the unused space that has not previously
jmasa@698 151 // been mangled and that has not been allocated since being
jmasa@698 152 // mangled.
jmasa@698 153 void MutableSpace::mangle_unused_area() {
jmasa@698 154 mangler()->mangle_unused_area();
jmasa@698 155 }
jmasa@698 156
jmasa@698 157 void MutableSpace::mangle_unused_area_complete() {
jmasa@698 158 mangler()->mangle_unused_area_complete();
jmasa@698 159 }
jmasa@698 160
jmasa@698 161 void MutableSpace::mangle_region(MemRegion mr) {
jmasa@698 162 SpaceMangler::mangle_region(mr);
jmasa@698 163 }
jmasa@698 164
jmasa@698 165 void MutableSpace::set_top_for_allocations(HeapWord* v) {
jmasa@698 166 mangler()->set_top_for_allocations(v);
jmasa@698 167 }
jmasa@698 168
jmasa@698 169 void MutableSpace::set_top_for_allocations() {
jmasa@698 170 mangler()->set_top_for_allocations(top());
jmasa@698 171 }
jmasa@698 172 #endif
jmasa@698 173
duke@435 174 // This version requires locking. */
duke@435 175 HeapWord* MutableSpace::allocate(size_t size) {
duke@435 176 assert(Heap_lock->owned_by_self() ||
duke@435 177 (SafepointSynchronize::is_at_safepoint() &&
duke@435 178 Thread::current()->is_VM_thread()),
duke@435 179 "not locked");
duke@435 180 HeapWord* obj = top();
duke@435 181 if (pointer_delta(end(), obj) >= size) {
duke@435 182 HeapWord* new_top = obj + size;
duke@435 183 set_top(new_top);
duke@435 184 assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top),
duke@435 185 "checking alignment");
duke@435 186 return obj;
duke@435 187 } else {
duke@435 188 return NULL;
duke@435 189 }
duke@435 190 }
duke@435 191
duke@435 192 // This version is lock-free.
duke@435 193 HeapWord* MutableSpace::cas_allocate(size_t size) {
duke@435 194 do {
duke@435 195 HeapWord* obj = top();
duke@435 196 if (pointer_delta(end(), obj) >= size) {
duke@435 197 HeapWord* new_top = obj + size;
duke@435 198 HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
duke@435 199 // result can be one of two:
duke@435 200 // the old top value: the exchange succeeded
duke@435 201 // otherwise: the new value of the top is returned.
duke@435 202 if (result != obj) {
duke@435 203 continue; // another thread beat us to the allocation, try again
duke@435 204 }
duke@435 205 assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top),
duke@435 206 "checking alignment");
duke@435 207 return obj;
duke@435 208 } else {
duke@435 209 return NULL;
duke@435 210 }
duke@435 211 } while (true);
duke@435 212 }
duke@435 213
duke@435 214 // Try to deallocate previous allocation. Returns true upon success.
duke@435 215 bool MutableSpace::cas_deallocate(HeapWord *obj, size_t size) {
duke@435 216 HeapWord* expected_top = obj + size;
duke@435 217 return (HeapWord*)Atomic::cmpxchg_ptr(obj, top_addr(), expected_top) == expected_top;
duke@435 218 }
duke@435 219
coleenp@4037 220 void MutableSpace::oop_iterate(ExtendedOopClosure* cl) {
duke@435 221 HeapWord* obj_addr = bottom();
duke@435 222 HeapWord* t = top();
duke@435 223 // Could call objects iterate, but this is easier.
duke@435 224 while (obj_addr < t) {
duke@435 225 obj_addr += oop(obj_addr)->oop_iterate(cl);
duke@435 226 }
duke@435 227 }
duke@435 228
coleenp@4037 229 void MutableSpace::oop_iterate_no_header(OopClosure* cl) {
coleenp@4037 230 HeapWord* obj_addr = bottom();
coleenp@4037 231 HeapWord* t = top();
coleenp@4037 232 // Could call objects iterate, but this is easier.
coleenp@4037 233 while (obj_addr < t) {
coleenp@4037 234 obj_addr += oop(obj_addr)->oop_iterate_no_header(cl);
coleenp@4037 235 }
coleenp@4037 236 }
coleenp@4037 237
duke@435 238 void MutableSpace::object_iterate(ObjectClosure* cl) {
duke@435 239 HeapWord* p = bottom();
duke@435 240 while (p < top()) {
duke@435 241 cl->do_object(oop(p));
duke@435 242 p += oop(p)->size();
duke@435 243 }
duke@435 244 }
duke@435 245
duke@435 246 void MutableSpace::print_short() const { print_short_on(tty); }
duke@435 247 void MutableSpace::print_short_on( outputStream* st) const {
duke@435 248 st->print(" space " SIZE_FORMAT "K, %d%% used", capacity_in_bytes() / K,
duke@435 249 (int) ((double) used_in_bytes() * 100 / capacity_in_bytes()));
duke@435 250 }
duke@435 251
duke@435 252 void MutableSpace::print() const { print_on(tty); }
duke@435 253 void MutableSpace::print_on(outputStream* st) const {
duke@435 254 MutableSpace::print_short_on(st);
duke@435 255 st->print_cr(" [" INTPTR_FORMAT "," INTPTR_FORMAT "," INTPTR_FORMAT ")",
duke@435 256 bottom(), top(), end());
duke@435 257 }
duke@435 258
brutisso@3711 259 void MutableSpace::verify() {
duke@435 260 HeapWord* p = bottom();
duke@435 261 HeapWord* t = top();
duke@435 262 HeapWord* prev_p = NULL;
duke@435 263 while (p < t) {
duke@435 264 oop(p)->verify();
duke@435 265 prev_p = p;
duke@435 266 p += oop(p)->size();
duke@435 267 }
duke@435 268 guarantee(p == top(), "end of last object must match end of space");
duke@435 269 }

mercurial