0N/A
0N/A/*
3636N/A * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "gc_implementation/shared/mutableNUMASpace.hpp"
1879N/A#include "gc_implementation/shared/spaceDecorator.hpp"
1879N/A#include "memory/sharedHeap.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#ifdef TARGET_OS_FAMILY_linux
1879N/A# include "thread_linux.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_solaris
1879N/A# include "thread_solaris.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_windows
1879N/A# include "thread_windows.inline.hpp"
1879N/A#endif
2796N/A#ifdef TARGET_OS_FAMILY_bsd
2796N/A# include "thread_bsd.inline.hpp"
2796N/A#endif
0N/A
0N/A
535N/AMutableNUMASpace::MutableNUMASpace(size_t alignment) : MutableSpace(alignment) {
3863N/A _lgrp_spaces = new (ResourceObj::C_HEAP, mtGC) GrowableArray<LGRPSpace*>(0, true);
0N/A _page_size = os::vm_page_size();
0N/A _adaptation_cycles = 0;
0N/A _samples_count = 0;
0N/A update_layout(true);
0N/A}
0N/A
0N/AMutableNUMASpace::~MutableNUMASpace() {
0N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
0N/A delete lgrp_spaces()->at(i);
0N/A }
0N/A delete lgrp_spaces();
0N/A}
0N/A
263N/A#ifndef PRODUCT
0N/Avoid MutableNUMASpace::mangle_unused_area() {
263N/A // This method should do nothing.
263N/A // It can be called on a numa space during a full compaction.
263N/A}
263N/Avoid MutableNUMASpace::mangle_unused_area_complete() {
263N/A // This method should do nothing.
263N/A // It can be called on a numa space during a full compaction.
263N/A}
263N/Avoid MutableNUMASpace::mangle_region(MemRegion mr) {
263N/A // This method should do nothing because numa spaces are not mangled.
0N/A}
263N/Avoid MutableNUMASpace::set_top_for_allocations(HeapWord* v) {
263N/A assert(false, "Do not mangle MutableNUMASpace's");
263N/A}
263N/Avoid MutableNUMASpace::set_top_for_allocations() {
263N/A // This method should do nothing.
263N/A}
263N/Avoid MutableNUMASpace::check_mangled_unused_area(HeapWord* limit) {
263N/A // This method should do nothing.
263N/A}
263N/Avoid MutableNUMASpace::check_mangled_unused_area_complete() {
263N/A // This method should do nothing.
263N/A}
263N/A#endif // NOT_PRODUCT
0N/A
0N/A// There may be unallocated holes in the middle chunks
0N/A// that should be filled with dead objects to ensure parseability.
0N/Avoid MutableNUMASpace::ensure_parsability() {
0N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
0N/A LGRPSpace *ls = lgrp_spaces()->at(i);
0N/A MutableSpace *s = ls->space();
605N/A if (s->top() < top()) { // For all spaces preceding the one containing top()
0N/A if (s->free_in_words() > 0) {
3636N/A intptr_t cur_top = (intptr_t)s->top();
3636N/A size_t words_left_to_fill = pointer_delta(s->end(), s->top());;
3636N/A while (words_left_to_fill > 0) {
3636N/A size_t words_to_fill = MIN2(words_left_to_fill, CollectedHeap::filler_array_max_size());
3636N/A assert(words_to_fill >= CollectedHeap::min_fill_size(),
3636N/A err_msg("Remaining size ("SIZE_FORMAT ") is too small to fill (based on " SIZE_FORMAT " and " SIZE_FORMAT ")",
3636N/A words_to_fill, words_left_to_fill, CollectedHeap::filler_array_max_size()));
3636N/A CollectedHeap::fill_with_object((HeapWord*)cur_top, words_to_fill);
3636N/A if (!os::numa_has_static_binding()) {
3636N/A size_t touched_words = words_to_fill;
0N/A#ifndef ASSERT
3636N/A if (!ZapUnusedHeapArea) {
3636N/A touched_words = MIN2((size_t)align_object_size(typeArrayOopDesc::header_size(T_INT)),
3636N/A touched_words);
3636N/A }
0N/A#endif
3636N/A MemRegion invalid;
3636N/A HeapWord *crossing_start = (HeapWord*)round_to(cur_top, os::vm_page_size());
3636N/A HeapWord *crossing_end = (HeapWord*)round_to(cur_top + touched_words, os::vm_page_size());
3636N/A if (crossing_start != crossing_end) {
3636N/A // If object header crossed a small page boundary we mark the area
3636N/A // as invalid rounding it to a page_size().
3636N/A HeapWord *start = MAX2((HeapWord*)round_down(cur_top, page_size()), s->bottom());
3636N/A HeapWord *end = MIN2((HeapWord*)round_to(cur_top + touched_words, page_size()), s->end());
3636N/A invalid = MemRegion(start, end);
3636N/A }
3636N/A
3636N/A ls->add_invalid_region(invalid);
141N/A }
3636N/A cur_top = cur_top + (words_to_fill * HeapWordSize);
3636N/A words_left_to_fill -= words_to_fill;
0N/A }
0N/A }
0N/A } else {
141N/A if (!os::numa_has_static_binding()) {
0N/A#ifdef ASSERT
0N/A MemRegion invalid(s->top(), s->end());
0N/A ls->add_invalid_region(invalid);
141N/A#else
141N/A if (ZapUnusedHeapArea) {
141N/A MemRegion invalid(s->top(), s->end());
141N/A ls->add_invalid_region(invalid);
144N/A } else {
144N/A return;
144N/A }
0N/A#endif
144N/A } else {
144N/A return;
141N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/Asize_t MutableNUMASpace::used_in_words() const {
0N/A size_t s = 0;
0N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
0N/A s += lgrp_spaces()->at(i)->space()->used_in_words();
0N/A }
0N/A return s;
0N/A}
0N/A
0N/Asize_t MutableNUMASpace::free_in_words() const {
0N/A size_t s = 0;
0N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
0N/A s += lgrp_spaces()->at(i)->space()->free_in_words();
0N/A }
0N/A return s;
0N/A}
0N/A
0N/A
0N/Asize_t MutableNUMASpace::tlab_capacity(Thread *thr) const {
0N/A guarantee(thr != NULL, "No thread");
0N/A int lgrp_id = thr->lgrp_id();
268N/A if (lgrp_id == -1) {
268N/A // This case can occur after the topology of the system has
268N/A // changed. Thread can change their location, the new home
268N/A // group will be determined during the first allocation
268N/A // attempt. For now we can safely assume that all spaces
268N/A // have equal size because the whole space will be reinitialized.
268N/A if (lgrp_spaces()->length() > 0) {
268N/A return capacity_in_bytes() / lgrp_spaces()->length();
268N/A } else {
268N/A assert(false, "There should be at least one locality group");
268N/A return 0;
268N/A }
268N/A }
268N/A // That's the normal case, where we know the locality group of the thread.
0N/A int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
0N/A if (i == -1) {
0N/A return 0;
0N/A }
0N/A return lgrp_spaces()->at(i)->space()->capacity_in_bytes();
0N/A}
0N/A
0N/Asize_t MutableNUMASpace::unsafe_max_tlab_alloc(Thread *thr) const {
268N/A // Please see the comments for tlab_capacity().
0N/A guarantee(thr != NULL, "No thread");
0N/A int lgrp_id = thr->lgrp_id();
268N/A if (lgrp_id == -1) {
268N/A if (lgrp_spaces()->length() > 0) {
268N/A return free_in_bytes() / lgrp_spaces()->length();
268N/A } else {
268N/A assert(false, "There should be at least one locality group");
268N/A return 0;
268N/A }
268N/A }
0N/A int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
0N/A if (i == -1) {
0N/A return 0;
0N/A }
0N/A return lgrp_spaces()->at(i)->space()->free_in_bytes();
0N/A}
0N/A
373N/A
373N/Asize_t MutableNUMASpace::capacity_in_words(Thread* thr) const {
373N/A guarantee(thr != NULL, "No thread");
373N/A int lgrp_id = thr->lgrp_id();
373N/A if (lgrp_id == -1) {
373N/A if (lgrp_spaces()->length() > 0) {
373N/A return capacity_in_words() / lgrp_spaces()->length();
373N/A } else {
373N/A assert(false, "There should be at least one locality group");
373N/A return 0;
373N/A }
373N/A }
373N/A int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
373N/A if (i == -1) {
373N/A return 0;
373N/A }
373N/A return lgrp_spaces()->at(i)->space()->capacity_in_words();
373N/A}
373N/A
0N/A// Check if the NUMA topology has changed. Add and remove spaces if needed.
0N/A// The update can be forced by setting the force parameter equal to true.
0N/Abool MutableNUMASpace::update_layout(bool force) {
0N/A // Check if the topology had changed.
0N/A bool changed = os::numa_topology_changed();
0N/A if (force || changed) {
0N/A // Compute lgrp intersection. Add/remove spaces.
0N/A int lgrp_limit = (int)os::numa_get_groups_num();
3863N/A int *lgrp_ids = NEW_C_HEAP_ARRAY(int, lgrp_limit, mtGC);
0N/A int lgrp_num = (int)os::numa_get_leaf_groups(lgrp_ids, lgrp_limit);
0N/A assert(lgrp_num > 0, "There should be at least one locality group");
0N/A // Add new spaces for the new nodes
0N/A for (int i = 0; i < lgrp_num; i++) {
0N/A bool found = false;
0N/A for (int j = 0; j < lgrp_spaces()->length(); j++) {
0N/A if (lgrp_spaces()->at(j)->lgrp_id() == lgrp_ids[i]) {
0N/A found = true;
0N/A break;
0N/A }
0N/A }
0N/A if (!found) {
535N/A lgrp_spaces()->append(new LGRPSpace(lgrp_ids[i], alignment()));
0N/A }
0N/A }
0N/A
0N/A // Remove spaces for the removed nodes.
0N/A for (int i = 0; i < lgrp_spaces()->length();) {
0N/A bool found = false;
0N/A for (int j = 0; j < lgrp_num; j++) {
0N/A if (lgrp_spaces()->at(i)->lgrp_id() == lgrp_ids[j]) {
0N/A found = true;
0N/A break;
0N/A }
0N/A }
0N/A if (!found) {
0N/A delete lgrp_spaces()->at(i);
0N/A lgrp_spaces()->remove_at(i);
0N/A } else {
0N/A i++;
0N/A }
0N/A }
0N/A
3863N/A FREE_C_HEAP_ARRAY(int, lgrp_ids, mtGC);
0N/A
0N/A if (changed) {
0N/A for (JavaThread *thread = Threads::first(); thread; thread = thread->next()) {
0N/A thread->set_lgrp_id(-1);
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A return false;
0N/A}
0N/A
0N/A// Bias region towards the first-touching lgrp. Set the right page sizes.
141N/Avoid MutableNUMASpace::bias_region(MemRegion mr, int lgrp_id) {
0N/A HeapWord *start = (HeapWord*)round_to((intptr_t)mr.start(), page_size());
0N/A HeapWord *end = (HeapWord*)round_down((intptr_t)mr.end(), page_size());
0N/A if (end > start) {
0N/A MemRegion aligned_region(start, end);
0N/A assert((intptr_t)aligned_region.start() % page_size() == 0 &&
0N/A (intptr_t)aligned_region.byte_size() % page_size() == 0, "Bad alignment");
0N/A assert(region().contains(aligned_region), "Sanity");
141N/A // First we tell the OS which page size we want in the given range. The underlying
141N/A // large page can be broken down if we require small pages.
0N/A os::realign_memory((char*)aligned_region.start(), aligned_region.byte_size(), page_size());
141N/A // Then we uncommit the pages in the range.
3014N/A os::free_memory((char*)aligned_region.start(), aligned_region.byte_size(), page_size());
141N/A // And make them local/first-touch biased.
141N/A os::numa_make_local((char*)aligned_region.start(), aligned_region.byte_size(), lgrp_id);
0N/A }
0N/A}
0N/A
0N/A// Free all pages in the region.
0N/Avoid MutableNUMASpace::free_region(MemRegion mr) {
0N/A HeapWord *start = (HeapWord*)round_to((intptr_t)mr.start(), page_size());
0N/A HeapWord *end = (HeapWord*)round_down((intptr_t)mr.end(), page_size());
0N/A if (end > start) {
0N/A MemRegion aligned_region(start, end);
0N/A assert((intptr_t)aligned_region.start() % page_size() == 0 &&
0N/A (intptr_t)aligned_region.byte_size() % page_size() == 0, "Bad alignment");
0N/A assert(region().contains(aligned_region), "Sanity");
3014N/A os::free_memory((char*)aligned_region.start(), aligned_region.byte_size(), page_size());
0N/A }
0N/A}
0N/A
0N/A// Update space layout. Perform adaptation.
0N/Avoid MutableNUMASpace::update() {
0N/A if (update_layout(false)) {
0N/A // If the topology has changed, make all chunks zero-sized.
268N/A // And clear the alloc-rate statistics.
268N/A // In future we may want to handle this more gracefully in order
268N/A // to avoid the reallocation of the pages as much as possible.
0N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
268N/A LGRPSpace *ls = lgrp_spaces()->at(i);
268N/A MutableSpace *s = ls->space();
0N/A s->set_end(s->bottom());
0N/A s->set_top(s->bottom());
268N/A ls->clear_alloc_rate();
0N/A }
263N/A // A NUMA space is never mangled
263N/A initialize(region(),
263N/A SpaceDecorator::Clear,
263N/A SpaceDecorator::DontMangle);
0N/A } else {
0N/A bool should_initialize = false;
141N/A if (!os::numa_has_static_binding()) {
141N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
141N/A if (!lgrp_spaces()->at(i)->invalid_region().is_empty()) {
141N/A should_initialize = true;
141N/A break;
141N/A }
0N/A }
0N/A }
0N/A
0N/A if (should_initialize ||
0N/A (UseAdaptiveNUMAChunkSizing && adaptation_cycles() < samples_count())) {
263N/A // A NUMA space is never mangled
263N/A initialize(region(),
263N/A SpaceDecorator::Clear,
263N/A SpaceDecorator::DontMangle);
0N/A }
0N/A }
0N/A
0N/A if (NUMAStats) {
0N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
0N/A lgrp_spaces()->at(i)->accumulate_statistics(page_size());
0N/A }
0N/A }
0N/A
0N/A scan_pages(NUMAPageScanRate);
0N/A}
0N/A
0N/A// Scan pages. Free pages that have smaller size or wrong placement.
0N/Avoid MutableNUMASpace::scan_pages(size_t page_count)
0N/A{
0N/A size_t pages_per_chunk = page_count / lgrp_spaces()->length();
0N/A if (pages_per_chunk > 0) {
0N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
0N/A LGRPSpace *ls = lgrp_spaces()->at(i);
0N/A ls->scan_pages(page_size(), pages_per_chunk);
0N/A }
0N/A }
0N/A}
0N/A
0N/A// Accumulate statistics about the allocation rate of each lgrp.
0N/Avoid MutableNUMASpace::accumulate_statistics() {
0N/A if (UseAdaptiveNUMAChunkSizing) {
0N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
0N/A lgrp_spaces()->at(i)->sample();
0N/A }
0N/A increment_samples_count();
0N/A }
0N/A
0N/A if (NUMAStats) {
0N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
0N/A lgrp_spaces()->at(i)->accumulate_statistics(page_size());
0N/A }
0N/A }
0N/A}
0N/A
0N/A// Get the current size of a chunk.
0N/A// This function computes the size of the chunk based on the
0N/A// difference between chunk ends. This allows it to work correctly in
0N/A// case the whole space is resized and during the process of adaptive
0N/A// chunk resizing.
0N/Asize_t MutableNUMASpace::current_chunk_size(int i) {
0N/A HeapWord *cur_end, *prev_end;
0N/A if (i == 0) {
0N/A prev_end = bottom();
0N/A } else {
0N/A prev_end = lgrp_spaces()->at(i - 1)->space()->end();
0N/A }
0N/A if (i == lgrp_spaces()->length() - 1) {
0N/A cur_end = end();
0N/A } else {
0N/A cur_end = lgrp_spaces()->at(i)->space()->end();
0N/A }
0N/A if (cur_end > prev_end) {
0N/A return pointer_delta(cur_end, prev_end, sizeof(char));
0N/A }
0N/A return 0;
0N/A}
0N/A
0N/A// Return the default chunk size by equally diving the space.
0N/A// page_size() aligned.
0N/Asize_t MutableNUMASpace::default_chunk_size() {
0N/A return base_space_size() / lgrp_spaces()->length() * page_size();
0N/A}
0N/A
0N/A// Produce a new chunk size. page_size() aligned.
391N/A// This function is expected to be called on sequence of i's from 0 to
391N/A// lgrp_spaces()->length().
0N/Asize_t MutableNUMASpace::adaptive_chunk_size(int i, size_t limit) {
0N/A size_t pages_available = base_space_size();
0N/A for (int j = 0; j < i; j++) {
0N/A pages_available -= round_down(current_chunk_size(j), page_size()) / page_size();
0N/A }
0N/A pages_available -= lgrp_spaces()->length() - i - 1;
0N/A assert(pages_available > 0, "No pages left");
0N/A float alloc_rate = 0;
0N/A for (int j = i; j < lgrp_spaces()->length(); j++) {
0N/A alloc_rate += lgrp_spaces()->at(j)->alloc_rate()->average();
0N/A }
0N/A size_t chunk_size = 0;
0N/A if (alloc_rate > 0) {
0N/A LGRPSpace *ls = lgrp_spaces()->at(i);
391N/A chunk_size = (size_t)(ls->alloc_rate()->average() / alloc_rate * pages_available) * page_size();
0N/A }
0N/A chunk_size = MAX2(chunk_size, page_size());
0N/A
0N/A if (limit > 0) {
0N/A limit = round_down(limit, page_size());
0N/A if (chunk_size > current_chunk_size(i)) {
462N/A size_t upper_bound = pages_available * page_size();
462N/A if (upper_bound > limit &&
462N/A current_chunk_size(i) < upper_bound - limit) {
462N/A // The resulting upper bound should not exceed the available
462N/A // amount of memory (pages_available * page_size()).
462N/A upper_bound = current_chunk_size(i) + limit;
462N/A }
462N/A chunk_size = MIN2(chunk_size, upper_bound);
0N/A } else {
462N/A size_t lower_bound = page_size();
462N/A if (current_chunk_size(i) > limit) { // lower_bound shouldn't underflow.
462N/A lower_bound = current_chunk_size(i) - limit;
462N/A }
462N/A chunk_size = MAX2(chunk_size, lower_bound);
0N/A }
0N/A }
0N/A assert(chunk_size <= pages_available * page_size(), "Chunk size out of range");
0N/A return chunk_size;
0N/A}
0N/A
0N/A
0N/A// Return the bottom_region and the top_region. Align them to page_size() boundary.
0N/A// |------------------new_region---------------------------------|
0N/A// |----bottom_region--|---intersection---|------top_region------|
0N/Avoid MutableNUMASpace::select_tails(MemRegion new_region, MemRegion intersection,
0N/A MemRegion* bottom_region, MemRegion *top_region) {
0N/A // Is there bottom?
0N/A if (new_region.start() < intersection.start()) { // Yes
0N/A // Try to coalesce small pages into a large one.
535N/A if (UseLargePages && page_size() >= alignment()) {
535N/A HeapWord* p = (HeapWord*)round_to((intptr_t) intersection.start(), alignment());
0N/A if (new_region.contains(p)
535N/A && pointer_delta(p, new_region.start(), sizeof(char)) >= alignment()) {
0N/A if (intersection.contains(p)) {
0N/A intersection = MemRegion(p, intersection.end());
0N/A } else {
0N/A intersection = MemRegion(p, p);
0N/A }
0N/A }
0N/A }
0N/A *bottom_region = MemRegion(new_region.start(), intersection.start());
0N/A } else {
0N/A *bottom_region = MemRegion();
0N/A }
0N/A
0N/A // Is there top?
0N/A if (intersection.end() < new_region.end()) { // Yes
0N/A // Try to coalesce small pages into a large one.
535N/A if (UseLargePages && page_size() >= alignment()) {
535N/A HeapWord* p = (HeapWord*)round_down((intptr_t) intersection.end(), alignment());
0N/A if (new_region.contains(p)
535N/A && pointer_delta(new_region.end(), p, sizeof(char)) >= alignment()) {
0N/A if (intersection.contains(p)) {
0N/A intersection = MemRegion(intersection.start(), p);
0N/A } else {
0N/A intersection = MemRegion(p, p);
0N/A }
0N/A }
0N/A }
0N/A *top_region = MemRegion(intersection.end(), new_region.end());
0N/A } else {
0N/A *top_region = MemRegion();
0N/A }
0N/A}
0N/A
0N/A// Try to merge the invalid region with the bottom or top region by decreasing
0N/A// the intersection area. Return the invalid_region aligned to the page_size()
0N/A// boundary if it's inside the intersection. Return non-empty invalid_region
0N/A// if it lies inside the intersection (also page-aligned).
0N/A// |------------------new_region---------------------------------|
0N/A// |----------------|-------invalid---|--------------------------|
0N/A// |----bottom_region--|---intersection---|------top_region------|
0N/Avoid MutableNUMASpace::merge_regions(MemRegion new_region, MemRegion* intersection,
0N/A MemRegion *invalid_region) {
0N/A if (intersection->start() >= invalid_region->start() && intersection->contains(invalid_region->end())) {
0N/A *intersection = MemRegion(invalid_region->end(), intersection->end());
0N/A *invalid_region = MemRegion();
0N/A } else
0N/A if (intersection->end() <= invalid_region->end() && intersection->contains(invalid_region->start())) {
0N/A *intersection = MemRegion(intersection->start(), invalid_region->start());
0N/A *invalid_region = MemRegion();
0N/A } else
0N/A if (intersection->equals(*invalid_region) || invalid_region->contains(*intersection)) {
0N/A *intersection = MemRegion(new_region.start(), new_region.start());
0N/A *invalid_region = MemRegion();
0N/A } else
0N/A if (intersection->contains(invalid_region)) {
0N/A // That's the only case we have to make an additional bias_region() call.
0N/A HeapWord* start = invalid_region->start();
0N/A HeapWord* end = invalid_region->end();
535N/A if (UseLargePages && page_size() >= alignment()) {
535N/A HeapWord *p = (HeapWord*)round_down((intptr_t) start, alignment());
0N/A if (new_region.contains(p)) {
0N/A start = p;
0N/A }
535N/A p = (HeapWord*)round_to((intptr_t) end, alignment());
0N/A if (new_region.contains(end)) {
0N/A end = p;
0N/A }
0N/A }
0N/A if (intersection->start() > start) {
0N/A *intersection = MemRegion(start, intersection->end());
0N/A }
0N/A if (intersection->end() < end) {
0N/A *intersection = MemRegion(intersection->start(), end);
0N/A }
0N/A *invalid_region = MemRegion(start, end);
0N/A }
0N/A}
0N/A
263N/Avoid MutableNUMASpace::initialize(MemRegion mr,
263N/A bool clear_space,
535N/A bool mangle_space,
535N/A bool setup_pages) {
0N/A assert(clear_space, "Reallocation will destory data!");
0N/A assert(lgrp_spaces()->length() > 0, "There should be at least one space");
0N/A
0N/A MemRegion old_region = region(), new_region;
0N/A set_bottom(mr.start());
0N/A set_end(mr.end());
263N/A // Must always clear the space
263N/A clear(SpaceDecorator::DontMangle);
0N/A
0N/A // Compute chunk sizes
0N/A size_t prev_page_size = page_size();
535N/A set_page_size(UseLargePages ? alignment() : os::vm_page_size());
0N/A HeapWord* rounded_bottom = (HeapWord*)round_to((intptr_t) bottom(), page_size());
0N/A HeapWord* rounded_end = (HeapWord*)round_down((intptr_t) end(), page_size());
0N/A size_t base_space_size_pages = pointer_delta(rounded_end, rounded_bottom, sizeof(char)) / page_size();
0N/A
0N/A // Try small pages if the chunk size is too small
0N/A if (base_space_size_pages / lgrp_spaces()->length() == 0
0N/A && page_size() > (size_t)os::vm_page_size()) {
0N/A set_page_size(os::vm_page_size());
0N/A rounded_bottom = (HeapWord*)round_to((intptr_t) bottom(), page_size());
0N/A rounded_end = (HeapWord*)round_down((intptr_t) end(), page_size());
0N/A base_space_size_pages = pointer_delta(rounded_end, rounded_bottom, sizeof(char)) / page_size();
0N/A }
0N/A guarantee(base_space_size_pages / lgrp_spaces()->length() > 0, "Space too small");
0N/A set_base_space_size(base_space_size_pages);
0N/A
0N/A // Handle space resize
0N/A MemRegion top_region, bottom_region;
0N/A if (!old_region.equals(region())) {
0N/A new_region = MemRegion(rounded_bottom, rounded_end);
0N/A MemRegion intersection = new_region.intersection(old_region);
0N/A if (intersection.start() == NULL ||
0N/A intersection.end() == NULL ||
0N/A prev_page_size > page_size()) { // If the page size got smaller we have to change
0N/A // the page size preference for the whole space.
0N/A intersection = MemRegion(new_region.start(), new_region.start());
0N/A }
0N/A select_tails(new_region, intersection, &bottom_region, &top_region);
141N/A bias_region(bottom_region, lgrp_spaces()->at(0)->lgrp_id());
141N/A bias_region(top_region, lgrp_spaces()->at(lgrp_spaces()->length() - 1)->lgrp_id());
0N/A }
0N/A
0N/A // Check if the space layout has changed significantly?
0N/A // This happens when the space has been resized so that either head or tail
0N/A // chunk became less than a page.
0N/A bool layout_valid = UseAdaptiveNUMAChunkSizing &&
0N/A current_chunk_size(0) > page_size() &&
0N/A current_chunk_size(lgrp_spaces()->length() - 1) > page_size();
0N/A
0N/A
0N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
0N/A LGRPSpace *ls = lgrp_spaces()->at(i);
0N/A MutableSpace *s = ls->space();
0N/A old_region = s->region();
0N/A
0N/A size_t chunk_byte_size = 0, old_chunk_byte_size = 0;
0N/A if (i < lgrp_spaces()->length() - 1) {
0N/A if (!UseAdaptiveNUMAChunkSizing ||
0N/A (UseAdaptiveNUMAChunkSizing && NUMAChunkResizeWeight == 0) ||
0N/A samples_count() < AdaptiveSizePolicyReadyThreshold) {
0N/A // No adaptation. Divide the space equally.
0N/A chunk_byte_size = default_chunk_size();
0N/A } else
0N/A if (!layout_valid || NUMASpaceResizeRate == 0) {
0N/A // Fast adaptation. If no space resize rate is set, resize
0N/A // the chunks instantly.
0N/A chunk_byte_size = adaptive_chunk_size(i, 0);
0N/A } else {
0N/A // Slow adaptation. Resize the chunks moving no more than
0N/A // NUMASpaceResizeRate bytes per collection.
0N/A size_t limit = NUMASpaceResizeRate /
0N/A (lgrp_spaces()->length() * (lgrp_spaces()->length() + 1) / 2);
0N/A chunk_byte_size = adaptive_chunk_size(i, MAX2(limit * (i + 1), page_size()));
0N/A }
0N/A
0N/A assert(chunk_byte_size >= page_size(), "Chunk size too small");
0N/A assert(chunk_byte_size <= capacity_in_bytes(), "Sanity check");
0N/A }
0N/A
0N/A if (i == 0) { // Bottom chunk
0N/A if (i != lgrp_spaces()->length() - 1) {
0N/A new_region = MemRegion(bottom(), rounded_bottom + (chunk_byte_size >> LogHeapWordSize));
0N/A } else {
0N/A new_region = MemRegion(bottom(), end());
0N/A }
0N/A } else
0N/A if (i < lgrp_spaces()->length() - 1) { // Middle chunks
0N/A MutableSpace *ps = lgrp_spaces()->at(i - 1)->space();
0N/A new_region = MemRegion(ps->end(),
0N/A ps->end() + (chunk_byte_size >> LogHeapWordSize));
0N/A } else { // Top chunk
0N/A MutableSpace *ps = lgrp_spaces()->at(i - 1)->space();
0N/A new_region = MemRegion(ps->end(), end());
0N/A }
0N/A guarantee(region().contains(new_region), "Region invariant");
0N/A
0N/A
0N/A // The general case:
0N/A // |---------------------|--invalid---|--------------------------|
0N/A // |------------------new_region---------------------------------|
0N/A // |----bottom_region--|---intersection---|------top_region------|
0N/A // |----old_region----|
0N/A // The intersection part has all pages in place we don't need to migrate them.
0N/A // Pages for the top and bottom part should be freed and then reallocated.
0N/A
0N/A MemRegion intersection = old_region.intersection(new_region);
0N/A
0N/A if (intersection.start() == NULL || intersection.end() == NULL) {
0N/A intersection = MemRegion(new_region.start(), new_region.start());
0N/A }
0N/A
141N/A if (!os::numa_has_static_binding()) {
141N/A MemRegion invalid_region = ls->invalid_region().intersection(new_region);
141N/A // Invalid region is a range of memory that could've possibly
141N/A // been allocated on the other node. That's relevant only on Solaris where
141N/A // there is no static memory binding.
141N/A if (!invalid_region.is_empty()) {
141N/A merge_regions(new_region, &intersection, &invalid_region);
141N/A free_region(invalid_region);
141N/A ls->set_invalid_region(MemRegion());
141N/A }
0N/A }
141N/A
0N/A select_tails(new_region, intersection, &bottom_region, &top_region);
141N/A
141N/A if (!os::numa_has_static_binding()) {
141N/A // If that's a system with the first-touch policy then it's enough
141N/A // to free the pages.
141N/A free_region(bottom_region);
141N/A free_region(top_region);
141N/A } else {
141N/A // In a system with static binding we have to change the bias whenever
141N/A // we reshape the heap.
141N/A bias_region(bottom_region, ls->lgrp_id());
141N/A bias_region(top_region, ls->lgrp_id());
141N/A }
0N/A
263N/A // Clear space (set top = bottom) but never mangle.
535N/A s->initialize(new_region, SpaceDecorator::Clear, SpaceDecorator::DontMangle, MutableSpace::DontSetupPages);
0N/A
0N/A set_adaptation_cycles(samples_count());
0N/A }
0N/A}
0N/A
0N/A// Set the top of the whole space.
0N/A// Mark the the holes in chunks below the top() as invalid.
0N/Avoid MutableNUMASpace::set_top(HeapWord* value) {
0N/A bool found_top = false;
190N/A for (int i = 0; i < lgrp_spaces()->length();) {
0N/A LGRPSpace *ls = lgrp_spaces()->at(i);
0N/A MutableSpace *s = ls->space();
0N/A HeapWord *top = MAX2((HeapWord*)round_down((intptr_t)s->top(), page_size()), s->bottom());
0N/A
0N/A if (s->contains(value)) {
190N/A // Check if setting the chunk's top to a given value would create a hole less than
190N/A // a minimal object; assuming that's not the last chunk in which case we don't care.
190N/A if (i < lgrp_spaces()->length() - 1) {
190N/A size_t remainder = pointer_delta(s->end(), value);
481N/A const size_t min_fill_size = CollectedHeap::min_fill_size();
481N/A if (remainder < min_fill_size && remainder > 0) {
481N/A // Add a minimum size filler object; it will cross the chunk boundary.
481N/A CollectedHeap::fill_with_object(value, min_fill_size);
481N/A value += min_fill_size;
190N/A assert(!s->contains(value), "Should be in the next chunk");
190N/A // Restart the loop from the same chunk, since the value has moved
190N/A // to the next one.
190N/A continue;
190N/A }
190N/A }
190N/A
141N/A if (!os::numa_has_static_binding() && top < value && top < s->end()) {
0N/A ls->add_invalid_region(MemRegion(top, value));
0N/A }
0N/A s->set_top(value);
0N/A found_top = true;
0N/A } else {
0N/A if (found_top) {
0N/A s->set_top(s->bottom());
0N/A } else {
141N/A if (!os::numa_has_static_binding() && top < s->end()) {
141N/A ls->add_invalid_region(MemRegion(top, s->end()));
141N/A }
141N/A s->set_top(s->end());
0N/A }
0N/A }
190N/A i++;
0N/A }
0N/A MutableSpace::set_top(value);
0N/A}
0N/A
263N/Avoid MutableNUMASpace::clear(bool mangle_space) {
0N/A MutableSpace::set_top(bottom());
0N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
263N/A // Never mangle NUMA spaces because the mangling will
263N/A // bind the memory to a possibly unwanted lgroup.
263N/A lgrp_spaces()->at(i)->space()->clear(SpaceDecorator::DontMangle);
0N/A }
0N/A}
0N/A
141N/A/*
141N/A Linux supports static memory binding, therefore the most part of the
141N/A logic dealing with the possible invalid page allocation is effectively
141N/A disabled. Besides there is no notion of the home node in Linux. A
141N/A thread is allowed to migrate freely. Although the scheduler is rather
141N/A reluctant to move threads between the nodes. We check for the current
141N/A node every allocation. And with a high probability a thread stays on
141N/A the same node for some time allowing local access to recently allocated
141N/A objects.
141N/A */
141N/A
0N/AHeapWord* MutableNUMASpace::allocate(size_t size) {
141N/A Thread* thr = Thread::current();
141N/A int lgrp_id = thr->lgrp_id();
141N/A if (lgrp_id == -1 || !os::numa_has_group_homing()) {
0N/A lgrp_id = os::numa_get_group_id();
141N/A thr->set_lgrp_id(lgrp_id);
0N/A }
0N/A
0N/A int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
0N/A
0N/A // It is possible that a new CPU has been hotplugged and
0N/A // we haven't reshaped the space accordingly.
0N/A if (i == -1) {
0N/A i = os::random() % lgrp_spaces()->length();
0N/A }
0N/A
373N/A LGRPSpace* ls = lgrp_spaces()->at(i);
373N/A MutableSpace *s = ls->space();
0N/A HeapWord *p = s->allocate(size);
0N/A
144N/A if (p != NULL) {
144N/A size_t remainder = s->free_in_words();
1491N/A if (remainder < CollectedHeap::min_fill_size() && remainder > 0) {
144N/A s->set_top(s->top() - size);
144N/A p = NULL;
144N/A }
0N/A }
0N/A if (p != NULL) {
0N/A if (top() < s->top()) { // Keep _top updated.
0N/A MutableSpace::set_top(s->top());
0N/A }
0N/A }
141N/A // Make the page allocation happen here if there is no static binding..
141N/A if (p != NULL && !os::numa_has_static_binding()) {
0N/A for (HeapWord *i = p; i < p + size; i += os::vm_page_size() >> LogHeapWordSize) {
0N/A *(int*)i = 0;
0N/A }
0N/A }
373N/A if (p == NULL) {
373N/A ls->set_allocation_failed();
373N/A }
0N/A return p;
0N/A}
0N/A
0N/A// This version is lock-free.
0N/AHeapWord* MutableNUMASpace::cas_allocate(size_t size) {
141N/A Thread* thr = Thread::current();
141N/A int lgrp_id = thr->lgrp_id();
141N/A if (lgrp_id == -1 || !os::numa_has_group_homing()) {
0N/A lgrp_id = os::numa_get_group_id();
141N/A thr->set_lgrp_id(lgrp_id);
0N/A }
0N/A
0N/A int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
0N/A // It is possible that a new CPU has been hotplugged and
0N/A // we haven't reshaped the space accordingly.
0N/A if (i == -1) {
0N/A i = os::random() % lgrp_spaces()->length();
0N/A }
373N/A LGRPSpace *ls = lgrp_spaces()->at(i);
373N/A MutableSpace *s = ls->space();
0N/A HeapWord *p = s->cas_allocate(size);
144N/A if (p != NULL) {
190N/A size_t remainder = pointer_delta(s->end(), p + size);
1491N/A if (remainder < CollectedHeap::min_fill_size() && remainder > 0) {
144N/A if (s->cas_deallocate(p, size)) {
144N/A // We were the last to allocate and created a fragment less than
144N/A // a minimal object.
144N/A p = NULL;
190N/A } else {
190N/A guarantee(false, "Deallocation should always succeed");
144N/A }
0N/A }
0N/A }
0N/A if (p != NULL) {
0N/A HeapWord* cur_top, *cur_chunk_top = p + size;
0N/A while ((cur_top = top()) < cur_chunk_top) { // Keep _top updated.
0N/A if (Atomic::cmpxchg_ptr(cur_chunk_top, top_addr(), cur_top) == cur_top) {
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
141N/A // Make the page allocation happen here if there is no static binding.
141N/A if (p != NULL && !os::numa_has_static_binding() ) {
0N/A for (HeapWord *i = p; i < p + size; i += os::vm_page_size() >> LogHeapWordSize) {
0N/A *(int*)i = 0;
0N/A }
0N/A }
373N/A if (p == NULL) {
373N/A ls->set_allocation_failed();
373N/A }
0N/A return p;
0N/A}
0N/A
0N/Avoid MutableNUMASpace::print_short_on(outputStream* st) const {
0N/A MutableSpace::print_short_on(st);
0N/A st->print(" (");
0N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
0N/A st->print("lgrp %d: ", lgrp_spaces()->at(i)->lgrp_id());
0N/A lgrp_spaces()->at(i)->space()->print_short_on(st);
0N/A if (i < lgrp_spaces()->length() - 1) {
0N/A st->print(", ");
0N/A }
0N/A }
0N/A st->print(")");
0N/A}
0N/A
0N/Avoid MutableNUMASpace::print_on(outputStream* st) const {
0N/A MutableSpace::print_on(st);
0N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
0N/A LGRPSpace *ls = lgrp_spaces()->at(i);
0N/A st->print(" lgrp %d", ls->lgrp_id());
0N/A ls->space()->print_on(st);
0N/A if (NUMAStats) {
144N/A for (int i = 0; i < lgrp_spaces()->length(); i++) {
144N/A lgrp_spaces()->at(i)->accumulate_statistics(page_size());
144N/A }
0N/A st->print(" local/remote/unbiased/uncommitted: %dK/%dK/%dK/%dK, large/small pages: %d/%d\n",
0N/A ls->space_stats()->_local_space / K,
0N/A ls->space_stats()->_remote_space / K,
0N/A ls->space_stats()->_unbiased_space / K,
0N/A ls->space_stats()->_uncommited_space / K,
0N/A ls->space_stats()->_large_pages,
0N/A ls->space_stats()->_small_pages);
0N/A }
0N/A }
0N/A}
0N/A
3679N/Avoid MutableNUMASpace::verify() {
190N/A // This can be called after setting an arbitary value to the space's top,
190N/A // so an object can cross the chunk boundary. We ensure the parsablity
190N/A // of the space and just walk the objects in linear fashion.
190N/A ensure_parsability();
3679N/A MutableSpace::verify();
0N/A}
0N/A
0N/A// Scan pages and gather stats about page placement and size.
0N/Avoid MutableNUMASpace::LGRPSpace::accumulate_statistics(size_t page_size) {
0N/A clear_space_stats();
0N/A char *start = (char*)round_to((intptr_t) space()->bottom(), page_size);
0N/A char* end = (char*)round_down((intptr_t) space()->end(), page_size);
0N/A if (start < end) {
0N/A for (char *p = start; p < end;) {
0N/A os::page_info info;
0N/A if (os::get_page_info(p, &info)) {
0N/A if (info.size > 0) {
0N/A if (info.size > (size_t)os::vm_page_size()) {
0N/A space_stats()->_large_pages++;
0N/A } else {
0N/A space_stats()->_small_pages++;
0N/A }
0N/A if (info.lgrp_id == lgrp_id()) {
0N/A space_stats()->_local_space += info.size;
0N/A } else {
0N/A space_stats()->_remote_space += info.size;
0N/A }
0N/A p += info.size;
0N/A } else {
0N/A p += os::vm_page_size();
0N/A space_stats()->_uncommited_space += os::vm_page_size();
0N/A }
0N/A } else {
0N/A return;
0N/A }
0N/A }
0N/A }
0N/A space_stats()->_unbiased_space = pointer_delta(start, space()->bottom(), sizeof(char)) +
0N/A pointer_delta(space()->end(), end, sizeof(char));
0N/A
0N/A}
0N/A
0N/A// Scan page_count pages and verify if they have the right size and right placement.
0N/A// If invalid pages are found they are freed in hope that subsequent reallocation
0N/A// will be more successful.
0N/Avoid MutableNUMASpace::LGRPSpace::scan_pages(size_t page_size, size_t page_count)
0N/A{
0N/A char* range_start = (char*)round_to((intptr_t) space()->bottom(), page_size);
0N/A char* range_end = (char*)round_down((intptr_t) space()->end(), page_size);
0N/A
0N/A if (range_start > last_page_scanned() || last_page_scanned() >= range_end) {
0N/A set_last_page_scanned(range_start);
0N/A }
0N/A
0N/A char *scan_start = last_page_scanned();
0N/A char* scan_end = MIN2(scan_start + page_size * page_count, range_end);
0N/A
0N/A os::page_info page_expected, page_found;
0N/A page_expected.size = page_size;
0N/A page_expected.lgrp_id = lgrp_id();
0N/A
0N/A char *s = scan_start;
0N/A while (s < scan_end) {
0N/A char *e = os::scan_pages(s, (char*)scan_end, &page_expected, &page_found);
0N/A if (e == NULL) {
0N/A break;
0N/A }
0N/A if (e != scan_end) {
0N/A if ((page_expected.size != page_size || page_expected.lgrp_id != lgrp_id())
0N/A && page_expected.size != 0) {
3014N/A os::free_memory(s, pointer_delta(e, s, sizeof(char)), page_size);
0N/A }
0N/A page_expected = page_found;
0N/A }
0N/A s = e;
0N/A }
0N/A
0N/A set_last_page_scanned(scan_end);
0N/A}