annotate src/share/vm/gc_implementation/shared/mutableNUMASpace.cpp @ 0:a61af66fc99e

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children fcbfc50865ab
rev   line source
duke@0 1
duke@0 2 /*
duke@0 3 * Copyright 2006-2007 Sun Microsystems, Inc. All Rights Reserved.
duke@0 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 5 *
duke@0 6 * This code is free software; you can redistribute it and/or modify it
duke@0 7 * under the terms of the GNU General Public License version 2 only, as
duke@0 8 * published by the Free Software Foundation.
duke@0 9 *
duke@0 10 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 13 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 14 * accompanied this code).
duke@0 15 *
duke@0 16 * You should have received a copy of the GNU General Public License version
duke@0 17 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 19 *
duke@0 20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 21 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 22 * have any questions.
duke@0 23 *
duke@0 24 */
duke@0 25
duke@0 26 # include "incls/_precompiled.incl"
duke@0 27 # include "incls/_mutableNUMASpace.cpp.incl"
duke@0 28
duke@0 29
duke@0 30 MutableNUMASpace::MutableNUMASpace() {
duke@0 31 _lgrp_spaces = new (ResourceObj::C_HEAP) GrowableArray<LGRPSpace*>(0, true);
duke@0 32 _page_size = os::vm_page_size();
duke@0 33 _adaptation_cycles = 0;
duke@0 34 _samples_count = 0;
duke@0 35 update_layout(true);
duke@0 36 }
duke@0 37
duke@0 38 MutableNUMASpace::~MutableNUMASpace() {
duke@0 39 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 40 delete lgrp_spaces()->at(i);
duke@0 41 }
duke@0 42 delete lgrp_spaces();
duke@0 43 }
duke@0 44
duke@0 45 void MutableNUMASpace::mangle_unused_area() {
duke@0 46 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 47 LGRPSpace *ls = lgrp_spaces()->at(i);
duke@0 48 MutableSpace *s = ls->space();
duke@0 49 HeapWord *top = MAX2((HeapWord*)round_down((intptr_t)s->top(), page_size()), s->bottom());
duke@0 50 if (top < s->end()) {
duke@0 51 ls->add_invalid_region(MemRegion(top, s->end()));
duke@0 52 }
duke@0 53 s->mangle_unused_area();
duke@0 54 }
duke@0 55 }
duke@0 56
duke@0 57 // There may be unallocated holes in the middle chunks
duke@0 58 // that should be filled with dead objects to ensure parseability.
duke@0 59 void MutableNUMASpace::ensure_parsability() {
duke@0 60 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 61 LGRPSpace *ls = lgrp_spaces()->at(i);
duke@0 62 MutableSpace *s = ls->space();
duke@0 63 if (!s->contains(top())) {
duke@0 64 if (s->free_in_words() > 0) {
duke@0 65 SharedHeap::fill_region_with_object(MemRegion(s->top(), s->end()));
duke@0 66 size_t area_touched_words = pointer_delta(s->end(), s->top(), sizeof(HeapWordSize));
duke@0 67 #ifndef ASSERT
duke@0 68 if (!ZapUnusedHeapArea) {
duke@0 69 area_touched_words = MIN2((size_t)align_object_size(typeArrayOopDesc::header_size(T_INT)),
duke@0 70 area_touched_words);
duke@0 71 }
duke@0 72 #endif
duke@0 73 MemRegion invalid;
duke@0 74 HeapWord *crossing_start = (HeapWord*)round_to((intptr_t)s->top(), os::vm_page_size());
duke@0 75 HeapWord *crossing_end = (HeapWord*)round_to((intptr_t)(s->top() + area_touched_words),
duke@0 76 os::vm_page_size());
duke@0 77 if (crossing_start != crossing_end) {
duke@0 78 // If object header crossed a small page boundary we mark the area
duke@0 79 // as invalid rounding it to a page_size().
duke@0 80 HeapWord *start = MAX2((HeapWord*)round_down((intptr_t)s->top(), page_size()), s->bottom());
duke@0 81 HeapWord *end = MIN2((HeapWord*)round_to((intptr_t)(s->top() + area_touched_words), page_size()),
duke@0 82 s->end());
duke@0 83 invalid = MemRegion(start, end);
duke@0 84 }
duke@0 85
duke@0 86 ls->add_invalid_region(invalid);
duke@0 87 s->set_top(s->end());
duke@0 88 }
duke@0 89 } else {
duke@0 90 #ifdef ASSERT
duke@0 91 MemRegion invalid(s->top(), s->end());
duke@0 92 ls->add_invalid_region(invalid);
duke@0 93 #else
duke@0 94 if (ZapUnusedHeapArea) {
duke@0 95 MemRegion invalid(s->top(), s->end());
duke@0 96 ls->add_invalid_region(invalid);
duke@0 97 } else break;
duke@0 98 #endif
duke@0 99 }
duke@0 100 }
duke@0 101 }
duke@0 102
duke@0 103 size_t MutableNUMASpace::used_in_words() const {
duke@0 104 size_t s = 0;
duke@0 105 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 106 s += lgrp_spaces()->at(i)->space()->used_in_words();
duke@0 107 }
duke@0 108 return s;
duke@0 109 }
duke@0 110
duke@0 111 size_t MutableNUMASpace::free_in_words() const {
duke@0 112 size_t s = 0;
duke@0 113 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 114 s += lgrp_spaces()->at(i)->space()->free_in_words();
duke@0 115 }
duke@0 116 return s;
duke@0 117 }
duke@0 118
duke@0 119
duke@0 120 size_t MutableNUMASpace::tlab_capacity(Thread *thr) const {
duke@0 121 guarantee(thr != NULL, "No thread");
duke@0 122 int lgrp_id = thr->lgrp_id();
duke@0 123 assert(lgrp_id != -1, "No lgrp_id set");
duke@0 124 int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
duke@0 125 if (i == -1) {
duke@0 126 return 0;
duke@0 127 }
duke@0 128 return lgrp_spaces()->at(i)->space()->capacity_in_bytes();
duke@0 129 }
duke@0 130
duke@0 131 size_t MutableNUMASpace::unsafe_max_tlab_alloc(Thread *thr) const {
duke@0 132 guarantee(thr != NULL, "No thread");
duke@0 133 int lgrp_id = thr->lgrp_id();
duke@0 134 assert(lgrp_id != -1, "No lgrp_id set");
duke@0 135 int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
duke@0 136 if (i == -1) {
duke@0 137 return 0;
duke@0 138 }
duke@0 139 return lgrp_spaces()->at(i)->space()->free_in_bytes();
duke@0 140 }
duke@0 141
duke@0 142 // Check if the NUMA topology has changed. Add and remove spaces if needed.
duke@0 143 // The update can be forced by setting the force parameter equal to true.
duke@0 144 bool MutableNUMASpace::update_layout(bool force) {
duke@0 145 // Check if the topology had changed.
duke@0 146 bool changed = os::numa_topology_changed();
duke@0 147 if (force || changed) {
duke@0 148 // Compute lgrp intersection. Add/remove spaces.
duke@0 149 int lgrp_limit = (int)os::numa_get_groups_num();
duke@0 150 int *lgrp_ids = NEW_C_HEAP_ARRAY(int, lgrp_limit);
duke@0 151 int lgrp_num = (int)os::numa_get_leaf_groups(lgrp_ids, lgrp_limit);
duke@0 152 assert(lgrp_num > 0, "There should be at least one locality group");
duke@0 153 // Add new spaces for the new nodes
duke@0 154 for (int i = 0; i < lgrp_num; i++) {
duke@0 155 bool found = false;
duke@0 156 for (int j = 0; j < lgrp_spaces()->length(); j++) {
duke@0 157 if (lgrp_spaces()->at(j)->lgrp_id() == lgrp_ids[i]) {
duke@0 158 found = true;
duke@0 159 break;
duke@0 160 }
duke@0 161 }
duke@0 162 if (!found) {
duke@0 163 lgrp_spaces()->append(new LGRPSpace(lgrp_ids[i]));
duke@0 164 }
duke@0 165 }
duke@0 166
duke@0 167 // Remove spaces for the removed nodes.
duke@0 168 for (int i = 0; i < lgrp_spaces()->length();) {
duke@0 169 bool found = false;
duke@0 170 for (int j = 0; j < lgrp_num; j++) {
duke@0 171 if (lgrp_spaces()->at(i)->lgrp_id() == lgrp_ids[j]) {
duke@0 172 found = true;
duke@0 173 break;
duke@0 174 }
duke@0 175 }
duke@0 176 if (!found) {
duke@0 177 delete lgrp_spaces()->at(i);
duke@0 178 lgrp_spaces()->remove_at(i);
duke@0 179 } else {
duke@0 180 i++;
duke@0 181 }
duke@0 182 }
duke@0 183
duke@0 184 FREE_C_HEAP_ARRAY(int, lgrp_ids);
duke@0 185
duke@0 186 if (changed) {
duke@0 187 for (JavaThread *thread = Threads::first(); thread; thread = thread->next()) {
duke@0 188 thread->set_lgrp_id(-1);
duke@0 189 }
duke@0 190 }
duke@0 191 return true;
duke@0 192 }
duke@0 193 return false;
duke@0 194 }
duke@0 195
duke@0 196 // Bias region towards the first-touching lgrp. Set the right page sizes.
duke@0 197 void MutableNUMASpace::bias_region(MemRegion mr) {
duke@0 198 HeapWord *start = (HeapWord*)round_to((intptr_t)mr.start(), page_size());
duke@0 199 HeapWord *end = (HeapWord*)round_down((intptr_t)mr.end(), page_size());
duke@0 200 if (end > start) {
duke@0 201 MemRegion aligned_region(start, end);
duke@0 202 assert((intptr_t)aligned_region.start() % page_size() == 0 &&
duke@0 203 (intptr_t)aligned_region.byte_size() % page_size() == 0, "Bad alignment");
duke@0 204 assert(region().contains(aligned_region), "Sanity");
duke@0 205 os::free_memory((char*)aligned_region.start(), aligned_region.byte_size());
duke@0 206 os::realign_memory((char*)aligned_region.start(), aligned_region.byte_size(), page_size());
duke@0 207 os::numa_make_local((char*)aligned_region.start(), aligned_region.byte_size());
duke@0 208 }
duke@0 209 }
duke@0 210
duke@0 211 // Free all pages in the region.
duke@0 212 void MutableNUMASpace::free_region(MemRegion mr) {
duke@0 213 HeapWord *start = (HeapWord*)round_to((intptr_t)mr.start(), page_size());
duke@0 214 HeapWord *end = (HeapWord*)round_down((intptr_t)mr.end(), page_size());
duke@0 215 if (end > start) {
duke@0 216 MemRegion aligned_region(start, end);
duke@0 217 assert((intptr_t)aligned_region.start() % page_size() == 0 &&
duke@0 218 (intptr_t)aligned_region.byte_size() % page_size() == 0, "Bad alignment");
duke@0 219 assert(region().contains(aligned_region), "Sanity");
duke@0 220 os::free_memory((char*)aligned_region.start(), aligned_region.byte_size());
duke@0 221 }
duke@0 222 }
duke@0 223
duke@0 224 // Update space layout. Perform adaptation.
duke@0 225 void MutableNUMASpace::update() {
duke@0 226 if (update_layout(false)) {
duke@0 227 // If the topology has changed, make all chunks zero-sized.
duke@0 228 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 229 MutableSpace *s = lgrp_spaces()->at(i)->space();
duke@0 230 s->set_end(s->bottom());
duke@0 231 s->set_top(s->bottom());
duke@0 232 }
duke@0 233 initialize(region(), true);
duke@0 234 } else {
duke@0 235 bool should_initialize = false;
duke@0 236 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 237 if (!lgrp_spaces()->at(i)->invalid_region().is_empty()) {
duke@0 238 should_initialize = true;
duke@0 239 break;
duke@0 240 }
duke@0 241 }
duke@0 242
duke@0 243 if (should_initialize ||
duke@0 244 (UseAdaptiveNUMAChunkSizing && adaptation_cycles() < samples_count())) {
duke@0 245 initialize(region(), true);
duke@0 246 }
duke@0 247 }
duke@0 248
duke@0 249 if (NUMAStats) {
duke@0 250 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 251 lgrp_spaces()->at(i)->accumulate_statistics(page_size());
duke@0 252 }
duke@0 253 }
duke@0 254
duke@0 255 scan_pages(NUMAPageScanRate);
duke@0 256 }
duke@0 257
duke@0 258 // Scan pages. Free pages that have smaller size or wrong placement.
duke@0 259 void MutableNUMASpace::scan_pages(size_t page_count)
duke@0 260 {
duke@0 261 size_t pages_per_chunk = page_count / lgrp_spaces()->length();
duke@0 262 if (pages_per_chunk > 0) {
duke@0 263 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 264 LGRPSpace *ls = lgrp_spaces()->at(i);
duke@0 265 ls->scan_pages(page_size(), pages_per_chunk);
duke@0 266 }
duke@0 267 }
duke@0 268 }
duke@0 269
duke@0 270 // Accumulate statistics about the allocation rate of each lgrp.
duke@0 271 void MutableNUMASpace::accumulate_statistics() {
duke@0 272 if (UseAdaptiveNUMAChunkSizing) {
duke@0 273 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 274 lgrp_spaces()->at(i)->sample();
duke@0 275 }
duke@0 276 increment_samples_count();
duke@0 277 }
duke@0 278
duke@0 279 if (NUMAStats) {
duke@0 280 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 281 lgrp_spaces()->at(i)->accumulate_statistics(page_size());
duke@0 282 }
duke@0 283 }
duke@0 284 }
duke@0 285
duke@0 286 // Get the current size of a chunk.
duke@0 287 // This function computes the size of the chunk based on the
duke@0 288 // difference between chunk ends. This allows it to work correctly in
duke@0 289 // case the whole space is resized and during the process of adaptive
duke@0 290 // chunk resizing.
duke@0 291 size_t MutableNUMASpace::current_chunk_size(int i) {
duke@0 292 HeapWord *cur_end, *prev_end;
duke@0 293 if (i == 0) {
duke@0 294 prev_end = bottom();
duke@0 295 } else {
duke@0 296 prev_end = lgrp_spaces()->at(i - 1)->space()->end();
duke@0 297 }
duke@0 298 if (i == lgrp_spaces()->length() - 1) {
duke@0 299 cur_end = end();
duke@0 300 } else {
duke@0 301 cur_end = lgrp_spaces()->at(i)->space()->end();
duke@0 302 }
duke@0 303 if (cur_end > prev_end) {
duke@0 304 return pointer_delta(cur_end, prev_end, sizeof(char));
duke@0 305 }
duke@0 306 return 0;
duke@0 307 }
duke@0 308
duke@0 309 // Return the default chunk size by equally diving the space.
duke@0 310 // page_size() aligned.
duke@0 311 size_t MutableNUMASpace::default_chunk_size() {
duke@0 312 return base_space_size() / lgrp_spaces()->length() * page_size();
duke@0 313 }
duke@0 314
duke@0 315 // Produce a new chunk size. page_size() aligned.
duke@0 316 size_t MutableNUMASpace::adaptive_chunk_size(int i, size_t limit) {
duke@0 317 size_t pages_available = base_space_size();
duke@0 318 for (int j = 0; j < i; j++) {
duke@0 319 pages_available -= round_down(current_chunk_size(j), page_size()) / page_size();
duke@0 320 }
duke@0 321 pages_available -= lgrp_spaces()->length() - i - 1;
duke@0 322 assert(pages_available > 0, "No pages left");
duke@0 323 float alloc_rate = 0;
duke@0 324 for (int j = i; j < lgrp_spaces()->length(); j++) {
duke@0 325 alloc_rate += lgrp_spaces()->at(j)->alloc_rate()->average();
duke@0 326 }
duke@0 327 size_t chunk_size = 0;
duke@0 328 if (alloc_rate > 0) {
duke@0 329 LGRPSpace *ls = lgrp_spaces()->at(i);
duke@0 330 chunk_size = (size_t)(ls->alloc_rate()->average() * pages_available / alloc_rate) * page_size();
duke@0 331 }
duke@0 332 chunk_size = MAX2(chunk_size, page_size());
duke@0 333
duke@0 334 if (limit > 0) {
duke@0 335 limit = round_down(limit, page_size());
duke@0 336 if (chunk_size > current_chunk_size(i)) {
duke@0 337 chunk_size = MIN2((off_t)chunk_size, (off_t)current_chunk_size(i) + (off_t)limit);
duke@0 338 } else {
duke@0 339 chunk_size = MAX2((off_t)chunk_size, (off_t)current_chunk_size(i) - (off_t)limit);
duke@0 340 }
duke@0 341 }
duke@0 342 assert(chunk_size <= pages_available * page_size(), "Chunk size out of range");
duke@0 343 return chunk_size;
duke@0 344 }
duke@0 345
duke@0 346
duke@0 347 // Return the bottom_region and the top_region. Align them to page_size() boundary.
duke@0 348 // |------------------new_region---------------------------------|
duke@0 349 // |----bottom_region--|---intersection---|------top_region------|
duke@0 350 void MutableNUMASpace::select_tails(MemRegion new_region, MemRegion intersection,
duke@0 351 MemRegion* bottom_region, MemRegion *top_region) {
duke@0 352 // Is there bottom?
duke@0 353 if (new_region.start() < intersection.start()) { // Yes
duke@0 354 // Try to coalesce small pages into a large one.
duke@0 355 if (UseLargePages && page_size() >= os::large_page_size()) {
duke@0 356 HeapWord* p = (HeapWord*)round_to((intptr_t) intersection.start(), os::large_page_size());
duke@0 357 if (new_region.contains(p)
duke@0 358 && pointer_delta(p, new_region.start(), sizeof(char)) >= os::large_page_size()) {
duke@0 359 if (intersection.contains(p)) {
duke@0 360 intersection = MemRegion(p, intersection.end());
duke@0 361 } else {
duke@0 362 intersection = MemRegion(p, p);
duke@0 363 }
duke@0 364 }
duke@0 365 }
duke@0 366 *bottom_region = MemRegion(new_region.start(), intersection.start());
duke@0 367 } else {
duke@0 368 *bottom_region = MemRegion();
duke@0 369 }
duke@0 370
duke@0 371 // Is there top?
duke@0 372 if (intersection.end() < new_region.end()) { // Yes
duke@0 373 // Try to coalesce small pages into a large one.
duke@0 374 if (UseLargePages && page_size() >= os::large_page_size()) {
duke@0 375 HeapWord* p = (HeapWord*)round_down((intptr_t) intersection.end(), os::large_page_size());
duke@0 376 if (new_region.contains(p)
duke@0 377 && pointer_delta(new_region.end(), p, sizeof(char)) >= os::large_page_size()) {
duke@0 378 if (intersection.contains(p)) {
duke@0 379 intersection = MemRegion(intersection.start(), p);
duke@0 380 } else {
duke@0 381 intersection = MemRegion(p, p);
duke@0 382 }
duke@0 383 }
duke@0 384 }
duke@0 385 *top_region = MemRegion(intersection.end(), new_region.end());
duke@0 386 } else {
duke@0 387 *top_region = MemRegion();
duke@0 388 }
duke@0 389 }
duke@0 390
duke@0 391 // Try to merge the invalid region with the bottom or top region by decreasing
duke@0 392 // the intersection area. Return the invalid_region aligned to the page_size()
duke@0 393 // boundary if it's inside the intersection. Return non-empty invalid_region
duke@0 394 // if it lies inside the intersection (also page-aligned).
duke@0 395 // |------------------new_region---------------------------------|
duke@0 396 // |----------------|-------invalid---|--------------------------|
duke@0 397 // |----bottom_region--|---intersection---|------top_region------|
duke@0 398 void MutableNUMASpace::merge_regions(MemRegion new_region, MemRegion* intersection,
duke@0 399 MemRegion *invalid_region) {
duke@0 400 if (intersection->start() >= invalid_region->start() && intersection->contains(invalid_region->end())) {
duke@0 401 *intersection = MemRegion(invalid_region->end(), intersection->end());
duke@0 402 *invalid_region = MemRegion();
duke@0 403 } else
duke@0 404 if (intersection->end() <= invalid_region->end() && intersection->contains(invalid_region->start())) {
duke@0 405 *intersection = MemRegion(intersection->start(), invalid_region->start());
duke@0 406 *invalid_region = MemRegion();
duke@0 407 } else
duke@0 408 if (intersection->equals(*invalid_region) || invalid_region->contains(*intersection)) {
duke@0 409 *intersection = MemRegion(new_region.start(), new_region.start());
duke@0 410 *invalid_region = MemRegion();
duke@0 411 } else
duke@0 412 if (intersection->contains(invalid_region)) {
duke@0 413 // That's the only case we have to make an additional bias_region() call.
duke@0 414 HeapWord* start = invalid_region->start();
duke@0 415 HeapWord* end = invalid_region->end();
duke@0 416 if (UseLargePages && page_size() >= os::large_page_size()) {
duke@0 417 HeapWord *p = (HeapWord*)round_down((intptr_t) start, os::large_page_size());
duke@0 418 if (new_region.contains(p)) {
duke@0 419 start = p;
duke@0 420 }
duke@0 421 p = (HeapWord*)round_to((intptr_t) end, os::large_page_size());
duke@0 422 if (new_region.contains(end)) {
duke@0 423 end = p;
duke@0 424 }
duke@0 425 }
duke@0 426 if (intersection->start() > start) {
duke@0 427 *intersection = MemRegion(start, intersection->end());
duke@0 428 }
duke@0 429 if (intersection->end() < end) {
duke@0 430 *intersection = MemRegion(intersection->start(), end);
duke@0 431 }
duke@0 432 *invalid_region = MemRegion(start, end);
duke@0 433 }
duke@0 434 }
duke@0 435
duke@0 436 void MutableNUMASpace::initialize(MemRegion mr, bool clear_space) {
duke@0 437 assert(clear_space, "Reallocation will destory data!");
duke@0 438 assert(lgrp_spaces()->length() > 0, "There should be at least one space");
duke@0 439
duke@0 440 MemRegion old_region = region(), new_region;
duke@0 441 set_bottom(mr.start());
duke@0 442 set_end(mr.end());
duke@0 443 MutableSpace::set_top(bottom());
duke@0 444
duke@0 445 // Compute chunk sizes
duke@0 446 size_t prev_page_size = page_size();
duke@0 447 set_page_size(UseLargePages ? os::large_page_size() : os::vm_page_size());
duke@0 448 HeapWord* rounded_bottom = (HeapWord*)round_to((intptr_t) bottom(), page_size());
duke@0 449 HeapWord* rounded_end = (HeapWord*)round_down((intptr_t) end(), page_size());
duke@0 450 size_t base_space_size_pages = pointer_delta(rounded_end, rounded_bottom, sizeof(char)) / page_size();
duke@0 451
duke@0 452 // Try small pages if the chunk size is too small
duke@0 453 if (base_space_size_pages / lgrp_spaces()->length() == 0
duke@0 454 && page_size() > (size_t)os::vm_page_size()) {
duke@0 455 set_page_size(os::vm_page_size());
duke@0 456 rounded_bottom = (HeapWord*)round_to((intptr_t) bottom(), page_size());
duke@0 457 rounded_end = (HeapWord*)round_down((intptr_t) end(), page_size());
duke@0 458 base_space_size_pages = pointer_delta(rounded_end, rounded_bottom, sizeof(char)) / page_size();
duke@0 459 }
duke@0 460 guarantee(base_space_size_pages / lgrp_spaces()->length() > 0, "Space too small");
duke@0 461 set_base_space_size(base_space_size_pages);
duke@0 462
duke@0 463 // Handle space resize
duke@0 464 MemRegion top_region, bottom_region;
duke@0 465 if (!old_region.equals(region())) {
duke@0 466 new_region = MemRegion(rounded_bottom, rounded_end);
duke@0 467 MemRegion intersection = new_region.intersection(old_region);
duke@0 468 if (intersection.start() == NULL ||
duke@0 469 intersection.end() == NULL ||
duke@0 470 prev_page_size > page_size()) { // If the page size got smaller we have to change
duke@0 471 // the page size preference for the whole space.
duke@0 472 intersection = MemRegion(new_region.start(), new_region.start());
duke@0 473 }
duke@0 474 select_tails(new_region, intersection, &bottom_region, &top_region);
duke@0 475 bias_region(bottom_region);
duke@0 476 bias_region(top_region);
duke@0 477 }
duke@0 478
duke@0 479 // Check if the space layout has changed significantly?
duke@0 480 // This happens when the space has been resized so that either head or tail
duke@0 481 // chunk became less than a page.
duke@0 482 bool layout_valid = UseAdaptiveNUMAChunkSizing &&
duke@0 483 current_chunk_size(0) > page_size() &&
duke@0 484 current_chunk_size(lgrp_spaces()->length() - 1) > page_size();
duke@0 485
duke@0 486
duke@0 487 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 488 LGRPSpace *ls = lgrp_spaces()->at(i);
duke@0 489 MutableSpace *s = ls->space();
duke@0 490 old_region = s->region();
duke@0 491
duke@0 492 size_t chunk_byte_size = 0, old_chunk_byte_size = 0;
duke@0 493 if (i < lgrp_spaces()->length() - 1) {
duke@0 494 if (!UseAdaptiveNUMAChunkSizing ||
duke@0 495 (UseAdaptiveNUMAChunkSizing && NUMAChunkResizeWeight == 0) ||
duke@0 496 samples_count() < AdaptiveSizePolicyReadyThreshold) {
duke@0 497 // No adaptation. Divide the space equally.
duke@0 498 chunk_byte_size = default_chunk_size();
duke@0 499 } else
duke@0 500 if (!layout_valid || NUMASpaceResizeRate == 0) {
duke@0 501 // Fast adaptation. If no space resize rate is set, resize
duke@0 502 // the chunks instantly.
duke@0 503 chunk_byte_size = adaptive_chunk_size(i, 0);
duke@0 504 } else {
duke@0 505 // Slow adaptation. Resize the chunks moving no more than
duke@0 506 // NUMASpaceResizeRate bytes per collection.
duke@0 507 size_t limit = NUMASpaceResizeRate /
duke@0 508 (lgrp_spaces()->length() * (lgrp_spaces()->length() + 1) / 2);
duke@0 509 chunk_byte_size = adaptive_chunk_size(i, MAX2(limit * (i + 1), page_size()));
duke@0 510 }
duke@0 511
duke@0 512 assert(chunk_byte_size >= page_size(), "Chunk size too small");
duke@0 513 assert(chunk_byte_size <= capacity_in_bytes(), "Sanity check");
duke@0 514 }
duke@0 515
duke@0 516 if (i == 0) { // Bottom chunk
duke@0 517 if (i != lgrp_spaces()->length() - 1) {
duke@0 518 new_region = MemRegion(bottom(), rounded_bottom + (chunk_byte_size >> LogHeapWordSize));
duke@0 519 } else {
duke@0 520 new_region = MemRegion(bottom(), end());
duke@0 521 }
duke@0 522 } else
duke@0 523 if (i < lgrp_spaces()->length() - 1) { // Middle chunks
duke@0 524 MutableSpace *ps = lgrp_spaces()->at(i - 1)->space();
duke@0 525 new_region = MemRegion(ps->end(),
duke@0 526 ps->end() + (chunk_byte_size >> LogHeapWordSize));
duke@0 527 } else { // Top chunk
duke@0 528 MutableSpace *ps = lgrp_spaces()->at(i - 1)->space();
duke@0 529 new_region = MemRegion(ps->end(), end());
duke@0 530 }
duke@0 531 guarantee(region().contains(new_region), "Region invariant");
duke@0 532
duke@0 533
duke@0 534 // The general case:
duke@0 535 // |---------------------|--invalid---|--------------------------|
duke@0 536 // |------------------new_region---------------------------------|
duke@0 537 // |----bottom_region--|---intersection---|------top_region------|
duke@0 538 // |----old_region----|
duke@0 539 // The intersection part has all pages in place we don't need to migrate them.
duke@0 540 // Pages for the top and bottom part should be freed and then reallocated.
duke@0 541
duke@0 542 MemRegion intersection = old_region.intersection(new_region);
duke@0 543
duke@0 544 if (intersection.start() == NULL || intersection.end() == NULL) {
duke@0 545 intersection = MemRegion(new_region.start(), new_region.start());
duke@0 546 }
duke@0 547
duke@0 548 MemRegion invalid_region = ls->invalid_region().intersection(new_region);
duke@0 549 if (!invalid_region.is_empty()) {
duke@0 550 merge_regions(new_region, &intersection, &invalid_region);
duke@0 551 free_region(invalid_region);
duke@0 552 }
duke@0 553 select_tails(new_region, intersection, &bottom_region, &top_region);
duke@0 554 free_region(bottom_region);
duke@0 555 free_region(top_region);
duke@0 556
duke@0 557 // If we clear the region, we would mangle it in debug. That would cause page
duke@0 558 // allocation in a different place. Hence setting the top directly.
duke@0 559 s->initialize(new_region, false);
duke@0 560 s->set_top(s->bottom());
duke@0 561
duke@0 562 ls->set_invalid_region(MemRegion());
duke@0 563
duke@0 564 set_adaptation_cycles(samples_count());
duke@0 565 }
duke@0 566 }
duke@0 567
duke@0 568 // Set the top of the whole space.
duke@0 569 // Mark the the holes in chunks below the top() as invalid.
duke@0 570 void MutableNUMASpace::set_top(HeapWord* value) {
duke@0 571 bool found_top = false;
duke@0 572 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 573 LGRPSpace *ls = lgrp_spaces()->at(i);
duke@0 574 MutableSpace *s = ls->space();
duke@0 575 HeapWord *top = MAX2((HeapWord*)round_down((intptr_t)s->top(), page_size()), s->bottom());
duke@0 576
duke@0 577 if (s->contains(value)) {
duke@0 578 if (top < value && top < s->end()) {
duke@0 579 ls->add_invalid_region(MemRegion(top, value));
duke@0 580 }
duke@0 581 s->set_top(value);
duke@0 582 found_top = true;
duke@0 583 } else {
duke@0 584 if (found_top) {
duke@0 585 s->set_top(s->bottom());
duke@0 586 } else {
duke@0 587 if (top < s->end()) {
duke@0 588 ls->add_invalid_region(MemRegion(top, s->end()));
duke@0 589 }
duke@0 590 s->set_top(s->end());
duke@0 591 }
duke@0 592 }
duke@0 593 }
duke@0 594 MutableSpace::set_top(value);
duke@0 595 }
duke@0 596
duke@0 597 void MutableNUMASpace::clear() {
duke@0 598 MutableSpace::set_top(bottom());
duke@0 599 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 600 lgrp_spaces()->at(i)->space()->clear();
duke@0 601 }
duke@0 602 }
duke@0 603
duke@0 604 HeapWord* MutableNUMASpace::allocate(size_t size) {
duke@0 605 int lgrp_id = Thread::current()->lgrp_id();
duke@0 606 if (lgrp_id == -1) {
duke@0 607 lgrp_id = os::numa_get_group_id();
duke@0 608 Thread::current()->set_lgrp_id(lgrp_id);
duke@0 609 }
duke@0 610
duke@0 611 int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
duke@0 612
duke@0 613 // It is possible that a new CPU has been hotplugged and
duke@0 614 // we haven't reshaped the space accordingly.
duke@0 615 if (i == -1) {
duke@0 616 i = os::random() % lgrp_spaces()->length();
duke@0 617 }
duke@0 618
duke@0 619 MutableSpace *s = lgrp_spaces()->at(i)->space();
duke@0 620 HeapWord *p = s->allocate(size);
duke@0 621
duke@0 622 if (p != NULL && s->free_in_words() < (size_t)oopDesc::header_size()) {
duke@0 623 s->set_top(s->top() - size);
duke@0 624 p = NULL;
duke@0 625 }
duke@0 626 if (p != NULL) {
duke@0 627 if (top() < s->top()) { // Keep _top updated.
duke@0 628 MutableSpace::set_top(s->top());
duke@0 629 }
duke@0 630 }
duke@0 631 // Make the page allocation happen here.
duke@0 632 if (p != NULL) {
duke@0 633 for (HeapWord *i = p; i < p + size; i += os::vm_page_size() >> LogHeapWordSize) {
duke@0 634 *(int*)i = 0;
duke@0 635 }
duke@0 636 }
duke@0 637
duke@0 638 return p;
duke@0 639 }
duke@0 640
duke@0 641 // This version is lock-free.
duke@0 642 HeapWord* MutableNUMASpace::cas_allocate(size_t size) {
duke@0 643 int lgrp_id = Thread::current()->lgrp_id();
duke@0 644 if (lgrp_id == -1) {
duke@0 645 lgrp_id = os::numa_get_group_id();
duke@0 646 Thread::current()->set_lgrp_id(lgrp_id);
duke@0 647 }
duke@0 648
duke@0 649 int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
duke@0 650 // It is possible that a new CPU has been hotplugged and
duke@0 651 // we haven't reshaped the space accordingly.
duke@0 652 if (i == -1) {
duke@0 653 i = os::random() % lgrp_spaces()->length();
duke@0 654 }
duke@0 655 MutableSpace *s = lgrp_spaces()->at(i)->space();
duke@0 656 HeapWord *p = s->cas_allocate(size);
duke@0 657 if (p != NULL && s->free_in_words() < (size_t)oopDesc::header_size()) {
duke@0 658 if (s->cas_deallocate(p, size)) {
duke@0 659 // We were the last to allocate and created a fragment less than
duke@0 660 // a minimal object.
duke@0 661 p = NULL;
duke@0 662 }
duke@0 663 }
duke@0 664 if (p != NULL) {
duke@0 665 HeapWord* cur_top, *cur_chunk_top = p + size;
duke@0 666 while ((cur_top = top()) < cur_chunk_top) { // Keep _top updated.
duke@0 667 if (Atomic::cmpxchg_ptr(cur_chunk_top, top_addr(), cur_top) == cur_top) {
duke@0 668 break;
duke@0 669 }
duke@0 670 }
duke@0 671 }
duke@0 672
duke@0 673 // Make the page allocation happen here.
duke@0 674 if (p != NULL) {
duke@0 675 for (HeapWord *i = p; i < p + size; i += os::vm_page_size() >> LogHeapWordSize) {
duke@0 676 *(int*)i = 0;
duke@0 677 }
duke@0 678 }
duke@0 679 return p;
duke@0 680 }
duke@0 681
duke@0 682 void MutableNUMASpace::print_short_on(outputStream* st) const {
duke@0 683 MutableSpace::print_short_on(st);
duke@0 684 st->print(" (");
duke@0 685 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 686 st->print("lgrp %d: ", lgrp_spaces()->at(i)->lgrp_id());
duke@0 687 lgrp_spaces()->at(i)->space()->print_short_on(st);
duke@0 688 if (i < lgrp_spaces()->length() - 1) {
duke@0 689 st->print(", ");
duke@0 690 }
duke@0 691 }
duke@0 692 st->print(")");
duke@0 693 }
duke@0 694
duke@0 695 void MutableNUMASpace::print_on(outputStream* st) const {
duke@0 696 MutableSpace::print_on(st);
duke@0 697 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 698 LGRPSpace *ls = lgrp_spaces()->at(i);
duke@0 699 st->print(" lgrp %d", ls->lgrp_id());
duke@0 700 ls->space()->print_on(st);
duke@0 701 if (NUMAStats) {
duke@0 702 st->print(" local/remote/unbiased/uncommitted: %dK/%dK/%dK/%dK, large/small pages: %d/%d\n",
duke@0 703 ls->space_stats()->_local_space / K,
duke@0 704 ls->space_stats()->_remote_space / K,
duke@0 705 ls->space_stats()->_unbiased_space / K,
duke@0 706 ls->space_stats()->_uncommited_space / K,
duke@0 707 ls->space_stats()->_large_pages,
duke@0 708 ls->space_stats()->_small_pages);
duke@0 709 }
duke@0 710 }
duke@0 711 }
duke@0 712
duke@0 713 void MutableNUMASpace::verify(bool allow_dirty) const {
duke@0 714 for (int i = 0; i < lgrp_spaces()->length(); i++) {
duke@0 715 lgrp_spaces()->at(i)->space()->verify(allow_dirty);
duke@0 716 }
duke@0 717 }
duke@0 718
duke@0 719 // Scan pages and gather stats about page placement and size.
duke@0 720 void MutableNUMASpace::LGRPSpace::accumulate_statistics(size_t page_size) {
duke@0 721 clear_space_stats();
duke@0 722 char *start = (char*)round_to((intptr_t) space()->bottom(), page_size);
duke@0 723 char* end = (char*)round_down((intptr_t) space()->end(), page_size);
duke@0 724 if (start < end) {
duke@0 725 for (char *p = start; p < end;) {
duke@0 726 os::page_info info;
duke@0 727 if (os::get_page_info(p, &info)) {
duke@0 728 if (info.size > 0) {
duke@0 729 if (info.size > (size_t)os::vm_page_size()) {
duke@0 730 space_stats()->_large_pages++;
duke@0 731 } else {
duke@0 732 space_stats()->_small_pages++;
duke@0 733 }
duke@0 734 if (info.lgrp_id == lgrp_id()) {
duke@0 735 space_stats()->_local_space += info.size;
duke@0 736 } else {
duke@0 737 space_stats()->_remote_space += info.size;
duke@0 738 }
duke@0 739 p += info.size;
duke@0 740 } else {
duke@0 741 p += os::vm_page_size();
duke@0 742 space_stats()->_uncommited_space += os::vm_page_size();
duke@0 743 }
duke@0 744 } else {
duke@0 745 return;
duke@0 746 }
duke@0 747 }
duke@0 748 }
duke@0 749 space_stats()->_unbiased_space = pointer_delta(start, space()->bottom(), sizeof(char)) +
duke@0 750 pointer_delta(space()->end(), end, sizeof(char));
duke@0 751
duke@0 752 }
duke@0 753
duke@0 754 // Scan page_count pages and verify if they have the right size and right placement.
duke@0 755 // If invalid pages are found they are freed in hope that subsequent reallocation
duke@0 756 // will be more successful.
duke@0 757 void MutableNUMASpace::LGRPSpace::scan_pages(size_t page_size, size_t page_count)
duke@0 758 {
duke@0 759 char* range_start = (char*)round_to((intptr_t) space()->bottom(), page_size);
duke@0 760 char* range_end = (char*)round_down((intptr_t) space()->end(), page_size);
duke@0 761
duke@0 762 if (range_start > last_page_scanned() || last_page_scanned() >= range_end) {
duke@0 763 set_last_page_scanned(range_start);
duke@0 764 }
duke@0 765
duke@0 766 char *scan_start = last_page_scanned();
duke@0 767 char* scan_end = MIN2(scan_start + page_size * page_count, range_end);
duke@0 768
duke@0 769 os::page_info page_expected, page_found;
duke@0 770 page_expected.size = page_size;
duke@0 771 page_expected.lgrp_id = lgrp_id();
duke@0 772
duke@0 773 char *s = scan_start;
duke@0 774 while (s < scan_end) {
duke@0 775 char *e = os::scan_pages(s, (char*)scan_end, &page_expected, &page_found);
duke@0 776 if (e == NULL) {
duke@0 777 break;
duke@0 778 }
duke@0 779 if (e != scan_end) {
duke@0 780 if ((page_expected.size != page_size || page_expected.lgrp_id != lgrp_id())
duke@0 781 && page_expected.size != 0) {
duke@0 782 os::free_memory(s, pointer_delta(e, s, sizeof(char)));
duke@0 783 }
duke@0 784 page_expected = page_found;
duke@0 785 }
duke@0 786 s = e;
duke@0 787 }
duke@0 788
duke@0 789 set_last_page_scanned(scan_end);
duke@0 790 }