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_VM_GC_SHENANDOAH_SHENANDOAHOLDGENERATION_HPP
 26 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHOLDGENERATION_HPP
 27 
 28 #include "gc/shenandoah/shenandoahGeneration.hpp"
 29 
 30 class ShenandoahHeapRegion;
 31 class ShenandoahHeapRegionClosure;
 32 class ShenandoahOldHeuristics;
 33 
 34 class ShenandoahOldGeneration : public ShenandoahGeneration {
 35 private:
 36   ShenandoahHeapRegion** _coalesce_and_fill_region_array;
 37   ShenandoahOldHeuristics* _old_heuristics;
 38 
 39   bool coalesce_and_fill();
 40 
 41 public:
 42   ShenandoahOldGeneration(uint max_queues, size_t max_capacity, size_t soft_max_capacity);
 43 
 44   virtual ShenandoahHeuristics* initialize_heuristics(ShenandoahMode* gc_mode) override;
 45 
 46   const char* name() const override {
 47     return "OLD";
 48   }
 49 
 50   void parallel_heap_region_iterate(ShenandoahHeapRegionClosure* cl) override;
 51   void heap_region_iterate(ShenandoahHeapRegionClosure* cl) override;
 52 
 53   bool contains(ShenandoahHeapRegion* region) const override;
 54   bool contains(oop obj) const override;
 55 
 56   void set_concurrent_mark_in_progress(bool in_progress) override;
 57   bool is_concurrent_mark_in_progress() override;
 58 
 59   bool entry_coalesce_and_fill();
 60   virtual void prepare_gc() override;
 61   void prepare_regions_and_collection_set(bool concurrent) override;
 62   virtual void record_success_concurrent(bool abbreviated) override;
 63   virtual void cancel_marking() override;
 64 
 65   // We leave the SATB barrier on for the entirety of the old generation
 66   // marking phase. In some cases, this can cause a write to a perfectly
 67   // reachable oop to enqueue a pointer that later becomes garbage (because
 68   // it points at an object that is later chosen for the collection set). There are
 69   // also cases where the referent of a weak reference ends up in the SATB
 70   // and is later collected. In these cases the oop in the SATB buffer becomes
 71   // invalid and the _next_ cycle will crash during its marking phase. To
 72   // avoid this problem, we "purge" the SATB buffers during the final update
 73   // references phase if (and only if) an old generation mark is in progress.
 74   // At this stage we can safely determine if any of the oops in the SATB
 75   // buffer belong to trashed regions (before they are recycled). As it
 76   // happens, flushing a SATB queue also filters out oops which have already
 77   // been marked - which is the case for anything that is being evacuated
 78   // from the collection set.
 79   //
 80   // Alternatively, we could inspect the state of the heap and the age of the
 81   // object at the barrier, but we reject this approach because it is likely
 82   // the performance impact would be too severe.
 83   void transfer_pointers_from_satb();
 84 
 85 public:
 86   enum State {
 87     FILLING, WAITING_FOR_BOOTSTRAP, BOOTSTRAPPING, MARKING, EVACUATING
 88   };
 89 
 90 private:
 91   State _state;
 92 
 93   static const size_t FRACTIONAL_DENOMINATOR = 64536;
 94 
 95   // During initialization of the JVM, we search for the correct old-gen size by initially performing old-gen
 96   // collection when old-gen usage is 50% more (INITIAL_GROWTH_BEFORE_COMPACTION) than the initial old-gen size
 97   // estimate (3.125% of heap).  The next old-gen trigger occurs when old-gen grows 25% larger than its live
 98   // memory at the end of the first old-gen collection.  Then we trigger again when old-gen grows 12.5%
 99   // more than its live memory at the end of the previous old-gen collection.  Thereafter, we trigger each time
100   // old-gen grows more than 12.5% following the end of its previous old-gen collection.
101   static const size_t INITIAL_GROWTH_BEFORE_COMPACTION = FRACTIONAL_DENOMINATOR / 2;        //  50.0%
102 
103   // INITIAL_LIVE_FRACTION represents the initial guess of how large old-gen should be.  We estimate that old-gen
104   // needs to consume 6.25% of the total heap size.  And we "pretend" that we start out with this amount of live
105   // old-gen memory.  The first old-collection trigger will occur when old-gen occupies 50% more than this initial
106   // approximation of the old-gen memory requirement, in other words when old-gen usage is 150% of 6.25%, which
107   // is 9.375% of the total heap size.
108   static const uint16_t INITIAL_LIVE_FRACTION = FRACTIONAL_DENOMINATOR / 16;                //   6.25%
109 
110   size_t _live_bytes_after_last_mark;
111 
112   // How much growth in usage before we trigger old collection, per FRACTIONAL_DENOMINATOR (65_536)
113   size_t _growth_before_compaction;
114   const size_t _min_growth_before_compaction;                                               // Default is 12.5%
115 
116   void validate_transition(State new_state) NOT_DEBUG_RETURN;
117 
118 public:
119   State state() const {
120     return _state;
121   }
122 
123   const char* state_name() const {
124     return state_name(_state);
125   }
126 
127   void transition_to(State new_state);
128 
129   size_t get_live_bytes_after_last_mark() const;
130   void set_live_bytes_after_last_mark(size_t new_live);
131 
132   size_t usage_trigger_threshold() const;
133 
134   bool can_start_gc() {
135     return _state == WAITING_FOR_BOOTSTRAP;
136   }
137 
138   static const char* state_name(State state);
139 };
140 
141 
142 #endif //SHARE_VM_GC_SHENANDOAH_SHENANDOAHOLDGENERATION_HPP