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/g1FullGCCompactTask.hpp"
31#include "gc/g1/heapRegion.inline.hpp"
32#include "gc/shared/gcTraceTime.inline.hpp"
33#include "logging/log.hpp"
34#include "oops/oop.inline.hpp"
35#include "utilities/ticks.hpp"
36
37class G1ResetHumongousClosure : public HeapRegionClosure {
38 G1CMBitMap* _bitmap;
39
40public:
41 G1ResetHumongousClosure(G1CMBitMap* bitmap) :
42 _bitmap(bitmap) { }
43
44 bool do_heap_region(HeapRegion* current) {
45 if (current->is_humongous()) {
46 if (current->is_starts_humongous()) {
47 oop obj = oop(current->bottom());
48 if (_bitmap->is_marked(obj)) {
49 // Clear bitmap and fix mark word.
50 _bitmap->clear(obj);
51 obj->init_mark_raw();
52 } else {
53 assert(current->is_empty(), "Should have been cleared in phase 2.");
54 }
55 }
56 current->reset_during_compaction();
57 }
58 return false;
59 }
60};
61
62size_t G1FullGCCompactTask::G1CompactRegionClosure::apply(oop obj) {
63 size_t size = obj->size();
64 HeapWord* destination = (HeapWord*)obj->forwardee();
65 if (destination == NULL) {
66 // Object not moving
67 return size;
68 }
69
70 // copy object and reinit its mark
71 HeapWord* obj_addr = (HeapWord*) obj;
72 assert(obj_addr != destination, "everything in this pass should be moving");
73 Copy::aligned_conjoint_words(obj_addr, destination, size);
74 oop(destination)->init_mark_raw();
75 assert(oop(destination)->klass() != NULL, "should have a class");
76
77 return size;
78}
79
80void G1FullGCCompactTask::compact_region(HeapRegion* hr) {
81 assert(!hr->is_humongous(), "Should be no humongous regions in compaction queue");
82 G1CompactRegionClosure compact(collector()->mark_bitmap());
83 hr->apply_to_marked_objects(collector()->mark_bitmap(), &compact);
84 // Once all objects have been moved the liveness information
85 // needs be cleared.
86 collector()->mark_bitmap()->clear_region(hr);
87 hr->complete_compaction();
88}
89
90void G1FullGCCompactTask::work(uint worker_id) {
91 Ticks start = Ticks::now();
92 GrowableArray<HeapRegion*>* compaction_queue = collector()->compaction_point(worker_id)->regions();
93 for (GrowableArrayIterator<HeapRegion*> it = compaction_queue->begin();
94 it != compaction_queue->end();
95 ++it) {
96 compact_region(*it);
97 }
98
99 G1ResetHumongousClosure hc(collector()->mark_bitmap());
100 G1CollectedHeap::heap()->heap_region_par_iterate_from_worker_offset(&hc, &_claimer, worker_id);
101 log_task("Compaction task", worker_id, start);
102}
103
104void G1FullGCCompactTask::serial_compaction() {
105 GCTraceTime(Debug, gc, phases) tm("Phase 4: Serial Compaction", collector()->scope()->timer());
106 GrowableArray<HeapRegion*>* compaction_queue = collector()->serial_compaction_point()->regions();
107 for (GrowableArrayIterator<HeapRegion*> it = compaction_queue->begin();
108 it != compaction_queue->end();
109 ++it) {
110 compact_region(*it);
111 }
112}
113