aoqi@0: /* aoqi@0: * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_GC_IMPLEMENTATION_SHARED_MUTABLENUMASPACE_HPP aoqi@0: #define SHARE_VM_GC_IMPLEMENTATION_SHARED_MUTABLENUMASPACE_HPP aoqi@0: aoqi@0: #include "utilities/macros.hpp" aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: #include "gc_implementation/shared/gcUtil.hpp" aoqi@0: #include "gc_implementation/shared/mutableSpace.hpp" aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: aoqi@0: /* aoqi@0: * The NUMA-aware allocator (MutableNUMASpace) is basically a modification aoqi@0: * of MutableSpace which preserves interfaces but implements different aoqi@0: * functionality. The space is split into chunks for each locality group aoqi@0: * (resizing for adaptive size policy is also supported). For each thread aoqi@0: * allocations are performed in the chunk corresponding to the home locality aoqi@0: * group of the thread. Whenever any chunk fills-in the young generation aoqi@0: * collection occurs. aoqi@0: * The chunks can be also be adaptively resized. The idea behind the adaptive aoqi@0: * sizing is to reduce the loss of the space in the eden due to fragmentation. aoqi@0: * The main cause of fragmentation is uneven allocation rates of threads. aoqi@0: * The allocation rate difference between locality groups may be caused either by aoqi@0: * application specifics or by uneven LWP distribution by the OS. Besides, aoqi@0: * application can have less threads then the number of locality groups. aoqi@0: * In order to resize the chunk we measure the allocation rate of the aoqi@0: * application between collections. After that we reshape the chunks to reflect aoqi@0: * the allocation rate pattern. The AdaptiveWeightedAverage exponentially aoqi@0: * decaying average is used to smooth the measurements. The NUMASpaceResizeRate aoqi@0: * parameter is used to control the adaptation speed by restricting the number of aoqi@0: * bytes that can be moved during the adaptation phase. aoqi@0: * Chunks may contain pages from a wrong locality group. The page-scanner has aoqi@0: * been introduced to address the problem. Remote pages typically appear due to aoqi@0: * the memory shortage in the target locality group. Besides Solaris would aoqi@0: * allocate a large page from the remote locality group even if there are small aoqi@0: * local pages available. The page-scanner scans the pages right after the aoqi@0: * collection and frees remote pages in hope that subsequent reallocation would aoqi@0: * be more successful. This approach proved to be useful on systems with high aoqi@0: * load where multiple processes are competing for the memory. aoqi@0: */ aoqi@0: aoqi@0: class MutableNUMASpace : public MutableSpace { aoqi@0: friend class VMStructs; aoqi@0: aoqi@0: class LGRPSpace : public CHeapObj { aoqi@0: int _lgrp_id; aoqi@0: MutableSpace* _space; aoqi@0: MemRegion _invalid_region; aoqi@0: AdaptiveWeightedAverage *_alloc_rate; aoqi@0: bool _allocation_failed; aoqi@0: aoqi@0: struct SpaceStats { aoqi@0: size_t _local_space, _remote_space, _unbiased_space, _uncommited_space; aoqi@0: size_t _large_pages, _small_pages; aoqi@0: aoqi@0: SpaceStats() { aoqi@0: _local_space = 0; aoqi@0: _remote_space = 0; aoqi@0: _unbiased_space = 0; aoqi@0: _uncommited_space = 0; aoqi@0: _large_pages = 0; aoqi@0: _small_pages = 0; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: SpaceStats _space_stats; aoqi@0: aoqi@0: char* _last_page_scanned; aoqi@0: char* last_page_scanned() { return _last_page_scanned; } aoqi@0: void set_last_page_scanned(char* p) { _last_page_scanned = p; } aoqi@0: public: aoqi@0: LGRPSpace(int l, size_t alignment) : _lgrp_id(l), _last_page_scanned(NULL), _allocation_failed(false) { aoqi@0: _space = new MutableSpace(alignment); aoqi@0: _alloc_rate = new AdaptiveWeightedAverage(NUMAChunkResizeWeight); aoqi@0: } aoqi@0: ~LGRPSpace() { aoqi@0: delete _space; aoqi@0: delete _alloc_rate; aoqi@0: } aoqi@0: aoqi@0: void add_invalid_region(MemRegion r) { aoqi@0: if (!_invalid_region.is_empty()) { aoqi@0: _invalid_region.set_start(MIN2(_invalid_region.start(), r.start())); aoqi@0: _invalid_region.set_end(MAX2(_invalid_region.end(), r.end())); aoqi@0: } else { aoqi@0: _invalid_region = r; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static bool equals(void* lgrp_id_value, LGRPSpace* p) { aoqi@0: return *(int*)lgrp_id_value == p->lgrp_id(); aoqi@0: } aoqi@0: aoqi@0: // Report a failed allocation. aoqi@0: void set_allocation_failed() { _allocation_failed = true; } aoqi@0: aoqi@0: void sample() { aoqi@0: // If there was a failed allocation make allocation rate equal aoqi@0: // to the size of the whole chunk. This ensures the progress of aoqi@0: // the adaptation process. aoqi@0: size_t alloc_rate_sample; aoqi@0: if (_allocation_failed) { aoqi@0: alloc_rate_sample = space()->capacity_in_bytes(); aoqi@0: _allocation_failed = false; aoqi@0: } else { aoqi@0: alloc_rate_sample = space()->used_in_bytes(); aoqi@0: } aoqi@0: alloc_rate()->sample(alloc_rate_sample); aoqi@0: } aoqi@0: aoqi@0: MemRegion invalid_region() const { return _invalid_region; } aoqi@0: void set_invalid_region(MemRegion r) { _invalid_region = r; } aoqi@0: int lgrp_id() const { return _lgrp_id; } aoqi@0: MutableSpace* space() const { return _space; } aoqi@0: AdaptiveWeightedAverage* alloc_rate() const { return _alloc_rate; } aoqi@0: void clear_alloc_rate() { _alloc_rate->clear(); } aoqi@0: SpaceStats* space_stats() { return &_space_stats; } aoqi@0: void clear_space_stats() { _space_stats = SpaceStats(); } aoqi@0: aoqi@0: void accumulate_statistics(size_t page_size); aoqi@0: void scan_pages(size_t page_size, size_t page_count); aoqi@0: }; aoqi@0: aoqi@0: GrowableArray* _lgrp_spaces; aoqi@0: size_t _page_size; aoqi@0: unsigned _adaptation_cycles, _samples_count; aoqi@0: aoqi@0: void set_page_size(size_t psz) { _page_size = psz; } aoqi@0: size_t page_size() const { return _page_size; } aoqi@0: aoqi@0: unsigned adaptation_cycles() { return _adaptation_cycles; } aoqi@0: void set_adaptation_cycles(int v) { _adaptation_cycles = v; } aoqi@0: aoqi@0: unsigned samples_count() { return _samples_count; } aoqi@0: void increment_samples_count() { ++_samples_count; } aoqi@0: aoqi@0: size_t _base_space_size; aoqi@0: void set_base_space_size(size_t v) { _base_space_size = v; } aoqi@0: size_t base_space_size() const { return _base_space_size; } aoqi@0: aoqi@0: // Check if the NUMA topology has changed. Add and remove spaces if needed. aoqi@0: // The update can be forced by setting the force parameter equal to true. aoqi@0: bool update_layout(bool force); aoqi@0: // Bias region towards the lgrp. aoqi@0: void bias_region(MemRegion mr, int lgrp_id); aoqi@0: // Free pages in a given region. aoqi@0: void free_region(MemRegion mr); aoqi@0: // Get current chunk size. aoqi@0: size_t current_chunk_size(int i); aoqi@0: // Get default chunk size (equally divide the space). aoqi@0: size_t default_chunk_size(); aoqi@0: // Adapt the chunk size to follow the allocation rate. aoqi@0: size_t adaptive_chunk_size(int i, size_t limit); aoqi@0: // Scan and free invalid pages. aoqi@0: void scan_pages(size_t page_count); aoqi@0: // Return the bottom_region and the top_region. Align them to page_size() boundary. aoqi@0: // |------------------new_region---------------------------------| aoqi@0: // |----bottom_region--|---intersection---|------top_region------| aoqi@0: void select_tails(MemRegion new_region, MemRegion intersection, aoqi@0: MemRegion* bottom_region, MemRegion *top_region); aoqi@0: // Try to merge the invalid region with the bottom or top region by decreasing aoqi@0: // the intersection area. Return the invalid_region aligned to the page_size() aoqi@0: // boundary if it's inside the intersection. Return non-empty invalid_region aoqi@0: // if it lies inside the intersection (also page-aligned). aoqi@0: // |------------------new_region---------------------------------| aoqi@0: // |----------------|-------invalid---|--------------------------| aoqi@0: // |----bottom_region--|---intersection---|------top_region------| aoqi@0: void merge_regions(MemRegion new_region, MemRegion* intersection, aoqi@0: MemRegion *invalid_region); aoqi@0: aoqi@0: public: aoqi@0: GrowableArray* lgrp_spaces() const { return _lgrp_spaces; } aoqi@0: MutableNUMASpace(size_t alignment); aoqi@0: virtual ~MutableNUMASpace(); aoqi@0: // Space initialization. aoqi@0: virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space, bool setup_pages = SetupPages); aoqi@0: // Update space layout if necessary. Do all adaptive resizing job. aoqi@0: virtual void update(); aoqi@0: // Update allocation rate averages. aoqi@0: virtual void accumulate_statistics(); aoqi@0: aoqi@0: virtual void clear(bool mangle_space); aoqi@0: virtual void mangle_unused_area() PRODUCT_RETURN; aoqi@0: virtual void mangle_unused_area_complete() PRODUCT_RETURN; aoqi@0: virtual void mangle_region(MemRegion mr) PRODUCT_RETURN; aoqi@0: virtual void check_mangled_unused_area(HeapWord* limit) PRODUCT_RETURN; aoqi@0: virtual void check_mangled_unused_area_complete() PRODUCT_RETURN; aoqi@0: virtual void set_top_for_allocations(HeapWord* v) PRODUCT_RETURN; aoqi@0: virtual void set_top_for_allocations() PRODUCT_RETURN; aoqi@0: aoqi@0: virtual void ensure_parsability(); aoqi@0: virtual size_t used_in_words() const; aoqi@0: virtual size_t free_in_words() const; aoqi@0: aoqi@0: using MutableSpace::capacity_in_words; aoqi@0: virtual size_t capacity_in_words(Thread* thr) const; aoqi@0: virtual size_t tlab_capacity(Thread* thr) const; aoqi@0: virtual size_t tlab_used(Thread* thr) const; aoqi@0: virtual size_t unsafe_max_tlab_alloc(Thread* thr) const; aoqi@0: aoqi@0: // Allocation (return NULL if full) aoqi@0: virtual HeapWord* allocate(size_t word_size); aoqi@0: virtual HeapWord* cas_allocate(size_t word_size); aoqi@0: aoqi@0: // Debugging aoqi@0: virtual void print_on(outputStream* st) const; aoqi@0: virtual void print_short_on(outputStream* st) const; aoqi@0: virtual void verify(); aoqi@0: aoqi@0: virtual void set_top(HeapWord* value); aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_GC_IMPLEMENTATION_SHARED_MUTABLENUMASPACE_HPP