1 /*
  2  * Copyright (c) 2021, 2026, Oracle and/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 
 26 #include "compiler/oopMap.hpp"
 27 #include "cppstdlib/new.hpp"
 28 #include "gc/g1/g1CardSetMemory.hpp"
 29 #include "gc/g1/g1CardTableEntryClosure.hpp"
 30 #include "gc/g1/g1CollectedHeap.inline.hpp"
 31 #include "gc/g1/g1CollectionSetCandidates.inline.hpp"
 32 #include "gc/g1/g1CollectorState.inline.hpp"
 33 #include "gc/g1/g1ConcurrentMark.inline.hpp"
 34 #include "gc/g1/g1EvacFailureRegions.inline.hpp"
 35 #include "gc/g1/g1EvacInfo.hpp"
 36 #include "gc/g1/g1EvacStats.inline.hpp"
 37 #include "gc/g1/g1HeapRegion.inline.hpp"
 38 #include "gc/g1/g1HeapRegionPrinter.hpp"
 39 #include "gc/g1/g1HeapRegionRemSet.inline.hpp"
 40 #include "gc/g1/g1OopClosures.inline.hpp"
 41 #include "gc/g1/g1ParScanThreadState.hpp"
 42 #include "gc/g1/g1RemSet.hpp"
 43 #include "gc/g1/g1YoungGCPostEvacuateTasks.hpp"
 44 #include "gc/shared/bufferNode.hpp"
 45 #include "gc/shared/partialArrayState.hpp"
 46 #include "jfr/jfrEvents.hpp"
 47 #include "oops/access.inline.hpp"
 48 #include "oops/compressedOops.inline.hpp"
 49 #include "oops/oop.inline.hpp"
 50 #include "runtime/atomic.hpp"
 51 #include "runtime/prefetch.inline.hpp"
 52 #include "runtime/threads.hpp"
 53 #include "runtime/threadSMR.hpp"
 54 #include "utilities/bitMap.inline.hpp"
 55 #include "utilities/ticks.hpp"
 56 
 57 class G1PostEvacuateCollectionSetCleanupTask1::MergePssTask : public G1AbstractSubTask {
 58   G1ParScanThreadStateSet* _per_thread_states;
 59 
 60 public:
 61   MergePssTask(G1ParScanThreadStateSet* per_thread_states) :
 62     G1AbstractSubTask(G1GCPhaseTimes::MergePSS),
 63     _per_thread_states(per_thread_states) { }
 64 
 65   double worker_cost() const override { return 1.0; }
 66 
 67   void do_work(uint worker_id) override { _per_thread_states->flush_stats(); }
 68 };
 69 
 70 class G1PostEvacuateCollectionSetCleanupTask1::RecalculateUsedTask : public G1AbstractSubTask {
 71   bool _evacuation_failed;
 72   bool _allocation_failed;
 73 
 74 public:
 75   RecalculateUsedTask(bool evacuation_failed, bool allocation_failed) :
 76     G1AbstractSubTask(G1GCPhaseTimes::RecalculateUsed),
 77     _evacuation_failed(evacuation_failed),
 78     _allocation_failed(allocation_failed) { }
 79 
 80   double worker_cost() const override {
 81     // If there is no evacuation failure, the work to perform is minimal.
 82     return _evacuation_failed ? 1.0 : AlmostNoWork;
 83   }
 84 
 85   void do_work(uint worker_id) override {
 86     G1CollectedHeap::heap()->update_used_after_gc(_evacuation_failed);
 87     if (_allocation_failed) {
 88       // Reset the G1GCAllocationFailureALot counters and flags
 89       G1CollectedHeap::heap()->allocation_failure_injector()->reset();
 90     }
 91   }
 92 };
 93 
 94 class G1PostEvacuateCollectionSetCleanupTask1::SampleCollectionSetCandidatesTask : public G1AbstractSubTask {
 95 public:
 96   SampleCollectionSetCandidatesTask() : G1AbstractSubTask(G1GCPhaseTimes::SampleCollectionSetCandidates) { }
 97 
 98   static bool should_execute() {
 99     return G1CollectedHeap::heap()->should_sample_collection_set_candidates();
100   }
101 
102   double worker_cost() const override {
103     return should_execute() ? 1.0 : AlmostNoWork;
104   }
105 
106   void do_work(uint worker_id) override {
107     G1CollectedHeap* g1h = G1CollectedHeap::heap();
108 
109     G1MonotonicArenaMemoryStats _total;
110     G1CollectionSetCandidates* candidates = g1h->collection_set()->candidates();
111     for (G1CSetCandidateGroup* gr : candidates->from_marking_groups()) {
112       _total.add(gr->card_set_memory_stats());
113     }
114 
115     for (G1CSetCandidateGroup* gr : candidates->retained_groups()) {
116       _total.add(gr->card_set_memory_stats());
117     }
118     g1h->set_collection_set_candidates_stats(_total);
119   }
120 };
121 
122 class G1PostEvacuateCollectionSetCleanupTask1::RestoreEvacFailureRegionsTask : public G1AbstractSubTask {
123   G1CollectedHeap* _g1h;
124   G1ConcurrentMark* _cm;
125 
126   G1EvacFailureRegions* _evac_failure_regions;
127   CHeapBitMap _chunk_bitmap;
128 
129   uint _num_chunks_per_region;
130   uint _num_evac_fail_regions;
131   size_t _chunk_size;
132 
133   class PhaseTimesStat {
134     static constexpr G1GCPhaseTimes::GCParPhases phase_name =
135       G1GCPhaseTimes::RemoveSelfForwards;
136 
137     G1GCPhaseTimes* _phase_times;
138     uint _worker_id;
139     Ticks _start;
140 
141   public:
142     PhaseTimesStat(G1GCPhaseTimes* phase_times, uint worker_id) :
143       _phase_times(phase_times),
144       _worker_id(worker_id),
145       _start(Ticks::now()) { }
146 
147     ~PhaseTimesStat() {
148       _phase_times->record_or_add_time_secs(phase_name,
149                                             _worker_id,
150                                             (Ticks::now() - _start).seconds());
151     }
152 
153     void register_empty_chunk() {
154       _phase_times->record_or_add_thread_work_item(phase_name,
155                                                    _worker_id,
156                                                    1,
157                                                    G1GCPhaseTimes::RemoveSelfForwardEmptyChunksNum);
158     }
159 
160     void register_nonempty_chunk() {
161       _phase_times->record_or_add_thread_work_item(phase_name,
162                                                    _worker_id,
163                                                    1,
164                                                    G1GCPhaseTimes::RemoveSelfForwardChunksNum);
165     }
166 
167     void register_objects_count_and_size(size_t num_marked_obj, size_t marked_words) {
168       _phase_times->record_or_add_thread_work_item(phase_name,
169                                                    _worker_id,
170                                                    num_marked_obj,
171                                                    G1GCPhaseTimes::RemoveSelfForwardObjectsNum);
172 
173       size_t marked_bytes = marked_words * HeapWordSize;
174       _phase_times->record_or_add_thread_work_item(phase_name,
175                                                    _worker_id,
176                                                    marked_bytes,
177                                                    G1GCPhaseTimes::RemoveSelfForwardObjectsBytes);
178     }
179   };
180 
181   // Fill the memory area from start to end with filler objects, and update the BOT
182   // accordingly. Since we clear and use the bitmap for marking objects that failed
183   // evacuation, there is no other work to be done there.
184   static size_t zap_dead_objects(G1HeapRegion* hr, HeapWord* start, HeapWord* end) {
185     assert(start <= end, "precondition");
186     if (start == end) {
187       return 0;
188     }
189 
190     hr->fill_range_with_dead_objects(start, end);
191     return pointer_delta(end, start);
192   }
193 
194   static void update_garbage_words_in_hr(G1HeapRegion* hr, size_t garbage_words) {
195     if (garbage_words != 0) {
196       hr->note_self_forward_chunk_done(garbage_words * HeapWordSize);
197     }
198   }
199 
200   static void prefetch_obj(HeapWord* obj_addr) {
201     Prefetch::write(obj_addr, PrefetchScanIntervalInBytes);
202   }
203 
204   bool claim_chunk(uint chunk_idx) {
205     return _chunk_bitmap.par_set_bit(chunk_idx);
206   }
207 
208   void process_chunk(uint worker_id, uint chunk_idx) {
209     PhaseTimesStat stat(_g1h->phase_times(), worker_id);
210 
211     G1CMBitMap* bitmap = _cm->mark_bitmap();
212     const uint region_idx = _evac_failure_regions->get_region_idx(chunk_idx / _num_chunks_per_region);
213     G1HeapRegion* hr = _g1h->region_at(region_idx);
214 
215     HeapWord* hr_bottom = hr->bottom();
216     HeapWord* hr_top = hr->top();
217     HeapWord* chunk_start = hr_bottom + (chunk_idx % _num_chunks_per_region) * _chunk_size;
218 
219     assert(chunk_start < hr->end(), "inv");
220     if (chunk_start >= hr_top) {
221       return;
222     }
223 
224     HeapWord* chunk_end = MIN2(chunk_start + _chunk_size, hr_top);
225     HeapWord* first_marked_addr = bitmap->get_next_marked_addr(chunk_start, hr_top);
226 
227     size_t garbage_words = 0;
228 
229     if (chunk_start == hr_bottom) {
230       // This is the bottom-most chunk in this region; zap [bottom, first_marked_addr).
231       garbage_words += zap_dead_objects(hr, hr_bottom, first_marked_addr);
232     }
233 
234     if (first_marked_addr >= chunk_end) {
235       stat.register_empty_chunk();
236       update_garbage_words_in_hr(hr, garbage_words);
237       return;
238     }
239 
240     stat.register_nonempty_chunk();
241 
242     size_t num_marked_objs = 0;
243     size_t marked_words = 0;
244 
245     HeapWord* obj_addr = first_marked_addr;
246     assert(chunk_start <= obj_addr && obj_addr < chunk_end,
247            "object " PTR_FORMAT " must be within chunk [" PTR_FORMAT ", " PTR_FORMAT "[",
248            p2i(obj_addr), p2i(chunk_start), p2i(chunk_end));
249     do {
250       assert(bitmap->is_marked(obj_addr), "inv");
251       prefetch_obj(obj_addr);
252 
253       oop obj = cast_to_oop(obj_addr);
254       const size_t obj_size = obj->size();
255       HeapWord* const obj_end_addr = obj_addr + obj_size;
256 
257       {
258         // Process marked object.
259         assert(obj->is_self_forwarded(), "must be self-forwarded");
260         obj->unset_self_forwarded();
261         hr->update_bot_for_block(obj_addr, obj_end_addr);
262 
263         // Statistics
264         num_marked_objs++;
265         marked_words += obj_size;
266       }
267 
268       assert(obj_end_addr <= hr_top, "inv");
269       // Use hr_top as the limit so that we zap dead ranges up to the next
270       // marked obj or hr_top.
271       HeapWord* next_marked_obj_addr = bitmap->get_next_marked_addr(obj_end_addr, hr_top);
272       garbage_words += zap_dead_objects(hr, obj_end_addr, next_marked_obj_addr);
273       obj_addr = next_marked_obj_addr;
274     } while (obj_addr < chunk_end);
275 
276     assert(marked_words > 0 && num_marked_objs > 0, "inv");
277 
278     stat.register_objects_count_and_size(num_marked_objs, marked_words);
279 
280     update_garbage_words_in_hr(hr, garbage_words);
281   }
282 
283 public:
284   RestoreEvacFailureRegionsTask(G1EvacFailureRegions* evac_failure_regions) :
285     G1AbstractSubTask(G1GCPhaseTimes::RestoreEvacuationFailedRegions),
286     _g1h(G1CollectedHeap::heap()),
287     _cm(_g1h->concurrent_mark()),
288     _evac_failure_regions(evac_failure_regions),
289     _chunk_bitmap(mtGC) {
290 
291     _num_evac_fail_regions = _evac_failure_regions->num_regions_evac_failed();
292     _num_chunks_per_region = G1CollectedHeap::get_chunks_per_region_for_scan();
293 
294     _chunk_size = static_cast<uint>(G1HeapRegion::GrainWords / _num_chunks_per_region);
295 
296     log_debug(gc, ergo)("Initializing removing self forwards with %u chunks per region",
297                         _num_chunks_per_region);
298 
299     _chunk_bitmap.resize(_num_chunks_per_region * _num_evac_fail_regions);
300   }
301 
302   double worker_cost() const override {
303     assert(_evac_failure_regions->has_regions_evac_failed(), "Should not call this if there were no evacuation failures");
304 
305     double workers_per_region = (double)G1CollectedHeap::get_chunks_per_region_for_scan() / G1RestoreRetainedRegionChunksPerWorker;
306     return workers_per_region * _evac_failure_regions->num_regions_evac_failed();
307   }
308 
309   void do_work(uint worker_id) override {
310     const uint total_workers = G1CollectedHeap::heap()->workers()->active_workers();
311     const uint total_chunks = _num_chunks_per_region * _num_evac_fail_regions;
312     const uint start_chunk_idx = worker_id * total_chunks / total_workers;
313 
314     for (uint i = 0; i < total_chunks; i++) {
315       const uint chunk_idx = (start_chunk_idx + i) % total_chunks;
316       if (claim_chunk(chunk_idx)) {
317         process_chunk(worker_id, chunk_idx);
318       }
319     }
320   }
321 };
322 
323 G1PostEvacuateCollectionSetCleanupTask1::G1PostEvacuateCollectionSetCleanupTask1(G1ParScanThreadStateSet* per_thread_states,
324                                                                                  G1EvacFailureRegions* evac_failure_regions) :
325   G1BatchedTask("Post Evacuate Cleanup 1", G1CollectedHeap::heap()->phase_times())
326 {
327   bool evac_failed = evac_failure_regions->has_regions_evac_failed();
328   bool alloc_failed = evac_failure_regions->has_regions_alloc_failed();
329 
330   add_serial_task(new MergePssTask(per_thread_states));
331   add_serial_task(new RecalculateUsedTask(evac_failed, alloc_failed));
332   if (SampleCollectionSetCandidatesTask::should_execute()) {
333     add_serial_task(new SampleCollectionSetCandidatesTask());
334   }
335   add_parallel_task(G1CollectedHeap::heap()->rem_set()->create_cleanup_after_scan_heap_roots_task());
336   if (evac_failed) {
337     add_parallel_task(new RestoreEvacFailureRegionsTask(evac_failure_regions));
338   }
339 }
340 
341 class G1FreeHumongousRegionClosure : public G1HeapRegionIndexClosure {
342   uint _humongous_objects_reclaimed;
343   uint _humongous_regions_reclaimed;
344   size_t _freed_bytes;
345   G1CollectedHeap* _g1h;
346 
347   // Returns whether the given humongous object defined by the start region index
348   // is reclaimable.
349   //
350   // At this point in the garbage collection, checking whether the humongous object
351   // is still a candidate is sufficient because:
352   //
353   // - if it has not been a candidate at the start of collection, it will never
354   // changed to be a candidate during the gc (and live).
355   // - any found outstanding (i.e. in its remembered set, or from the collection
356   // set) references will set the candidate state to false.
357   // - there can be no references from within humongous starts regions referencing
358   // the object because we never allocate other objects into them.
359   // (I.e. there can be no intra-region references within humongous objects)
360   //
361   // It is not required to check whether the object has been found dead by marking
362   // or not, in fact it would prevent reclamation within a concurrent cycle, as
363   // all objects allocated during that time are considered live.
364   // SATB marking is even more conservative than the remembered set.
365   // So if at this point in the collection we did not find a reference during gc
366   // (or it had enough references to not be a candidate, having many remembered
367   // set entries), nobody has a reference to it.
368   //
369   // Since remembered sets are only ever updated by concurrent refinement threads
370   // at mutator time, the remembered sets do not need to be checked again.
371   //
372   // Other implementation considerations:
373   // - never consider non-typeArrays during marking as there is a considerable cost
374   // for maintaining the SATB invariant.
375   bool is_reclaimable(uint region_idx) const {
376     return G1CollectedHeap::heap()->is_humongous_reclaim_candidate(region_idx);
377   }
378 
379 public:
380   G1FreeHumongousRegionClosure() :
381     _humongous_objects_reclaimed(0),
382     _humongous_regions_reclaimed(0),
383     _freed_bytes(0),
384     _g1h(G1CollectedHeap::heap())
385   {}
386 
387   bool do_heap_region_index(uint region_index) override {
388     if (!is_reclaimable(region_index)) {
389       return false;
390     }
391 
392     G1HeapRegion* r = _g1h->region_at(region_index);
393 
394     oop obj = cast_to_oop(r->bottom());
395     {
396       ResourceMark rm;
397       bool mark_in_progress = _g1h->collector_state()->is_in_marking();
398       bool allocated_after_mark_start = false;
399       if (mark_in_progress) {
400         // top_at_mark_start() will assert if we are not in marking, so check first.
401         allocated_after_mark_start = r->bottom() == _g1h->concurrent_mark()->top_at_mark_start(r);
402       }
403 
404       guarantee(_g1h->can_be_marked_through_immediately(obj) || (allocated_after_mark_start || !mark_in_progress),
405                 "Only eagerly reclaiming arrays without oops is always supported, other humongous objects only if allocated after mark start, but the object "
406                 PTR_FORMAT " (%s) is not (allocated after mark: %d mark in progress %d marked immediately %d is_array %d array_with_oops %d).",
407                 p2i(r->bottom()), obj->klass()->name()->as_C_string(), allocated_after_mark_start, mark_in_progress, _g1h->can_be_marked_through_immediately(obj), obj->is_array(), obj->is_array_with_oops());
408     }
409     log_debug(gc, humongous)("Reclaimed humongous region %u (object size %zu @ " PTR_FORMAT ")",
410                              region_index,
411                              obj->size() * HeapWordSize,
412                              p2i(r->bottom())
413                             );
414 
415     G1ConcurrentMark* const cm = _g1h->concurrent_mark();
416     cm->humongous_object_eagerly_reclaimed(r);
417     assert(!cm->is_marked_in_bitmap(obj),
418            "Eagerly reclaimed humongous region %u should not be marked at all but is in bitmap %s",
419            region_index,
420            BOOL_TO_STR(cm->is_marked_in_bitmap(obj)));
421     _humongous_objects_reclaimed++;
422 
423     auto free_humongous_region = [&] (G1HeapRegion* r) {
424       _freed_bytes += r->used();
425       r->set_containing_set(nullptr);
426       _humongous_regions_reclaimed++;
427       G1HeapRegionPrinter::eager_reclaim(r);
428       // Humongous non-typeArrays may have dirty card tables. Need to be cleared. Do it
429       // for all types just in case.
430       r->clear_both_card_tables();
431       _g1h->free_humongous_region(r, nullptr);
432     };
433 
434     _g1h->humongous_obj_regions_iterate(r, free_humongous_region);
435 
436     return false;
437   }
438 
439   uint humongous_objects_reclaimed() {
440     return _humongous_objects_reclaimed;
441   }
442 
443   uint humongous_regions_reclaimed() {
444     return _humongous_regions_reclaimed;
445   }
446 
447   size_t bytes_freed() const {
448     return _freed_bytes;
449   }
450 };
451 
452 #ifdef COMPILER2
453 class G1PostEvacuateCollectionSetCleanupTask2::UpdateDerivedPointersTask : public G1AbstractSubTask {
454 public:
455   UpdateDerivedPointersTask() : G1AbstractSubTask(G1GCPhaseTimes::UpdateDerivedPointers) { }
456 
457   double worker_cost() const override { return 1.0; }
458   void do_work(uint worker_id) override {   DerivedPointerTable::update_pointers(); }
459 };
460 #endif // COMPILER2
461 
462 class G1PostEvacuateCollectionSetCleanupTask2::EagerlyReclaimHumongousObjectsTask : public G1AbstractSubTask {
463   uint _humongous_regions_reclaimed;
464   size_t _bytes_freed;
465 
466 public:
467   EagerlyReclaimHumongousObjectsTask() :
468     G1AbstractSubTask(G1GCPhaseTimes::EagerlyReclaimHumongousObjects),
469     _humongous_regions_reclaimed(0),
470     _bytes_freed(0) { }
471 
472   virtual ~EagerlyReclaimHumongousObjectsTask() {
473     G1CollectedHeap* g1h = G1CollectedHeap::heap();
474 
475     g1h->remove_from_old_gen_sets(0, _humongous_regions_reclaimed);
476     g1h->decrement_summary_bytes(_bytes_freed);
477   }
478 
479   double worker_cost() const override { return 1.0; }
480   void do_work(uint worker_id) override {
481     G1CollectedHeap* g1h = G1CollectedHeap::heap();
482 
483     G1FreeHumongousRegionClosure cl;
484     g1h->heap_region_iterate(&cl);
485 
486     record_work_item(worker_id, G1GCPhaseTimes::EagerlyReclaimNumTotal, g1h->num_humongous_objects());
487     record_work_item(worker_id, G1GCPhaseTimes::EagerlyReclaimNumCandidates, g1h->num_humongous_reclaim_candidates());
488     record_work_item(worker_id, G1GCPhaseTimes::EagerlyReclaimNumReclaimed, cl.humongous_objects_reclaimed());
489 
490     _humongous_regions_reclaimed = cl.humongous_regions_reclaimed();
491     _bytes_freed = cl.bytes_freed();
492   }
493 };
494 
495 class G1PostEvacuateCollectionSetCleanupTask2::ProcessEvacuationFailedRegionsTask : public G1AbstractSubTask {
496   G1EvacFailureRegions* _evac_failure_regions;
497   G1HeapRegionClaimer _claimer;
498 
499   class ProcessEvacuationFailedRegionsClosure : public G1HeapRegionClosure {
500   public:
501 
502     bool do_heap_region(G1HeapRegion* r) override {
503       G1CollectedHeap* g1h = G1CollectedHeap::heap();
504       G1ConcurrentMark* cm = g1h->concurrent_mark();
505 
506       // Retained regions are root regions for marking, so we must clear their mark data
507       // (tams, bitmap, ...). Outside of Concurrent Start GC we must always clear the mark data
508       // for the next GC.
509       bool clear_mark_data = !g1h->collector_state()->is_in_concurrent_start_gc() ||
510                              g1h->policy()->should_retain_evac_failed_region(r);
511 
512       if (clear_mark_data) {
513         g1h->clear_bitmap_for_region(r);
514         // Must be because this is a region that should not have been selected to
515         // be marked through.
516         cm->assert_top_at_mark_start_is_bottom(r);
517       } else {
518         // This evacuation failed region is going to be marked through. Update mark data.
519         // Since we have some marked live data information, pass that too.
520         cm->assert_statistics_clear(r);
521         cm->notify_new_region(r, r->live_bytes());
522       }
523       return false;
524     }
525   };
526 
527 public:
528   ProcessEvacuationFailedRegionsTask(G1EvacFailureRegions* evac_failure_regions) :
529     G1AbstractSubTask(G1GCPhaseTimes::ProcessEvacuationFailedRegions),
530     _evac_failure_regions(evac_failure_regions),
531     _claimer(0) {
532   }
533 
534   void set_max_workers(uint max_workers) override {
535     _claimer.set_n_workers(max_workers);
536   }
537 
538   double worker_cost() const override {
539     return _evac_failure_regions->num_regions_evac_failed();
540   }
541 
542   void do_work(uint worker_id) override {
543     ProcessEvacuationFailedRegionsClosure cl;
544     _evac_failure_regions->par_iterate(&cl, &_claimer, worker_id);
545   }
546 };
547 
548 // Helper class to keep statistics for the collection set freeing
549 class FreeCSetStats {
550   size_t _before_used_bytes;   // Usage in regions successfully evacuate
551   size_t _after_used_bytes;    // Usage in regions failing evacuation
552   size_t _bytes_allocated_in_old_since_last_pause; // Size of young regions turned into old
553   size_t _failure_used_words;  // Live size in failed regions
554   size_t _failure_waste_words; // Wasted size in failed regions
555   uint _regions_freed;         // Number of regions freed
556 
557 public:
558   FreeCSetStats() :
559       _before_used_bytes(0),
560       _after_used_bytes(0),
561       _bytes_allocated_in_old_since_last_pause(0),
562       _failure_used_words(0),
563       _failure_waste_words(0),
564       _regions_freed(0) { }
565 
566   void merge_stats(FreeCSetStats* other) {
567     assert(other != nullptr, "invariant");
568     _before_used_bytes += other->_before_used_bytes;
569     _after_used_bytes += other->_after_used_bytes;
570     _bytes_allocated_in_old_since_last_pause += other->_bytes_allocated_in_old_since_last_pause;
571     _failure_used_words += other->_failure_used_words;
572     _failure_waste_words += other->_failure_waste_words;
573     _regions_freed += other->_regions_freed;
574   }
575 
576   void report(G1CollectedHeap* g1h, G1EvacInfo* evacuation_info) {
577     evacuation_info->set_regions_freed(_regions_freed);
578     evacuation_info->set_collection_set_used_before(_before_used_bytes + _after_used_bytes);
579     evacuation_info->increment_collection_set_used_after(_after_used_bytes);
580 
581     g1h->decrement_summary_bytes(_before_used_bytes);
582     g1h->alloc_buffer_stats(G1HeapRegionAttr::Old)->add_failure_used_and_waste(_failure_used_words, _failure_waste_words);
583 
584     G1Policy *policy = g1h->policy();
585     policy->old_gen_alloc_tracker()->add_allocated_non_humongous_bytes(_bytes_allocated_in_old_since_last_pause);
586 
587     policy->cset_regions_freed();
588   }
589 
590   void account_failed_region(G1HeapRegion* r) {
591     size_t used_words = r->live_bytes() / HeapWordSize;
592     _failure_used_words += used_words;
593     _failure_waste_words += G1HeapRegion::GrainWords - used_words;
594     _after_used_bytes += r->used();
595 
596     // When moving a young gen region to old gen, we "allocate" that whole
597     // region there. This is in addition to any already evacuated objects.
598     // Notify the policy about that. Old gen regions do not cause an
599     // additional allocation: both the objects still in the region and the
600     // ones already moved are accounted for elsewhere.
601     if (r->is_young()) {
602       _bytes_allocated_in_old_since_last_pause += G1HeapRegion::GrainBytes;
603     }
604   }
605 
606   void account_evacuated_region(G1HeapRegion* r) {
607     size_t used = r->used();
608     assert(used > 0, "region %u %s zero used", r->hrm_index(), r->get_short_type_str());
609     _before_used_bytes += used;
610     _regions_freed += 1;
611   }
612 };
613 
614 // Closure applied to all regions in the collection set.
615 class FreeCSetClosure : public G1HeapRegionClosure {
616   // Helper to send JFR events for regions.
617   class JFREventForRegion {
618     EventGCPhaseParallel _event;
619 
620   public:
621     JFREventForRegion(G1HeapRegion* region, uint worker_id) : _event() {
622       _event.set_gcId(GCId::current());
623       _event.set_gcWorkerId(worker_id);
624       if (region->is_young()) {
625         _event.set_name(G1GCPhaseTimes::phase_name(G1GCPhaseTimes::YoungFreeCSet));
626       } else {
627         _event.set_name(G1GCPhaseTimes::phase_name(G1GCPhaseTimes::NonYoungFreeCSet));
628       }
629     }
630 
631     ~JFREventForRegion() {
632       _event.commit();
633     }
634   };
635 
636   // Helper to do timing for region work.
637   class TimerForRegion {
638     Tickspan& _time;
639     Ticks     _start_time;
640   public:
641     TimerForRegion(Tickspan& time) : _time(time), _start_time(Ticks::now()) { }
642     ~TimerForRegion() {
643       _time += Ticks::now() - _start_time;
644     }
645   };
646 
647   // FreeCSetClosure members
648   G1CollectedHeap* _g1h;
649   const size_t*    _surviving_young_words;
650   uint             _worker_id;
651   Tickspan         _young_time;
652   Tickspan         _non_young_time;
653   FreeCSetStats*   _stats;
654   G1EvacFailureRegions* _evac_failure_regions;
655   uint             _num_retained_regions;
656 
657   void assert_tracks_surviving_words(G1HeapRegion* r) {
658     assert(r->young_index_in_cset() != 0 &&
659            (uint)r->young_index_in_cset() <= _g1h->collection_set()->num_young_regions(),
660            "Young index %u is wrong for region %u of type %s with %u young regions",
661            r->young_index_in_cset(), r->hrm_index(), r->get_type_str(), _g1h->collection_set()->num_young_regions());
662   }
663 
664   void handle_evacuated_region(G1HeapRegion* r) {
665     assert(!r->is_empty(), "Region %u is an empty region in the collection set.", r->hrm_index());
666     stats()->account_evacuated_region(r);
667 
668     G1HeapRegionPrinter::evac_reclaim(r);
669     // Free the region and its remembered set.
670     _g1h->free_region(r, nullptr);
671   }
672 
673   void handle_failed_region(G1HeapRegion* r) {
674     // Do some allocation statistics accounting. Regions that failed evacuation
675     // are always made old, so there is no need to update anything in the young
676     // gen statistics, but we need to update old gen statistics.
677     stats()->account_failed_region(r);
678 
679     G1GCPhaseTimes* p = _g1h->phase_times();
680     assert(r->in_collection_set(), "Failed evacuation of region %u not in collection set", r->hrm_index());
681 
682     p->record_or_add_thread_work_item(G1GCPhaseTimes::RestoreEvacuationFailedRegions,
683                                       _worker_id,
684                                       1,
685                                       G1GCPhaseTimes::RestoreEvacFailureRegionsEvacFailedNum);
686 
687     bool retain_region = _g1h->policy()->should_retain_evac_failed_region(r);
688     // Update the region state due to the failed evacuation.
689     r->handle_evacuation_failure(retain_region);
690     assert(r->is_old(), "must already be relabelled as old");
691 
692     if (retain_region) {
693       _g1h->retain_region(r);
694       _num_retained_regions++;
695     }
696     assert(retain_region == r->rem_set()->is_tracked(), "When retaining a region, remembered set should be kept.");
697 
698     // Add region to old set, need to hold lock.
699     MutexLocker x(G1OldSets_lock, Mutex::_no_safepoint_check_flag);
700     _g1h->old_set_add(r);
701   }
702 
703   Tickspan& timer_for_region(G1HeapRegion* r) {
704     return r->is_young() ? _young_time : _non_young_time;
705   }
706 
707   FreeCSetStats* stats() {
708     return _stats;
709   }
710 
711 public:
712   FreeCSetClosure(const size_t* surviving_young_words,
713                   uint worker_id,
714                   FreeCSetStats* stats,
715                   G1EvacFailureRegions* evac_failure_regions) :
716       G1HeapRegionClosure(),
717       _g1h(G1CollectedHeap::heap()),
718       _surviving_young_words(surviving_young_words),
719       _worker_id(worker_id),
720       _young_time(),
721       _non_young_time(),
722       _stats(stats),
723       _evac_failure_regions(evac_failure_regions),
724       _num_retained_regions(0) { }
725 
726   virtual bool do_heap_region(G1HeapRegion* r) {
727     assert(r->in_collection_set(), "Invariant: %u missing from CSet", r->hrm_index());
728     JFREventForRegion event(r, _worker_id);
729     TimerForRegion timer(timer_for_region(r));
730 
731     if (r->is_young()) {
732       assert_tracks_surviving_words(r);
733       r->record_surv_words_in_group(_surviving_young_words[r->young_index_in_cset()]);
734     }
735 
736     if (_evac_failure_regions->contains(r->hrm_index())) {
737       handle_failed_region(r);
738     } else {
739       handle_evacuated_region(r);
740     }
741     assert(!_g1h->is_on_master_free_list(r), "sanity");
742 
743     return false;
744   }
745 
746   void report_timing() {
747     G1GCPhaseTimes* pt = _g1h->phase_times();
748     if (_young_time.value() > 0) {
749       pt->record_time_secs(G1GCPhaseTimes::YoungFreeCSet, _worker_id, _young_time.seconds());
750     }
751     if (_non_young_time.value() > 0) {
752       pt->record_time_secs(G1GCPhaseTimes::NonYoungFreeCSet, _worker_id, _non_young_time.seconds());
753     }
754   }
755 
756   bool num_retained_regions() const { return _num_retained_regions; }
757 };
758 
759 class G1PostEvacuateCollectionSetCleanupTask2::FreeCollectionSetTask : public G1AbstractSubTask {
760   G1CollectedHeap*    _g1h;
761   G1EvacInfo*         _evacuation_info;
762   FreeCSetStats*      _worker_stats;
763   G1HeapRegionClaimer _claimer;
764   const size_t*       _surviving_young_words;
765   uint                _active_workers;
766   G1EvacFailureRegions* _evac_failure_regions;
767   Atomic<uint>        _num_retained_regions;
768 
769   FreeCSetStats* worker_stats(uint worker) {
770     return &_worker_stats[worker];
771   }
772 
773   void report_statistics() {
774     // Merge the accounting
775     FreeCSetStats total_stats;
776     for (uint worker = 0; worker < _active_workers; worker++) {
777       total_stats.merge_stats(worker_stats(worker));
778     }
779     total_stats.report(_g1h, _evacuation_info);
780   }
781 
782 public:
783   FreeCollectionSetTask(G1EvacInfo* evacuation_info,
784                         const size_t* surviving_young_words,
785                         G1EvacFailureRegions* evac_failure_regions) :
786     G1AbstractSubTask(G1GCPhaseTimes::FreeCollectionSet),
787     _g1h(G1CollectedHeap::heap()),
788     _evacuation_info(evacuation_info),
789     _worker_stats(nullptr),
790     _claimer(0),
791     _surviving_young_words(surviving_young_words),
792     _active_workers(0),
793     _evac_failure_regions(evac_failure_regions),
794     _num_retained_regions(0) {
795 
796     _g1h->clear_eden();
797   }
798 
799   virtual ~FreeCollectionSetTask() {
800     Ticks serial_time = Ticks::now();
801 
802     bool has_new_retained_regions = _num_retained_regions.load_relaxed() != 0;
803     if (has_new_retained_regions) {
804       G1CollectionSetCandidates* candidates = _g1h->collection_set()->candidates();
805       candidates->sort_by_efficiency();
806     }
807 
808     report_statistics();
809     for (uint worker = 0; worker < _active_workers; worker++) {
810       _worker_stats[worker].~FreeCSetStats();
811     }
812     FREE_C_HEAP_ARRAY(_worker_stats);
813 
814     _g1h->clear_collection_set();
815 
816     G1GCPhaseTimes* p = _g1h->phase_times();
817     p->record_serial_free_cset_time_ms((Ticks::now() - serial_time).seconds() * 1000.0);
818   }
819 
820   double worker_cost() const override { return G1CollectedHeap::heap()->collection_set()->num_initial_regions(); }
821 
822   void set_max_workers(uint max_workers) override {
823     _active_workers = max_workers;
824     _worker_stats = NEW_C_HEAP_ARRAY(FreeCSetStats, max_workers, mtGC);
825     ::new (_worker_stats) FreeCSetStats[_active_workers]{};
826     _claimer.set_n_workers(_active_workers);
827   }
828 
829   void do_work(uint worker_id) override {
830     FreeCSetClosure cl(_surviving_young_words, worker_id, worker_stats(worker_id), _evac_failure_regions);
831     _g1h->collection_set_par_iterate_all(&cl, &_claimer, worker_id);
832     // Report per-region type timings.
833     cl.report_timing();
834 
835     _num_retained_regions.add_then_fetch(cl.num_retained_regions(), memory_order_relaxed);
836   }
837 };
838 
839 class G1PostEvacuateCollectionSetCleanupTask2::ResizeTLABsAndSwapCardTableTask : public G1AbstractSubTask {
840   G1JavaThreadsListClaimer _claimer;
841 
842   // There is not much work per thread so the number of threads per worker is high.
843   static const uint ThreadsPerWorker = 250;
844 
845 public:
846   ResizeTLABsAndSwapCardTableTask()
847     : G1AbstractSubTask(G1GCPhaseTimes::ResizeThreadLABs), _claimer(ThreadsPerWorker)
848   {
849     G1BarrierSet::g1_barrier_set()->swap_global_card_table();
850   }
851 
852   void do_work(uint worker_id) override {
853 
854     class ResizeAndSwapCardTableClosure : public ThreadClosure {
855     public:
856 
857       void do_thread(Thread* thread) {
858         if (UseTLAB && ResizeTLAB) {
859           thread->tlab().resize();
860         }
861 
862         G1BarrierSet::g1_barrier_set()->update_card_table_base(thread);
863       }
864     } resize_and_swap_cl;
865 
866     _claimer.apply(&resize_and_swap_cl);
867   }
868 
869   double worker_cost() const override {
870     return (double)_claimer.length() / ThreadsPerWorker;
871   }
872 };
873 
874 class G1PostEvacuateCollectionSetCleanupTask2::ResetPartialArrayStateManagerTask
875   : public G1AbstractSubTask
876 {
877 public:
878   ResetPartialArrayStateManagerTask()
879     : G1AbstractSubTask(G1GCPhaseTimes::ResetPartialArrayStateManager)
880   {}
881 
882   double worker_cost() const override {
883     return AlmostNoWork;
884   }
885 
886   void do_work(uint worker_id) override {
887     // This must be in phase2 cleanup, after phase1 has destroyed all of the
888     // associated allocators.
889     G1CollectedHeap::heap()->partial_array_state_manager()->reset();
890   }
891 };
892 
893 G1PostEvacuateCollectionSetCleanupTask2::G1PostEvacuateCollectionSetCleanupTask2(G1ParScanThreadStateSet* per_thread_states,
894                                                                                  G1EvacInfo* evacuation_info,
895                                                                                  G1EvacFailureRegions* evac_failure_regions) :
896   G1BatchedTask("Post Evacuate Cleanup 2", G1CollectedHeap::heap()->phase_times())
897 {
898 #ifdef COMPILER2
899   add_serial_task(new UpdateDerivedPointersTask());
900 #endif // COMPILER2
901   if (G1CollectedHeap::heap()->has_humongous_reclaim_candidates()) {
902     add_serial_task(new EagerlyReclaimHumongousObjectsTask());
903   }
904   add_serial_task(new ResetPartialArrayStateManagerTask());
905 
906   if (evac_failure_regions->has_regions_evac_failed()) {
907     add_parallel_task(new ProcessEvacuationFailedRegionsTask(evac_failure_regions));
908   }
909 
910   add_parallel_task(new ResizeTLABsAndSwapCardTableTask());
911   add_parallel_task(new FreeCollectionSetTask(evacuation_info,
912                                               per_thread_states->surviving_young_words(),
913                                               evac_failure_regions));
914 }