--- old/src/share/vm/gc_implementation/g1/g1CollectedHeap.cpp 2013-02-01 15:46:02.000000000 +0100 +++ new/src/share/vm/gc_implementation/g1/g1CollectedHeap.cpp 2013-02-01 15:46:02.000000000 +0100 @@ -37,6 +37,7 @@ #include "gc_implementation/g1/g1Log.hpp" #include "gc_implementation/g1/g1MarkSweep.hpp" #include "gc_implementation/g1/g1OopClosures.inline.hpp" +#include "gc_implementation/g1/g1PreserveMarkQueue.hpp" #include "gc_implementation/g1/g1RemSet.inline.hpp" #include "gc_implementation/g1/heapRegion.inline.hpp" #include "gc_implementation/g1/heapRegionRemSet.hpp" @@ -1893,7 +1894,7 @@ _ref_processor_stw(NULL), _process_strong_tasks(new SubTasksDone(G1H_PS_NumElements)), _bot_shared(NULL), - _objs_with_preserved_marks(NULL), _preserved_marks_of_objs(NULL), + _preserved_marks(40, 10000), _evac_failure_scan_stack(NULL) , _mark_in_progress(false), _cg1r(NULL), _summary_bytes_used(0), @@ -4215,21 +4216,9 @@ assert(check_cset_heap_region_claim_values(HeapRegion::InitialClaimValue), "sanity"); // Now restore saved marks, if any. - if (_objs_with_preserved_marks != NULL) { - assert(_preserved_marks_of_objs != NULL, "Both or none."); - guarantee(_objs_with_preserved_marks->length() == - _preserved_marks_of_objs->length(), "Both or none."); - for (int i = 0; i < _objs_with_preserved_marks->length(); i++) { - oop obj = _objs_with_preserved_marks->at(i); - markOop m = _preserved_marks_of_objs->at(i); - obj->set_mark(m); - } - - // Delete the preserved marks growable arrays (allocated on the C heap). - delete _objs_with_preserved_marks; - delete _preserved_marks_of_objs; - _objs_with_preserved_marks = NULL; - _preserved_marks_of_objs = NULL; + while (_preserved_marks.has_data()) { + G1PreserveMarkQueueEntry e = _preserved_marks.remove_first(); + e.obj->set_mark(e.mark); } } @@ -4313,15 +4302,7 @@ // We want to call the "for_promotion_failure" version only in the // case of a promotion failure. if (m->must_be_preserved_for_promotion_failure(obj)) { - if (_objs_with_preserved_marks == NULL) { - assert(_preserved_marks_of_objs == NULL, "Both or none."); - _objs_with_preserved_marks = - new (ResourceObj::C_HEAP, mtGC) GrowableArray(40, true); - _preserved_marks_of_objs = - new (ResourceObj::C_HEAP, mtGC) GrowableArray(40, true); - } - _objs_with_preserved_marks->push(obj); - _preserved_marks_of_objs->push(m); + _preserved_marks.append(obj, m); } } --- old/src/share/vm/gc_implementation/g1/g1CollectedHeap.hpp 2013-02-01 15:46:03.000000000 +0100 +++ new/src/share/vm/gc_implementation/g1/g1CollectedHeap.hpp 2013-02-01 15:46:02.000000000 +0100 @@ -28,8 +28,9 @@ #include "gc_implementation/g1/concurrentMark.hpp" #include "gc_implementation/g1/g1AllocRegion.hpp" #include "gc_implementation/g1/g1HRPrinter.hpp" -#include "gc_implementation/g1/g1RemSet.hpp" #include "gc_implementation/g1/g1MonitoringSupport.hpp" +#include "gc_implementation/g1/g1PreserveMarkQueue.hpp" +#include "gc_implementation/g1/g1RemSet.hpp" #include "gc_implementation/g1/heapRegionSeq.hpp" #include "gc_implementation/g1/heapRegionSets.hpp" #include "gc_implementation/shared/hSpaceCounters.hpp" @@ -877,10 +878,8 @@ // forwarding pointers to themselves. Reset them. void remove_self_forwarding_pointers(); - // When one is non-null, so is the other. Together, they each pair is - // an object with a preserved mark, and its mark value. - GrowableArray* _objs_with_preserved_marks; - GrowableArray* _preserved_marks_of_objs; + // Objects with a preserved mark, and its mark value. + G1PreserveMarkQueue _preserved_marks; // Preserve the mark of "obj", if necessary, in preparation for its mark // word being overwritten with a self-forwarding-pointer. --- old/src/share/vm/prims/jni.cpp 2013-02-01 15:46:03.000000000 +0100 +++ new/src/share/vm/prims/jni.cpp 2013-02-01 15:46:03.000000000 +0100 @@ -5039,6 +5039,7 @@ #ifndef PRODUCT +#include "gc_implementation/g1/g1PreserveMarkQueue.hpp" #include "gc_interface/collectedHeap.hpp" #include "utilities/quickSort.hpp" #if INCLUDE_VM_STRUCTS @@ -5056,6 +5057,8 @@ run_unit_test(CollectedHeap::test_is_in()); run_unit_test(QuickSort::test_quick_sort()); run_unit_test(AltHashing::test_alt_hash()); + run_unit_test(g1PreserveMarkChunkTests()); + run_unit_test(g1PreserveMarkQueueTests()); #if INCLUDE_VM_STRUCTS run_unit_test(VMStructs::test()); #endif --- /dev/null 2013-02-01 15:46:04.000000000 +0100 +++ new/src/share/vm/gc_implementation/g1/g1PreserveMarkQueue.cpp 2013-02-01 15:46:03.000000000 +0100 @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2013, 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_implementation/g1/g1PreserveMarkQueue.hpp" +#include "memory/allocation.hpp" +#include "memory/allocation.inline.hpp" +#include "oops/oopsHierarchy.hpp" + +class G1PreserveMarkChunk { + const size_t _capacity; + size_t _write_index; + size_t _read_index; + G1PreserveMarkQueueEntry* _data; + + public: + G1PreserveMarkChunk(size_t capacity) : _capacity(capacity), _write_index(0), _read_index(0), _next(NULL) { + _data = NEW_C_HEAP_ARRAY(G1PreserveMarkQueueEntry, _capacity, mtGC); + } + + ~G1PreserveMarkChunk() { + FREE_C_HEAP_ARRAY(G1PreserveMarkQueueEntry, _data, mtGC); + } + + bool has_data() { + return _write_index > _read_index; + } + + bool is_full() { + assert(_write_index <= _capacity, "written too far"); + return _write_index == _capacity; + } + + void append(const G1PreserveMarkQueueEntry& entry) { + assert(!is_full(), "overflow"); + _data[_write_index++] = entry; + } + + G1PreserveMarkQueueEntry remove_first() { + assert(has_data(), "empty"); + return _data[_read_index++]; + } + + G1PreserveMarkChunk* _next; +}; + +bool G1PreserveMarkQueue::has_data() { + return _current_read_chunk != NULL && _current_read_chunk->has_data(); +} + +void G1PreserveMarkQueue::append(const oop& obj, const markOop& mark) { + G1PreserveMarkQueueEntry entry = {obj, mark}; + if (_current_write_chunk == NULL) { + assert(_current_read_chunk == NULL, "must be"); + _current_write_chunk = new G1PreserveMarkChunk(new_chunk_capacity()); + _current_read_chunk = _current_write_chunk; + } else if (_current_write_chunk->is_full()) { + G1PreserveMarkChunk* new_chunk = new G1PreserveMarkChunk(new_chunk_capacity()); + _current_write_chunk->_next = new_chunk; + _current_write_chunk = new_chunk; + } + + assert(!_current_write_chunk->is_full(), "full"); + + _current_write_chunk->append(entry); +} + +G1PreserveMarkQueueEntry G1PreserveMarkQueue::remove_first() { + assert(has_data(), "empty"); + G1PreserveMarkQueueEntry entry = _current_read_chunk->remove_first(); + + if (!_current_read_chunk->has_data()) { + G1PreserveMarkChunk* old_chunk = _current_read_chunk; + _current_read_chunk = old_chunk->_next; + + if (old_chunk == _current_write_chunk) { + assert(_current_read_chunk == NULL, "we are deleting the last chunk"); + _current_write_chunk = NULL; + } + + delete old_chunk; + } + + return entry; +} + +/////////////// Unit tests /////////////// + +#ifndef PRODUCT + +void g1PreserveMarkChunkTests() { + const int chunk_size = 5; + + G1PreserveMarkChunk* chunk = new G1PreserveMarkChunk(chunk_size); + + assert(!chunk->is_full(), ""); + assert(!chunk->has_data(), ""); + + for (int i = 0; i < chunk_size; i++) { + G1PreserveMarkQueueEntry entry = {(oop)i, (markOop)(chunk_size - i)}; + chunk->append(entry); + } + + assert(chunk->is_full(), ""); + assert(chunk->has_data(), ""); + + for (int i = 0; i < chunk_size; i++) { + assert(chunk->has_data(), ""); + G1PreserveMarkQueueEntry entry = chunk->remove_first(); + assert((oop)i == entry.obj, ""); + assert((markOop)(chunk_size - i) == entry.mark, ""); + } + + assert(chunk->is_full(), ""); + assert(!chunk->has_data(), ""); + + delete chunk; +} + +void G1PreserveMarkQueue::verify_empty() { + assert(_current_write_chunk == NULL, "_current_write_chunk not null"); + assert(_current_read_chunk == NULL, "_current_read_chunk not null"); +} + +void g1PreserveMarkQueueUse(G1PreserveMarkQueue& queue) { + assert(!queue.has_data(), ""); + + const int num_entries = 51; + for (int i = 0; i < num_entries; i++) { + queue.append((oop)i, (markOop)(num_entries - i)); + } + + assert(queue.has_data(), ""); + + for (int i = 0; i < num_entries; i++) { + assert(queue.has_data(), ""); + G1PreserveMarkQueueEntry entry = queue.remove_first(); + assert((oop)i == entry.obj, err_msg("wrong data (" PTR_FORMAT ") at index %d", (oopDesc*)(entry.obj), i)); + assert((markOop)(num_entries - i) == entry.mark, err_msg("wrong data (" PTR_FORMAT ") at index %d", entry.mark, i)); + } + + assert(!queue.has_data(), ""); + + queue.verify_empty(); +} + +void g1PreserveMarkQueueTestOneUse() { + G1PreserveMarkQueue queue(5, 10); + g1PreserveMarkQueueUse(queue); +} + + +void g1PreserveMarkQueueTestMultiUse() { + G1PreserveMarkQueue queue(5, 10); + g1PreserveMarkQueueUse(queue); + g1PreserveMarkQueueUse(queue); + g1PreserveMarkQueueUse(queue); +} + +void g1PreserveMarkQueueTests() { + g1PreserveMarkQueueTestOneUse(); + g1PreserveMarkQueueTestMultiUse(); +} + +#endif + + --- /dev/null 2013-02-01 15:46:04.000000000 +0100 +++ new/src/share/vm/gc_implementation/g1/g1PreserveMarkQueue.hpp 2013-02-01 15:46:04.000000000 +0100 @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2013, 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. + * + */ + +#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_PRESERVEMARKQUEUE_HPP +#define SHARE_VM_GC_IMPLEMENTATION_G1_PRESERVEMARKQUEUE_HPP + +#include "oops/oopsHierarchy.hpp" + +struct G1PreserveMarkQueueEntry { + oop obj; + markOop mark; +}; + +class G1PreserveMarkChunk; + +class G1PreserveMarkQueue { + const size_t _initial_chunk_capacity; + const size_t _max_chunk_capacity; + size_t _current_capacity; + G1PreserveMarkChunk* _current_write_chunk; + G1PreserveMarkChunk* _current_read_chunk; + + public: + G1PreserveMarkQueue(size_t initial_chunk_capacity, size_t max_chunk_capacity) : + _initial_chunk_capacity(initial_chunk_capacity), _max_chunk_capacity(max_chunk_capacity), + _current_write_chunk(NULL), _current_read_chunk(NULL) { + assert(_initial_chunk_capacity <= _max_chunk_capacity, "initial must be <= max"); + } + + bool has_data(); + void append(const oop& obj, const markOop& mark); + G1PreserveMarkQueueEntry remove_first(); + + size_t new_chunk_capacity() { + if (_current_write_chunk == NULL) { + _current_capacity = _initial_chunk_capacity; + } else if (_current_capacity == _max_chunk_capacity) { + // do nothing. already at max capacity + } else { + // Grow with 50%, similar to java.util.ArrayList + _current_capacity += _current_capacity >> 1; + _current_capacity = MIN2(_current_capacity, _max_chunk_capacity); + } + + assert(_current_capacity >= _initial_chunk_capacity, "too small capacity"); + assert(_current_capacity <= _max_chunk_capacity, "too large capacity"); + + return _current_capacity; + } + + NOT_PRODUCT(void verify_empty()); +}; + +NOT_PRODUCT(void g1PreserveMarkChunkTests()); +NOT_PRODUCT(void g1PreserveMarkQueueTests()); + +#endif // SHARE_VM_GC_IMPLEMENTATION_G1_PRESERVEMARKQUEUE_HPP