Path: blob/master/src/hotspot/share/gc/parallel/mutableNUMASpace.cpp
41149 views
/*1* Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "gc/parallel/mutableNUMASpace.hpp"26#include "gc/shared/collectedHeap.hpp"27#include "gc/shared/gc_globals.hpp"28#include "gc/shared/spaceDecorator.hpp"29#include "gc/shared/workgroup.hpp"30#include "memory/allocation.inline.hpp"31#include "oops/oop.inline.hpp"32#include "oops/typeArrayOop.hpp"33#include "runtime/atomic.hpp"34#include "runtime/java.hpp"35#include "runtime/os.inline.hpp"36#include "runtime/thread.inline.hpp"37#include "runtime/threadSMR.hpp"38#include "utilities/align.hpp"3940MutableNUMASpace::MutableNUMASpace(size_t alignment) : MutableSpace(alignment), _must_use_large_pages(false) {41_lgrp_spaces = new (ResourceObj::C_HEAP, mtGC) GrowableArray<LGRPSpace*>(0, mtGC);42_page_size = os::vm_page_size();43_adaptation_cycles = 0;44_samples_count = 0;4546#ifdef LINUX47// Changing the page size can lead to freeing of memory. When using large pages48// and the memory has been both reserved and committed, Linux does not support49// freeing parts of it.50if (UseLargePages && !os::can_commit_large_page_memory()) {51_must_use_large_pages = true;52}53#endif // LINUX5455update_layout(true);56}5758MutableNUMASpace::~MutableNUMASpace() {59for (int i = 0; i < lgrp_spaces()->length(); i++) {60delete lgrp_spaces()->at(i);61}62delete lgrp_spaces();63}6465#ifndef PRODUCT66void MutableNUMASpace::mangle_unused_area() {67// This method should do nothing.68// It can be called on a numa space during a full compaction.69}70void MutableNUMASpace::mangle_unused_area_complete() {71// This method should do nothing.72// It can be called on a numa space during a full compaction.73}74void MutableNUMASpace::mangle_region(MemRegion mr) {75// This method should do nothing because numa spaces are not mangled.76}77void MutableNUMASpace::set_top_for_allocations(HeapWord* v) {78assert(false, "Do not mangle MutableNUMASpace's");79}80void MutableNUMASpace::set_top_for_allocations() {81// This method should do nothing.82}83void MutableNUMASpace::check_mangled_unused_area(HeapWord* limit) {84// This method should do nothing.85}86void MutableNUMASpace::check_mangled_unused_area_complete() {87// This method should do nothing.88}89#endif // NOT_PRODUCT9091// There may be unallocated holes in the middle chunks92// that should be filled with dead objects to ensure parsability.93void MutableNUMASpace::ensure_parsability() {94for (int i = 0; i < lgrp_spaces()->length(); i++) {95LGRPSpace *ls = lgrp_spaces()->at(i);96MutableSpace *s = ls->space();97if (s->top() < top()) { // For all spaces preceding the one containing top()98if (s->free_in_words() > 0) {99HeapWord* cur_top = s->top();100size_t words_left_to_fill = pointer_delta(s->end(), s->top());;101while (words_left_to_fill > 0) {102size_t words_to_fill = MIN2(words_left_to_fill, CollectedHeap::filler_array_max_size());103assert(words_to_fill >= CollectedHeap::min_fill_size(),104"Remaining size (" SIZE_FORMAT ") is too small to fill (based on " SIZE_FORMAT " and " SIZE_FORMAT ")",105words_to_fill, words_left_to_fill, CollectedHeap::filler_array_max_size());106CollectedHeap::fill_with_object(cur_top, words_to_fill);107if (!os::numa_has_static_binding()) {108size_t touched_words = words_to_fill;109#ifndef ASSERT110if (!ZapUnusedHeapArea) {111touched_words = MIN2((size_t)align_object_size(typeArrayOopDesc::header_size(T_INT)),112touched_words);113}114#endif115MemRegion invalid;116HeapWord *crossing_start = align_up(cur_top, os::vm_page_size());117HeapWord *crossing_end = align_down(cur_top + touched_words, os::vm_page_size());118if (crossing_start != crossing_end) {119// If object header crossed a small page boundary we mark the area120// as invalid rounding it to a page_size().121HeapWord *start = MAX2(align_down(cur_top, page_size()), s->bottom());122HeapWord *end = MIN2(align_up(cur_top + touched_words, page_size()), s->end());123invalid = MemRegion(start, end);124}125126ls->add_invalid_region(invalid);127}128cur_top += words_to_fill;129words_left_to_fill -= words_to_fill;130}131}132} else {133if (!os::numa_has_static_binding()) {134#ifdef ASSERT135MemRegion invalid(s->top(), s->end());136ls->add_invalid_region(invalid);137#else138if (ZapUnusedHeapArea) {139MemRegion invalid(s->top(), s->end());140ls->add_invalid_region(invalid);141} else {142return;143}144#endif145} else {146return;147}148}149}150}151152size_t MutableNUMASpace::used_in_words() const {153size_t s = 0;154for (int i = 0; i < lgrp_spaces()->length(); i++) {155s += lgrp_spaces()->at(i)->space()->used_in_words();156}157return s;158}159160size_t MutableNUMASpace::free_in_words() const {161size_t s = 0;162for (int i = 0; i < lgrp_spaces()->length(); i++) {163s += lgrp_spaces()->at(i)->space()->free_in_words();164}165return s;166}167168169size_t MutableNUMASpace::tlab_capacity(Thread *thr) const {170guarantee(thr != NULL, "No thread");171int lgrp_id = thr->lgrp_id();172if (lgrp_id == -1) {173// This case can occur after the topology of the system has174// changed. Thread can change their location, the new home175// group will be determined during the first allocation176// attempt. For now we can safely assume that all spaces177// have equal size because the whole space will be reinitialized.178if (lgrp_spaces()->length() > 0) {179return capacity_in_bytes() / lgrp_spaces()->length();180} else {181assert(false, "There should be at least one locality group");182return 0;183}184}185// That's the normal case, where we know the locality group of the thread.186int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);187if (i == -1) {188return 0;189}190return lgrp_spaces()->at(i)->space()->capacity_in_bytes();191}192193size_t MutableNUMASpace::tlab_used(Thread *thr) const {194// Please see the comments for tlab_capacity().195guarantee(thr != NULL, "No thread");196int lgrp_id = thr->lgrp_id();197if (lgrp_id == -1) {198if (lgrp_spaces()->length() > 0) {199return (used_in_bytes()) / lgrp_spaces()->length();200} else {201assert(false, "There should be at least one locality group");202return 0;203}204}205int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);206if (i == -1) {207return 0;208}209return lgrp_spaces()->at(i)->space()->used_in_bytes();210}211212213size_t MutableNUMASpace::unsafe_max_tlab_alloc(Thread *thr) const {214// Please see the comments for tlab_capacity().215guarantee(thr != NULL, "No thread");216int lgrp_id = thr->lgrp_id();217if (lgrp_id == -1) {218if (lgrp_spaces()->length() > 0) {219return free_in_bytes() / lgrp_spaces()->length();220} else {221assert(false, "There should be at least one locality group");222return 0;223}224}225int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);226if (i == -1) {227return 0;228}229return lgrp_spaces()->at(i)->space()->free_in_bytes();230}231232233size_t MutableNUMASpace::capacity_in_words(Thread* thr) const {234guarantee(thr != NULL, "No thread");235int lgrp_id = thr->lgrp_id();236if (lgrp_id == -1) {237if (lgrp_spaces()->length() > 0) {238return capacity_in_words() / lgrp_spaces()->length();239} else {240assert(false, "There should be at least one locality group");241return 0;242}243}244int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);245if (i == -1) {246return 0;247}248return lgrp_spaces()->at(i)->space()->capacity_in_words();249}250251// Check if the NUMA topology has changed. Add and remove spaces if needed.252// The update can be forced by setting the force parameter equal to true.253bool MutableNUMASpace::update_layout(bool force) {254// Check if the topology had changed.255bool changed = os::numa_topology_changed();256if (force || changed) {257// Compute lgrp intersection. Add/remove spaces.258int lgrp_limit = (int)os::numa_get_groups_num();259int *lgrp_ids = NEW_C_HEAP_ARRAY(int, lgrp_limit, mtGC);260int lgrp_num = (int)os::numa_get_leaf_groups(lgrp_ids, lgrp_limit);261assert(lgrp_num > 0, "There should be at least one locality group");262// Add new spaces for the new nodes263for (int i = 0; i < lgrp_num; i++) {264bool found = false;265for (int j = 0; j < lgrp_spaces()->length(); j++) {266if (lgrp_spaces()->at(j)->lgrp_id() == lgrp_ids[i]) {267found = true;268break;269}270}271if (!found) {272lgrp_spaces()->append(new LGRPSpace(lgrp_ids[i], alignment()));273}274}275276// Remove spaces for the removed nodes.277for (int i = 0; i < lgrp_spaces()->length();) {278bool found = false;279for (int j = 0; j < lgrp_num; j++) {280if (lgrp_spaces()->at(i)->lgrp_id() == lgrp_ids[j]) {281found = true;282break;283}284}285if (!found) {286delete lgrp_spaces()->at(i);287lgrp_spaces()->remove_at(i);288} else {289i++;290}291}292293FREE_C_HEAP_ARRAY(int, lgrp_ids);294295if (changed) {296for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {297thread->set_lgrp_id(-1);298}299}300return true;301}302return false;303}304305// Bias region towards the first-touching lgrp. Set the right page sizes.306void MutableNUMASpace::bias_region(MemRegion mr, int lgrp_id) {307HeapWord *start = align_up(mr.start(), page_size());308HeapWord *end = align_down(mr.end(), page_size());309if (end > start) {310MemRegion aligned_region(start, end);311assert((intptr_t)aligned_region.start() % page_size() == 0 &&312(intptr_t)aligned_region.byte_size() % page_size() == 0, "Bad alignment");313assert(region().contains(aligned_region), "Sanity");314// First we tell the OS which page size we want in the given range. The underlying315// large page can be broken down if we require small pages.316os::realign_memory((char*)aligned_region.start(), aligned_region.byte_size(), page_size());317// Then we uncommit the pages in the range.318os::free_memory((char*)aligned_region.start(), aligned_region.byte_size(), page_size());319// And make them local/first-touch biased.320os::numa_make_local((char*)aligned_region.start(), aligned_region.byte_size(), lgrp_id);321}322}323324// Free all pages in the region.325void MutableNUMASpace::free_region(MemRegion mr) {326HeapWord *start = align_up(mr.start(), page_size());327HeapWord *end = align_down(mr.end(), page_size());328if (end > start) {329MemRegion aligned_region(start, end);330assert((intptr_t)aligned_region.start() % page_size() == 0 &&331(intptr_t)aligned_region.byte_size() % page_size() == 0, "Bad alignment");332assert(region().contains(aligned_region), "Sanity");333os::free_memory((char*)aligned_region.start(), aligned_region.byte_size(), page_size());334}335}336337// Update space layout. Perform adaptation.338void MutableNUMASpace::update() {339if (update_layout(false)) {340// If the topology has changed, make all chunks zero-sized.341// And clear the alloc-rate statistics.342// In future we may want to handle this more gracefully in order343// to avoid the reallocation of the pages as much as possible.344for (int i = 0; i < lgrp_spaces()->length(); i++) {345LGRPSpace *ls = lgrp_spaces()->at(i);346MutableSpace *s = ls->space();347s->set_end(s->bottom());348s->set_top(s->bottom());349ls->clear_alloc_rate();350}351// A NUMA space is never mangled352initialize(region(),353SpaceDecorator::Clear,354SpaceDecorator::DontMangle);355} else {356bool should_initialize = false;357if (!os::numa_has_static_binding()) {358for (int i = 0; i < lgrp_spaces()->length(); i++) {359if (!lgrp_spaces()->at(i)->invalid_region().is_empty()) {360should_initialize = true;361break;362}363}364}365366if (should_initialize ||367(UseAdaptiveNUMAChunkSizing && adaptation_cycles() < samples_count())) {368// A NUMA space is never mangled369initialize(region(),370SpaceDecorator::Clear,371SpaceDecorator::DontMangle);372}373}374375if (NUMAStats) {376for (int i = 0; i < lgrp_spaces()->length(); i++) {377lgrp_spaces()->at(i)->accumulate_statistics(page_size());378}379}380381scan_pages(NUMAPageScanRate);382}383384// Scan pages. Free pages that have smaller size or wrong placement.385void MutableNUMASpace::scan_pages(size_t page_count)386{387size_t pages_per_chunk = page_count / lgrp_spaces()->length();388if (pages_per_chunk > 0) {389for (int i = 0; i < lgrp_spaces()->length(); i++) {390LGRPSpace *ls = lgrp_spaces()->at(i);391ls->scan_pages(page_size(), pages_per_chunk);392}393}394}395396// Accumulate statistics about the allocation rate of each lgrp.397void MutableNUMASpace::accumulate_statistics() {398if (UseAdaptiveNUMAChunkSizing) {399for (int i = 0; i < lgrp_spaces()->length(); i++) {400lgrp_spaces()->at(i)->sample();401}402increment_samples_count();403}404405if (NUMAStats) {406for (int i = 0; i < lgrp_spaces()->length(); i++) {407lgrp_spaces()->at(i)->accumulate_statistics(page_size());408}409}410}411412// Get the current size of a chunk.413// This function computes the size of the chunk based on the414// difference between chunk ends. This allows it to work correctly in415// case the whole space is resized and during the process of adaptive416// chunk resizing.417size_t MutableNUMASpace::current_chunk_size(int i) {418HeapWord *cur_end, *prev_end;419if (i == 0) {420prev_end = bottom();421} else {422prev_end = lgrp_spaces()->at(i - 1)->space()->end();423}424if (i == lgrp_spaces()->length() - 1) {425cur_end = end();426} else {427cur_end = lgrp_spaces()->at(i)->space()->end();428}429if (cur_end > prev_end) {430return pointer_delta(cur_end, prev_end, sizeof(char));431}432return 0;433}434435// Return the default chunk size by equally diving the space.436// page_size() aligned.437size_t MutableNUMASpace::default_chunk_size() {438return base_space_size() / lgrp_spaces()->length() * page_size();439}440441// Produce a new chunk size. page_size() aligned.442// This function is expected to be called on sequence of i's from 0 to443// lgrp_spaces()->length().444size_t MutableNUMASpace::adaptive_chunk_size(int i, size_t limit) {445size_t pages_available = base_space_size();446for (int j = 0; j < i; j++) {447pages_available -= align_down(current_chunk_size(j), page_size()) / page_size();448}449pages_available -= lgrp_spaces()->length() - i - 1;450assert(pages_available > 0, "No pages left");451float alloc_rate = 0;452for (int j = i; j < lgrp_spaces()->length(); j++) {453alloc_rate += lgrp_spaces()->at(j)->alloc_rate()->average();454}455size_t chunk_size = 0;456if (alloc_rate > 0) {457LGRPSpace *ls = lgrp_spaces()->at(i);458chunk_size = (size_t)(ls->alloc_rate()->average() / alloc_rate * pages_available) * page_size();459}460chunk_size = MAX2(chunk_size, page_size());461462if (limit > 0) {463limit = align_down(limit, page_size());464if (chunk_size > current_chunk_size(i)) {465size_t upper_bound = pages_available * page_size();466if (upper_bound > limit &&467current_chunk_size(i) < upper_bound - limit) {468// The resulting upper bound should not exceed the available469// amount of memory (pages_available * page_size()).470upper_bound = current_chunk_size(i) + limit;471}472chunk_size = MIN2(chunk_size, upper_bound);473} else {474size_t lower_bound = page_size();475if (current_chunk_size(i) > limit) { // lower_bound shouldn't underflow.476lower_bound = current_chunk_size(i) - limit;477}478chunk_size = MAX2(chunk_size, lower_bound);479}480}481assert(chunk_size <= pages_available * page_size(), "Chunk size out of range");482return chunk_size;483}484485486// Return the bottom_region and the top_region. Align them to page_size() boundary.487// |------------------new_region---------------------------------|488// |----bottom_region--|---intersection---|------top_region------|489void MutableNUMASpace::select_tails(MemRegion new_region, MemRegion intersection,490MemRegion* bottom_region, MemRegion *top_region) {491// Is there bottom?492if (new_region.start() < intersection.start()) { // Yes493// Try to coalesce small pages into a large one.494if (UseLargePages && page_size() >= alignment()) {495HeapWord* p = align_up(intersection.start(), alignment());496if (new_region.contains(p)497&& pointer_delta(p, new_region.start(), sizeof(char)) >= alignment()) {498if (intersection.contains(p)) {499intersection = MemRegion(p, intersection.end());500} else {501intersection = MemRegion(p, p);502}503}504}505*bottom_region = MemRegion(new_region.start(), intersection.start());506} else {507*bottom_region = MemRegion();508}509510// Is there top?511if (intersection.end() < new_region.end()) { // Yes512// Try to coalesce small pages into a large one.513if (UseLargePages && page_size() >= alignment()) {514HeapWord* p = align_down(intersection.end(), alignment());515if (new_region.contains(p)516&& pointer_delta(new_region.end(), p, sizeof(char)) >= alignment()) {517if (intersection.contains(p)) {518intersection = MemRegion(intersection.start(), p);519} else {520intersection = MemRegion(p, p);521}522}523}524*top_region = MemRegion(intersection.end(), new_region.end());525} else {526*top_region = MemRegion();527}528}529530// Try to merge the invalid region with the bottom or top region by decreasing531// the intersection area. Return the invalid_region aligned to the page_size()532// boundary if it's inside the intersection. Return non-empty invalid_region533// if it lies inside the intersection (also page-aligned).534// |------------------new_region---------------------------------|535// |----------------|-------invalid---|--------------------------|536// |----bottom_region--|---intersection---|------top_region------|537void MutableNUMASpace::merge_regions(MemRegion new_region, MemRegion* intersection,538MemRegion *invalid_region) {539if (intersection->start() >= invalid_region->start() && intersection->contains(invalid_region->end())) {540*intersection = MemRegion(invalid_region->end(), intersection->end());541*invalid_region = MemRegion();542} else543if (intersection->end() <= invalid_region->end() && intersection->contains(invalid_region->start())) {544*intersection = MemRegion(intersection->start(), invalid_region->start());545*invalid_region = MemRegion();546} else547if (intersection->equals(*invalid_region) || invalid_region->contains(*intersection)) {548*intersection = MemRegion(new_region.start(), new_region.start());549*invalid_region = MemRegion();550} else551if (intersection->contains(invalid_region)) {552// That's the only case we have to make an additional bias_region() call.553HeapWord* start = invalid_region->start();554HeapWord* end = invalid_region->end();555if (UseLargePages && page_size() >= alignment()) {556HeapWord *p = align_down(start, alignment());557if (new_region.contains(p)) {558start = p;559}560p = align_up(end, alignment());561if (new_region.contains(end)) {562end = p;563}564}565if (intersection->start() > start) {566*intersection = MemRegion(start, intersection->end());567}568if (intersection->end() < end) {569*intersection = MemRegion(intersection->start(), end);570}571*invalid_region = MemRegion(start, end);572}573}574575void MutableNUMASpace::initialize(MemRegion mr,576bool clear_space,577bool mangle_space,578bool setup_pages,579WorkGang* pretouch_gang) {580assert(clear_space, "Reallocation will destroy data!");581assert(lgrp_spaces()->length() > 0, "There should be at least one space");582583MemRegion old_region = region(), new_region;584set_bottom(mr.start());585set_end(mr.end());586// Must always clear the space587clear(SpaceDecorator::DontMangle);588589// Compute chunk sizes590size_t prev_page_size = page_size();591set_page_size(UseLargePages ? alignment() : os::vm_page_size());592HeapWord* rounded_bottom = align_up(bottom(), page_size());593HeapWord* rounded_end = align_down(end(), page_size());594size_t base_space_size_pages = pointer_delta(rounded_end, rounded_bottom, sizeof(char)) / page_size();595596// Try small pages if the chunk size is too small597if (base_space_size_pages / lgrp_spaces()->length() == 0598&& page_size() > (size_t)os::vm_page_size()) {599// Changing the page size below can lead to freeing of memory. So we fail initialization.600if (_must_use_large_pages) {601vm_exit_during_initialization("Failed initializing NUMA with large pages. Too small heap size");602}603set_page_size(os::vm_page_size());604rounded_bottom = align_up(bottom(), page_size());605rounded_end = align_down(end(), page_size());606base_space_size_pages = pointer_delta(rounded_end, rounded_bottom, sizeof(char)) / page_size();607}608guarantee(base_space_size_pages / lgrp_spaces()->length() > 0, "Space too small");609set_base_space_size(base_space_size_pages);610611// Handle space resize612MemRegion top_region, bottom_region;613if (!old_region.equals(region())) {614new_region = MemRegion(rounded_bottom, rounded_end);615MemRegion intersection = new_region.intersection(old_region);616if (intersection.start() == NULL ||617intersection.end() == NULL ||618prev_page_size > page_size()) { // If the page size got smaller we have to change619// the page size preference for the whole space.620intersection = MemRegion(new_region.start(), new_region.start());621}622select_tails(new_region, intersection, &bottom_region, &top_region);623bias_region(bottom_region, lgrp_spaces()->at(0)->lgrp_id());624bias_region(top_region, lgrp_spaces()->at(lgrp_spaces()->length() - 1)->lgrp_id());625}626627// Check if the space layout has changed significantly?628// This happens when the space has been resized so that either head or tail629// chunk became less than a page.630bool layout_valid = UseAdaptiveNUMAChunkSizing &&631current_chunk_size(0) > page_size() &&632current_chunk_size(lgrp_spaces()->length() - 1) > page_size();633634635for (int i = 0; i < lgrp_spaces()->length(); i++) {636LGRPSpace *ls = lgrp_spaces()->at(i);637MutableSpace *s = ls->space();638old_region = s->region();639640size_t chunk_byte_size = 0, old_chunk_byte_size = 0;641if (i < lgrp_spaces()->length() - 1) {642if (!UseAdaptiveNUMAChunkSizing ||643(UseAdaptiveNUMAChunkSizing && NUMAChunkResizeWeight == 0) ||644samples_count() < AdaptiveSizePolicyReadyThreshold) {645// No adaptation. Divide the space equally.646chunk_byte_size = default_chunk_size();647} else648if (!layout_valid || NUMASpaceResizeRate == 0) {649// Fast adaptation. If no space resize rate is set, resize650// the chunks instantly.651chunk_byte_size = adaptive_chunk_size(i, 0);652} else {653// Slow adaptation. Resize the chunks moving no more than654// NUMASpaceResizeRate bytes per collection.655size_t limit = NUMASpaceResizeRate /656(lgrp_spaces()->length() * (lgrp_spaces()->length() + 1) / 2);657chunk_byte_size = adaptive_chunk_size(i, MAX2(limit * (i + 1), page_size()));658}659660assert(chunk_byte_size >= page_size(), "Chunk size too small");661assert(chunk_byte_size <= capacity_in_bytes(), "Sanity check");662}663664if (i == 0) { // Bottom chunk665if (i != lgrp_spaces()->length() - 1) {666new_region = MemRegion(bottom(), rounded_bottom + (chunk_byte_size >> LogHeapWordSize));667} else {668new_region = MemRegion(bottom(), end());669}670} else671if (i < lgrp_spaces()->length() - 1) { // Middle chunks672MutableSpace *ps = lgrp_spaces()->at(i - 1)->space();673new_region = MemRegion(ps->end(),674ps->end() + (chunk_byte_size >> LogHeapWordSize));675} else { // Top chunk676MutableSpace *ps = lgrp_spaces()->at(i - 1)->space();677new_region = MemRegion(ps->end(), end());678}679guarantee(region().contains(new_region), "Region invariant");680681682// The general case:683// |---------------------|--invalid---|--------------------------|684// |------------------new_region---------------------------------|685// |----bottom_region--|---intersection---|------top_region------|686// |----old_region----|687// The intersection part has all pages in place we don't need to migrate them.688// Pages for the top and bottom part should be freed and then reallocated.689690MemRegion intersection = old_region.intersection(new_region);691692if (intersection.start() == NULL || intersection.end() == NULL) {693intersection = MemRegion(new_region.start(), new_region.start());694}695696if (!os::numa_has_static_binding()) {697MemRegion invalid_region = ls->invalid_region().intersection(new_region);698// Invalid region is a range of memory that could've possibly699// been allocated on the other node. That's relevant only on Solaris where700// there is no static memory binding.701if (!invalid_region.is_empty()) {702merge_regions(new_region, &intersection, &invalid_region);703free_region(invalid_region);704ls->set_invalid_region(MemRegion());705}706}707708select_tails(new_region, intersection, &bottom_region, &top_region);709710if (!os::numa_has_static_binding()) {711// If that's a system with the first-touch policy then it's enough712// to free the pages.713free_region(bottom_region);714free_region(top_region);715} else {716// In a system with static binding we have to change the bias whenever717// we reshape the heap.718bias_region(bottom_region, ls->lgrp_id());719bias_region(top_region, ls->lgrp_id());720}721722// Clear space (set top = bottom) but never mangle.723s->initialize(new_region, SpaceDecorator::Clear, SpaceDecorator::DontMangle, MutableSpace::DontSetupPages);724725set_adaptation_cycles(samples_count());726}727}728729// Set the top of the whole space.730// Mark the the holes in chunks below the top() as invalid.731void MutableNUMASpace::set_top(HeapWord* value) {732bool found_top = false;733for (int i = 0; i < lgrp_spaces()->length();) {734LGRPSpace *ls = lgrp_spaces()->at(i);735MutableSpace *s = ls->space();736HeapWord *top = MAX2(align_down(s->top(), page_size()), s->bottom());737738if (s->contains(value)) {739// Check if setting the chunk's top to a given value would create a hole less than740// a minimal object; assuming that's not the last chunk in which case we don't care.741if (i < lgrp_spaces()->length() - 1) {742size_t remainder = pointer_delta(s->end(), value);743const size_t min_fill_size = CollectedHeap::min_fill_size();744if (remainder < min_fill_size && remainder > 0) {745// Add a minimum size filler object; it will cross the chunk boundary.746CollectedHeap::fill_with_object(value, min_fill_size);747value += min_fill_size;748assert(!s->contains(value), "Should be in the next chunk");749// Restart the loop from the same chunk, since the value has moved750// to the next one.751continue;752}753}754755if (!os::numa_has_static_binding() && top < value && top < s->end()) {756ls->add_invalid_region(MemRegion(top, value));757}758s->set_top(value);759found_top = true;760} else {761if (found_top) {762s->set_top(s->bottom());763} else {764if (!os::numa_has_static_binding() && top < s->end()) {765ls->add_invalid_region(MemRegion(top, s->end()));766}767s->set_top(s->end());768}769}770i++;771}772MutableSpace::set_top(value);773}774775void MutableNUMASpace::clear(bool mangle_space) {776MutableSpace::set_top(bottom());777for (int i = 0; i < lgrp_spaces()->length(); i++) {778// Never mangle NUMA spaces because the mangling will779// bind the memory to a possibly unwanted lgroup.780lgrp_spaces()->at(i)->space()->clear(SpaceDecorator::DontMangle);781}782}783784/*785Linux supports static memory binding, therefore the most part of the786logic dealing with the possible invalid page allocation is effectively787disabled. Besides there is no notion of the home node in Linux. A788thread is allowed to migrate freely. Although the scheduler is rather789reluctant to move threads between the nodes. We check for the current790node every allocation. And with a high probability a thread stays on791the same node for some time allowing local access to recently allocated792objects.793*/794795HeapWord* MutableNUMASpace::cas_allocate(size_t size) {796Thread* thr = Thread::current();797int lgrp_id = thr->lgrp_id();798if (lgrp_id == -1 || !os::numa_has_group_homing()) {799lgrp_id = os::numa_get_group_id();800thr->set_lgrp_id(lgrp_id);801}802803int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);804// It is possible that a new CPU has been hotplugged and805// we haven't reshaped the space accordingly.806if (i == -1) {807i = os::random() % lgrp_spaces()->length();808}809LGRPSpace *ls = lgrp_spaces()->at(i);810MutableSpace *s = ls->space();811HeapWord *p = s->cas_allocate(size);812if (p != NULL) {813size_t remainder = pointer_delta(s->end(), p + size);814if (remainder < CollectedHeap::min_fill_size() && remainder > 0) {815if (s->cas_deallocate(p, size)) {816// We were the last to allocate and created a fragment less than817// a minimal object.818p = NULL;819} else {820guarantee(false, "Deallocation should always succeed");821}822}823}824if (p != NULL) {825HeapWord* cur_top, *cur_chunk_top = p + size;826while ((cur_top = top()) < cur_chunk_top) { // Keep _top updated.827if (Atomic::cmpxchg(top_addr(), cur_top, cur_chunk_top) == cur_top) {828break;829}830}831}832833// Make the page allocation happen here if there is no static binding.834if (p != NULL && !os::numa_has_static_binding() ) {835for (HeapWord *i = p; i < p + size; i += os::vm_page_size() >> LogHeapWordSize) {836*(int*)i = 0;837}838}839if (p == NULL) {840ls->set_allocation_failed();841}842return p;843}844845void MutableNUMASpace::print_short_on(outputStream* st) const {846MutableSpace::print_short_on(st);847st->print(" (");848for (int i = 0; i < lgrp_spaces()->length(); i++) {849st->print("lgrp %d: ", lgrp_spaces()->at(i)->lgrp_id());850lgrp_spaces()->at(i)->space()->print_short_on(st);851if (i < lgrp_spaces()->length() - 1) {852st->print(", ");853}854}855st->print(")");856}857858void MutableNUMASpace::print_on(outputStream* st) const {859MutableSpace::print_on(st);860for (int i = 0; i < lgrp_spaces()->length(); i++) {861LGRPSpace *ls = lgrp_spaces()->at(i);862st->print(" lgrp %d", ls->lgrp_id());863ls->space()->print_on(st);864if (NUMAStats) {865for (int i = 0; i < lgrp_spaces()->length(); i++) {866lgrp_spaces()->at(i)->accumulate_statistics(page_size());867}868st->print(" local/remote/unbiased/uncommitted: " SIZE_FORMAT "K/"869SIZE_FORMAT "K/" SIZE_FORMAT "K/" SIZE_FORMAT870"K, large/small pages: " SIZE_FORMAT "/" SIZE_FORMAT "\n",871ls->space_stats()->_local_space / K,872ls->space_stats()->_remote_space / K,873ls->space_stats()->_unbiased_space / K,874ls->space_stats()->_uncommited_space / K,875ls->space_stats()->_large_pages,876ls->space_stats()->_small_pages);877}878}879}880881void MutableNUMASpace::verify() {882// This can be called after setting an arbitrary value to the space's top,883// so an object can cross the chunk boundary. We ensure the parsability884// of the space and just walk the objects in linear fashion.885ensure_parsability();886MutableSpace::verify();887}888889// Scan pages and gather stats about page placement and size.890void MutableNUMASpace::LGRPSpace::accumulate_statistics(size_t page_size) {891clear_space_stats();892char *start = (char*)align_up(space()->bottom(), page_size);893char* end = (char*)align_down(space()->end(), page_size);894if (start < end) {895for (char *p = start; p < end;) {896os::page_info info;897if (os::get_page_info(p, &info)) {898if (info.size > 0) {899if (info.size > (size_t)os::vm_page_size()) {900space_stats()->_large_pages++;901} else {902space_stats()->_small_pages++;903}904if (info.lgrp_id == lgrp_id()) {905space_stats()->_local_space += info.size;906} else {907space_stats()->_remote_space += info.size;908}909p += info.size;910} else {911p += os::vm_page_size();912space_stats()->_uncommited_space += os::vm_page_size();913}914} else {915return;916}917}918}919space_stats()->_unbiased_space = pointer_delta(start, space()->bottom(), sizeof(char)) +920pointer_delta(space()->end(), end, sizeof(char));921922}923924// Scan page_count pages and verify if they have the right size and right placement.925// If invalid pages are found they are freed in hope that subsequent reallocation926// will be more successful.927void MutableNUMASpace::LGRPSpace::scan_pages(size_t page_size, size_t page_count)928{929char* range_start = (char*)align_up(space()->bottom(), page_size);930char* range_end = (char*)align_down(space()->end(), page_size);931932if (range_start > last_page_scanned() || last_page_scanned() >= range_end) {933set_last_page_scanned(range_start);934}935936char *scan_start = last_page_scanned();937char* scan_end = MIN2(scan_start + page_size * page_count, range_end);938939os::page_info page_expected, page_found;940page_expected.size = page_size;941page_expected.lgrp_id = lgrp_id();942943char *s = scan_start;944while (s < scan_end) {945char *e = os::scan_pages(s, (char*)scan_end, &page_expected, &page_found);946if (e == NULL) {947break;948}949if (e != scan_end) {950assert(e < scan_end, "e: " PTR_FORMAT " scan_end: " PTR_FORMAT, p2i(e), p2i(scan_end));951952if ((page_expected.size != page_size || page_expected.lgrp_id != lgrp_id())953&& page_expected.size != 0) {954os::free_memory(s, pointer_delta(e, s, sizeof(char)), page_size);955}956page_expected = page_found;957}958s = e;959}960961set_last_page_scanned(scan_end);962}963964965