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   ShenandoahHeap* const heap = ShenandoahHeap::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 
 86 bool ShenandoahOldGC::collect(GCCause::Cause cause) {
 87   auto heap = ShenandoahGenerationalHeap::heap();
 88   assert(!_old_generation->is_doing_mixed_evacuations(), "Should not start an old gc with pending mixed evacuations");
 89   assert(!_old_generation->is_preparing_for_mark(), "Old regions need to be parsable during concurrent mark.");
 90 
 91   // Enable preemption of old generation mark.
 92   _allow_preemption.set();
 93 
 94   // Continue concurrent mark, do not reset regions, do not mark roots, do not collect $200.
 95   entry_mark();
 96 
 97   // If we failed to unset the preemption flag, it means another thread has already unset it.
 98   if (!_allow_preemption.try_unset()) {
 99     // The regulator thread has unset the preemption guard. That thread will shortly cancel
100     // the gc, but the control thread is now racing it. Wait until this thread sees the
101     // cancellation.
102     while (!heap->cancelled_gc()) {
103       SpinPause();
104     }
105   }
106 
107   if (heap->cancelled_gc()) {
108     return false;
109   }
110 
111   // Complete marking under STW
112   vmop_entry_final_mark();
113 
114   // We aren't dealing with old generation evacuation yet. Our heuristic
115   // should not have built a cset in final mark.
116   assert(!heap->is_evacuation_in_progress(), "Old gen evacuations are not supported");
117 
118   // Process weak roots that might still point to regions that would be broken by cleanup
119   if (heap->is_concurrent_weak_root_in_progress()) {
120     entry_weak_refs();
121     entry_weak_roots();
122   }
123 
124   // Final mark might have reclaimed some immediate garbage, kick cleanup to reclaim
125   // the space. This would be the last action if there is nothing to evacuate.
126   entry_cleanup_early();
127 
128   {
129     ShenandoahHeapLocker locker(heap->lock());
130     heap->free_set()->log_status();
131   }
132 
133 
134   // TODO: Old marking doesn't support class unloading yet
135   // Perform concurrent class unloading
136   // if (heap->unload_classes() &&
137   //     heap->is_concurrent_weak_root_in_progress()) {
138   //   entry_class_unloading();
139   // }
140 
141 
142   assert(!heap->is_concurrent_strong_root_in_progress(), "No evacuations during old gc.");
143 
144   // We must execute this vm operation if we completed final mark. We cannot
145   // return from here with weak roots in progress. This is not a valid gc state
146   // for any young collections (or allocation failures) that interrupt the old
147   // collection.
148   vmop_entry_final_roots();
149 
150   // We do not rebuild_free following increments of old marking because memory has not been reclaimed. However, we may
151   // need to transfer memory to OLD in order to efficiently support the mixed evacuations that might immediately follow.
152   size_t allocation_runway = heap->young_generation()->heuristics()->bytes_of_allocation_runway_before_gc_trigger(0);
153   heap->compute_old_generation_balance(allocation_runway, 0);
154 
155   ShenandoahGenerationalHeap::TransferResult result;
156   {
157     ShenandoahHeapLocker locker(heap->lock());
158     result = heap->balance_generations();
159   }
160 
161   LogTarget(Info, gc, ergo) lt;
162   if (lt.is_enabled()) {
163     LogStream ls(lt);
164     result.print_on("Old Mark", &ls);
165   }
166   return true;
167 }