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 
 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   if (_generation->is_concurrent_mark_in_progress()) {
115     assert(heap->cancelled_gc(), "Safepoint operation observed gc cancellation");
116     // GC may have been cancelled before final mark, but after the preceding cancellation check.
117     return false;
118   }
119 
120   // We aren't dealing with old generation evacuation yet. Our heuristic
121   // should not have built a cset in final mark.
122   assert(!heap->is_evacuation_in_progress(), "Old gen evacuations are not supported");
123 
124   // Process weak roots that might still point to regions that would be broken by cleanup
125   if (heap->is_concurrent_weak_root_in_progress()) {
126     entry_weak_refs();
127     entry_weak_roots();
128   }
129 
130   // Final mark might have reclaimed some immediate garbage, kick cleanup to reclaim
131   // the space. This would be the last action if there is nothing to evacuate.
132   entry_cleanup_early();
133 
134   heap->free_set()->log_status_under_lock();
135 
136   assert(!heap->is_concurrent_strong_root_in_progress(), "No evacuations during old gc.");
137 
138   // We must execute this vm operation if we completed final mark. We cannot
139   // return from here with weak roots in progress. This is not a valid gc state
140   // for any young collections (or allocation failures) that interrupt the old
141   // collection.
142   vmop_entry_final_roots();
143 
144   // We do not rebuild_free following increments of old marking because memory has not been reclaimed. However, we may
145   // need to transfer memory to OLD in order to efficiently support the mixed evacuations that might immediately follow.
146   size_t allocation_runway = heap->young_generation()->heuristics()->bytes_of_allocation_runway_before_gc_trigger(0);
147   heap->compute_old_generation_balance(allocation_runway, 0);
148 
149   ShenandoahGenerationalHeap::TransferResult result;
150   {
151     ShenandoahHeapLocker locker(heap->lock());
152     result = heap->balance_generations();
153   }
154 
155   LogTarget(Info, gc, ergo) lt;
156   if (lt.is_enabled()) {
157     LogStream ls(lt);
158     result.print_on("Old Mark", &ls);
159   }
160   return true;
161 }