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 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHGENERATIONALFULLGC_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHGENERATIONALFULLGC_HPP
 27 
 28 #include "gc/shared/preservedMarks.hpp"
 29 #include "memory/iterator.hpp"
 30 #include "oops/oop.inline.hpp"
 31 #include "utilities/growableArray.hpp"
 32 
 33 class ShenandoahHeap;
 34 class ShenandoahHeapRegion;
 35 
 36 class ShenandoahGenerationalFullGC {
 37 public:
 38   // Prepares the generational mode heap for a full collection.
 39   static void prepare();
 40 
 41   // Full GC may have compacted objects in the old generation, so we need to rebuild the card tables.
 42   static void rebuild_remembered_set(ShenandoahHeap* heap);
 43 
 44   // Records end of cycle for young and old and establishes size of live bytes in old
 45   static void handle_completion(ShenandoahHeap* heap);
 46 
 47   // Full GC may have promoted regions and may have temporarily violated constraints on the usage and
 48   // capacity of the old generation. This method will balance the accounting of regions between the
 49   // young and old generations. This is somewhat vestigial, but the outcome of this method is used
 50   // when rebuilding the free sets.
 51   static void balance_generations_after_gc(ShenandoahHeap* heap);
 52 
 53   // This will compute the target size for the old generation. It will be expressed in terms of
 54   // a region surplus and deficit, which will be redistributed accordingly after rebuilding the
 55   // free set.
 56   static void compute_balances();
 57 
 58   // Rebuilding the free set may have resulted in regions being pulled in to the old generation
 59   // evacuation reserve. For this reason, we must update the usage and capacity of the generations
 60   // again. In the distant past, the free set did not know anything about generations, so we had
 61   // a layer built above it to represent how much young/old memory was available. This layer is
 62   // redundant and adds complexity. We would like to one day remove it. Until then, we must keep it
 63   // synchronized with the free set's view of things.
 64   static void balance_generations_after_rebuilding_free_set();
 65 
 66   // Logs the number of live bytes marked in the old generation. This is _not_ the same
 67   // value used as the baseline for the old generation _after_ the full gc is complete.
 68   // The value reported in the logs does not include objects and regions that may be
 69   // promoted during the full gc.
 70   static void log_live_in_old(ShenandoahHeap* heap);
 71 
 72   // This is used to tally the number, usage and space wasted by humongous objects for each generation.
 73   static void account_for_region(ShenandoahHeapRegion* r, size_t &region_count, size_t &region_usage, size_t &humongous_waste);
 74 
 75   // Regions which are scheduled for in-place promotion during evacuation temporarily
 76   // have their top set to their end to prevent new objects from being allocated in them
 77   // before they are promoted. If the full GC encounters such a region, it means the
 78   // in-place promotion did not happen, and we must restore the original value of top.
 79   static void restore_top_before_promote(ShenandoahHeap* heap);
 80 
 81   // Pinned regions are not compacted, so they may still hold unmarked objects with
 82   // references to reclaimed memory. Remembered set scanning will crash if it attempts
 83   // to iterate the oops in these objects. This method fills in dead objects for pinned,
 84   // old regions.
 85   static void maybe_coalesce_and_fill_region(ShenandoahHeapRegion* r);
 86 };
 87 
 88 class ShenandoahPrepareForGenerationalCompactionObjectClosure : public ObjectClosure {
 89 private:
 90   PreservedMarks*             const _preserved_marks;
 91   ShenandoahGenerationalHeap* const _heap;
 92   uint                              _tenuring_threshold;
 93 
 94   // _empty_regions is a thread-local list of heap regions that have been completely emptied by this worker thread's
 95   // compaction efforts.  The worker thread that drives these efforts adds compacted regions to this list if the
 96   // region has not been compacted onto itself.
 97   GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
 98   int _empty_regions_pos;
 99   ShenandoahHeapRegion*          _old_to_region;
100   ShenandoahHeapRegion*          _young_to_region;
101   ShenandoahHeapRegion*          _from_region;
102   ShenandoahAffiliation          _from_affiliation;
103   HeapWord*                      _old_compact_point;
104   HeapWord*                      _young_compact_point;
105   uint                           _worker_id;
106 
107 public:
108   ShenandoahPrepareForGenerationalCompactionObjectClosure(PreservedMarks* preserved_marks,
109                                                           GrowableArray<ShenandoahHeapRegion*>& empty_regions,
110                                                           ShenandoahHeapRegion* from_region, uint worker_id);
111 
112   void set_from_region(ShenandoahHeapRegion* from_region);
113   void finish();
114   void finish_old_region();
115   void finish_young_region();
116   bool is_compact_same_region();
117   int empty_regions_pos() const { return _empty_regions_pos; }
118 
119   void do_object(oop p) override;
120 };
121 
122 #endif //SHARE_GC_SHENANDOAH_SHENANDOAHGENERATIONALFULLGC_HPP