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

Fri, 25 Sep 2009 12:17:06 -0700

author
trims
date
Fri, 25 Sep 2009 12:17:06 -0700
changeset 1417
7a102acc9f17
parent 1014
0fbdb4381b99
child 1907
c18cbe5936b8
permissions
-rw-r--r--

Merge

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

mercurial