Path: blob/master/test/hotspot/gtest/gc/g1/test_heapRegion.cpp
41152 views
/*1* Copyright (c) 2017, 2018, 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*/2223#include "precompiled.hpp"24#include "gc/g1/g1BlockOffsetTable.hpp"25#include "gc/g1/g1CollectedHeap.hpp"26#include "gc/g1/g1ConcurrentMarkBitMap.inline.hpp"27#include "gc/g1/heapRegion.inline.hpp"28#include "gc/shared/referenceProcessor.hpp"29#include "runtime/interfaceSupport.inline.hpp"30#include "runtime/vmOperations.hpp"31#include "runtime/vmThread.hpp"32#include "unittest.hpp"3334class VerifyAndCountMarkClosure : public StackObj {35int _count;36G1CMBitMap* _bm;3738void ensure_marked(HeapWord* addr) {39ASSERT_TRUE(_bm->is_marked(addr));40}4142public:43VerifyAndCountMarkClosure(G1CMBitMap* bm) : _count(0), _bm(bm) { }4445virtual size_t apply(oop object) {46_count++;47ensure_marked(cast_from_oop<HeapWord*>(object));48// Must return positive size to advance the iteration.49return MinObjAlignment;50}5152void reset() {53_count = 0;54}5556int count() {57return _count;58}59};6061#define MARK_OFFSET_1 ( 17 * MinObjAlignment)62#define MARK_OFFSET_2 ( 99 * MinObjAlignment)63#define MARK_OFFSET_3 (337 * MinObjAlignment)6465class VM_HeapRegionApplyToMarkedObjectsTest : public VM_GTestExecuteAtSafepoint {66public:67void doit();68};6970void VM_HeapRegionApplyToMarkedObjectsTest::doit() {71G1CollectedHeap* heap = G1CollectedHeap::heap();7273// Using region 0 for testing.74HeapRegion* region = heap->heap_region_containing(heap->bottom_addr_for_region(0));7576// Mark some "oops" in the bitmap.77G1CMBitMap* bitmap = heap->concurrent_mark()->next_mark_bitmap();78bitmap->mark(region->bottom());79bitmap->mark(region->bottom() + MARK_OFFSET_1);80bitmap->mark(region->bottom() + MARK_OFFSET_2);81bitmap->mark(region->bottom() + MARK_OFFSET_3);82bitmap->mark(region->end());8384VerifyAndCountMarkClosure cl(bitmap);8586HeapWord* old_top = region->top();8788// When top is equal to bottom the closure should not be89// applied to any object because apply_to_marked_objects90// will stop at HeapRegion::scan_limit which is equal to top.91region->set_top(region->bottom());92region->apply_to_marked_objects(bitmap, &cl);93EXPECT_EQ(0, cl.count());94cl.reset();9596// Set top to offset_1 and expect only to find 1 entry (bottom)97region->set_top(region->bottom() + MARK_OFFSET_1);98region->apply_to_marked_objects(bitmap, &cl);99EXPECT_EQ(1, cl.count());100cl.reset();101102// Set top to (offset_2 + 1) and expect only to find 3103// entries (bottom, offset_1 and offset_2)104region->set_top(region->bottom() + MARK_OFFSET_2 + MinObjAlignment);105region->apply_to_marked_objects(bitmap, &cl);106EXPECT_EQ(3, cl.count());107cl.reset();108109// Still expect same 3 entries when top is (offset_3 - 1)110region->set_top(region->bottom() + MARK_OFFSET_3 - MinObjAlignment);111region->apply_to_marked_objects(bitmap, &cl);112EXPECT_EQ(3, cl.count());113cl.reset();114115// Setting top to end should render 4 entries.116region->set_top(region->end());117region->apply_to_marked_objects(bitmap, &cl);118EXPECT_EQ(4, cl.count());119cl.reset();120121region->set_top(old_top);122}123124TEST_VM(HeapRegion, apply_to_marked_object) {125if (!UseG1GC) {126return;127}128129// Run the test in our very own safepoint, because otherwise it130// modifies a region behind the back of a possibly using allocation131// or running GC.132VM_HeapRegionApplyToMarkedObjectsTest op;133ThreadInVMfromNative invm(JavaThread::current());134VMThread::execute(&op);135}136137138