parCardTableModRefBS.cpp revision 2353
0N/A/*
2353N/A * Copyright (c) 2007, 2011 Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A#include "memory/cardTableModRefBS.hpp"
1879N/A#include "memory/cardTableRS.hpp"
1879N/A#include "memory/sharedHeap.hpp"
1879N/A#include "memory/space.inline.hpp"
1879N/A#include "memory/universe.hpp"
1879N/A#include "runtime/java.hpp"
1879N/A#include "runtime/mutexLocker.hpp"
1879N/A#include "runtime/virtualspace.hpp"
0N/A
0N/Avoid CardTableModRefBS::par_non_clean_card_iterate_work(Space* sp, MemRegion mr,
0N/A DirtyCardToOopClosure* dcto_cl,
0N/A MemRegionClosure* cl,
0N/A int n_threads) {
0N/A if (n_threads > 0) {
845N/A assert((n_threads == 1 && ParallelGCThreads == 0) ||
845N/A n_threads <= (int)ParallelGCThreads,
845N/A "# worker threads != # requested!");
845N/A // Make sure the LNC array is valid for the space.
0N/A jbyte** lowest_non_clean;
0N/A uintptr_t lowest_non_clean_base_chunk_index;
0N/A size_t lowest_non_clean_chunk_size;
0N/A get_LNC_array_for_space(sp, lowest_non_clean,
0N/A lowest_non_clean_base_chunk_index,
0N/A lowest_non_clean_chunk_size);
0N/A
0N/A int n_strides = n_threads * StridesPerThread;
0N/A SequentialSubTasksDone* pst = sp->par_seq_tasks();
1753N/A pst->set_n_threads(n_threads);
0N/A pst->set_n_tasks(n_strides);
0N/A
0N/A int stride = 0;
0N/A while (!pst->is_task_claimed(/* reference */ stride)) {
2353N/A process_stride(sp, mr, stride, n_strides, dcto_cl, cl,
0N/A lowest_non_clean,
0N/A lowest_non_clean_base_chunk_index,
0N/A lowest_non_clean_chunk_size);
0N/A }
0N/A if (pst->all_tasks_completed()) {
0N/A // Clear lowest_non_clean array for next time.
0N/A intptr_t first_chunk_index = addr_to_chunk_index(mr.start());
0N/A uintptr_t last_chunk_index = addr_to_chunk_index(mr.last());
0N/A for (uintptr_t ch = first_chunk_index; ch <= last_chunk_index; ch++) {
0N/A intptr_t ind = ch - lowest_non_clean_base_chunk_index;
0N/A assert(0 <= ind && ind < (intptr_t)lowest_non_clean_chunk_size,
0N/A "Bounds error");
0N/A lowest_non_clean[ind] = NULL;
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid
0N/ACardTableModRefBS::
0N/Aprocess_stride(Space* sp,
0N/A MemRegion used,
0N/A jint stride, int n_strides,
0N/A DirtyCardToOopClosure* dcto_cl,
0N/A MemRegionClosure* cl,
0N/A jbyte** lowest_non_clean,
0N/A uintptr_t lowest_non_clean_base_chunk_index,
0N/A size_t lowest_non_clean_chunk_size) {
0N/A // We don't have to go downwards here; it wouldn't help anyway,
0N/A // because of parallelism.
0N/A
0N/A // Find the first card address of the first chunk in the stride that is
0N/A // at least "bottom" of the used region.
0N/A jbyte* start_card = byte_for(used.start());
0N/A jbyte* end_card = byte_after(used.last());
0N/A uintptr_t start_chunk = addr_to_chunk_index(used.start());
0N/A uintptr_t start_chunk_stride_num = start_chunk % n_strides;
0N/A jbyte* chunk_card_start;
0N/A
0N/A if ((uintptr_t)stride >= start_chunk_stride_num) {
0N/A chunk_card_start = (jbyte*)(start_card +
0N/A (stride - start_chunk_stride_num) *
0N/A CardsPerStrideChunk);
0N/A } else {
0N/A // Go ahead to the next chunk group boundary, then to the requested stride.
0N/A chunk_card_start = (jbyte*)(start_card +
0N/A (n_strides - start_chunk_stride_num + stride) *
0N/A CardsPerStrideChunk);
0N/A }
0N/A
0N/A while (chunk_card_start < end_card) {
0N/A // We don't have to go downwards here; it wouldn't help anyway,
0N/A // because of parallelism. (We take care with "min_done"; see below.)
0N/A // Invariant: chunk_mr should be fully contained within the "used" region.
0N/A jbyte* chunk_card_end = chunk_card_start + CardsPerStrideChunk;
0N/A MemRegion chunk_mr = MemRegion(addr_for(chunk_card_start),
0N/A chunk_card_end >= end_card ?
0N/A used.end() : addr_for(chunk_card_end));
0N/A assert(chunk_mr.word_size() > 0, "[chunk_card_start > used_end)");
0N/A assert(used.contains(chunk_mr), "chunk_mr should be subset of used");
0N/A
0N/A // Process the chunk.
0N/A process_chunk_boundaries(sp,
0N/A dcto_cl,
0N/A chunk_mr,
0N/A used,
0N/A lowest_non_clean,
0N/A lowest_non_clean_base_chunk_index,
0N/A lowest_non_clean_chunk_size);
0N/A
2353N/A non_clean_card_iterate_work(chunk_mr, cl);
0N/A
0N/A // Find the next chunk of the stride.
0N/A chunk_card_start += CardsPerStrideChunk * n_strides;
0N/A }
0N/A}
0N/A
0N/Avoid
0N/ACardTableModRefBS::
0N/Aprocess_chunk_boundaries(Space* sp,
0N/A DirtyCardToOopClosure* dcto_cl,
0N/A MemRegion chunk_mr,
0N/A MemRegion used,
0N/A jbyte** lowest_non_clean,
0N/A uintptr_t lowest_non_clean_base_chunk_index,
0N/A size_t lowest_non_clean_chunk_size)
0N/A{
0N/A // We must worry about the chunk boundaries.
0N/A
0N/A // First, set our max_to_do:
0N/A HeapWord* max_to_do = NULL;
0N/A uintptr_t cur_chunk_index = addr_to_chunk_index(chunk_mr.start());
0N/A cur_chunk_index = cur_chunk_index - lowest_non_clean_base_chunk_index;
0N/A
0N/A if (chunk_mr.end() < used.end()) {
0N/A // This is not the last chunk in the used region. What is the last
0N/A // object?
0N/A HeapWord* last_block = sp->block_start(chunk_mr.end());
0N/A assert(last_block <= chunk_mr.end(), "In case this property changes.");
0N/A if (last_block == chunk_mr.end()
0N/A || !sp->block_is_obj(last_block)) {
0N/A max_to_do = chunk_mr.end();
0N/A
0N/A } else {
0N/A // It is an object and starts before the end of the current chunk.
0N/A // last_obj_card is the card corresponding to the start of the last object
0N/A // in the chunk. Note that the last object may not start in
0N/A // the chunk.
0N/A jbyte* last_obj_card = byte_for(last_block);
0N/A if (!card_may_have_been_dirty(*last_obj_card)) {
0N/A // The card containing the head is not dirty. Any marks in
0N/A // subsequent cards still in this chunk must have been made
0N/A // precisely; we can cap processing at the end.
0N/A max_to_do = chunk_mr.end();
0N/A } else {
0N/A // The last object must be considered dirty, and extends onto the
0N/A // following chunk. Look for a dirty card in that chunk that will
0N/A // bound our processing.
0N/A jbyte* limit_card = NULL;
0N/A size_t last_block_size = sp->block_size(last_block);
0N/A jbyte* last_card_of_last_obj =
0N/A byte_for(last_block + last_block_size - 1);
0N/A jbyte* first_card_of_next_chunk = byte_for(chunk_mr.end());
0N/A // This search potentially goes a long distance looking
0N/A // for the next card that will be scanned. For example,
0N/A // an object that is an array of primitives will not
0N/A // have any cards covering regions interior to the array
0N/A // that will need to be scanned. The scan can be terminated
0N/A // at the last card of the next chunk. That would leave
0N/A // limit_card as NULL and would result in "max_to_do"
0N/A // being set with the LNC value or with the end
0N/A // of the last block.
0N/A jbyte* last_card_of_next_chunk = first_card_of_next_chunk +
0N/A CardsPerStrideChunk;
0N/A assert(byte_for(chunk_mr.end()) - byte_for(chunk_mr.start())
0N/A == CardsPerStrideChunk, "last card of next chunk may be wrong");
0N/A jbyte* last_card_to_check = (jbyte*) MIN2(last_card_of_last_obj,
0N/A last_card_of_next_chunk);
0N/A for (jbyte* cur = first_card_of_next_chunk;
0N/A cur <= last_card_to_check; cur++) {
0N/A if (card_will_be_scanned(*cur)) {
0N/A limit_card = cur; break;
0N/A }
0N/A }
0N/A assert(0 <= cur_chunk_index+1 &&
0N/A cur_chunk_index+1 < lowest_non_clean_chunk_size,
0N/A "Bounds error.");
0N/A // LNC for the next chunk
0N/A jbyte* lnc_card = lowest_non_clean[cur_chunk_index+1];
0N/A if (limit_card == NULL) {
0N/A limit_card = lnc_card;
0N/A }
0N/A if (limit_card != NULL) {
0N/A if (lnc_card != NULL) {
0N/A limit_card = (jbyte*)MIN2((intptr_t)limit_card,
0N/A (intptr_t)lnc_card);
0N/A }
0N/A max_to_do = addr_for(limit_card);
0N/A } else {
0N/A max_to_do = last_block + last_block_size;
0N/A }
0N/A }
0N/A }
0N/A assert(max_to_do != NULL, "OOPS!");
0N/A } else {
0N/A max_to_do = used.end();
0N/A }
0N/A // Now we can set the closure we're using so it doesn't to beyond
0N/A // max_to_do.
0N/A dcto_cl->set_min_done(max_to_do);
0N/A#ifndef PRODUCT
0N/A dcto_cl->set_last_bottom(max_to_do);
0N/A#endif
0N/A
0N/A // Now we set *our" lowest_non_clean entry.
0N/A // Find the object that spans our boundary, if one exists.
0N/A // Nothing to do on the first chunk.
0N/A if (chunk_mr.start() > used.start()) {
0N/A // first_block is the block possibly spanning the chunk start
0N/A HeapWord* first_block = sp->block_start(chunk_mr.start());
0N/A // Does the block span the start of the chunk and is it
0N/A // an object?
0N/A if (first_block < chunk_mr.start() &&
0N/A sp->block_is_obj(first_block)) {
0N/A jbyte* first_dirty_card = NULL;
0N/A jbyte* last_card_of_first_obj =
0N/A byte_for(first_block + sp->block_size(first_block) - 1);
0N/A jbyte* first_card_of_cur_chunk = byte_for(chunk_mr.start());
0N/A jbyte* last_card_of_cur_chunk = byte_for(chunk_mr.last());
0N/A jbyte* last_card_to_check =
0N/A (jbyte*) MIN2((intptr_t) last_card_of_cur_chunk,
0N/A (intptr_t) last_card_of_first_obj);
0N/A for (jbyte* cur = first_card_of_cur_chunk;
0N/A cur <= last_card_to_check; cur++) {
0N/A if (card_will_be_scanned(*cur)) {
0N/A first_dirty_card = cur; break;
0N/A }
0N/A }
0N/A if (first_dirty_card != NULL) {
0N/A assert(0 <= cur_chunk_index &&
0N/A cur_chunk_index < lowest_non_clean_chunk_size,
0N/A "Bounds error.");
0N/A lowest_non_clean[cur_chunk_index] = first_dirty_card;
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid
0N/ACardTableModRefBS::
0N/Aget_LNC_array_for_space(Space* sp,
0N/A jbyte**& lowest_non_clean,
0N/A uintptr_t& lowest_non_clean_base_chunk_index,
0N/A size_t& lowest_non_clean_chunk_size) {
0N/A
0N/A int i = find_covering_region_containing(sp->bottom());
0N/A MemRegion covered = _covered[i];
0N/A size_t n_chunks = chunks_to_cover(covered);
0N/A
0N/A // Only the first thread to obtain the lock will resize the
0N/A // LNC array for the covered region. Any later expansion can't affect
0N/A // the used_at_save_marks region.
0N/A // (I observed a bug in which the first thread to execute this would
0N/A // resize, and then it would cause "expand_and_allocates" that would
0N/A // Increase the number of chunks in the covered region. Then a second
0N/A // thread would come and execute this, see that the size didn't match,
0N/A // and free and allocate again. So the first thread would be using a
0N/A // freed "_lowest_non_clean" array.)
0N/A
0N/A // Do a dirty read here. If we pass the conditional then take the rare
0N/A // event lock and do the read again in case some other thread had already
0N/A // succeeded and done the resize.
0N/A int cur_collection = Universe::heap()->total_collections();
0N/A if (_last_LNC_resizing_collection[i] != cur_collection) {
0N/A MutexLocker x(ParGCRareEvent_lock);
0N/A if (_last_LNC_resizing_collection[i] != cur_collection) {
0N/A if (_lowest_non_clean[i] == NULL ||
0N/A n_chunks != _lowest_non_clean_chunk_size[i]) {
0N/A
0N/A // Should we delete the old?
0N/A if (_lowest_non_clean[i] != NULL) {
0N/A assert(n_chunks != _lowest_non_clean_chunk_size[i],
0N/A "logical consequence");
0N/A FREE_C_HEAP_ARRAY(CardPtr, _lowest_non_clean[i]);
0N/A _lowest_non_clean[i] = NULL;
0N/A }
0N/A // Now allocate a new one if necessary.
0N/A if (_lowest_non_clean[i] == NULL) {
0N/A _lowest_non_clean[i] = NEW_C_HEAP_ARRAY(CardPtr, n_chunks);
0N/A _lowest_non_clean_chunk_size[i] = n_chunks;
0N/A _lowest_non_clean_base_chunk_index[i] = addr_to_chunk_index(covered.start());
0N/A for (int j = 0; j < (int)n_chunks; j++)
0N/A _lowest_non_clean[i][j] = NULL;
0N/A }
0N/A }
0N/A _last_LNC_resizing_collection[i] = cur_collection;
0N/A }
0N/A }
0N/A // In any case, now do the initialization.
0N/A lowest_non_clean = _lowest_non_clean[i];
0N/A lowest_non_clean_base_chunk_index = _lowest_non_clean_base_chunk_index[i];
0N/A lowest_non_clean_chunk_size = _lowest_non_clean_chunk_size[i];
0N/A}