1 /*
  2  * Copyright (c) 2013, 2021, Red Hat, Inc. All rights reserved.
  3  * Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
 28 #include "gc/shenandoah/shenandoahConcurrentGC.hpp"
 29 #include "gc/shenandoah/shenandoahControlThread.hpp"
 30 #include "gc/shenandoah/shenandoahDegeneratedGC.hpp"
 31 #include "gc/shenandoah/shenandoahFreeSet.hpp"
 32 #include "gc/shenandoah/shenandoahFullGC.hpp"
 33 #include "gc/shenandoah/shenandoahGeneration.hpp"
 34 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 35 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
 36 #include "gc/shenandoah/shenandoahPacer.inline.hpp"
 37 #include "gc/shenandoah/shenandoahUtils.hpp"
 38 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
 39 #include "gc/shenandoah/mode/shenandoahMode.hpp"
 40 #include "logging/log.hpp"
 41 #include "memory/metaspaceUtils.hpp"
 42 #include "memory/metaspaceStats.hpp"
 43 
 44 ShenandoahControlThread::ShenandoahControlThread() :
 45   ShenandoahController(),
 46   _requested_gc_cause(GCCause::_no_cause_specified),
 47   _degen_point(ShenandoahGC::_degenerated_outside_cycle) {
 48   set_name("Shenandoah Control Thread");
 49   create_and_start();
 50 }
 51 
 52 void ShenandoahControlThread::run_service() {
 53   ShenandoahHeap* const heap = ShenandoahHeap::heap();
 54 
 55   const GCMode default_mode = concurrent_normal;
 56   const GCCause::Cause default_cause = GCCause::_shenandoah_concurrent_gc;
 57   int sleep = ShenandoahControlIntervalMin;
 58 
 59   double last_shrink_time = os::elapsedTime();
 60   double last_sleep_adjust_time = os::elapsedTime();
 61 
 62   // Shrink period avoids constantly polling regions for shrinking.
 63   // Having a period 10x lower than the delay would mean we hit the
 64   // shrinking with lag of less than 1/10-th of true delay.
 65   // ShenandoahUncommitDelay is in msecs, but shrink_period is in seconds.
 66   const double shrink_period = (double)ShenandoahUncommitDelay / 1000 / 10;
 67 
 68   ShenandoahCollectorPolicy* const policy = heap->shenandoah_policy();
 69   ShenandoahHeuristics* const heuristics = heap->heuristics();
 70   while (!in_graceful_shutdown() && !should_terminate()) {
 71     // Figure out if we have pending requests.
 72     const bool alloc_failure_pending = _alloc_failure_gc.is_set();
 73     const bool is_gc_requested = _gc_requested.is_set();
 74     const GCCause::Cause requested_gc_cause = _requested_gc_cause;
 75 
 76     // This control loop iteration has seen this much allocation.
 77     const size_t allocs_seen = reset_allocs_seen();
 78 
 79     // Check if we have seen a new target for soft max heap size.
 80     const bool soft_max_changed = heap->check_soft_max_changed();
 81 
 82     // Choose which GC mode to run in. The block below should select a single mode.
 83     GCMode mode = none;
 84     GCCause::Cause cause = GCCause::_last_gc_cause;
 85     ShenandoahGC::ShenandoahDegenPoint degen_point = ShenandoahGC::_degenerated_unset;
 86 
 87     if (alloc_failure_pending) {
 88       // Allocation failure takes precedence: we have to deal with it first thing
 89       log_info(gc)("Trigger: Handle Allocation Failure");
 90 
 91       cause = GCCause::_allocation_failure;
 92 
 93       // Consume the degen point, and seed it with default value
 94       degen_point = _degen_point;
 95       _degen_point = ShenandoahGC::_degenerated_outside_cycle;
 96 
 97       if (ShenandoahDegeneratedGC && heuristics->should_degenerate_cycle()) {
 98         heuristics->record_allocation_failure_gc();
 99         policy->record_alloc_failure_to_degenerated(degen_point);
100         mode = stw_degenerated;
101       } else {
102         heuristics->record_allocation_failure_gc();
103         policy->record_alloc_failure_to_full();
104         mode = stw_full;
105       }
106     } else if (is_gc_requested) {
107       cause = requested_gc_cause;
108       log_info(gc)("Trigger: GC request (%s)", GCCause::to_string(cause));
109       heuristics->record_requested_gc();
110 
111       if (ShenandoahCollectorPolicy::should_run_full_gc(cause)) {
112         mode = stw_full;
113       } else {
114         mode = default_mode;
115         // Unload and clean up everything
116         heap->set_unload_classes(heuristics->can_unload_classes());
117       }
118     } else {
119       // Potential normal cycle: ask heuristics if it wants to act
120       if (heuristics->should_start_gc()) {
121         mode = default_mode;
122         cause = default_cause;
123       }
124 
125       // Ask policy if this cycle wants to process references or unload classes
126       heap->set_unload_classes(heuristics->should_unload_classes());
127     }
128 
129     // Blow all soft references on this cycle, if handling allocation failure,
130     // either implicit or explicit GC request,  or we are requested to do so unconditionally.
131     if (alloc_failure_pending || is_gc_requested || ShenandoahAlwaysClearSoftRefs) {
132       heap->soft_ref_policy()->set_should_clear_all_soft_refs(true);
133     }
134 
135     const bool gc_requested = (mode != none);
136     assert (!gc_requested || cause != GCCause::_last_gc_cause, "GC cause should be set");
137 
138     if (gc_requested) {
139       // GC is starting, bump the internal ID
140       update_gc_id();
141 
142       heap->reset_bytes_allocated_since_gc_start();
143 
144       MetaspaceCombinedStats meta_sizes = MetaspaceUtils::get_combined_statistics();
145 
146       // If GC was requested, we are sampling the counters even without actual triggers
147       // from allocation machinery. This captures GC phases more accurately.
148       heap->set_forced_counters_update(true);
149 
150       // If GC was requested, we better dump freeset data for performance debugging
151       heap->free_set()->log_status_under_lock();
152 
153       switch (mode) {
154         case concurrent_normal:
155           service_concurrent_normal_cycle(cause);
156           break;
157         case stw_degenerated:
158           service_stw_degenerated_cycle(cause, degen_point);
159           break;
160         case stw_full:
161           service_stw_full_cycle(cause);
162           break;
163         default:
164           ShouldNotReachHere();
165       }
166 
167       // If this was the requested GC cycle, notify waiters about it
168       if (is_gc_requested) {
169         notify_gc_waiters();
170       }
171 
172       // If this was the allocation failure GC cycle, notify waiters about it
173       if (alloc_failure_pending) {
174         notify_alloc_failure_waiters();
175       }
176 
177       // Report current free set state at the end of cycle, whether
178       // it is a normal completion, or the abort.
179       heap->free_set()->log_status_under_lock();
180 
181       // Notify Universe about new heap usage. This has implications for
182       // global soft refs policy, and we better report it every time heap
183       // usage goes down.
184       heap->update_capacity_and_used_at_gc();
185 
186       // Signal that we have completed a visit to all live objects.
187       heap->record_whole_heap_examined_timestamp();
188 
189       // Disable forced counters update, and update counters one more time
190       // to capture the state at the end of GC session.
191       heap->handle_force_counters_update();
192       heap->set_forced_counters_update(false);
193 
194       // Retract forceful part of soft refs policy
195       heap->soft_ref_policy()->set_should_clear_all_soft_refs(false);
196 
197       // Clear metaspace oom flag, if current cycle unloaded classes
198       if (heap->unload_classes()) {
199         heuristics->clear_metaspace_oom();
200       }
201 
202       // Commit worker statistics to cycle data
203       heap->phase_timings()->flush_par_workers_to_cycle();
204       if (ShenandoahPacing) {
205         heap->pacer()->flush_stats_to_cycle();
206       }
207 
208       // Print GC stats for current cycle
209       {
210         LogTarget(Info, gc, stats) lt;
211         if (lt.is_enabled()) {
212           ResourceMark rm;
213           LogStream ls(lt);
214           heap->phase_timings()->print_cycle_on(&ls);
215           if (ShenandoahPacing) {
216             heap->pacer()->print_cycle_on(&ls);
217           }
218         }
219       }
220 
221       // Commit statistics to globals
222       heap->phase_timings()->flush_cycle_to_global();
223 
224       // Print Metaspace change following GC (if logging is enabled).
225       MetaspaceUtils::print_metaspace_change(meta_sizes);
226 
227       // GC is over, we are at idle now
228       if (ShenandoahPacing) {
229         heap->pacer()->setup_for_idle();
230       }
231     } else {
232       // Report to pacer that we have seen this many words allocated
233       if (ShenandoahPacing && (allocs_seen > 0)) {
234         heap->pacer()->report_alloc(allocs_seen);
235       }
236     }
237 
238     const double current = os::elapsedTime();
239 
240     if (ShenandoahUncommit && (is_gc_requested || soft_max_changed || (current - last_shrink_time > shrink_period))) {
241       // Explicit GC tries to uncommit everything down to min capacity.
242       // Soft max change tries to uncommit everything down to target capacity.
243       // Periodic uncommit tries to uncommit suitable regions down to min capacity.
244 
245       double shrink_before = (is_gc_requested || soft_max_changed) ?
246                              current :
247                              current - (ShenandoahUncommitDelay / 1000.0);
248 
249       size_t shrink_until = soft_max_changed ?
250                              heap->soft_max_capacity() :
251                              heap->min_capacity();
252 
253       heap->maybe_uncommit(shrink_before, shrink_until);
254       heap->phase_timings()->flush_cycle_to_global();
255       last_shrink_time = current;
256     }
257 
258     // Wait before performing the next action. If allocation happened during this wait,
259     // we exit sooner, to let heuristics re-evaluate new conditions. If we are at idle,
260     // back off exponentially.
261     if (heap->has_changed()) {
262       sleep = ShenandoahControlIntervalMin;
263     } else if ((current - last_sleep_adjust_time) * 1000 > ShenandoahControlIntervalAdjustPeriod){
264       sleep = MIN2<int>(ShenandoahControlIntervalMax, MAX2(1, sleep * 2));
265       last_sleep_adjust_time = current;
266     }
267     os::naked_short_sleep(sleep);
268   }
269 
270   // Wait for the actual stop(), can't leave run_service() earlier.
271   while (!should_terminate()) {
272     os::naked_short_sleep(ShenandoahControlIntervalMin);
273   }
274 }
275 
276 void ShenandoahControlThread::service_concurrent_normal_cycle(GCCause::Cause cause) {
277   // Normal cycle goes via all concurrent phases. If allocation failure (af) happens during
278   // any of the concurrent phases, it first degrades to Degenerated GC and completes GC there.
279   // If second allocation failure happens during Degenerated GC cycle (for example, when GC
280   // tries to evac something and no memory is available), cycle degrades to Full GC.
281   //
282   // There are also a shortcut through the normal cycle: immediate garbage shortcut, when
283   // heuristics says there are no regions to compact, and all the collection comes from immediately
284   // reclaimable regions.
285   //
286   // ................................................................................................
287   //
288   //                                    (immediate garbage shortcut)                Concurrent GC
289   //                             /-------------------------------------------\
290   //                             |                                           |
291   //                             |                                           |
292   //                             |                                           |
293   //                             |                                           v
294   // [START] ----> Conc Mark ----o----> Conc Evac --o--> Conc Update-Refs ---o----> [END]
295   //                   |                    |                 |              ^
296   //                   | (af)               | (af)            | (af)         |
297   // ..................|....................|.................|..............|.......................
298   //                   |                    |                 |              |
299   //                   |                    |                 |              |      Degenerated GC
300   //                   v                    v                 v              |
301   //               STW Mark ----------> STW Evac ----> STW Update-Refs ----->o
302   //                   |                    |                 |              ^
303   //                   | (af)               | (af)            | (af)         |
304   // ..................|....................|.................|..............|.......................
305   //                   |                    |                 |              |
306   //                   |                    v                 |              |      Full GC
307   //                   \------------------->o<----------------/              |
308   //                                        |                                |
309   //                                        v                                |
310   //                                      Full GC  --------------------------/
311   //
312   ShenandoahHeap* heap = ShenandoahHeap::heap();
313   if (check_cancellation_or_degen(ShenandoahGC::_degenerated_outside_cycle)) return;
314 
315   GCIdMark gc_id_mark;
316   ShenandoahGCSession session(cause, heap->global_generation());
317 
318   TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters());
319 
320   ShenandoahConcurrentGC gc(heap->global_generation(), false);
321   if (gc.collect(cause)) {
322     // Cycle is complete.  There were no failed allocation requests and no degeneration, so count this as good progress.
323     heap->notify_gc_progress();
324     heap->global_generation()->heuristics()->record_success_concurrent();
325     heap->shenandoah_policy()->record_success_concurrent(false, gc.abbreviated());
326     heap->log_heap_status("At end of GC");
327   } else {
328     assert(heap->cancelled_gc(), "Must have been cancelled");
329     check_cancellation_or_degen(gc.degen_point());
330     heap->log_heap_status("At end of cancelled GC");
331   }
332 }
333 
334 bool ShenandoahControlThread::check_cancellation_or_degen(ShenandoahGC::ShenandoahDegenPoint point) {
335   ShenandoahHeap* heap = ShenandoahHeap::heap();
336   if (heap->cancelled_gc()) {
337     assert (is_alloc_failure_gc() || in_graceful_shutdown(), "Cancel GC either for alloc failure GC, or gracefully exiting");
338     if (!in_graceful_shutdown()) {
339       assert (_degen_point == ShenandoahGC::_degenerated_outside_cycle,
340               "Should not be set yet: %s", ShenandoahGC::degen_point_to_string(_degen_point));
341       _degen_point = point;
342     }
343     return true;
344   }
345   return false;
346 }
347 
348 void ShenandoahControlThread::stop_service() {
349   // Nothing to do here.
350 }
351 
352 void ShenandoahControlThread::service_stw_full_cycle(GCCause::Cause cause) {
353   ShenandoahHeap* const heap = ShenandoahHeap::heap();
354   GCIdMark gc_id_mark;
355   ShenandoahGCSession session(cause, heap->global_generation());
356 
357   ShenandoahFullGC gc;
358   gc.collect(cause);
359 }
360 
361 void ShenandoahControlThread::service_stw_degenerated_cycle(GCCause::Cause cause, ShenandoahGC::ShenandoahDegenPoint point) {
362   assert (point != ShenandoahGC::_degenerated_unset, "Degenerated point should be set");
363   ShenandoahHeap* const heap = ShenandoahHeap::heap();
364   GCIdMark gc_id_mark;
365   ShenandoahGCSession session(cause, heap->global_generation());
366 
367   ShenandoahDegenGC gc(point, heap->global_generation());
368   gc.collect(cause);
369 }
370 
371 void ShenandoahControlThread::request_gc(GCCause::Cause cause) {
372   if (ShenandoahCollectorPolicy::should_handle_requested_gc(cause)) {
373     handle_requested_gc(cause);
374   }
375 }
376 
377 void ShenandoahControlThread::handle_requested_gc(GCCause::Cause cause) {
378   // For normal requested GCs (System.gc) we want to block the caller. However,
379   // for whitebox requested GC, we want to initiate the GC and return immediately.
380   // The whitebox caller thread will arrange for itself to wait until the GC notifies
381   // it that has reached the requested breakpoint (phase in the GC).
382   if (cause == GCCause::_wb_breakpoint) {
383     _requested_gc_cause = cause;
384     _gc_requested.set();
385     return;
386   }
387 
388   // Make sure we have at least one complete GC cycle before unblocking
389   // from the explicit GC request.
390   //
391   // This is especially important for weak references cleanup and/or native
392   // resources (e.g. DirectByteBuffers) machinery: when explicit GC request
393   // comes very late in the already running cycle, it would miss lots of new
394   // opportunities for cleanup that were made available before the caller
395   // requested the GC.
396 
397   MonitorLocker ml(&_gc_waiters_lock);
398   size_t current_gc_id = get_gc_id();
399   size_t required_gc_id = current_gc_id + 1;
400   while (current_gc_id < required_gc_id) {
401     // Although setting gc request is under _gc_waiters_lock, but read side (run_service())
402     // does not take the lock. We need to enforce following order, so that read side sees
403     // latest requested gc cause when the flag is set.
404     _requested_gc_cause = cause;
405     _gc_requested.set();
406 
407     ml.wait();
408     current_gc_id = get_gc_id();
409   }
410 }
411 
412 void ShenandoahControlThread::notify_gc_waiters() {
413   _gc_requested.unset();
414   MonitorLocker ml(&_gc_waiters_lock);
415   ml.notify_all();
416 }