--- old/src/hotspot/share/gc/g1/heapRegionManagerForHeteroHeap.cpp 2018-11-19 17:24:50.023722500 -0800 +++ /dev/null 2018-11-19 17:24:51.000000000 -0800 @@ -1,446 +0,0 @@ -/* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - * - */ - -#include "precompiled.hpp" -#include "gc/g1/g1CollectedHeap.inline.hpp" -#include "gc/g1/g1ConcurrentRefine.hpp" -#include "gc/g1/heapRegion.hpp" -#include "gc/g1/heapRegionManager.inline.hpp" -#include "gc/g1/heapRegionManagerForHeteroHeap.hpp" -#include "gc/g1/heapRegionSet.inline.hpp" -#include "memory/allocation.hpp" - -// expand_by() is called to grow the heap. We grow into nvdimm now. -// Dram regions are committed later as needed during mutator region allocation or -// when young list target length is determined after gc cycle. -uint HeapRegionManagerForHeteroHeap::expand_by(uint num_regions, WorkGang* pretouch_workers) { - uint num_expanded = expand_nvdimm(MIN2(num_regions, max_expandable_length() - total_regions_committed()), pretouch_workers); - assert(total_regions_committed() <= max_expandable_length(), "must be"); - return num_expanded; -} - -// Expands heap starting from 'start' index. The question is should we expand from one memory (e.g. nvdimm) to another (e.g. dram). -// Looking at the code, expand_at() is called for humongous allocation where 'start' is in nv-dimm. -// So we only allocate regions in the same kind of memory as 'start'. -uint HeapRegionManagerForHeteroHeap::expand_at(uint start, uint num_regions, WorkGang* pretouch_workers) { - if (num_regions == 0) { - return 0; - } - num_regions = MIN2(num_regions, max_expandable_length() - total_regions_committed()); - uint end = is_in_nvdimm(start) ? end_index_of_nvdimm() : end_index_of_dram(); - uint num_expanded = expand_in_range(start, end, num_regions, pretouch_workers); - assert(total_regions_committed() <= max_expandable_length(), "must be"); - return num_expanded; -} - -// This function ensures that there are 'expected_num_regions' committed regions in dram. -// If new regions are committed, it un-commits that many regions from nv-dimm. -// If there are already more regions committed in dram, extra regions are un-committed. -void HeapRegionManagerForHeteroHeap::resize_dram_regions(uint expected_num_regions, WorkGang* pretouch_workers) { - - assert(total_regions_committed() <= max_expandable_length(), "must be"); - if(expected_num_regions > free_list_dram_length()) { - // If we are going to expand DRAM, we expand a little more so that we can absorb small variations in Young gen sizing. - uint targeted_dram_regions = expected_num_regions * (1 + (double)G1YoungExpansionBufferPerc / 100); - uint to_be_made_available = targeted_dram_regions - free_list_dram_length(); - -#ifdef ASSERT - uint total_committed_before = total_regions_committed(); -#endif - uint can_be_made_available = shrink_nvdimm(to_be_made_available); - uint ret = expand_dram(can_be_made_available, pretouch_workers); -#ifdef ASSERT - assert(ret == can_be_made_available, "should be equal"); - assert(total_committed_before == total_regions_committed(), "invariant not met"); - assert(total_regions_committed() <= _max_regions, "post-condition"); -#endif - } else { - uint to_be_released = free_list_dram_length() - expected_num_regions; - // if number of extra DRAM regions is small, do not shrink. - if (to_be_released < expected_num_regions * G1YoungExpansionBufferPerc / 100) { - return; - } - -#ifdef ASSERT - uint total_committed_before = total_regions_committed(); -#endif - uint ret = shrink_dram(to_be_released); - assert(ret == to_be_released, "Should be able to shrink by given amount"); - ret = expand_nvdimm(to_be_released, pretouch_workers); -#ifdef ASSERT - assert(ret == to_be_released, "Should be able to expand by given amount"); - assert(total_committed_before == total_regions_committed(), "invariant not met"); - assert(total_regions_committed() <= _max_regions, "post-condition"); -#endif - } - assert(total_regions_committed() <= max_expandable_length(), "must be"); -} - -uint HeapRegionManagerForHeteroHeap::total_regions_committed() const { - return num_committed_dram() + num_committed_nvdimm(); -} - -uint HeapRegionManagerForHeteroHeap::num_committed_dram() const { - // This class does not keep count of committed regions in dram and nv-dimm. - // G1RegionToHeteroSpaceMapper keeps this information. - return static_cast(_heap_mapper)->num_committed_dram(); -} - -uint HeapRegionManagerForHeteroHeap::num_committed_nvdimm() const { - // See comment for num_committed_dram() - return static_cast(_heap_mapper)->num_committed_nvdimm(); -} - -// Return maximum number of regions that heap can expand to. -uint HeapRegionManagerForHeteroHeap::max_expandable_length() const { - return _max_regions; -} - -uint HeapRegionManagerForHeteroHeap::find_unavailable_in_range(uint start_idx, uint end_idx, uint* res_idx) const { - guarantee(res_idx != NULL, "checking"); - guarantee(start_idx <= (max_length() + 1), "checking"); - - uint num_regions = 0; - - uint cur = start_idx; - while (cur <= end_idx && is_available(cur)) { - cur++; - } - if (cur == end_idx + 1) { - return num_regions; - } - *res_idx = cur; - while (cur <= end_idx && !is_available(cur)) { - cur++; - } - num_regions = cur - *res_idx; - -#ifdef ASSERT - for (uint i = *res_idx; i < (*res_idx + num_regions); i++) { - assert(!is_available(i), "just checking"); - } - assert(cur == end_idx + 1 || num_regions == 0 || is_available(cur), - "The region at the current position %u must be available or at the end", cur); -#endif - return num_regions; -} - -uint HeapRegionManagerForHeteroHeap::expand_dram(uint num_regions, WorkGang* pretouch_workers) { - return expand_in_range(start_index_of_dram(), end_index_of_dram(), num_regions, pretouch_workers); -} - -uint HeapRegionManagerForHeteroHeap::expand_nvdimm(uint num_regions, WorkGang* pretouch_workers) { - return expand_in_range(start_index_of_nvdimm(), end_index_of_nvdimm(), num_regions, pretouch_workers); -} - -// Follows same logic as expand_at() form HeapRegionManager. -uint HeapRegionManagerForHeteroHeap::expand_in_range(uint start, uint end, uint num_regions, WorkGang* pretouch_gang) { - - uint so_far = 0; - uint chunk_start = 0; - uint num_last_found = 0; - while (so_far < num_regions && - (num_last_found = find_unavailable_in_range(start, end, &chunk_start)) > 0) { - uint to_commit = MIN2(num_regions - so_far, num_last_found); - make_regions_available(chunk_start, to_commit, pretouch_gang); - so_far += to_commit; - start = chunk_start + to_commit + 1; - } - - return so_far; -} - -// Shrink in the range of indexes which are reserved for dram. -uint HeapRegionManagerForHeteroHeap::shrink_dram(uint num_regions, bool update_free_list) { - return shrink_in_range(start_index_of_dram(), end_index_of_dram(), num_regions, update_free_list); -} - -// Shrink in the range of indexes which are reserved for nv-dimm. -uint HeapRegionManagerForHeteroHeap::shrink_nvdimm(uint num_regions, bool update_free_list) { - return shrink_in_range(start_index_of_nvdimm(), end_index_of_nvdimm(), num_regions, update_free_list); -} - -// Find empty regions in given range, un-commit them and return the count. -uint HeapRegionManagerForHeteroHeap::shrink_in_range(uint start, uint end, uint num_regions, bool update_free_list) { - - if (num_regions == 0) { - return 0; - } - uint so_far = 0; - uint idx_last_found = 0; - uint num_last_found; - while (so_far < num_regions && - (num_last_found = find_empty_in_range_reverse(start, end, &idx_last_found)) > 0) { - uint to_uncommit = MIN2(num_regions - so_far, num_last_found); - if(update_free_list) { - _free_list.remove_starting_at(at(idx_last_found + num_last_found - to_uncommit), to_uncommit); - } - uncommit_regions(idx_last_found + num_last_found - to_uncommit, to_uncommit); - so_far += to_uncommit; - end = idx_last_found; - } - return so_far; -} - -uint HeapRegionManagerForHeteroHeap::find_empty_in_range_reverse(uint start_idx, uint end_idx, uint* res_idx) { - guarantee(res_idx != NULL, "checking"); - guarantee(start_idx < max_length(), "checking"); - guarantee(end_idx < max_length(), "checking"); - if(start_idx > end_idx) { - return 0; - } - - uint num_regions_found = 0; - - jlong cur = end_idx; - while (cur >= start_idx && !(is_available(cur) && at(cur)->is_empty())) { - cur--; - } - if (cur == start_idx - 1) { - return num_regions_found; - } - jlong old_cur = cur; - // cur indexes the first empty region - while (cur >= start_idx && (is_available(cur) && at(cur)->is_empty())) { - cur--; - } - *res_idx = cur + 1; - num_regions_found = old_cur - cur; - -#ifdef ASSERT - for (uint i = *res_idx; i < (*res_idx + num_regions_found); i++) { - assert(at(i)->is_empty(), "just checking"); - } -#endif - return num_regions_found; -} - -HeapRegion* HeapRegionManagerForHeteroHeap::allocate_free_region(bool is_old) { - // old region is allocated from nv-dimm, non-old region from dram - // assumption: dram regions take higher indexes - assert(total_regions_committed() <= max_expandable_length(), "must be"); - bool from_head = is_old ? true : false; - HeapRegion* hr = _free_list.remove_region(from_head); - - if (hr != NULL && ( (is_old && !is_in_nvdimm(hr->hrm_index())) || (!is_old && !is_in_dram(hr->hrm_index())) ) ) { - _free_list.add_ordered(hr); - hr = NULL; - } - -#ifdef ASSERT - uint total_committed_before = total_regions_committed(); -#endif - - if (hr == NULL) { - if (!is_old) { - uint ret = shrink_nvdimm(1); - if (ret == 1) { - ret = expand_dram(1, NULL); - assert(ret == 1, "We should be able to commit one region"); - hr = _free_list.remove_region(from_head); - } - } - else { /*is_old*/ - uint ret = shrink_dram(1); - if (ret == 1) { - ret = expand_nvdimm(1, NULL); - assert(ret == 1, "We should be able to commit one region"); - hr = _free_list.remove_region(from_head); - } - } - } -#ifdef ASSERT - assert(total_committed_before == total_regions_committed(), "invariant not met"); - assert(total_regions_committed() <= max_expandable_length(), "post-condition"); -#endif - - if (hr != NULL) { - assert(hr->next() == NULL, "Single region should not have next"); - assert(is_available(hr->hrm_index()), "Must be committed"); - } - assert(total_regions_committed() <= max_expandable_length(), "must be"); - return hr; -} - -uint HeapRegionManagerForHeteroHeap::find_contiguous_only_empty(size_t num) { - return find_contiguous(start_index_of_nvdimm(), end_index_of_nvdimm(), num, true); -} - -uint HeapRegionManagerForHeteroHeap::find_contiguous_empty_or_unavailable(size_t num) { - return find_contiguous(start_index_of_nvdimm(), end_index_of_nvdimm(), num, false); -} - -uint HeapRegionManagerForHeteroHeap::find_contiguous(size_t start, size_t end, size_t num, bool empty_only) { - uint found = 0; - size_t length_found = 0; - uint cur = (uint)start; - uint length_unavailable = 0; - - while (length_found < num && cur <= end) { - HeapRegion* hr = _regions.get_by_index(cur); - if ((!empty_only && !is_available(cur)) || (is_available(cur) && hr != NULL && hr->is_empty())) { - // This region is a potential candidate for allocation into. - if (!is_available(cur)) { - length_unavailable++; - } - length_found++; - } - else { - // This region is not a candidate. The next region is the next possible one. - found = cur + 1; - length_found = 0; - } - cur++; - } - - if (length_found == num) { - for (uint i = found; i < (found + num); i++) { - HeapRegion* hr = _regions.get_by_index(i); - // sanity check - guarantee((!empty_only && !is_available(i)) || (is_available(i) && hr != NULL && hr->is_empty()), - "Found region sequence starting at " UINT32_FORMAT ", length " SIZE_FORMAT - " that is not empty at " UINT32_FORMAT ". Hr is " PTR_FORMAT, found, num, i, p2i(hr)); - } - if (!empty_only && length_unavailable > (max_expandable_length() - total_regions_committed())) { - // if 'length_unavailable' number of regions will be made available, we will exceed max regions. - return G1_NO_HRM_INDEX; - } - return found; - } - else { - return G1_NO_HRM_INDEX; - } -} - -uint HeapRegionManagerForHeteroHeap::find_highest_free(bool* expanded) { - // Loop downwards from the highest dram region index, looking for an - // entry which is either free or not yet committed. If not yet - // committed, expand_at that index. - uint curr = end_index_of_dram(); - while (true) { - HeapRegion *hr = _regions.get_by_index(curr); - if (hr == NULL && !(total_regions_committed() < _max_regions)) { - uint res = shrink_nvdimm(1); - if (res == 1) { - res = expand_in_range(curr, curr, 1, NULL); - assert(res == 1, "We should be able to expand since shrink was successful"); - *expanded = true; - return curr; - } - } - else { - if (hr->is_free()) { - *expanded = false; - return curr; - } - } - if (curr == start_index_of_dram()) { - return G1_NO_HRM_INDEX; - } - curr--; - } -} - -// We need to override this since region 0 which serves are dummy region in base class may not be available here. -// This is a corner condition when either number of regions is small. When adaptive sizing is used, initial heap size -// could be just one region. This region is commited in dram to be used for young generation, leaving region 0 (which is in nvdimm) -// unavailable. -HeapRegion* HeapRegionManagerForHeteroHeap::get_dummy_region() { - uint curr = 0; - - while (curr < _regions.length()) { - if (is_available(curr)) { - return new_heap_region(curr); - } - curr++; - } - assert(false, "We should always find a region available for dummy region"); - return NULL; -} - -// First shrink in dram, then in nv-dimm. -uint HeapRegionManagerForHeteroHeap::shrink_by(uint num_regions) { - // This call is made at end of full collection. Before making this call the region sets are tore down (tear_down_region_sets()). - // So shrink() calls below do not need to remove uncomitted regions from free list. - uint ret = shrink_dram(num_regions, false /* update_free_list */); - ret += shrink_nvdimm(num_regions - ret, false /* update_free_list */); - return ret; -} - -void HeapRegionManagerForHeteroHeap::verify() { - HeapRegionManager::verify(); -} - -uint HeapRegionManagerForHeteroHeap::free_list_dram_length() const { - return _free_list.num_of_regions_in_range(start_index_of_dram(), end_index_of_dram()); -} - -uint HeapRegionManagerForHeteroHeap::free_list_nvdimm_length() const { - return _free_list.num_of_regions_in_range(start_index_of_nvdimm(), end_index_of_nvdimm()); -} - -bool HeapRegionManagerForHeteroHeap::is_in_nvdimm(uint index) const { - return index >= start_index_of_nvdimm() && index <= end_index_of_nvdimm(); -} - -bool HeapRegionManagerForHeteroHeap::is_in_dram(uint index) const { - return index >= start_index_of_dram() && index <= end_index_of_dram(); -} - -// We have to make sure full collection copies all surviving objects to NV-DIMM. -// We might not have enough regions in nvdimm_set, so we need to make more regions on NV-DIMM available for full collection. -// Note: by doing this we are breaking the in-variant that total number of committed regions is equal to current heap size. -// After full collection ends, we will re-establish this in-variant by freeing DRAM regions. -void HeapRegionManagerForHeteroHeap::prepare_for_full_collection_start() { - _total_commited_before_full_gc = total_regions_committed(); - expand_nvdimm(num_committed_dram(), NULL); - remove_all_free_regions(); -} - -// We need to bring back the total committed regions to before full collection start. -// All regular regions (not pinned regions) in DRAM should be free. -// We shrink all free regions in DRAM and if needed from NV-DIMM (when there are pinned DRAM regions) -void HeapRegionManagerForHeteroHeap::prepare_for_full_collection_end() { - uint shrink_size = total_regions_committed() - _total_commited_before_full_gc; - uint so_far = 0; - uint idx_last_found = 0; - uint num_last_found; - uint end = (uint)_regions.length() - 1; - while (so_far < shrink_size && - (num_last_found = find_empty_in_range_reverse(0, end, &idx_last_found)) > 0) { - uint to_uncommit = MIN2(shrink_size - so_far, num_last_found); - uncommit_regions(idx_last_found + num_last_found - to_uncommit, to_uncommit); - so_far += to_uncommit; - end = idx_last_found; - } - assert(so_far == shrink_size, "We should be able to shrink this many regions"); -} - -uint HeapRegionManagerForHeteroHeap::start_index_of_dram() const { return _max_regions;} - -uint HeapRegionManagerForHeteroHeap::end_index_of_dram() const { return 2*_max_regions - 1; } - -uint HeapRegionManagerForHeteroHeap::start_index_of_nvdimm() const { return 0; } - -uint HeapRegionManagerForHeteroHeap::end_index_of_nvdimm() const { return _max_regions - 1; }