1 /*
2  * Copyright (c) 2017, 2018, 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 
25 #include "precompiled.hpp"
26 #include "gc/g1/g1CollectedHeap.hpp"
27 #include "gc/g1/g1ConcurrentMarkBitMap.inline.hpp"
28 #include "gc/g1/g1FullCollector.hpp"
29 #include "gc/g1/g1FullGCCompactionPoint.hpp"
30 #include "gc/g1/g1FullGCMarker.hpp"
31 #include "gc/g1/g1FullGCOopClosures.inline.hpp"
32 #include "gc/g1/g1FullGCPrepareTask.hpp"
33 #include "gc/g1/g1HotCardCache.hpp"
34 #include "gc/g1/heapRegion.inline.hpp"
35 #include "gc/shared/gcTraceTime.inline.hpp"
36 #include "gc/shared/referenceProcessor.hpp"
37 #include "logging/log.hpp"
38 #include "memory/iterator.inline.hpp"
39 #include "oops/oop.inline.hpp"
40 #include "utilities/ticks.hpp"
41 
do_heap_region(HeapRegion * hr)42 bool G1FullGCPrepareTask::G1CalculatePointersClosure::do_heap_region(HeapRegion* hr) {
43   if (hr->is_humongous()) {
44     oop obj = oop(hr->humongous_start_region()->bottom());
45     if (_bitmap->is_marked(obj)) {
46       if (hr->is_starts_humongous()) {
47         obj->forward_to(obj);
48       }
49     } else {
50       free_humongous_region(hr);
51     }
52   } else if (!hr->is_pinned()) {
53     prepare_for_compaction(hr);
54   }
55 
56   // Reset data structures not valid after Full GC.
57   reset_region_metadata(hr);
58 
59   return false;
60 }
61 
G1FullGCPrepareTask(G1FullCollector * collector)62 G1FullGCPrepareTask::G1FullGCPrepareTask(G1FullCollector* collector) :
63     G1FullGCTask("G1 Prepare Compact Task", collector),
64     _freed_regions(false),
65     _hrclaimer(collector->workers()) {
66 }
67 
set_freed_regions()68 void G1FullGCPrepareTask::set_freed_regions() {
69   if (!_freed_regions) {
70     _freed_regions = true;
71   }
72 }
73 
has_freed_regions()74 bool G1FullGCPrepareTask::has_freed_regions() {
75   return _freed_regions;
76 }
77 
work(uint worker_id)78 void G1FullGCPrepareTask::work(uint worker_id) {
79   Ticks start = Ticks::now();
80   G1FullGCCompactionPoint* compaction_point = collector()->compaction_point(worker_id);
81   G1CalculatePointersClosure closure(collector()->mark_bitmap(), compaction_point);
82   G1CollectedHeap::heap()->heap_region_par_iterate_from_start(&closure, &_hrclaimer);
83 
84   // Update humongous region sets
85   closure.update_sets();
86   compaction_point->update();
87 
88   // Check if any regions was freed by this worker and store in task.
89   if (closure.freed_regions()) {
90     set_freed_regions();
91   }
92   log_task("Prepare compaction task", worker_id, start);
93 }
94 
G1CalculatePointersClosure(G1CMBitMap * bitmap,G1FullGCCompactionPoint * cp)95 G1FullGCPrepareTask::G1CalculatePointersClosure::G1CalculatePointersClosure(G1CMBitMap* bitmap,
96                                                                             G1FullGCCompactionPoint* cp) :
97     _g1h(G1CollectedHeap::heap()),
98     _bitmap(bitmap),
99     _cp(cp),
100     _humongous_regions_removed(0) { }
101 
free_humongous_region(HeapRegion * hr)102 void G1FullGCPrepareTask::G1CalculatePointersClosure::free_humongous_region(HeapRegion* hr) {
103   FreeRegionList dummy_free_list("Dummy Free List for G1MarkSweep");
104 
105   hr->set_containing_set(NULL);
106   _humongous_regions_removed++;
107 
108   _g1h->free_humongous_region(hr, &dummy_free_list);
109   prepare_for_compaction(hr);
110   dummy_free_list.remove_all();
111 }
112 
reset_region_metadata(HeapRegion * hr)113 void G1FullGCPrepareTask::G1CalculatePointersClosure::reset_region_metadata(HeapRegion* hr) {
114   hr->rem_set()->clear();
115   hr->clear_cardtable();
116 
117   G1HotCardCache* hcc = _g1h->hot_card_cache();
118   if (hcc->use_cache()) {
119     hcc->reset_card_counts(hr);
120   }
121 }
122 
G1PrepareCompactLiveClosure(G1FullGCCompactionPoint * cp)123 G1FullGCPrepareTask::G1PrepareCompactLiveClosure::G1PrepareCompactLiveClosure(G1FullGCCompactionPoint* cp) :
124     _cp(cp) { }
125 
apply(oop object)126 size_t G1FullGCPrepareTask::G1PrepareCompactLiveClosure::apply(oop object) {
127   size_t size = object->size();
128   _cp->forward(object, size);
129   return size;
130 }
131 
apply(oop obj)132 size_t G1FullGCPrepareTask::G1RePrepareClosure::apply(oop obj) {
133   // We only re-prepare objects forwarded within the current region, so
134   // skip objects that are already forwarded to another region.
135   oop forwarded_to = obj->forwardee();
136   if (forwarded_to != NULL && !_current->is_in(forwarded_to)) {
137     return obj->size();
138   }
139 
140   // Get size and forward.
141   size_t size = obj->size();
142   _cp->forward(obj, size);
143 
144   return size;
145 }
146 
prepare_for_compaction_work(G1FullGCCompactionPoint * cp,HeapRegion * hr)147 void G1FullGCPrepareTask::G1CalculatePointersClosure::prepare_for_compaction_work(G1FullGCCompactionPoint* cp,
148                                                                                   HeapRegion* hr) {
149   G1PrepareCompactLiveClosure prepare_compact(cp);
150   hr->set_compaction_top(hr->bottom());
151   hr->apply_to_marked_objects(_bitmap, &prepare_compact);
152 }
153 
prepare_for_compaction(HeapRegion * hr)154 void G1FullGCPrepareTask::G1CalculatePointersClosure::prepare_for_compaction(HeapRegion* hr) {
155   if (!_cp->is_initialized()) {
156     hr->set_compaction_top(hr->bottom());
157     _cp->initialize(hr, true);
158   }
159   // Add region to the compaction queue and prepare it.
160   _cp->add(hr);
161   prepare_for_compaction_work(_cp, hr);
162 }
163 
prepare_serial_compaction()164 void G1FullGCPrepareTask::prepare_serial_compaction() {
165   GCTraceTime(Debug, gc, phases) debug("Phase 2: Prepare Serial Compaction", collector()->scope()->timer());
166   // At this point we know that no regions were completely freed by
167   // the parallel compaction. That means that the last region of
168   // all compaction queues still have data in them. We try to compact
169   // these regions in serial to avoid a premature OOM.
170   for (uint i = 0; i < collector()->workers(); i++) {
171     G1FullGCCompactionPoint* cp = collector()->compaction_point(i);
172     if (cp->has_regions()) {
173       collector()->serial_compaction_point()->add(cp->remove_last());
174     }
175   }
176 
177   // Update the forwarding information for the regions in the serial
178   // compaction point.
179   G1FullGCCompactionPoint* cp = collector()->serial_compaction_point();
180   for (GrowableArrayIterator<HeapRegion*> it = cp->regions()->begin(); it != cp->regions()->end(); ++it) {
181     HeapRegion* current = *it;
182     if (!cp->is_initialized()) {
183       // Initialize the compaction point. Nothing more is needed for the first heap region
184       // since it is already prepared for compaction.
185       cp->initialize(current, false);
186     } else {
187       assert(!current->is_humongous(), "Should be no humongous regions in compaction queue");
188       G1RePrepareClosure re_prepare(cp, current);
189       current->set_compaction_top(current->bottom());
190       current->apply_to_marked_objects(collector()->mark_bitmap(), &re_prepare);
191     }
192   }
193   cp->update();
194 }
195 
update_sets()196 void G1FullGCPrepareTask::G1CalculatePointersClosure::update_sets() {
197   // We'll recalculate total used bytes and recreate the free list
198   // at the end of the GC, so no point in updating those values here.
199   _g1h->remove_from_old_sets(0, _humongous_regions_removed);
200 }
201 
freed_regions()202 bool G1FullGCPrepareTask::G1CalculatePointersClosure::freed_regions() {
203   if (_humongous_regions_removed > 0) {
204     // Free regions from dead humongous regions.
205     return true;
206   }
207 
208   if (!_cp->has_regions()) {
209     // No regions in queue, so no free ones either.
210     return false;
211   }
212 
213   if (_cp->current_region() != _cp->regions()->last()) {
214     // The current region used for compaction is not the last in the
215     // queue. That means there is at least one free region in the queue.
216     return true;
217   }
218 
219   // No free regions in the queue.
220   return false;
221 }
222