Path: blob/master/src/hotspot/share/gc/parallel/mutableSpace.cpp
41149 views
/*1* Copyright (c) 2001, 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/mutableSpace.hpp"26#include "gc/shared/pretouchTask.hpp"27#include "gc/shared/spaceDecorator.inline.hpp"28#include "memory/iterator.inline.hpp"29#include "memory/universe.hpp"30#include "oops/oop.inline.hpp"31#include "runtime/atomic.hpp"32#include "runtime/safepoint.hpp"33#include "runtime/thread.hpp"34#include "utilities/align.hpp"35#include "utilities/macros.hpp"3637MutableSpace::MutableSpace(size_t alignment) :38_mangler(NULL),39_last_setup_region(),40_alignment(alignment),41_bottom(NULL),42_top(NULL),43_end(NULL)44{45assert(MutableSpace::alignment() % os::vm_page_size() == 0,46"Space should be aligned");47_mangler = new MutableSpaceMangler(this);48}4950MutableSpace::~MutableSpace() {51delete _mangler;52}5354void MutableSpace::numa_setup_pages(MemRegion mr, bool clear_space) {55if (!mr.is_empty()) {56size_t page_size = UseLargePages ? alignment() : os::vm_page_size();57HeapWord *start = align_up(mr.start(), page_size);58HeapWord *end = align_down(mr.end(), page_size);59if (end > start) {60size_t size = pointer_delta(end, start, sizeof(char));61if (clear_space) {62// Prefer page reallocation to migration.63os::free_memory((char*)start, size, page_size);64}65os::numa_make_global((char*)start, size);66}67}68}6970void MutableSpace::initialize(MemRegion mr,71bool clear_space,72bool mangle_space,73bool setup_pages,74WorkGang* pretouch_gang) {7576assert(Universe::on_page_boundary(mr.start()) && Universe::on_page_boundary(mr.end()),77"invalid space boundaries");7879if (setup_pages && (UseNUMA || AlwaysPreTouch)) {80// The space may move left and right or expand/shrink.81// We'd like to enforce the desired page placement.82MemRegion head, tail;83if (last_setup_region().is_empty()) {84// If it's the first initialization don't limit the amount of work.85head = mr;86tail = MemRegion(mr.end(), mr.end());87} else {88// Is there an intersection with the address space?89MemRegion intersection = last_setup_region().intersection(mr);90if (intersection.is_empty()) {91intersection = MemRegion(mr.end(), mr.end());92}93// All the sizes below are in words.94size_t head_size = 0, tail_size = 0;95if (mr.start() <= intersection.start()) {96head_size = pointer_delta(intersection.start(), mr.start());97}98if(intersection.end() <= mr.end()) {99tail_size = pointer_delta(mr.end(), intersection.end());100}101// Limit the amount of page manipulation if necessary.102if (NUMASpaceResizeRate > 0 && !AlwaysPreTouch) {103const size_t change_size = head_size + tail_size;104const float setup_rate_words = NUMASpaceResizeRate >> LogBytesPerWord;105head_size = MIN2((size_t)(setup_rate_words * head_size / change_size),106head_size);107tail_size = MIN2((size_t)(setup_rate_words * tail_size / change_size),108tail_size);109}110head = MemRegion(intersection.start() - head_size, intersection.start());111tail = MemRegion(intersection.end(), intersection.end() + tail_size);112}113assert(mr.contains(head) && mr.contains(tail), "Sanity");114115if (UseNUMA) {116numa_setup_pages(head, clear_space);117numa_setup_pages(tail, clear_space);118}119120if (AlwaysPreTouch) {121size_t page_size = UseLargePages ? os::large_page_size() : os::vm_page_size();122123PretouchTask::pretouch("ParallelGC PreTouch head", (char*)head.start(), (char*)head.end(),124page_size, pretouch_gang);125126PretouchTask::pretouch("ParallelGC PreTouch tail", (char*)tail.start(), (char*)tail.end(),127page_size, pretouch_gang);128}129130// Remember where we stopped so that we can continue later.131set_last_setup_region(MemRegion(head.start(), tail.end()));132}133134set_bottom(mr.start());135// When expanding concurrently with callers of cas_allocate, setting end136// makes the new space available for allocation by other threads. So this137// assignment must follow all other configuration and initialization that138// might be done for expansion.139Atomic::release_store(end_addr(), mr.end());140141if (clear_space) {142clear(mangle_space);143}144}145146void MutableSpace::clear(bool mangle_space) {147set_top(bottom());148if (ZapUnusedHeapArea && mangle_space) {149mangle_unused_area();150}151}152153#ifndef PRODUCT154void MutableSpace::check_mangled_unused_area(HeapWord* limit) {155mangler()->check_mangled_unused_area(limit);156}157158void MutableSpace::check_mangled_unused_area_complete() {159mangler()->check_mangled_unused_area_complete();160}161162// Mangle only the unused space that has not previously163// been mangled and that has not been allocated since being164// mangled.165void MutableSpace::mangle_unused_area() {166mangler()->mangle_unused_area();167}168169void MutableSpace::mangle_unused_area_complete() {170mangler()->mangle_unused_area_complete();171}172173void MutableSpace::mangle_region(MemRegion mr) {174SpaceMangler::mangle_region(mr);175}176177void MutableSpace::set_top_for_allocations(HeapWord* v) {178mangler()->set_top_for_allocations(v);179}180181void MutableSpace::set_top_for_allocations() {182mangler()->set_top_for_allocations(top());183}184#endif185186HeapWord* MutableSpace::cas_allocate(size_t size) {187do {188// Read top before end, else the range check may pass when it shouldn't.189// If end is read first, other threads may advance end and top such that190// current top > old end and current top + size > current end. Then191// pointer_delta underflows, allowing installation of top > current end.192HeapWord* obj = Atomic::load_acquire(top_addr());193if (pointer_delta(end(), obj) >= size) {194HeapWord* new_top = obj + size;195HeapWord* result = Atomic::cmpxchg(top_addr(), obj, new_top);196// result can be one of two:197// the old top value: the exchange succeeded198// otherwise: the new value of the top is returned.199if (result != obj) {200continue; // another thread beat us to the allocation, try again201}202assert(is_object_aligned(obj) && is_object_aligned(new_top),203"checking alignment");204return obj;205} else {206return NULL;207}208} while (true);209}210211// Try to deallocate previous allocation. Returns true upon success.212bool MutableSpace::cas_deallocate(HeapWord *obj, size_t size) {213HeapWord* expected_top = obj + size;214return Atomic::cmpxchg(top_addr(), expected_top, obj) == expected_top;215}216217// Only used by oldgen allocation.218bool MutableSpace::needs_expand(size_t word_size) const {219assert_lock_strong(ExpandHeap_lock);220// Holding the lock means end is stable. So while top may be advancing221// via concurrent allocations, there is no need to order the reads of top222// and end here, unlike in cas_allocate.223return pointer_delta(end(), top()) < word_size;224}225226void MutableSpace::oop_iterate(OopIterateClosure* cl) {227HeapWord* obj_addr = bottom();228HeapWord* t = top();229// Could call objects iterate, but this is easier.230while (obj_addr < t) {231obj_addr += cast_to_oop(obj_addr)->oop_iterate_size(cl);232}233}234235void MutableSpace::object_iterate(ObjectClosure* cl) {236HeapWord* p = bottom();237while (p < top()) {238cl->do_object(cast_to_oop(p));239p += cast_to_oop(p)->size();240}241}242243void MutableSpace::print_short() const { print_short_on(tty); }244void MutableSpace::print_short_on( outputStream* st) const {245st->print(" space " SIZE_FORMAT "K, %d%% used", capacity_in_bytes() / K,246(int) ((double) used_in_bytes() * 100 / capacity_in_bytes()));247}248249void MutableSpace::print() const { print_on(tty); }250void MutableSpace::print_on(outputStream* st) const {251MutableSpace::print_short_on(st);252st->print_cr(" [" INTPTR_FORMAT "," INTPTR_FORMAT "," INTPTR_FORMAT ")",253p2i(bottom()), p2i(top()), p2i(end()));254}255256void MutableSpace::verify() {257HeapWord* p = bottom();258HeapWord* t = top();259HeapWord* prev_p = NULL;260while (p < t) {261oopDesc::verify(cast_to_oop(p));262prev_p = p;263p += cast_to_oop(p)->size();264}265guarantee(p == top(), "end of last object must match end of space");266}267268269