ysr@777: /* azakharov@7835: * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. ysr@777: * ysr@777: */ ysr@777: tschatzl@7091: #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONMANAGER_HPP tschatzl@7091: #define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONMANAGER_HPP stefank@2314: tschatzl@5773: #include "gc_implementation/g1/g1BiasedArray.hpp" tschatzl@7051: #include "gc_implementation/g1/g1RegionToSpaceMapper.hpp" tschatzl@7050: #include "gc_implementation/g1/heapRegionSet.hpp" azakharov@7835: #include "services/memoryUsage.hpp" tschatzl@5773: ysr@777: class HeapRegion; ysr@777: class HeapRegionClosure; tonyp@2963: class FreeRegionList; tonyp@2963: tschatzl@5773: class G1HeapRegionTable : public G1BiasedMappedArray { tschatzl@5773: protected: tschatzl@7050: virtual HeapRegion* default_value() const { return NULL; } tschatzl@5773: }; tschatzl@5773: tschatzl@7051: // This class keeps track of the actual heap memory, auxiliary data tschatzl@7051: // and its metadata (i.e., HeapRegion instances) and the list of free regions. tschatzl@7051: // tschatzl@7051: // This allows maximum flexibility for deciding what to commit or uncommit given tschatzl@7051: // a request from outside. tschatzl@7051: // tschatzl@7051: // HeapRegions are kept in the _regions array in address order. A region's tschatzl@7051: // index in the array corresponds to its index in the heap (i.e., 0 is the tschatzl@7051: // region at the bottom of the heap, 1 is the one after it, etc.). Two tschatzl@7051: // regions that are consecutive in the array should also be adjacent in the tschatzl@7051: // address space (i.e., region(i).end() == region(i+1).bottom(). tonyp@2963: // tonyp@2963: // We create a HeapRegion when we commit the region's address space tonyp@2963: // for the first time. When we uncommit the address space of a tonyp@2963: // region we retain the HeapRegion to be able to re-use it in the tonyp@2963: // future (in case we recommit it). tonyp@2963: // tonyp@2963: // We keep track of three lengths: tonyp@2963: // tschatzl@7051: // * _num_committed (returned by length()) is the number of currently tschatzl@7051: // committed regions. These may not be contiguous. tschatzl@7051: // * _allocated_heapregions_length (not exposed outside this class) is the tschatzl@7051: // number of regions+1 for which we have HeapRegions. tschatzl@5773: // * max_length() returns the maximum number of regions the heap can have. tonyp@2963: // ysr@777: tschatzl@7091: class HeapRegionManager: public CHeapObj { tonyp@3168: friend class VMStructs; ysr@777: tschatzl@5773: G1HeapRegionTable _regions; ysr@777: tschatzl@7051: G1RegionToSpaceMapper* _heap_mapper; tschatzl@7051: G1RegionToSpaceMapper* _prev_bitmap_mapper; tschatzl@7051: G1RegionToSpaceMapper* _next_bitmap_mapper; tschatzl@7051: G1RegionToSpaceMapper* _bot_mapper; tschatzl@7051: G1RegionToSpaceMapper* _cardtable_mapper; tschatzl@7051: G1RegionToSpaceMapper* _card_counts_mapper; tonyp@2963: tschatzl@7050: FreeRegionList _free_list; tonyp@2963: tschatzl@7051: // Each bit in this bitmap indicates that the corresponding region is available tschatzl@7051: // for allocation. tschatzl@7051: BitMap _available_map; tschatzl@7051: tschatzl@7050: // The number of regions committed in the heap. tschatzl@7050: uint _num_committed; tonyp@2963: tschatzl@7050: // Internal only. The highest heap region +1 we allocated a HeapRegion instance for. tschatzl@7050: uint _allocated_heapregions_length; tonyp@2963: tschatzl@7050: HeapWord* heap_bottom() const { return _regions.bottom_address_mapped(); } tschatzl@7050: HeapWord* heap_end() const {return _regions.end_address_mapped(); } tonyp@2963: tschatzl@7050: void make_regions_available(uint index, uint num_regions = 1); brutisso@5074: tschatzl@7050: // Pass down commit calls to the VirtualSpace. tschatzl@7050: void commit_regions(uint index, size_t num_regions = 1); tschatzl@7050: void uncommit_regions(uint index, size_t num_regions = 1); ysr@777: tschatzl@7050: // Notify other data structures about change in the heap layout. tschatzl@7050: void update_committed_space(HeapWord* old_end, HeapWord* new_end); tschatzl@7050: // Calculate the starting region for each worker during parallel iteration so tschatzl@7050: // that they do not all start from the same region. tschatzl@7050: uint start_region_for_worker(uint worker_i, uint num_workers, uint num_regions) const; tschatzl@5773: tschatzl@7051: // Find a contiguous set of empty or uncommitted regions of length num and return tschatzl@7091: // the index of the first region or G1_NO_HRM_INDEX if the search was unsuccessful. tschatzl@7051: // If only_empty is true, only empty regions are considered. tschatzl@7051: // Searches from bottom to top of the heap, doing a first-fit. tschatzl@7051: uint find_contiguous(size_t num, bool only_empty); tschatzl@7050: // Finds the next sequence of unavailable regions starting from start_idx. Returns the tschatzl@7050: // length of the sequence found. If this result is zero, no such sequence could be found, tschatzl@7050: // otherwise res_idx indicates the start index of these regions. tschatzl@7050: uint find_unavailable_from_idx(uint start_idx, uint* res_idx) const; tschatzl@7050: // Finds the next sequence of empty regions starting from start_idx, going backwards in tschatzl@7050: // the heap. Returns the length of the sequence found. If this value is zero, no tschatzl@7050: // sequence could be found, otherwise res_idx contains the start index of this range. tschatzl@7050: uint find_empty_from_idx_reverse(uint start_idx, uint* res_idx) const; tschatzl@7051: // Allocate a new HeapRegion for the given index. tschatzl@7091: HeapRegion* new_heap_region(uint hrm_index); tschatzl@7050: #ifdef ASSERT tschatzl@7050: public: tschatzl@7050: bool is_free(HeapRegion* hr) const; tschatzl@7050: #endif tschatzl@7050: // Returns whether the given region is available for allocation. tschatzl@7050: bool is_available(uint region) const; ysr@777: tschatzl@7091: public: tschatzl@7091: // Empty constructor, we'll initialize it with the initialize() method. tschatzl@7091: HeapRegionManager() : _regions(), _heap_mapper(NULL), _num_committed(0), tschatzl@7051: _next_bitmap_mapper(NULL), _prev_bitmap_mapper(NULL), _bot_mapper(NULL), tschatzl@7051: _allocated_heapregions_length(0), _available_map(), tschatzl@7051: _free_list("Free list", new MasterFreeRegionListMtSafeChecker()) tschatzl@7050: { } tschatzl@7050: tschatzl@7051: void initialize(G1RegionToSpaceMapper* heap_storage, tschatzl@7051: G1RegionToSpaceMapper* prev_bitmap, tschatzl@7051: G1RegionToSpaceMapper* next_bitmap, tschatzl@7051: G1RegionToSpaceMapper* bot, tschatzl@7051: G1RegionToSpaceMapper* cardtable, tschatzl@7051: G1RegionToSpaceMapper* card_counts); tschatzl@7050: tschatzl@7050: // Return the "dummy" region used for G1AllocRegion. This is currently a hardwired tschatzl@7050: // new HeapRegion that owns HeapRegion at index 0. Since at the moment we commit tschatzl@7050: // the heap from the lowest address, this region (and its associated data tschatzl@7050: // structures) are available and we do not need to check further. tschatzl@7050: HeapRegion* get_dummy_region() { return new_heap_region(0); } ysr@777: tonyp@2963: // Return the HeapRegion at the given index. Assume that the index tonyp@2963: // is valid. tonyp@3713: inline HeapRegion* at(uint index) const; ysr@777: tonyp@2963: // If addr is within the committed space return its corresponding tonyp@2963: // HeapRegion, otherwise return NULL. tonyp@2963: inline HeapRegion* addr_to_region(HeapWord* addr) const; ysr@777: tschatzl@7050: // Insert the given region into the free region list. tschatzl@7050: inline void insert_into_free_list(HeapRegion* hr); tschatzl@7050: tschatzl@7050: // Insert the given region list into the global free region list. tschatzl@7050: void insert_list_into_free_list(FreeRegionList* list) { tschatzl@7050: _free_list.add_ordered(list); tschatzl@7050: } tschatzl@7050: tschatzl@7050: HeapRegion* allocate_free_region(bool is_old) { tschatzl@7050: HeapRegion* hr = _free_list.remove_region(is_old); tschatzl@7050: tschatzl@7050: if (hr != NULL) { tschatzl@7050: assert(hr->next() == NULL, "Single region should not have next"); tschatzl@7091: assert(is_available(hr->hrm_index()), "Must be committed"); tschatzl@7050: } tschatzl@7050: return hr; tschatzl@7050: } tschatzl@7050: tschatzl@7050: inline void allocate_free_regions_starting_at(uint first, uint num_regions); tschatzl@7050: tschatzl@7050: // Remove all regions from the free list. tschatzl@7050: void remove_all_free_regions() { tschatzl@7050: _free_list.remove_all(); tschatzl@7050: } tschatzl@7050: tschatzl@7050: // Return the number of committed free regions in the heap. tschatzl@7050: uint num_free_regions() const { tschatzl@7050: return _free_list.length(); tschatzl@7050: } tschatzl@7050: tschatzl@7050: size_t total_capacity_bytes() const { tschatzl@7050: return num_free_regions() * HeapRegion::GrainBytes; tschatzl@7050: } tschatzl@7050: tschatzl@7050: // Return the number of available (uncommitted) regions. tschatzl@7050: uint available() const { return max_length() - length(); } tschatzl@7050: tonyp@2963: // Return the number of regions that have been committed in the heap. tschatzl@7050: uint length() const { return _num_committed; } tonyp@2963: tonyp@2963: // Return the maximum number of regions in the heap. tschatzl@5773: uint max_length() const { return (uint)_regions.length(); } tonyp@2963: azakharov@7835: MemoryUsage get_auxiliary_data_memory_usage() const; azakharov@7835: tschatzl@7050: MemRegion reserved() const { return MemRegion(heap_bottom(), heap_end()); } ysr@777: tschatzl@7050: // Expand the sequence to reflect that the heap has grown. Either create new tschatzl@7050: // HeapRegions, or re-use existing ones. Returns the number of regions the tschatzl@7050: // sequence was expanded by. If a HeapRegion allocation fails, the resulting tschatzl@7050: // number of regions might be smaller than what's desired. tschatzl@7050: uint expand_by(uint num_regions); tschatzl@7050: tschatzl@7050: // Makes sure that the regions from start to start+num_regions-1 are available tschatzl@7050: // for allocation. Returns the number of regions that were committed to achieve tschatzl@7050: // this. tschatzl@7050: uint expand_at(uint start, uint num_regions); tschatzl@7050: tschatzl@7051: // Find a contiguous set of empty regions of length num. Returns the start index of tschatzl@7091: // that set, or G1_NO_HRM_INDEX. tschatzl@7051: uint find_contiguous_only_empty(size_t num) { return find_contiguous(num, true); } tschatzl@7051: // Find a contiguous set of empty or unavailable regions of length num. Returns the tschatzl@7091: // start index of that set, or G1_NO_HRM_INDEX. tschatzl@7051: uint find_contiguous_empty_or_unavailable(size_t num) { return find_contiguous(num, false); } tschatzl@7050: tschatzl@7050: HeapRegion* next_region_in_heap(const HeapRegion* r) const; ysr@777: tonyp@2963: // Apply blk->doHeapRegion() on all committed regions in address order, tonyp@2963: // terminating the iteration early if doHeapRegion() returns true. tonyp@2963: void iterate(HeapRegionClosure* blk) const; ysr@777: tschatzl@7050: void par_iterate(HeapRegionClosure* blk, uint worker_id, uint no_of_par_workers, jint claim_value) const; ysr@777: tschatzl@7050: // Uncommit up to num_regions_to_remove regions that are completely free. tschatzl@7050: // Return the actual number of uncommitted regions. brutisso@5074: uint shrink_by(uint num_regions_to_remove); ysr@777: tschatzl@7050: void verify(); tschatzl@7050: tonyp@2963: // Do some sanity checking. tonyp@2963: void verify_optional() PRODUCT_RETURN; ysr@777: }; stefank@2314: tschatzl@7091: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONMANAGER_HPP