Path: blob/master/src/hotspot/share/gc/parallel/psCardTable.cpp
41149 views
/*1* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "gc/parallel/objectStartArray.inline.hpp"26#include "gc/parallel/parallelScavengeHeap.inline.hpp"27#include "gc/parallel/psCardTable.hpp"28#include "gc/parallel/psPromotionManager.inline.hpp"29#include "gc/parallel/psScavenge.inline.hpp"30#include "gc/parallel/psYoungGen.hpp"31#include "memory/iterator.inline.hpp"32#include "oops/access.inline.hpp"33#include "oops/oop.inline.hpp"34#include "runtime/prefetch.inline.hpp"35#include "utilities/align.hpp"3637// Checks an individual oop for missing precise marks. Mark38// may be either dirty or newgen.39class CheckForUnmarkedOops : public BasicOopIterateClosure {40private:41PSYoungGen* _young_gen;42PSCardTable* _card_table;43HeapWord* _unmarked_addr;4445protected:46template <class T> void do_oop_work(T* p) {47oop obj = RawAccess<>::oop_load(p);48if (_young_gen->is_in_reserved(obj) &&49!_card_table->addr_is_marked_imprecise(p)) {50// Don't overwrite the first missing card mark51if (_unmarked_addr == NULL) {52_unmarked_addr = (HeapWord*)p;53}54}55}5657public:58CheckForUnmarkedOops(PSYoungGen* young_gen, PSCardTable* card_table) :59_young_gen(young_gen), _card_table(card_table), _unmarked_addr(NULL) { }6061virtual void do_oop(oop* p) { CheckForUnmarkedOops::do_oop_work(p); }62virtual void do_oop(narrowOop* p) { CheckForUnmarkedOops::do_oop_work(p); }6364bool has_unmarked_oop() {65return _unmarked_addr != NULL;66}67};6869// Checks all objects for the existence of some type of mark,70// precise or imprecise, dirty or newgen.71class CheckForUnmarkedObjects : public ObjectClosure {72private:73PSYoungGen* _young_gen;74PSCardTable* _card_table;7576public:77CheckForUnmarkedObjects() {78ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();79_young_gen = heap->young_gen();80_card_table = heap->card_table();81}8283// Card marks are not precise. The current system can leave us with84// a mismatch of precise marks and beginning of object marks. This means85// we test for missing precise marks first. If any are found, we don't86// fail unless the object head is also unmarked.87virtual void do_object(oop obj) {88CheckForUnmarkedOops object_check(_young_gen, _card_table);89obj->oop_iterate(&object_check);90if (object_check.has_unmarked_oop()) {91guarantee(_card_table->addr_is_marked_imprecise(obj), "Found unmarked young_gen object");92}93}94};9596// Checks for precise marking of oops as newgen.97class CheckForPreciseMarks : public BasicOopIterateClosure {98private:99PSYoungGen* _young_gen;100PSCardTable* _card_table;101102protected:103template <class T> void do_oop_work(T* p) {104oop obj = RawAccess<IS_NOT_NULL>::oop_load(p);105if (_young_gen->is_in_reserved(obj)) {106assert(_card_table->addr_is_marked_precise(p), "Found unmarked precise oop");107_card_table->set_card_newgen(p);108}109}110111public:112CheckForPreciseMarks(PSYoungGen* young_gen, PSCardTable* card_table) :113_young_gen(young_gen), _card_table(card_table) { }114115virtual void do_oop(oop* p) { CheckForPreciseMarks::do_oop_work(p); }116virtual void do_oop(narrowOop* p) { CheckForPreciseMarks::do_oop_work(p); }117};118119// We get passed the space_top value to prevent us from traversing into120// the old_gen promotion labs, which cannot be safely parsed.121122// Do not call this method if the space is empty.123// It is a waste to start tasks and get here only to124// do no work. If this method needs to be called125// when the space is empty, fix the calculation of126// end_card to allow sp_top == sp->bottom().127128// The generation (old gen) is divided into slices, which are further129// subdivided into stripes, with one stripe per GC thread. The size of130// a stripe is a constant, ssize.131//132// +===============+ slice 0133// | stripe 0 |134// +---------------+135// | stripe 1 |136// +---------------+137// | stripe 2 |138// +---------------+139// | stripe 3 |140// +===============+ slice 1141// | stripe 0 |142// +---------------+143// | stripe 1 |144// +---------------+145// | stripe 2 |146// +---------------+147// | stripe 3 |148// +===============+ slice 2149// ...150//151// In this case there are 4 threads, so 4 stripes. A GC thread first works on152// its stripe within slice 0 and then moves to its stripe in the next slice153// until it has exceeded the top of the generation. The distance to stripe in154// the next slice is calculated based on the number of stripes. The next155// stripe is at ssize * number_of_stripes (= slice_stride).. So after156// finishing stripe 0 in slice 0, the thread finds the stripe 0 in slice1 by157// adding slice_stride to the start of stripe 0 in slice 0 to get to the start158// of stride 0 in slice 1.159160void PSCardTable::scavenge_contents_parallel(ObjectStartArray* start_array,161MutableSpace* sp,162HeapWord* space_top,163PSPromotionManager* pm,164uint stripe_number,165uint stripe_total) {166int ssize = 128; // Naked constant! Work unit = 64k.167int dirty_card_count = 0;168169// It is a waste to get here if empty.170assert(sp->bottom() < sp->top(), "Should not be called if empty");171oop* sp_top = (oop*)space_top;172CardValue* start_card = byte_for(sp->bottom());173CardValue* end_card = byte_for(sp_top - 1) + 1;174oop* last_scanned = NULL; // Prevent scanning objects more than once175// The width of the stripe ssize*stripe_total must be176// consistent with the number of stripes so that the complete slice177// is covered.178size_t slice_width = ssize * stripe_total;179for (CardValue* slice = start_card; slice < end_card; slice += slice_width) {180CardValue* worker_start_card = slice + stripe_number * ssize;181if (worker_start_card >= end_card)182return; // We're done.183184CardValue* worker_end_card = worker_start_card + ssize;185if (worker_end_card > end_card)186worker_end_card = end_card;187188// We do not want to scan objects more than once. In order to accomplish189// this, we assert that any object with an object head inside our 'slice'190// belongs to us. We may need to extend the range of scanned cards if the191// last object continues into the next 'slice'.192//193// Note! ending cards are exclusive!194HeapWord* slice_start = addr_for(worker_start_card);195HeapWord* slice_end = MIN2((HeapWord*) sp_top, addr_for(worker_end_card));196197#ifdef ASSERT198if (GCWorkerDelayMillis > 0) {199// Delay 1 worker so that it proceeds after all the work200// has been completed.201if (stripe_number < 2) {202os::naked_sleep(GCWorkerDelayMillis);203}204}205#endif206207// If there are not objects starting within the chunk, skip it.208if (!start_array->object_starts_in_range(slice_start, slice_end)) {209continue;210}211// Update our beginning addr212HeapWord* first_object = start_array->object_start(slice_start);213debug_only(oop* first_object_within_slice = (oop*) first_object;)214if (first_object < slice_start) {215last_scanned = (oop*)(first_object + cast_to_oop(first_object)->size());216debug_only(first_object_within_slice = last_scanned;)217worker_start_card = byte_for(last_scanned);218}219220// Update the ending addr221if (slice_end < (HeapWord*)sp_top) {222// The subtraction is important! An object may start precisely at slice_end.223HeapWord* last_object = start_array->object_start(slice_end - 1);224slice_end = last_object + cast_to_oop(last_object)->size();225// worker_end_card is exclusive, so bump it one past the end of last_object's226// covered span.227worker_end_card = byte_for(slice_end) + 1;228229if (worker_end_card > end_card)230worker_end_card = end_card;231}232233assert(slice_end <= (HeapWord*)sp_top, "Last object in slice crosses space boundary");234assert(is_valid_card_address(worker_start_card), "Invalid worker start card");235assert(is_valid_card_address(worker_end_card), "Invalid worker end card");236// Note that worker_start_card >= worker_end_card is legal, and happens when237// an object spans an entire slice.238assert(worker_start_card <= end_card, "worker start card beyond end card");239assert(worker_end_card <= end_card, "worker end card beyond end card");240241CardValue* current_card = worker_start_card;242while (current_card < worker_end_card) {243// Find an unclean card.244while (current_card < worker_end_card && card_is_clean(*current_card)) {245current_card++;246}247CardValue* first_unclean_card = current_card;248249// Find the end of a run of contiguous unclean cards250while (current_card < worker_end_card && !card_is_clean(*current_card)) {251while (current_card < worker_end_card && !card_is_clean(*current_card)) {252current_card++;253}254255if (current_card < worker_end_card) {256// Some objects may be large enough to span several cards. If such257// an object has more than one dirty card, separated by a clean card,258// we will attempt to scan it twice. The test against "last_scanned"259// prevents the redundant object scan, but it does not prevent newly260// marked cards from being cleaned.261HeapWord* last_object_in_dirty_region = start_array->object_start(addr_for(current_card)-1);262size_t size_of_last_object = cast_to_oop(last_object_in_dirty_region)->size();263HeapWord* end_of_last_object = last_object_in_dirty_region + size_of_last_object;264CardValue* ending_card_of_last_object = byte_for(end_of_last_object);265assert(ending_card_of_last_object <= worker_end_card, "ending_card_of_last_object is greater than worker_end_card");266if (ending_card_of_last_object > current_card) {267// This means the object spans the next complete card.268// We need to bump the current_card to ending_card_of_last_object269current_card = ending_card_of_last_object;270}271}272}273CardValue* following_clean_card = current_card;274275if (first_unclean_card < worker_end_card) {276oop* p = (oop*) start_array->object_start(addr_for(first_unclean_card));277assert((HeapWord*)p <= addr_for(first_unclean_card), "checking");278// "p" should always be >= "last_scanned" because newly GC dirtied279// cards are no longer scanned again (see comment at end280// of loop on the increment of "current_card"). Test that281// hypothesis before removing this code.282// If this code is removed, deal with the first time through283// the loop when the last_scanned is the object starting in284// the previous slice.285assert((p >= last_scanned) ||286(last_scanned == first_object_within_slice),287"Should no longer be possible");288if (p < last_scanned) {289// Avoid scanning more than once; this can happen because290// newgen cards set by GC may a different set than the291// originally dirty set292p = last_scanned;293}294oop* to = (oop*)addr_for(following_clean_card);295296// Test slice_end first!297if ((HeapWord*)to > slice_end) {298to = (oop*)slice_end;299} else if (to > sp_top) {300to = sp_top;301}302303// we know which cards to scan, now clear them304if (first_unclean_card <= worker_start_card+1)305first_unclean_card = worker_start_card+1;306if (following_clean_card >= worker_end_card-1)307following_clean_card = worker_end_card-1;308309while (first_unclean_card < following_clean_card) {310*first_unclean_card++ = clean_card;311}312313const int interval = PrefetchScanIntervalInBytes;314// scan all objects in the range315if (interval != 0) {316while (p < to) {317Prefetch::write(p, interval);318oop m = cast_to_oop(p);319assert(oopDesc::is_oop_or_null(m), "Expected an oop or NULL for header field at " PTR_FORMAT, p2i(m));320pm->push_contents(m);321p += m->size();322}323pm->drain_stacks_cond_depth();324} else {325while (p < to) {326oop m = cast_to_oop(p);327assert(oopDesc::is_oop_or_null(m), "Expected an oop or NULL for header field at " PTR_FORMAT, p2i(m));328pm->push_contents(m);329p += m->size();330}331pm->drain_stacks_cond_depth();332}333last_scanned = p;334}335// "current_card" is still the "following_clean_card" or336// the current_card is >= the worker_end_card so the337// loop will not execute again.338assert((current_card == following_clean_card) ||339(current_card >= worker_end_card),340"current_card should only be incremented if it still equals "341"following_clean_card");342// Increment current_card so that it is not processed again.343// It may now be dirty because a old-to-young pointer was344// found on it an updated. If it is now dirty, it cannot be345// be safely cleaned in the next iteration.346current_card++;347}348}349}350351// This should be called before a scavenge.352void PSCardTable::verify_all_young_refs_imprecise() {353CheckForUnmarkedObjects check;354355ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();356PSOldGen* old_gen = heap->old_gen();357358old_gen->object_iterate(&check);359}360361// This should be called immediately after a scavenge, before mutators resume.362void PSCardTable::verify_all_young_refs_precise() {363ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();364PSOldGen* old_gen = heap->old_gen();365366CheckForPreciseMarks check(heap->young_gen(), this);367368old_gen->oop_iterate(&check);369370verify_all_young_refs_precise_helper(old_gen->object_space()->used_region());371}372373void PSCardTable::verify_all_young_refs_precise_helper(MemRegion mr) {374CardValue* bot = byte_for(mr.start());375CardValue* top = byte_for(mr.end());376while (bot <= top) {377assert(*bot == clean_card || *bot == verify_card, "Found unwanted or unknown card mark");378if (*bot == verify_card)379*bot = youngergen_card;380bot++;381}382}383384bool PSCardTable::addr_is_marked_imprecise(void *addr) {385CardValue* p = byte_for(addr);386CardValue val = *p;387388if (card_is_dirty(val))389return true;390391if (card_is_newgen(val))392return true;393394if (card_is_clean(val))395return false;396397assert(false, "Found unhandled card mark type");398399return false;400}401402// Also includes verify_card403bool PSCardTable::addr_is_marked_precise(void *addr) {404CardValue* p = byte_for(addr);405CardValue val = *p;406407if (card_is_newgen(val))408return true;409410if (card_is_verify(val))411return true;412413if (card_is_clean(val))414return false;415416if (card_is_dirty(val))417return false;418419assert(false, "Found unhandled card mark type");420421return false;422}423424// Assumes that only the base or the end changes. This allows indentification425// of the region that is being resized. The426// CardTable::resize_covered_region() is used for the normal case427// where the covered regions are growing or shrinking at the high end.428// The method resize_covered_region_by_end() is analogous to429// CardTable::resize_covered_region() but430// for regions that grow or shrink at the low end.431void PSCardTable::resize_covered_region(MemRegion new_region) {432for (int i = 0; i < _cur_covered_regions; i++) {433if (_covered[i].start() == new_region.start()) {434// Found a covered region with the same start as the435// new region. The region is growing or shrinking436// from the start of the region.437resize_covered_region_by_start(new_region);438return;439}440if (_covered[i].start() > new_region.start()) {441break;442}443}444445int changed_region = -1;446for (int j = 0; j < _cur_covered_regions; j++) {447if (_covered[j].end() == new_region.end()) {448changed_region = j;449// This is a case where the covered region is growing or shrinking450// at the start of the region.451assert(changed_region != -1, "Don't expect to add a covered region");452assert(_covered[changed_region].byte_size() != new_region.byte_size(),453"The sizes should be different here");454resize_covered_region_by_end(changed_region, new_region);455return;456}457}458// This should only be a new covered region (where no existing459// covered region matches at the start or the end).460assert(_cur_covered_regions < _max_covered_regions,461"An existing region should have been found");462resize_covered_region_by_start(new_region);463}464465void PSCardTable::resize_covered_region_by_start(MemRegion new_region) {466CardTable::resize_covered_region(new_region);467debug_only(verify_guard();)468}469470void PSCardTable::resize_covered_region_by_end(int changed_region,471MemRegion new_region) {472assert(SafepointSynchronize::is_at_safepoint(),473"Only expect an expansion at the low end at a GC");474debug_only(verify_guard();)475#ifdef ASSERT476for (int k = 0; k < _cur_covered_regions; k++) {477if (_covered[k].end() == new_region.end()) {478assert(changed_region == k, "Changed region is incorrect");479break;480}481}482#endif483484// Commit new or uncommit old pages, if necessary.485if (resize_commit_uncommit(changed_region, new_region)) {486// Set the new start of the committed region487resize_update_committed_table(changed_region, new_region);488}489490// Update card table entries491resize_update_card_table_entries(changed_region, new_region);492493// Update the covered region494resize_update_covered_table(changed_region, new_region);495496int ind = changed_region;497log_trace(gc, barrier)("CardTable::resize_covered_region: ");498log_trace(gc, barrier)(" _covered[%d].start(): " INTPTR_FORMAT " _covered[%d].last(): " INTPTR_FORMAT,499ind, p2i(_covered[ind].start()), ind, p2i(_covered[ind].last()));500log_trace(gc, barrier)(" _committed[%d].start(): " INTPTR_FORMAT " _committed[%d].last(): " INTPTR_FORMAT,501ind, p2i(_committed[ind].start()), ind, p2i(_committed[ind].last()));502log_trace(gc, barrier)(" byte_for(start): " INTPTR_FORMAT " byte_for(last): " INTPTR_FORMAT,503p2i(byte_for(_covered[ind].start())), p2i(byte_for(_covered[ind].last())));504log_trace(gc, barrier)(" addr_for(start): " INTPTR_FORMAT " addr_for(last): " INTPTR_FORMAT,505p2i(addr_for((CardValue*) _committed[ind].start())), p2i(addr_for((CardValue*) _committed[ind].last())));506507debug_only(verify_guard();)508}509510bool PSCardTable::resize_commit_uncommit(int changed_region,511MemRegion new_region) {512bool result = false;513// Commit new or uncommit old pages, if necessary.514MemRegion cur_committed = _committed[changed_region];515assert(_covered[changed_region].end() == new_region.end(),516"The ends of the regions are expected to match");517// Extend the start of this _committed region to518// to cover the start of any previous _committed region.519// This forms overlapping regions, but never interior regions.520HeapWord* min_prev_start = lowest_prev_committed_start(changed_region);521if (min_prev_start < cur_committed.start()) {522// Only really need to set start of "cur_committed" to523// the new start (min_prev_start) but assertion checking code524// below use cur_committed.end() so make it correct.525MemRegion new_committed =526MemRegion(min_prev_start, cur_committed.end());527cur_committed = new_committed;528}529#ifdef ASSERT530ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();531assert(cur_committed.start() == align_up(cur_committed.start(), os::vm_page_size()),532"Starts should have proper alignment");533#endif534535CardValue* new_start = byte_for(new_region.start());536// Round down because this is for the start address537HeapWord* new_start_aligned = align_down((HeapWord*)new_start, os::vm_page_size());538// The guard page is always committed and should not be committed over.539// This method is used in cases where the generation is growing toward540// lower addresses but the guard region is still at the end of the541// card table. That still makes sense when looking for writes542// off the end of the card table.543if (new_start_aligned < cur_committed.start()) {544// Expand the committed region545//546// Case A547// |+ guard +|548// |+ cur committed +++++++++|549// |+ new committed +++++++++++++++++|550//551// Case B552// |+ guard +|553// |+ cur committed +|554// |+ new committed +++++++|555//556// These are not expected because the calculation of the557// cur committed region and the new committed region558// share the same end for the covered region.559// Case C560// |+ guard +|561// |+ cur committed +|562// |+ new committed +++++++++++++++++|563// Case D564// |+ guard +|565// |+ cur committed +++++++++++|566// |+ new committed +++++++|567568HeapWord* new_end_for_commit =569MIN2(cur_committed.end(), _guard_region.start());570if(new_start_aligned < new_end_for_commit) {571MemRegion new_committed =572MemRegion(new_start_aligned, new_end_for_commit);573os::commit_memory_or_exit((char*)new_committed.start(),574new_committed.byte_size(), !ExecMem,575"card table expansion");576}577result = true;578} else if (new_start_aligned > cur_committed.start()) {579// Shrink the committed region580#if 0 // uncommitting space is currently unsafe because of the interactions581// of growing and shrinking regions. One region A can uncommit space582// that it owns but which is being used by another region B (maybe).583// Region B has not committed the space because it was already584// committed by region A.585MemRegion uncommit_region = committed_unique_to_self(changed_region,586MemRegion(cur_committed.start(), new_start_aligned));587if (!uncommit_region.is_empty()) {588if (!os::uncommit_memory((char*)uncommit_region.start(),589uncommit_region.byte_size())) {590// If the uncommit fails, ignore it. Let the591// committed table resizing go even though the committed592// table will over state the committed space.593}594}595#else596assert(!result, "Should be false with current workaround");597#endif598}599assert(_committed[changed_region].end() == cur_committed.end(),600"end should not change");601return result;602}603604void PSCardTable::resize_update_committed_table(int changed_region,605MemRegion new_region) {606607CardValue* new_start = byte_for(new_region.start());608// Set the new start of the committed region609HeapWord* new_start_aligned = align_down((HeapWord*)new_start, os::vm_page_size());610MemRegion new_committed = MemRegion(new_start_aligned,611_committed[changed_region].end());612_committed[changed_region] = new_committed;613_committed[changed_region].set_start(new_start_aligned);614}615616void PSCardTable::resize_update_card_table_entries(int changed_region,617MemRegion new_region) {618debug_only(verify_guard();)619MemRegion original_covered = _covered[changed_region];620// Initialize the card entries. Only consider the621// region covered by the card table (_whole_heap)622CardValue* entry;623if (new_region.start() < _whole_heap.start()) {624entry = byte_for(_whole_heap.start());625} else {626entry = byte_for(new_region.start());627}628CardValue* end = byte_for(original_covered.start());629// If _whole_heap starts at the original covered regions start,630// this loop will not execute.631while (entry < end) { *entry++ = clean_card; }632}633634void PSCardTable::resize_update_covered_table(int changed_region,635MemRegion new_region) {636// Update the covered region637_covered[changed_region].set_start(new_region.start());638_covered[changed_region].set_word_size(new_region.word_size());639640// reorder regions. There should only be at most 1 out641// of order.642for (int i = _cur_covered_regions-1 ; i > 0; i--) {643if (_covered[i].start() < _covered[i-1].start()) {644MemRegion covered_mr = _covered[i-1];645_covered[i-1] = _covered[i];646_covered[i] = covered_mr;647MemRegion committed_mr = _committed[i-1];648_committed[i-1] = _committed[i];649_committed[i] = committed_mr;650break;651}652}653#ifdef ASSERT654for (int m = 0; m < _cur_covered_regions-1; m++) {655assert(_covered[m].start() <= _covered[m+1].start(),656"Covered regions out of order");657assert(_committed[m].start() <= _committed[m+1].start(),658"Committed regions out of order");659}660#endif661}662663// Returns the start of any committed region that is lower than664// the target committed region (index ind) and that intersects the665// target region. If none, return start of target region.666//667// -------------668// | |669// -------------670// ------------671// | target |672// ------------673// -------------674// | |675// -------------676// ^ returns this677//678// -------------679// | |680// -------------681// ------------682// | target |683// ------------684// -------------685// | |686// -------------687// ^ returns this688689HeapWord* PSCardTable::lowest_prev_committed_start(int ind) const {690assert(_cur_covered_regions >= 0, "Expecting at least on region");691HeapWord* min_start = _committed[ind].start();692for (int j = 0; j < ind; j++) {693HeapWord* this_start = _committed[j].start();694if ((this_start < min_start) &&695!(_committed[j].intersection(_committed[ind])).is_empty()) {696min_start = this_start;697}698}699return min_start;700}701702bool PSCardTable::is_in_young(oop obj) const {703return ParallelScavengeHeap::heap()->is_in_young(obj);704}705706707