tschatzl@7051: /* tschatzl@7781: * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved. tschatzl@7051: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. tschatzl@7051: * tschatzl@7051: * This code is free software; you can redistribute it and/or modify it tschatzl@7051: * under the terms of the GNU General Public License version 2 only, as tschatzl@7051: * published by the Free Software Foundation. tschatzl@7051: * tschatzl@7051: * This code is distributed in the hope that it will be useful, but WITHOUT tschatzl@7051: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or tschatzl@7051: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License tschatzl@7051: * version 2 for more details (a copy is included in the LICENSE file that tschatzl@7051: * accompanied this code). tschatzl@7051: * tschatzl@7051: * You should have received a copy of the GNU General Public License version tschatzl@7051: * 2 along with this work; if not, write to the Free Software Foundation, tschatzl@7051: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. tschatzl@7051: * tschatzl@7051: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA tschatzl@7051: * or visit www.oracle.com if you need additional information or have any tschatzl@7051: * questions. tschatzl@7051: * tschatzl@7051: */ tschatzl@7051: tschatzl@7051: #include "precompiled.hpp" tschatzl@7051: #include "gc_implementation/g1/g1BiasedArray.hpp" tschatzl@7051: #include "gc_implementation/g1/g1RegionToSpaceMapper.hpp" tschatzl@7053: #include "memory/allocation.inline.hpp" tschatzl@7051: #include "runtime/virtualspace.hpp" tschatzl@7051: #include "services/memTracker.hpp" tschatzl@7051: #include "utilities/bitMap.inline.hpp" tschatzl@7051: tschatzl@7051: G1RegionToSpaceMapper::G1RegionToSpaceMapper(ReservedSpace rs, tschatzl@7781: size_t used_size, tschatzl@7781: size_t page_size, tschatzl@7051: size_t region_granularity, tschatzl@7051: MemoryType type) : tschatzl@7781: _storage(rs, used_size, page_size), tschatzl@7051: _region_granularity(region_granularity), tschatzl@7051: _listener(NULL), tschatzl@7051: _commit_map() { tschatzl@7781: guarantee(is_power_of_2(page_size), "must be"); tschatzl@7051: guarantee(is_power_of_2(region_granularity), "must be"); tschatzl@7051: tschatzl@7051: MemTracker::record_virtual_memory_type((address)rs.base(), type); tschatzl@7051: } tschatzl@7051: tschatzl@7051: // G1RegionToSpaceMapper implementation where the region granularity is larger than tschatzl@7051: // or the same as the commit granularity. tschatzl@7051: // Basically, the space corresponding to one region region spans several OS pages. tschatzl@7051: class G1RegionsLargerThanCommitSizeMapper : public G1RegionToSpaceMapper { tschatzl@7051: private: tschatzl@7051: size_t _pages_per_region; tschatzl@7051: tschatzl@7051: public: tschatzl@7051: G1RegionsLargerThanCommitSizeMapper(ReservedSpace rs, tschatzl@7781: size_t actual_size, tschatzl@7781: size_t page_size, tschatzl@7051: size_t alloc_granularity, tschatzl@7051: size_t commit_factor, tschatzl@7051: MemoryType type) : tschatzl@7781: G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, type), tschatzl@7781: _pages_per_region(alloc_granularity / (page_size * commit_factor)) { tschatzl@7051: tschatzl@7781: guarantee(alloc_granularity >= page_size, "allocation granularity smaller than commit granularity"); tschatzl@7051: _commit_map.resize(rs.size() * commit_factor / alloc_granularity, /* in_resource_area */ false); tschatzl@7051: } tschatzl@7051: tschatzl@7781: virtual void commit_regions(uint start_idx, size_t num_regions) { tschatzl@7781: bool zero_filled = _storage.commit((size_t)start_idx * _pages_per_region, num_regions * _pages_per_region); tschatzl@7051: _commit_map.set_range(start_idx, start_idx + num_regions); sjohanss@7509: fire_on_commit(start_idx, num_regions, zero_filled); tschatzl@7051: } tschatzl@7051: tschatzl@7781: virtual void uncommit_regions(uint start_idx, size_t num_regions) { tschatzl@7781: _storage.uncommit((size_t)start_idx * _pages_per_region, num_regions * _pages_per_region); tschatzl@7051: _commit_map.clear_range(start_idx, start_idx + num_regions); tschatzl@7051: } tschatzl@7051: }; tschatzl@7051: tschatzl@7051: // G1RegionToSpaceMapper implementation where the region granularity is smaller tschatzl@7051: // than the commit granularity. tschatzl@7051: // Basically, the contents of one OS page span several regions. tschatzl@7051: class G1RegionsSmallerThanCommitSizeMapper : public G1RegionToSpaceMapper { tschatzl@7051: private: tschatzl@7051: class CommitRefcountArray : public G1BiasedMappedArray { tschatzl@7051: protected: tschatzl@7051: virtual uint default_value() const { return 0; } tschatzl@7051: }; tschatzl@7051: tschatzl@7051: size_t _regions_per_page; tschatzl@7051: tschatzl@7051: CommitRefcountArray _refcounts; tschatzl@7051: tschatzl@7051: uintptr_t region_idx_to_page_idx(uint region) const { tschatzl@7051: return region / _regions_per_page; tschatzl@7051: } tschatzl@7051: tschatzl@7051: public: tschatzl@7051: G1RegionsSmallerThanCommitSizeMapper(ReservedSpace rs, tschatzl@7781: size_t actual_size, tschatzl@7781: size_t page_size, tschatzl@7051: size_t alloc_granularity, tschatzl@7051: size_t commit_factor, tschatzl@7051: MemoryType type) : tschatzl@7781: G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, type), tschatzl@7781: _regions_per_page((page_size * commit_factor) / alloc_granularity), _refcounts() { tschatzl@7051: tschatzl@7781: guarantee((page_size * commit_factor) >= alloc_granularity, "allocation granularity smaller than commit granularity"); tschatzl@7781: _refcounts.initialize((HeapWord*)rs.base(), (HeapWord*)(rs.base() + align_size_up(rs.size(), page_size)), page_size); tschatzl@7051: _commit_map.resize(rs.size() * commit_factor / alloc_granularity, /* in_resource_area */ false); tschatzl@7051: } tschatzl@7051: tschatzl@7781: virtual void commit_regions(uint start_idx, size_t num_regions) { tschatzl@7781: for (uint i = start_idx; i < start_idx + num_regions; i++) { tschatzl@7781: assert(!_commit_map.at(i), err_msg("Trying to commit storage at region %u that is already committed", i)); tschatzl@7781: size_t idx = region_idx_to_page_idx(i); tschatzl@7051: uint old_refcount = _refcounts.get_by_index(idx); tschatzl@7257: bool zero_filled = false; tschatzl@7051: if (old_refcount == 0) { sjohanss@7509: zero_filled = _storage.commit(idx, 1); tschatzl@7051: } tschatzl@7051: _refcounts.set_by_index(idx, old_refcount + 1); tschatzl@7051: _commit_map.set_bit(i); tschatzl@7257: fire_on_commit(i, 1, zero_filled); tschatzl@7051: } tschatzl@7051: } tschatzl@7051: tschatzl@7781: virtual void uncommit_regions(uint start_idx, size_t num_regions) { tschatzl@7781: for (uint i = start_idx; i < start_idx + num_regions; i++) { tschatzl@7781: assert(_commit_map.at(i), err_msg("Trying to uncommit storage at region %u that is not committed", i)); tschatzl@7781: size_t idx = region_idx_to_page_idx(i); tschatzl@7051: uint old_refcount = _refcounts.get_by_index(idx); tschatzl@7051: assert(old_refcount > 0, "must be"); tschatzl@7051: if (old_refcount == 1) { tschatzl@7051: _storage.uncommit(idx, 1); tschatzl@7051: } tschatzl@7051: _refcounts.set_by_index(idx, old_refcount - 1); tschatzl@7051: _commit_map.clear_bit(i); tschatzl@7051: } tschatzl@7051: } tschatzl@7051: }; tschatzl@7051: tschatzl@7257: void G1RegionToSpaceMapper::fire_on_commit(uint start_idx, size_t num_regions, bool zero_filled) { tschatzl@7051: if (_listener != NULL) { tschatzl@7257: _listener->on_commit(start_idx, num_regions, zero_filled); tschatzl@7051: } tschatzl@7051: } tschatzl@7051: tschatzl@7051: G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_mapper(ReservedSpace rs, tschatzl@7781: size_t actual_size, tschatzl@7781: size_t page_size, tschatzl@7051: size_t region_granularity, tschatzl@7051: size_t commit_factor, tschatzl@7051: MemoryType type) { tschatzl@7051: tschatzl@7781: if (region_granularity >= (page_size * commit_factor)) { tschatzl@7781: return new G1RegionsLargerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type); tschatzl@7051: } else { tschatzl@7781: return new G1RegionsSmallerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type); tschatzl@7051: } tschatzl@7051: }