|
1 /* |
|
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 #include "precompiled.hpp" |
|
25 #include "gc/g1/g1BlockOffsetTable.hpp" |
|
26 #include "gc/g1/g1CollectedHeap.hpp" |
|
27 #include "gc/g1/g1ConcurrentMarkBitMap.inline.hpp" |
|
28 #include "gc/g1/heapRegion.inline.hpp" |
|
29 #include "gc/shared/referenceProcessor.hpp" |
|
30 #include "unittest.hpp" |
|
31 |
|
32 class VerifyAndCountMarkClosure : public StackObj { |
|
33 int _count; |
|
34 G1CMBitMap* _bm; |
|
35 |
|
36 void ensure_marked(HeapWord* addr) { |
|
37 ASSERT_TRUE(_bm->is_marked(addr)); |
|
38 } |
|
39 |
|
40 public: |
|
41 VerifyAndCountMarkClosure(G1CMBitMap* bm) : _count(0), _bm(bm) { } |
|
42 |
|
43 virtual size_t apply(oop object) { |
|
44 _count++; |
|
45 ensure_marked((HeapWord*) object); |
|
46 // Must return positive size to advance the iteration. |
|
47 return MinObjAlignment; |
|
48 } |
|
49 |
|
50 void reset() { |
|
51 _count = 0; |
|
52 } |
|
53 |
|
54 int count() { |
|
55 return _count; |
|
56 } |
|
57 }; |
|
58 |
|
59 #define MARK_OFFSET_1 ( 17 * MinObjAlignment) |
|
60 #define MARK_OFFSET_2 ( 99 * MinObjAlignment) |
|
61 #define MARK_OFFSET_3 (337 * MinObjAlignment) |
|
62 |
|
63 TEST_OTHER_VM(HeapRegion, apply_to_marked_objects) { |
|
64 if (!UseG1GC) { |
|
65 return; |
|
66 } |
|
67 |
|
68 G1CollectedHeap* heap = G1CollectedHeap::heap(); |
|
69 |
|
70 // Using region 0 for testing. |
|
71 HeapRegion* region = heap->heap_region_containing(heap->bottom_addr_for_region(0)); |
|
72 |
|
73 // Mark some "oops" in the bitmap. |
|
74 G1CMBitMap* bitmap = heap->concurrent_mark()->next_mark_bitmap(); |
|
75 bitmap->mark(region->bottom()); |
|
76 bitmap->mark(region->bottom() + MARK_OFFSET_1); |
|
77 bitmap->mark(region->bottom() + MARK_OFFSET_2); |
|
78 bitmap->mark(region->bottom() + MARK_OFFSET_3); |
|
79 bitmap->mark(region->end()); |
|
80 |
|
81 VerifyAndCountMarkClosure cl(bitmap); |
|
82 |
|
83 // When top is equal to bottom the closure should not be |
|
84 // applied to any object because apply_to_marked_objects |
|
85 // will stop at HeapRegion::scan_limit which is equal to top. |
|
86 region->set_top(region->bottom()); |
|
87 region->apply_to_marked_objects(bitmap, &cl); |
|
88 EXPECT_EQ(0, cl.count()); |
|
89 cl.reset(); |
|
90 |
|
91 // Set top to offset_1 and expect only to find 1 entry (bottom) |
|
92 region->set_top(region->bottom() + MARK_OFFSET_1); |
|
93 region->apply_to_marked_objects(bitmap, &cl); |
|
94 EXPECT_EQ(1, cl.count()); |
|
95 cl.reset(); |
|
96 |
|
97 // Set top to (offset_2 + 1) and expect only to find 3 |
|
98 // entries (bottom, offset_1 and offset_2) |
|
99 region->set_top(region->bottom() + MARK_OFFSET_2 + MinObjAlignment); |
|
100 region->apply_to_marked_objects(bitmap, &cl); |
|
101 EXPECT_EQ(3, cl.count()); |
|
102 cl.reset(); |
|
103 |
|
104 // Still expect same 3 entries when top is (offset_3 - 1) |
|
105 region->set_top(region->bottom() + MARK_OFFSET_3 - MinObjAlignment); |
|
106 region->apply_to_marked_objects(bitmap, &cl); |
|
107 EXPECT_EQ(3, cl.count()); |
|
108 cl.reset(); |
|
109 |
|
110 // Setting top to end should render 4 entries. |
|
111 region->set_top(region->end()); |
|
112 region->apply_to_marked_objects(bitmap, &cl); |
|
113 EXPECT_EQ(4, cl.count()); |
|
114 cl.reset(); |
|
115 } |
|
116 |