1 /*
  2  * Copyright (c) 2022, 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/shenandoahFreeSet.hpp"
 28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 29 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
 30 #include "gc/shenandoah/shenandoahOldGC.hpp"
 31 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
 32 #include "gc/shenandoah/shenandoahGeneration.hpp"
 33 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
 34 #include "prims/jvmtiTagMap.hpp"
 35 #include "utilities/events.hpp"
 36 
 37 
 38 
 39 
 40 ShenandoahOldGC::ShenandoahOldGC(ShenandoahGeneration* generation, ShenandoahSharedFlag& allow_preemption) :
 41     ShenandoahConcurrentGC(generation, false), _allow_preemption(allow_preemption) {
 42 }
 43 
 44 // Final mark for old-gen is different than for young or old, so we
 45 // override the implementation.
 46 void ShenandoahOldGC::op_final_mark() {
 47 
 48   ShenandoahHeap* const heap = ShenandoahHeap::heap();
 49   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Should be at safepoint");
 50   assert(!heap->has_forwarded_objects(), "No forwarded objects on this path");
 51 
 52   if (ShenandoahVerify) {
 53     heap->verifier()->verify_roots_no_forwarded();
 54   }
 55 
 56   if (!heap->cancelled_gc()) {
 57     assert(_mark.generation()->generation_mode() == OLD, "Generation of Old-Gen GC should be OLD");
 58     _mark.finish_mark();
 59     assert(!heap->cancelled_gc(), "STW mark cannot OOM");
 60 
 61     // Old collection is complete, the young generation no longer needs this
 62     // reference to the old concurrent mark so clean it up.
 63     heap->young_generation()->set_old_gen_task_queues(nullptr);
 64 
 65     // We need to do this because weak root cleaning reports the number of dead handles
 66     JvmtiTagMap::set_needs_cleaning();
 67 
 68     _generation->prepare_regions_and_collection_set(true);
 69 
 70     heap->set_unload_classes(false);
 71     heap->prepare_concurrent_roots();
 72 
 73     // Believe verification following old-gen concurrent mark needs to be different than verification following
 74     // young-gen concurrent mark, so am commenting this out for now:
 75     //   if (ShenandoahVerify) {
 76     //     heap->verifier()->verify_after_concmark();
 77     //   }
 78 
 79     if (VerifyAfterGC) {
 80       Universe::verify();
 81     }
 82   }
 83 }
 84 
 85 bool ShenandoahOldGC::collect(GCCause::Cause cause) {
 86   ShenandoahHeap* heap = ShenandoahHeap::heap();
 87   assert(!heap->doing_mixed_evacuations(), "Should not start an old gc with pending mixed evacuations");
 88   assert(!heap->is_prepare_for_old_mark_in_progress(), "Old regions need to be parseable during concurrent mark.");
 89 
 90   // Enable preemption of old generation mark.
 91   _allow_preemption.set();
 92 
 93   // Continue concurrent mark, do not reset regions, do not mark roots, do not collect $200.
 94   entry_mark();
 95 
 96   // If we failed to unset the preemption flag, it means another thread has already unset it.
 97   if (!_allow_preemption.try_unset()) {
 98     // The regulator thread has unset the preemption guard. That thread will shortly cancel
 99     // the gc, but the control thread is now racing it. Wait until this thread sees the
100     // cancellation.
101     while (!heap->cancelled_gc()) {
102       SpinPause();
103     }
104   }
105 
106   if (heap->cancelled_gc()) {
107     return false;
108   }
109 
110   // Complete marking under STW
111   vmop_entry_final_mark();
112 
113   // We aren't dealing with old generation evacuation yet. Our heuristic
114   // should not have built a cset in final mark.
115   assert(!heap->is_evacuation_in_progress(), "Old gen evacuations are not supported");
116 
117   // Process weak roots that might still point to regions that would be broken by cleanup
118   if (heap->is_concurrent_weak_root_in_progress()) {
119     entry_weak_refs();
120     entry_weak_roots();
121   }
122 
123   // Final mark might have reclaimed some immediate garbage, kick cleanup to reclaim
124   // the space. This would be the last action if there is nothing to evacuate.
125   entry_cleanup_early();
126 
127   {
128     ShenandoahHeapLocker locker(heap->lock());
129     heap->free_set()->log_status();
130   }
131 
132 
133   // TODO: Old marking doesn't support class unloading yet
134   // Perform concurrent class unloading
135   // if (heap->unload_classes() &&
136   //     heap->is_concurrent_weak_root_in_progress()) {
137   //   entry_class_unloading();
138   // }
139 
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   vmop_entry_final_roots(false);
148 
149   return true;
150 }