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