ysr@777: /* hseigel@5784: * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. ysr@777: * ysr@777: */ ysr@777: stefank@2314: #include "precompiled.hpp" tschatzl@7051: #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" stefank@2314: #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp" stefank@2314: #include "gc_implementation/g1/heapRegion.hpp" stefank@2314: #include "gc_implementation/g1/satbQueue.hpp" stefank@2314: #include "runtime/mutexLocker.hpp" goetz@6911: #include "runtime/orderAccess.inline.hpp" stefank@4299: #include "runtime/thread.inline.hpp" ysr@777: ysr@777: G1SATBCardTableModRefBS::G1SATBCardTableModRefBS(MemRegion whole_heap, ysr@777: int max_covered_regions) : ysr@777: CardTableModRefBSForCTRS(whole_heap, max_covered_regions) ysr@777: { ysr@777: _kind = G1SATBCT; ysr@777: } ysr@777: ysr@777: void G1SATBCardTableModRefBS::enqueue(oop pre_val) { johnc@2781: // Nulls should have been already filtered. johnc@2781: assert(pre_val->is_oop(true), "Error"); johnc@2781: tonyp@1752: if (!JavaThread::satb_mark_queue_set().is_active()) return; ysr@777: Thread* thr = Thread::current(); ysr@777: if (thr->is_Java_thread()) { ysr@777: JavaThread* jt = (JavaThread*)thr; ysr@777: jt->satb_mark_queue().enqueue(pre_val); ysr@777: } else { brutisso@5341: MutexLockerEx x(Shared_SATB_Q_lock, Mutex::_no_safepoint_check_flag); ysr@777: JavaThread::satb_mark_queue_set().shared_satb_queue()->enqueue(pre_val); ysr@777: } ysr@777: } ysr@777: ysr@1280: template void ysr@1280: G1SATBCardTableModRefBS::write_ref_array_pre_work(T* dst, int count) { tonyp@1752: if (!JavaThread::satb_mark_queue_set().is_active()) return; ysr@1280: T* elem_ptr = dst; ysr@1280: for (int i = 0; i < count; i++, elem_ptr++) { ysr@1280: T heap_oop = oopDesc::load_heap_oop(elem_ptr); ysr@1280: if (!oopDesc::is_null(heap_oop)) { ysr@1280: enqueue(oopDesc::decode_heap_oop_not_null(heap_oop)); ysr@1280: } ysr@777: } ysr@777: } ysr@777: mgerdin@6989: void G1SATBCardTableModRefBS::write_ref_array_pre(oop* dst, int count, bool dest_uninitialized) { mgerdin@6989: if (!dest_uninitialized) { mgerdin@6989: write_ref_array_pre_work(dst, count); mgerdin@6989: } mgerdin@6989: } mgerdin@6989: void G1SATBCardTableModRefBS::write_ref_array_pre(narrowOop* dst, int count, bool dest_uninitialized) { mgerdin@6989: if (!dest_uninitialized) { mgerdin@6989: write_ref_array_pre_work(dst, count); mgerdin@6989: } mgerdin@6989: } mgerdin@6989: mgerdin@5811: bool G1SATBCardTableModRefBS::mark_card_deferred(size_t card_index) { mgerdin@5811: jbyte val = _byte_map[card_index]; mgerdin@5811: // It's already processed mgerdin@5811: if ((val & (clean_card_mask_val() | deferred_card_val())) == deferred_card_val()) { mgerdin@5811: return false; mgerdin@5811: } mgerdin@5860: mgerdin@5860: if (val == g1_young_gen) { mgerdin@5860: // the card is for a young gen region. We don't need to keep track of all pointers into young mgerdin@5860: return false; mgerdin@5860: } mgerdin@5860: mgerdin@5811: // Cached bit can be installed either on a clean card or on a claimed card. mgerdin@5811: jbyte new_val = val; mgerdin@5811: if (val == clean_card_val()) { mgerdin@5811: new_val = (jbyte)deferred_card_val(); mgerdin@5811: } else { mgerdin@5811: if (val & claimed_card_val()) { mgerdin@5811: new_val = val | (jbyte)deferred_card_val(); mgerdin@5811: } mgerdin@5811: } mgerdin@5811: if (new_val != val) { mgerdin@5811: Atomic::cmpxchg(new_val, &_byte_map[card_index], val); mgerdin@5811: } mgerdin@5811: return true; mgerdin@5811: } mgerdin@5811: mgerdin@5860: void G1SATBCardTableModRefBS::g1_mark_as_young(const MemRegion& mr) { mgerdin@5860: jbyte *const first = byte_for(mr.start()); mgerdin@5860: jbyte *const last = byte_after(mr.last()); mgerdin@5860: pliden@6694: // Below we may use an explicit loop instead of memset() because on pliden@6694: // certain platforms memset() can give concurrent readers phantom zeros. pliden@6694: if (UseMemSetInBOT) { pliden@6694: memset(first, g1_young_gen, last - first); pliden@6694: } else { pliden@6694: for (jbyte* i = first; i < last; i++) { pliden@6694: *i = g1_young_gen; pliden@6694: } pliden@6694: } mgerdin@5860: } mgerdin@5860: mgerdin@5860: #ifndef PRODUCT mgerdin@5860: void G1SATBCardTableModRefBS::verify_g1_young_region(MemRegion mr) { mgerdin@5860: verify_region(mr, g1_young_gen, true); mgerdin@5860: } mgerdin@5860: #endif mgerdin@5860: tschatzl@7257: void G1SATBCardTableLoggingModRefBSChangedListener::on_commit(uint start_idx, size_t num_regions, bool zero_filled) { tschatzl@7257: // Default value for a clean card on the card table is -1. So we cannot take advantage of the zero_filled parameter. tschatzl@7051: MemRegion mr(G1CollectedHeap::heap()->bottom_addr_for_region(start_idx), num_regions * HeapRegion::GrainWords); tschatzl@7051: _card_table->clear(mr); tschatzl@7051: } tschatzl@7051: ysr@777: G1SATBCardTableLoggingModRefBS:: ysr@777: G1SATBCardTableLoggingModRefBS(MemRegion whole_heap, ysr@777: int max_covered_regions) : ysr@777: G1SATBCardTableModRefBS(whole_heap, max_covered_regions), tschatzl@7051: _dcqs(JavaThread::dirty_card_queue_set()), tschatzl@7051: _listener() ysr@777: { ysr@777: _kind = G1SATBCTLogging; tschatzl@7051: _listener.set_card_table(this); tschatzl@7051: } tschatzl@7051: tschatzl@7051: void G1SATBCardTableLoggingModRefBS::initialize(G1RegionToSpaceMapper* mapper) { tschatzl@7051: mapper->set_mapping_changed_listener(&_listener); tschatzl@7051: tschatzl@7051: _byte_map_size = mapper->reserved().byte_size(); tschatzl@7051: tschatzl@7051: _guard_index = cards_required(_whole_heap.word_size()) - 1; tschatzl@7051: _last_valid_index = _guard_index - 1; tschatzl@7051: tschatzl@7051: HeapWord* low_bound = _whole_heap.start(); tschatzl@7051: HeapWord* high_bound = _whole_heap.end(); tschatzl@7051: tschatzl@7051: _cur_covered_regions = 1; tschatzl@7051: _covered[0] = _whole_heap; tschatzl@7051: tschatzl@7051: _byte_map = (jbyte*) mapper->reserved().start(); tschatzl@7051: byte_map_base = _byte_map - (uintptr_t(low_bound) >> card_shift); tschatzl@7051: assert(byte_for(low_bound) == &_byte_map[0], "Checking start of map"); tschatzl@7051: assert(byte_for(high_bound-1) <= &_byte_map[_last_valid_index], "Checking end of map"); tschatzl@7051: tschatzl@7051: if (TraceCardTableModRefBS) { tschatzl@7051: gclog_or_tty->print_cr("G1SATBCardTableModRefBS::G1SATBCardTableModRefBS: "); tschatzl@7051: gclog_or_tty->print_cr(" " tschatzl@7051: " &_byte_map[0]: " INTPTR_FORMAT tschatzl@7051: " &_byte_map[_last_valid_index]: " INTPTR_FORMAT, tschatzl@7051: p2i(&_byte_map[0]), tschatzl@7051: p2i(&_byte_map[_last_valid_index])); tschatzl@7051: gclog_or_tty->print_cr(" " tschatzl@7051: " byte_map_base: " INTPTR_FORMAT, tschatzl@7051: p2i(byte_map_base)); tschatzl@7051: } ysr@777: } ysr@777: ysr@777: void ysr@777: G1SATBCardTableLoggingModRefBS::write_ref_field_work(void* field, goetz@6493: oop new_val, goetz@6493: bool release) { mgerdin@5860: volatile jbyte* byte = byte_for(field); mgerdin@5860: if (*byte == g1_young_gen) { mgerdin@5860: return; mgerdin@5860: } mgerdin@5860: OrderAccess::storeload(); ysr@777: if (*byte != dirty_card) { ysr@777: *byte = dirty_card; ysr@777: Thread* thr = Thread::current(); ysr@777: if (thr->is_Java_thread()) { ysr@777: JavaThread* jt = (JavaThread*)thr; ysr@777: jt->dirty_card_queue().enqueue(byte); ysr@777: } else { ysr@777: MutexLockerEx x(Shared_DirtyCardQ_lock, ysr@777: Mutex::_no_safepoint_check_flag); ysr@777: _dcqs.shared_dirty_card_queue()->enqueue(byte); ysr@777: } ysr@777: } ysr@777: } ysr@777: ysr@777: void ysr@777: G1SATBCardTableLoggingModRefBS::write_ref_field_static(void* field, ysr@777: oop new_val) { ysr@777: uintptr_t field_uint = (uintptr_t)field; hseigel@5784: uintptr_t new_val_uint = cast_from_oop(new_val); ysr@777: uintptr_t comb = field_uint ^ new_val_uint; ysr@777: comb = comb >> HeapRegion::LogOfHRGrainBytes; ysr@777: if (comb == 0) return; ysr@777: if (new_val == NULL) return; ysr@777: // Otherwise, log it. ysr@777: G1SATBCardTableLoggingModRefBS* g1_bs = ysr@777: (G1SATBCardTableLoggingModRefBS*)Universe::heap()->barrier_set(); ysr@777: g1_bs->write_ref_field_work(field, new_val); ysr@777: } ysr@777: ysr@777: void ysr@777: G1SATBCardTableLoggingModRefBS::invalidate(MemRegion mr, bool whole_heap) { mgerdin@5860: volatile jbyte* byte = byte_for(mr.start()); ysr@777: jbyte* last_byte = byte_for(mr.last()); ysr@777: Thread* thr = Thread::current(); ysr@777: if (whole_heap) { ysr@777: while (byte <= last_byte) { ysr@777: *byte = dirty_card; ysr@777: byte++; ysr@777: } ysr@777: } else { mgerdin@5860: // skip all consecutive young cards mgerdin@5860: for (; byte <= last_byte && *byte == g1_young_gen; byte++); mgerdin@5860: mgerdin@5860: if (byte <= last_byte) { mgerdin@5860: OrderAccess::storeload(); mgerdin@5860: // Enqueue if necessary. mgerdin@5860: if (thr->is_Java_thread()) { mgerdin@5860: JavaThread* jt = (JavaThread*)thr; mgerdin@5860: for (; byte <= last_byte; byte++) { mgerdin@5860: if (*byte == g1_young_gen) { mgerdin@5860: continue; mgerdin@5860: } mgerdin@5860: if (*byte != dirty_card) { mgerdin@5860: *byte = dirty_card; mgerdin@5860: jt->dirty_card_queue().enqueue(byte); mgerdin@5860: } ysr@777: } mgerdin@5860: } else { mgerdin@5860: MutexLockerEx x(Shared_DirtyCardQ_lock, mgerdin@5860: Mutex::_no_safepoint_check_flag); mgerdin@5860: for (; byte <= last_byte; byte++) { mgerdin@5860: if (*byte == g1_young_gen) { mgerdin@5860: continue; mgerdin@5860: } mgerdin@5860: if (*byte != dirty_card) { mgerdin@5860: *byte = dirty_card; mgerdin@5860: _dcqs.shared_dirty_card_queue()->enqueue(byte); mgerdin@5860: } ysr@777: } ysr@777: } ysr@777: } ysr@777: } ysr@777: }