1 /*
2 * Copyright Amazon.com Inc. 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
27 #include "gc/shenandoah/heuristics/shenandoahYoungHeuristics.hpp"
28 #include "gc/shenandoah/shenandoahFreeSet.hpp"
29 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp"
30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
31 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
32 #include "gc/shenandoah/shenandoahOldGC.hpp"
33 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
34 #include "gc/shenandoah/shenandoahGeneration.hpp"
35 #include "gc/shenandoah/shenandoahOldGeneration.hpp"
36 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
37 #include "prims/jvmtiTagMap.hpp"
38 #include "utilities/events.hpp"
39
40
41 ShenandoahOldGC::ShenandoahOldGC(ShenandoahOldGeneration* generation, ShenandoahSharedFlag& allow_preemption) :
42 ShenandoahConcurrentGC(generation, false), _old_generation(generation), _allow_preemption(allow_preemption) {
43 }
44
45 // Final mark for old-gen is different than for young or old, so we
46 // override the implementation.
47 void ShenandoahOldGC::op_final_mark() {
48
49 ShenandoahGenerationalHeap* const heap = ShenandoahGenerationalHeap::heap();
50 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Should be at safepoint");
51 assert(!heap->has_forwarded_objects(), "No forwarded objects on this path");
52
53 if (ShenandoahVerify) {
54 heap->verifier()->verify_roots_no_forwarded();
55 }
56
57 if (!heap->cancelled_gc()) {
58 assert(_mark.generation()->is_old(), "Generation of Old-Gen GC should be OLD");
59 _mark.finish_mark();
60 assert(!heap->cancelled_gc(), "STW mark cannot OOM");
61
62 // Old collection is complete, the young generation no longer needs this
63 // reference to the old concurrent mark so clean it up.
64 heap->young_generation()->set_old_gen_task_queues(nullptr);
65
66 // We need to do this because weak root cleaning reports the number of dead handles
67 JvmtiTagMap::set_needs_cleaning();
68
69 _generation->prepare_regions_and_collection_set(true);
70
71 heap->set_unload_classes(false);
72 heap->prepare_concurrent_roots();
73
74 // Believe verification following old-gen concurrent mark needs to be different than verification following
75 // young-gen concurrent mark, so am commenting this out for now:
76 // if (ShenandoahVerify) {
77 // heap->verifier()->verify_after_concmark();
78 // }
79
80 if (VerifyAfterGC) {
81 Universe::verify();
82 }
83
84 {
85 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::final_mark_propagate_gc_state);
86 heap->propagate_gc_state_to_all_threads();
87 }
88 }
89 }
90
91 bool ShenandoahOldGC::collect(GCCause::Cause cause) {
92 auto heap = ShenandoahGenerationalHeap::heap();
93 assert(!_old_generation->is_doing_mixed_evacuations(), "Should not start an old gc with pending mixed evacuations");
94 assert(!_old_generation->is_preparing_for_mark(), "Old regions need to be parsable during concurrent mark.");
95
96 // Enable preemption of old generation mark.
97 _allow_preemption.set();
98
99 // Continue concurrent mark, do not reset regions, do not mark roots, do not collect $200.
100 entry_mark();
101
102 // If we failed to unset the preemption flag, it means another thread has already unset it.
103 if (!_allow_preemption.try_unset()) {
104 // The regulator thread has unset the preemption guard. That thread will shortly cancel
105 // the gc, but the control thread is now racing it. Wait until this thread sees the
106 // cancellation.
107 while (!heap->cancelled_gc()) {
108 SpinPause();
109 }
110 }
111
112 if (heap->cancelled_gc()) {
113 return false;
114 }
115
116 // Complete marking under STW
117 vmop_entry_final_mark();
118
119 if (_generation->is_concurrent_mark_in_progress()) {
120 assert(heap->cancelled_gc(), "Safepoint operation observed gc cancellation");
121 // GC may have been cancelled before final mark, but after the preceding cancellation check.
122 return false;
123 }
124
125 // We aren't dealing with old generation evacuation yet. Our heuristic
126 // should not have built a cset in final mark.
127 assert(!heap->is_evacuation_in_progress(), "Old gen evacuations are not supported");
128
129 // Process weak roots that might still point to regions that would be broken by cleanup
130 if (heap->is_concurrent_weak_root_in_progress()) {
131 entry_weak_refs();
132 entry_weak_roots();
133 }
134
135 // Final mark might have reclaimed some immediate garbage, kick cleanup to reclaim
136 // the space. This would be the last action if there is nothing to evacuate.
137 entry_cleanup_early();
138
139 heap->free_set()->log_status_under_lock();
140
141 assert(!heap->is_concurrent_strong_root_in_progress(), "No evacuations during old gc.");
142
143 // We must execute this vm operation if we completed final mark. We cannot
144 // return from here with weak roots in progress. This is not a valid gc state
145 // for any young collections (or allocation failures) that interrupt the old
146 // collection.
147 heap->concurrent_final_roots();
148
149 // We do not rebuild_free following increments of old marking because memory has not been reclaimed. However, we may
150 // need to transfer memory to OLD in order to efficiently support the mixed evacuations that might immediately follow.
151 size_t allocation_runway = heap->young_generation()->heuristics()->bytes_of_allocation_runway_before_gc_trigger(0);
152 heap->compute_old_generation_balance(allocation_runway, 0);
153
154 ShenandoahGenerationalHeap::TransferResult result;
155 {
156 ShenandoahHeapLocker locker(heap->lock());
157 result = heap->balance_generations();
158 }
159
160 LogTarget(Info, gc, ergo) lt;
161 if (lt.is_enabled()) {
162 LogStream ls(lt);
163 result.print_on("Old Mark", &ls);
164 }
165 return true;
166 }