0N/A/*
3679N/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#ifndef SHARE_VM_GC_IMPLEMENTATION_SHARED_MUTABLENUMASPACE_HPP
1879N/A#define SHARE_VM_GC_IMPLEMENTATION_SHARED_MUTABLENUMASPACE_HPP
1879N/A
1879N/A#ifndef SERIALGC
1879N/A#include "gc_implementation/shared/gcUtil.hpp"
1879N/A#include "gc_implementation/shared/mutableSpace.hpp"
1879N/A#endif
1879N/A
0N/A/*
0N/A * The NUMA-aware allocator (MutableNUMASpace) is basically a modification
0N/A * of MutableSpace which preserves interfaces but implements different
0N/A * functionality. The space is split into chunks for each locality group
0N/A * (resizing for adaptive size policy is also supported). For each thread
0N/A * allocations are performed in the chunk corresponding to the home locality
0N/A * group of the thread. Whenever any chunk fills-in the young generation
0N/A * collection occurs.
0N/A * The chunks can be also be adaptively resized. The idea behind the adaptive
0N/A * sizing is to reduce the loss of the space in the eden due to fragmentation.
0N/A * The main cause of fragmentation is uneven allocation rates of threads.
0N/A * The allocation rate difference between locality groups may be caused either by
0N/A * application specifics or by uneven LWP distribution by the OS. Besides,
0N/A * application can have less threads then the number of locality groups.
0N/A * In order to resize the chunk we measure the allocation rate of the
0N/A * application between collections. After that we reshape the chunks to reflect
0N/A * the allocation rate pattern. The AdaptiveWeightedAverage exponentially
0N/A * decaying average is used to smooth the measurements. The NUMASpaceResizeRate
0N/A * parameter is used to control the adaptation speed by restricting the number of
0N/A * bytes that can be moved during the adaptation phase.
0N/A * Chunks may contain pages from a wrong locality group. The page-scanner has
0N/A * been introduced to address the problem. Remote pages typically appear due to
0N/A * the memory shortage in the target locality group. Besides Solaris would
0N/A * allocate a large page from the remote locality group even if there are small
0N/A * local pages available. The page-scanner scans the pages right after the
0N/A * collection and frees remote pages in hope that subsequent reallocation would
0N/A * be more successful. This approach proved to be useful on systems with high
0N/A * load where multiple processes are competing for the memory.
0N/A */
0N/A
0N/Aclass MutableNUMASpace : public MutableSpace {
0N/A friend class VMStructs;
0N/A
3863N/A class LGRPSpace : public CHeapObj<mtGC> {
0N/A int _lgrp_id;
0N/A MutableSpace* _space;
0N/A MemRegion _invalid_region;
0N/A AdaptiveWeightedAverage *_alloc_rate;
373N/A bool _allocation_failed;
0N/A
0N/A struct SpaceStats {
0N/A size_t _local_space, _remote_space, _unbiased_space, _uncommited_space;
0N/A size_t _large_pages, _small_pages;
0N/A
0N/A SpaceStats() {
0N/A _local_space = 0;
0N/A _remote_space = 0;
0N/A _unbiased_space = 0;
0N/A _uncommited_space = 0;
0N/A _large_pages = 0;
0N/A _small_pages = 0;
0N/A }
0N/A };
0N/A
0N/A SpaceStats _space_stats;
0N/A
0N/A char* _last_page_scanned;
0N/A char* last_page_scanned() { return _last_page_scanned; }
0N/A void set_last_page_scanned(char* p) { _last_page_scanned = p; }
0N/A public:
535N/A LGRPSpace(int l, size_t alignment) : _lgrp_id(l), _last_page_scanned(NULL), _allocation_failed(false) {
535N/A _space = new MutableSpace(alignment);
0N/A _alloc_rate = new AdaptiveWeightedAverage(NUMAChunkResizeWeight);
0N/A }
0N/A ~LGRPSpace() {
0N/A delete _space;
0N/A delete _alloc_rate;
0N/A }
0N/A
0N/A void add_invalid_region(MemRegion r) {
0N/A if (!_invalid_region.is_empty()) {
0N/A _invalid_region.set_start(MIN2(_invalid_region.start(), r.start()));
0N/A _invalid_region.set_end(MAX2(_invalid_region.end(), r.end()));
0N/A } else {
0N/A _invalid_region = r;
0N/A }
0N/A }
0N/A
0N/A static bool equals(void* lgrp_id_value, LGRPSpace* p) {
0N/A return *(int*)lgrp_id_value == p->lgrp_id();
0N/A }
0N/A
373N/A // Report a failed allocation.
373N/A void set_allocation_failed() { _allocation_failed = true; }
373N/A
0N/A void sample() {
373N/A // If there was a failed allocation make allocation rate equal
373N/A // to the size of the whole chunk. This ensures the progress of
373N/A // the adaptation process.
373N/A size_t alloc_rate_sample;
373N/A if (_allocation_failed) {
373N/A alloc_rate_sample = space()->capacity_in_bytes();
373N/A _allocation_failed = false;
373N/A } else {
373N/A alloc_rate_sample = space()->used_in_bytes();
373N/A }
373N/A alloc_rate()->sample(alloc_rate_sample);
0N/A }
0N/A
0N/A MemRegion invalid_region() const { return _invalid_region; }
0N/A void set_invalid_region(MemRegion r) { _invalid_region = r; }
0N/A int lgrp_id() const { return _lgrp_id; }
0N/A MutableSpace* space() const { return _space; }
0N/A AdaptiveWeightedAverage* alloc_rate() const { return _alloc_rate; }
268N/A void clear_alloc_rate() { _alloc_rate->clear(); }
0N/A SpaceStats* space_stats() { return &_space_stats; }
0N/A void clear_space_stats() { _space_stats = SpaceStats(); }
0N/A
0N/A void accumulate_statistics(size_t page_size);
0N/A void scan_pages(size_t page_size, size_t page_count);
0N/A };
0N/A
0N/A GrowableArray<LGRPSpace*>* _lgrp_spaces;
0N/A size_t _page_size;
0N/A unsigned _adaptation_cycles, _samples_count;
0N/A
0N/A void set_page_size(size_t psz) { _page_size = psz; }
0N/A size_t page_size() const { return _page_size; }
0N/A
0N/A unsigned adaptation_cycles() { return _adaptation_cycles; }
0N/A void set_adaptation_cycles(int v) { _adaptation_cycles = v; }
0N/A
0N/A unsigned samples_count() { return _samples_count; }
0N/A void increment_samples_count() { ++_samples_count; }
0N/A
0N/A size_t _base_space_size;
0N/A void set_base_space_size(size_t v) { _base_space_size = v; }
0N/A size_t base_space_size() const { return _base_space_size; }
0N/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/A bool update_layout(bool force);
141N/A // Bias region towards the lgrp.
141N/A void bias_region(MemRegion mr, int lgrp_id);
0N/A // Free pages in a given region.
0N/A void free_region(MemRegion mr);
0N/A // Get current chunk size.
0N/A size_t current_chunk_size(int i);
0N/A // Get default chunk size (equally divide the space).
0N/A size_t default_chunk_size();
0N/A // Adapt the chunk size to follow the allocation rate.
0N/A size_t adaptive_chunk_size(int i, size_t limit);
0N/A // Scan and free invalid pages.
0N/A void scan_pages(size_t page_count);
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/A void select_tails(MemRegion new_region, MemRegion intersection,
0N/A MemRegion* bottom_region, MemRegion *top_region);
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/A void merge_regions(MemRegion new_region, MemRegion* intersection,
0N/A MemRegion *invalid_region);
0N/A
0N/A public:
0N/A GrowableArray<LGRPSpace*>* lgrp_spaces() const { return _lgrp_spaces; }
535N/A MutableNUMASpace(size_t alignment);
0N/A virtual ~MutableNUMASpace();
0N/A // Space initialization.
535N/A virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space, bool setup_pages = SetupPages);
0N/A // Update space layout if necessary. Do all adaptive resizing job.
0N/A virtual void update();
0N/A // Update allocation rate averages.
0N/A virtual void accumulate_statistics();
0N/A
263N/A virtual void clear(bool mangle_space);
263N/A virtual void mangle_unused_area() PRODUCT_RETURN;
263N/A virtual void mangle_unused_area_complete() PRODUCT_RETURN;
263N/A virtual void mangle_region(MemRegion mr) PRODUCT_RETURN;
263N/A virtual void check_mangled_unused_area(HeapWord* limit) PRODUCT_RETURN;
263N/A virtual void check_mangled_unused_area_complete() PRODUCT_RETURN;
263N/A virtual void set_top_for_allocations(HeapWord* v) PRODUCT_RETURN;
263N/A virtual void set_top_for_allocations() PRODUCT_RETURN;
263N/A
0N/A virtual void ensure_parsability();
0N/A virtual size_t used_in_words() const;
0N/A virtual size_t free_in_words() const;
373N/A
373N/A using MutableSpace::capacity_in_words;
373N/A virtual size_t capacity_in_words(Thread* thr) const;
0N/A virtual size_t tlab_capacity(Thread* thr) const;
0N/A virtual size_t unsafe_max_tlab_alloc(Thread* thr) const;
0N/A
0N/A // Allocation (return NULL if full)
0N/A virtual HeapWord* allocate(size_t word_size);
0N/A virtual HeapWord* cas_allocate(size_t word_size);
0N/A
0N/A // Debugging
0N/A virtual void print_on(outputStream* st) const;
0N/A virtual void print_short_on(outputStream* st) const;
3679N/A virtual void verify();
0N/A
0N/A virtual void set_top(HeapWord* value);
0N/A};
1879N/A
1879N/A#endif // SHARE_VM_GC_IMPLEMENTATION_SHARED_MUTABLENUMASPACE_HPP