2014-08-19 14:09:10 +02:00
|
|
|
/*
|
2018-02-21 07:46:40 +01:00
|
|
|
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
|
2014-08-19 14:09:10 +02:00
|
|
|
* 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"
|
2015-05-13 15:16:06 +02:00
|
|
|
#include "gc/g1/g1PageBasedVirtualSpace.hpp"
|
2016-09-16 11:33:47 +02:00
|
|
|
#include "gc/shared/workgroup.hpp"
|
2019-08-19 11:30:03 +02:00
|
|
|
#include "oops/markWord.hpp"
|
2014-08-19 14:09:10 +02:00
|
|
|
#include "oops/oop.inline.hpp"
|
2016-09-16 11:33:47 +02:00
|
|
|
#include "runtime/atomic.hpp"
|
2016-07-13 12:23:05 +02:00
|
|
|
#include "runtime/os.inline.hpp"
|
2014-08-19 14:09:10 +02:00
|
|
|
#include "services/memTracker.hpp"
|
2017-07-05 11:33:17 +02:00
|
|
|
#include "utilities/align.hpp"
|
2014-08-19 14:09:10 +02:00
|
|
|
#include "utilities/bitMap.inline.hpp"
|
|
|
|
|
2015-04-07 10:53:51 +02:00
|
|
|
G1PageBasedVirtualSpace::G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size) :
|
2018-08-08 15:31:06 +02:00
|
|
|
_low_boundary(NULL), _high_boundary(NULL), _tail_size(0), _page_size(0),
|
|
|
|
_committed(mtGC), _dirty(mtGC), _special(false), _executable(false) {
|
2015-04-07 10:53:51 +02:00
|
|
|
initialize_with_page_size(rs, used_size, page_size);
|
2014-08-19 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 10:53:51 +02:00
|
|
|
void G1PageBasedVirtualSpace::initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size) {
|
|
|
|
guarantee(rs.is_reserved(), "Given reserved space must have been reserved already.");
|
|
|
|
|
|
|
|
vmassert(_low_boundary == NULL, "VirtualSpace already initialized");
|
|
|
|
vmassert(page_size > 0, "Page size must be non-zero.");
|
|
|
|
|
2017-07-04 15:58:10 +02:00
|
|
|
guarantee(is_aligned(rs.base(), page_size),
|
2015-09-29 11:02:08 +02:00
|
|
|
"Reserved space base " PTR_FORMAT " is not aligned to requested page size " SIZE_FORMAT, p2i(rs.base()), page_size);
|
2017-07-04 15:58:10 +02:00
|
|
|
guarantee(is_aligned(used_size, os::vm_page_size()),
|
2015-09-29 11:02:08 +02:00
|
|
|
"Given used reserved space size needs to be OS page size aligned (%d bytes) but is " SIZE_FORMAT, os::vm_page_size(), used_size);
|
2015-04-07 10:53:51 +02:00
|
|
|
guarantee(used_size <= rs.size(),
|
2015-09-29 11:02:08 +02:00
|
|
|
"Used size of reserved space " SIZE_FORMAT " bytes is smaller than reservation at " SIZE_FORMAT " bytes", used_size, rs.size());
|
2017-07-04 15:58:10 +02:00
|
|
|
guarantee(is_aligned(rs.size(), page_size),
|
2015-09-29 11:02:08 +02:00
|
|
|
"Expected that the virtual space is size aligned, but " SIZE_FORMAT " is not aligned to page size " SIZE_FORMAT, rs.size(), page_size);
|
2014-08-19 14:09:10 +02:00
|
|
|
|
|
|
|
_low_boundary = rs.base();
|
2015-04-07 10:53:51 +02:00
|
|
|
_high_boundary = _low_boundary + used_size;
|
2014-08-19 14:09:10 +02:00
|
|
|
|
|
|
|
_special = rs.special();
|
|
|
|
_executable = rs.executable();
|
|
|
|
|
|
|
|
_page_size = page_size;
|
|
|
|
|
2015-04-07 10:53:51 +02:00
|
|
|
vmassert(_committed.size() == 0, "virtual space initialized more than once");
|
|
|
|
BitMap::idx_t size_in_pages = rs.size() / page_size;
|
2016-05-03 22:45:27 +02:00
|
|
|
_committed.initialize(size_in_pages);
|
2015-01-12 15:24:29 +01:00
|
|
|
if (_special) {
|
2016-05-03 22:45:27 +02:00
|
|
|
_dirty.initialize(size_in_pages);
|
2015-01-12 15:24:29 +01:00
|
|
|
}
|
2014-08-19 14:09:10 +02:00
|
|
|
|
2015-04-07 10:53:51 +02:00
|
|
|
_tail_size = used_size % _page_size;
|
2014-08-19 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
G1PageBasedVirtualSpace::~G1PageBasedVirtualSpace() {
|
|
|
|
// This does not release memory it never reserved.
|
|
|
|
// Caller must release via rs.release();
|
|
|
|
_low_boundary = NULL;
|
|
|
|
_high_boundary = NULL;
|
|
|
|
_special = false;
|
|
|
|
_executable = false;
|
|
|
|
_page_size = 0;
|
2015-04-07 10:53:51 +02:00
|
|
|
_tail_size = 0;
|
2014-08-19 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t G1PageBasedVirtualSpace::committed_size() const {
|
2015-04-07 10:53:51 +02:00
|
|
|
size_t result = _committed.count_one_bits() * _page_size;
|
|
|
|
// The last page might not be in full.
|
|
|
|
if (is_last_page_partial() && _committed.at(_committed.size() - 1)) {
|
|
|
|
result -= _page_size - _tail_size;
|
|
|
|
}
|
|
|
|
return result;
|
2014-08-19 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t G1PageBasedVirtualSpace::reserved_size() const {
|
|
|
|
return pointer_delta(_high_boundary, _low_boundary, sizeof(char));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t G1PageBasedVirtualSpace::uncommitted_size() const {
|
|
|
|
return reserved_size() - committed_size();
|
|
|
|
}
|
|
|
|
|
2018-12-21 08:18:59 -08:00
|
|
|
void G1PageBasedVirtualSpace::commit_and_set_special() {
|
|
|
|
commit_internal(addr_to_page_index(_low_boundary), addr_to_page_index(_high_boundary));
|
|
|
|
_special = true;
|
|
|
|
_dirty.initialize(reserved_size()/_page_size);
|
|
|
|
}
|
|
|
|
|
2015-04-07 10:53:51 +02:00
|
|
|
size_t G1PageBasedVirtualSpace::addr_to_page_index(char* addr) const {
|
2014-08-19 14:09:10 +02:00
|
|
|
return (addr - _low_boundary) / _page_size;
|
|
|
|
}
|
|
|
|
|
2015-04-07 10:53:51 +02:00
|
|
|
bool G1PageBasedVirtualSpace::is_area_committed(size_t start_page, size_t size_in_pages) const {
|
|
|
|
size_t end_page = start_page + size_in_pages;
|
|
|
|
return _committed.get_next_zero_offset(start_page, end_page) >= end_page;
|
2014-08-19 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 10:53:51 +02:00
|
|
|
bool G1PageBasedVirtualSpace::is_area_uncommitted(size_t start_page, size_t size_in_pages) const {
|
|
|
|
size_t end_page = start_page + size_in_pages;
|
|
|
|
return _committed.get_next_one_offset(start_page, end_page) >= end_page;
|
2014-08-19 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 10:53:51 +02:00
|
|
|
char* G1PageBasedVirtualSpace::page_start(size_t index) const {
|
2014-08-19 14:09:10 +02:00
|
|
|
return _low_boundary + index * _page_size;
|
|
|
|
}
|
|
|
|
|
2015-04-07 10:53:51 +02:00
|
|
|
bool G1PageBasedVirtualSpace::is_after_last_page(size_t index) const {
|
|
|
|
guarantee(index <= _committed.size(),
|
2015-09-29 11:02:08 +02:00
|
|
|
"Given boundary page " SIZE_FORMAT " is beyond managed page count " SIZE_FORMAT, index, _committed.size());
|
2015-04-07 10:53:51 +02:00
|
|
|
return index == _committed.size();
|
2014-08-19 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 10:53:51 +02:00
|
|
|
void G1PageBasedVirtualSpace::commit_preferred_pages(size_t start, size_t num_pages) {
|
|
|
|
vmassert(num_pages > 0, "No full pages to commit");
|
|
|
|
vmassert(start + num_pages <= _committed.size(),
|
2015-09-29 11:02:08 +02:00
|
|
|
"Tried to commit area from page " SIZE_FORMAT " to page " SIZE_FORMAT " "
|
|
|
|
"that is outside of managed space of " SIZE_FORMAT " pages",
|
|
|
|
start, start + num_pages, _committed.size());
|
2015-04-07 10:53:51 +02:00
|
|
|
|
|
|
|
char* start_addr = page_start(start);
|
|
|
|
size_t size = num_pages * _page_size;
|
|
|
|
|
|
|
|
os::commit_memory_or_exit(start_addr, size, _page_size, _executable,
|
|
|
|
err_msg("Failed to commit area from " PTR_FORMAT " to " PTR_FORMAT " of length " SIZE_FORMAT ".",
|
2015-09-29 11:02:08 +02:00
|
|
|
p2i(start_addr), p2i(start_addr + size), size));
|
2015-04-07 10:53:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void G1PageBasedVirtualSpace::commit_tail() {
|
|
|
|
vmassert(_tail_size > 0, "The size of the tail area must be > 0 when reaching here");
|
|
|
|
|
2017-07-04 15:58:10 +02:00
|
|
|
char* const aligned_end_address = align_down(_high_boundary, _page_size);
|
2015-04-07 10:53:51 +02:00
|
|
|
os::commit_memory_or_exit(aligned_end_address, _tail_size, os::vm_page_size(), _executable,
|
|
|
|
err_msg("Failed to commit tail area from " PTR_FORMAT " to " PTR_FORMAT " of length " SIZE_FORMAT ".",
|
2015-09-29 11:02:08 +02:00
|
|
|
p2i(aligned_end_address), p2i(_high_boundary), _tail_size));
|
2015-04-07 10:53:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void G1PageBasedVirtualSpace::commit_internal(size_t start_page, size_t end_page) {
|
|
|
|
guarantee(start_page < end_page,
|
2015-09-29 11:02:08 +02:00
|
|
|
"Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page);
|
2015-04-07 10:53:51 +02:00
|
|
|
guarantee(end_page <= _committed.size(),
|
2015-09-29 11:02:08 +02:00
|
|
|
"Given end page " SIZE_FORMAT " is beyond end of managed page amount of " SIZE_FORMAT, end_page, _committed.size());
|
2015-04-07 10:53:51 +02:00
|
|
|
|
|
|
|
size_t pages = end_page - start_page;
|
|
|
|
bool need_to_commit_tail = is_after_last_page(end_page) && is_last_page_partial();
|
|
|
|
|
|
|
|
// If we have to commit some (partial) tail area, decrease the amount of pages to avoid
|
|
|
|
// committing that in the full-page commit code.
|
|
|
|
if (need_to_commit_tail) {
|
|
|
|
pages--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pages > 0) {
|
|
|
|
commit_preferred_pages(start_page, pages);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_to_commit_tail) {
|
|
|
|
commit_tail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char* G1PageBasedVirtualSpace::bounded_end_addr(size_t end_page) const {
|
|
|
|
return MIN2(_high_boundary, page_start(end_page));
|
|
|
|
}
|
|
|
|
|
|
|
|
void G1PageBasedVirtualSpace::pretouch_internal(size_t start_page, size_t end_page) {
|
|
|
|
guarantee(start_page < end_page,
|
2015-09-29 11:02:08 +02:00
|
|
|
"Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page);
|
2015-04-07 10:53:51 +02:00
|
|
|
|
2016-09-16 11:33:47 +02:00
|
|
|
os::pretouch_memory(page_start(start_page), bounded_end_addr(end_page), _page_size);
|
2015-04-07 10:53:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool G1PageBasedVirtualSpace::commit(size_t start_page, size_t size_in_pages) {
|
2014-08-19 14:09:10 +02:00
|
|
|
// We need to make sure to commit all pages covered by the given area.
|
2015-04-07 10:53:51 +02:00
|
|
|
guarantee(is_area_uncommitted(start_page, size_in_pages), "Specified area is not uncommitted");
|
2014-08-19 14:09:10 +02:00
|
|
|
|
2015-01-12 15:24:29 +01:00
|
|
|
bool zero_filled = true;
|
2015-04-07 10:53:51 +02:00
|
|
|
size_t end_page = start_page + size_in_pages;
|
2015-01-12 15:24:29 +01:00
|
|
|
|
|
|
|
if (_special) {
|
|
|
|
// Check for dirty pages and update zero_filled if any found.
|
2015-04-07 10:53:51 +02:00
|
|
|
if (_dirty.get_next_one_offset(start_page, end_page) < end_page) {
|
2015-01-12 15:24:29 +01:00
|
|
|
zero_filled = false;
|
2015-04-07 10:53:51 +02:00
|
|
|
_dirty.clear_range(start_page, end_page);
|
2015-01-12 15:24:29 +01:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-07 10:53:51 +02:00
|
|
|
commit_internal(start_page, end_page);
|
2014-08-19 14:09:10 +02:00
|
|
|
}
|
2015-04-07 10:53:51 +02:00
|
|
|
_committed.set_range(start_page, end_page);
|
2014-08-19 14:09:10 +02:00
|
|
|
|
2015-01-12 15:24:29 +01:00
|
|
|
return zero_filled;
|
2014-08-19 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 10:53:51 +02:00
|
|
|
void G1PageBasedVirtualSpace::uncommit_internal(size_t start_page, size_t end_page) {
|
|
|
|
guarantee(start_page < end_page,
|
2015-09-29 11:02:08 +02:00
|
|
|
"Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page);
|
2015-04-07 10:53:51 +02:00
|
|
|
|
|
|
|
char* start_addr = page_start(start_page);
|
|
|
|
os::uncommit_memory(start_addr, pointer_delta(bounded_end_addr(end_page), start_addr, sizeof(char)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void G1PageBasedVirtualSpace::uncommit(size_t start_page, size_t size_in_pages) {
|
|
|
|
guarantee(is_area_committed(start_page, size_in_pages), "checking");
|
2014-08-19 14:09:10 +02:00
|
|
|
|
2015-04-07 10:53:51 +02:00
|
|
|
size_t end_page = start_page + size_in_pages;
|
2015-01-12 15:24:29 +01:00
|
|
|
if (_special) {
|
|
|
|
// Mark that memory is dirty. If committed again the memory might
|
|
|
|
// need to be cleared explicitly.
|
2015-04-07 10:53:51 +02:00
|
|
|
_dirty.set_range(start_page, end_page);
|
2015-01-12 15:24:29 +01:00
|
|
|
} else {
|
2015-04-07 10:53:51 +02:00
|
|
|
uncommit_internal(start_page, end_page);
|
2014-08-19 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 10:53:51 +02:00
|
|
|
_committed.clear_range(start_page, end_page);
|
2014-08-19 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
2016-09-16 11:33:47 +02:00
|
|
|
class G1PretouchTask : public AbstractGangTask {
|
|
|
|
private:
|
|
|
|
char* volatile _cur_addr;
|
|
|
|
char* const _start_addr;
|
|
|
|
char* const _end_addr;
|
2018-11-22 09:26:51 +01:00
|
|
|
size_t _page_size;
|
2016-09-16 11:33:47 +02:00
|
|
|
public:
|
|
|
|
G1PretouchTask(char* start_address, char* end_address, size_t page_size) :
|
2018-02-21 07:46:40 +01:00
|
|
|
AbstractGangTask("G1 PreTouch"),
|
2016-09-16 11:33:47 +02:00
|
|
|
_cur_addr(start_address),
|
|
|
|
_start_addr(start_address),
|
|
|
|
_end_addr(end_address),
|
2018-11-22 09:26:51 +01:00
|
|
|
_page_size(0) {
|
|
|
|
#ifdef LINUX
|
|
|
|
_page_size = UseTransparentHugePages ? (size_t)os::vm_page_size(): page_size;
|
|
|
|
#else
|
|
|
|
_page_size = page_size;
|
|
|
|
#endif
|
2016-09-16 11:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void work(uint worker_id) {
|
|
|
|
size_t const actual_chunk_size = MAX2(chunk_size(), _page_size);
|
|
|
|
while (true) {
|
2017-10-16 22:36:06 -04:00
|
|
|
char* touch_addr = Atomic::add(actual_chunk_size, &_cur_addr) - actual_chunk_size;
|
2016-09-16 11:33:47 +02:00
|
|
|
if (touch_addr < _start_addr || touch_addr >= _end_addr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
char* end_addr = touch_addr + MIN2(actual_chunk_size, pointer_delta(_end_addr, touch_addr, sizeof(char)));
|
|
|
|
os::pretouch_memory(touch_addr, end_addr, _page_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t chunk_size() { return PreTouchParallelChunkSize; }
|
|
|
|
};
|
|
|
|
|
|
|
|
void G1PageBasedVirtualSpace::pretouch(size_t start_page, size_t size_in_pages, WorkGang* pretouch_gang) {
|
2016-11-24 10:05:47 +01:00
|
|
|
G1PretouchTask cl(page_start(start_page), bounded_end_addr(start_page + size_in_pages), _page_size);
|
2016-09-16 11:33:47 +02:00
|
|
|
|
2016-11-24 10:05:47 +01:00
|
|
|
if (pretouch_gang != NULL) {
|
|
|
|
size_t num_chunks = MAX2((size_t)1, size_in_pages * _page_size / MAX2(G1PretouchTask::chunk_size(), _page_size));
|
2016-09-16 11:33:47 +02:00
|
|
|
|
2016-11-24 10:05:47 +01:00
|
|
|
uint num_workers = MIN2((uint)num_chunks, pretouch_gang->active_workers());
|
|
|
|
log_debug(gc, heap)("Running %s with %u workers for " SIZE_FORMAT " work units pre-touching " SIZE_FORMAT "B.",
|
|
|
|
cl.name(), num_workers, num_chunks, size_in_pages * _page_size);
|
|
|
|
pretouch_gang->run_task(&cl, num_workers);
|
|
|
|
} else {
|
|
|
|
log_debug(gc, heap)("Running %s pre-touching " SIZE_FORMAT "B.",
|
|
|
|
cl.name(), size_in_pages * _page_size);
|
|
|
|
cl.work(0);
|
|
|
|
}
|
2016-09-16 11:33:47 +02:00
|
|
|
}
|
|
|
|
|
2014-08-19 14:09:10 +02:00
|
|
|
bool G1PageBasedVirtualSpace::contains(const void* p) const {
|
|
|
|
return _low_boundary <= (const char*) p && (const char*) p < _high_boundary;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef PRODUCT
|
|
|
|
void G1PageBasedVirtualSpace::print_on(outputStream* out) {
|
|
|
|
out->print ("Virtual space:");
|
2015-01-12 15:24:29 +01:00
|
|
|
if (_special) out->print(" (pinned in memory)");
|
2014-08-19 14:09:10 +02:00
|
|
|
out->cr();
|
|
|
|
out->print_cr(" - committed: " SIZE_FORMAT, committed_size());
|
|
|
|
out->print_cr(" - reserved: " SIZE_FORMAT, reserved_size());
|
2015-04-07 10:53:51 +02:00
|
|
|
out->print_cr(" - preferred page size: " SIZE_FORMAT, _page_size);
|
|
|
|
out->print_cr(" - [low_b, high_b]: [" PTR_FORMAT ", " PTR_FORMAT "]", p2i(_low_boundary), p2i(_high_boundary));
|
2014-08-19 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void G1PageBasedVirtualSpace::print() {
|
|
|
|
print_on(tty);
|
|
|
|
}
|
|
|
|
#endif
|