1 /*
  2  * Copyright (c) 2014, 2021, 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 #ifndef SHARE_GC_G1_G1PARSCANTHREADSTATE_HPP
 26 #define SHARE_GC_G1_G1PARSCANTHREADSTATE_HPP
 27 
 28 #include "gc/g1/g1CollectedHeap.hpp"
 29 #include "gc/g1/g1RedirtyCardsQueue.hpp"
 30 #include "gc/g1/g1OopClosures.hpp"
 31 #include "gc/g1/g1YoungGCEvacFailureInjector.hpp"
 32 #include "gc/g1/g1_globals.hpp"
 33 #include "gc/shared/ageTable.hpp"
 34 #include "gc/shared/copyFailedInfo.hpp"
 35 #include "gc/shared/partialArrayTaskStepper.hpp"
 36 #include "gc/shared/preservedMarks.hpp"
 37 #include "gc/shared/stringdedup/stringDedup.hpp"
 38 #include "gc/shared/taskqueue.hpp"
 39 #include "memory/allocation.hpp"
 40 #include "oops/oop.hpp"
 41 #include "utilities/ticks.hpp"
 42 
 43 class G1CardTable;
 44 class G1EvacFailureRegions;
 45 class G1EvacuationRootClosures;
 46 class G1OopStarChunkedList;
 47 class G1PLABAllocator;
 48 class HeapRegion;
 49 class PreservedMarks;
 50 class PreservedMarksSet;
 51 class outputStream;
 52 
 53 class G1ParScanThreadState : public CHeapObj<mtGC> {
 54   G1CollectedHeap* _g1h;
 55   G1ScannerTasksQueue* _task_queue;
 56   G1RedirtyCardsLocalQueueSet _rdc_local_qset;
 57   G1CardTable* _ct;
 58   G1EvacuationRootClosures* _closures;
 59 
 60   G1PLABAllocator* _plab_allocator;
 61 
 62   AgeTable _age_table;
 63   // Local tenuring threshold.
 64   uint _tenuring_threshold;
 65   G1ScanEvacuatedObjClosure _scanner;
 66 
 67   uint _worker_id;
 68 
 69   // Remember the last enqueued card to avoid enqueuing the same card over and over;
 70   // since we only ever scan a card once, this is sufficient.
 71   size_t _last_enqueued_card;
 72 
 73   // Upper and lower threshold to start and end work queue draining.
 74   uint const _stack_trim_upper_threshold;
 75   uint const _stack_trim_lower_threshold;
 76 
 77   Tickspan _trim_ticks;
 78   // Map from young-age-index (0 == not young, 1 is youngest) to
 79   // surviving words. base is what we get back from the malloc call
 80   size_t* _surviving_young_words_base;
 81   // this points into the array, as we use the first few entries for padding
 82   size_t* _surviving_young_words;
 83   // Number of elements in the array above.
 84   size_t _surviving_words_length;
 85   // Indicates whether in the last generation (old) there is no more space
 86   // available for allocation.
 87   bool _old_gen_is_full;
 88   // Size (in elements) of a partial objArray task chunk.
 89   int _partial_objarray_chunk_size;
 90   PartialArrayTaskStepper _partial_array_stepper;
 91   StringDedup::Requests _string_dedup_requests;
 92 
 93   G1CardTable* ct() { return _ct; }
 94 
 95   size_t _num_optional_regions;
 96   G1OopStarChunkedList* _oops_into_optional_regions;
 97 
 98   G1NUMA* _numa;
 99   // Records how many object allocations happened at each node during copy to survivor.
100   // Only starts recording when log of gc+heap+numa is enabled and its data is
101   // transferred when flushed.
102   size_t* _obj_alloc_stat;
103 
104   // Per-thread evacuation failure data structures.
105   EVAC_FAILURE_INJECTOR_ONLY(size_t _evac_failure_inject_counter;)
106 
107   PreservedMarks* _preserved_marks;
108   EvacuationFailedInfo _evacuation_failed_info;
109   G1EvacFailureRegions* _evac_failure_regions;
110 
111   bool inject_evacuation_failure(uint region_idx) EVAC_FAILURE_INJECTOR_RETURN_( return false; );
112 
113 public:
114   G1ParScanThreadState(G1CollectedHeap* g1h,
115                        G1RedirtyCardsQueueSet* rdcqs,
116                        PreservedMarks* preserved_marks,
117                        uint worker_id,
118                        uint n_workers,
119                        size_t young_cset_length,
120                        size_t optional_cset_length,
121                        G1EvacFailureRegions* evac_failure_regions);
122   virtual ~G1ParScanThreadState();
123 
124   void set_ref_discoverer(ReferenceDiscoverer* rd) { _scanner.set_ref_discoverer(rd); }
125 
126 #ifdef ASSERT
127   bool queue_is_empty() const { return _task_queue->is_empty(); }
128 #endif
129 
130   void verify_task(narrowOop* task) const NOT_DEBUG_RETURN;
131   void verify_task(oop* task) const NOT_DEBUG_RETURN;
132   void verify_task(PartialArrayScanTask task) const NOT_DEBUG_RETURN;
133   void verify_task(ScannerTask task) const NOT_DEBUG_RETURN;
134 
135   void push_on_queue(ScannerTask task);
136 
137   // Apply the post barrier to the given reference field. Enqueues the card of p
138   // if the barrier does not filter out the reference for some reason (e.g.
139   // p and q are in the same region, p is in survivor, p is in collection set)
140   // To be called during GC if nothing particular about p and obj are known.
141   template <class T> void write_ref_field_post(T* p, oop obj);
142 
143   // Enqueue the card if the reference's target region's remembered set is tracked.
144   // Assumes that a significant amount of pre-filtering (like done by
145   // write_ref_field_post() above) has already been performed.
146   template <class T> void enqueue_card_if_tracked(G1HeapRegionAttr region_attr, T* p, oop o);
147 
148   G1EvacuationRootClosures* closures() { return _closures; }
149   uint worker_id() { return _worker_id; }
150 
151   size_t lab_waste_words() const;
152   size_t lab_undo_waste_words() const;
153 
154   // Pass locally gathered statistics to global state. Returns the total number of
155   // HeapWords copied.
156   size_t flush_stats(size_t* surviving_young_words, uint num_workers);
157 
158 private:
159   void do_partial_array(PartialArrayScanTask task);
160   void start_partial_objarray(G1HeapRegionAttr dest_dir, oop from, oop to);
161 
162   HeapWord* allocate_copy_slow(G1HeapRegionAttr* dest_attr,
163                                oop old,
164                                size_t word_sz,
165                                uint age,
166                                uint node_index);
167 
168   void undo_allocation(G1HeapRegionAttr dest_addr,
169                        HeapWord* obj_ptr,
170                        size_t word_sz,
171                        uint node_index);
172 
173   void update_bot_after_copying(oop obj, size_t word_sz);
174 
175   oop do_copy_to_survivor_space(G1HeapRegionAttr region_attr,
176                                 oop obj,
177                                 markWord old_mark);
178 
179   // This method is applied to the fields of the objects that have just been copied.
180   template <class T> void do_oop_evac(T* p);
181 
182   void dispatch_task(ScannerTask task);
183 
184   // Tries to allocate word_sz in the PLAB of the next "generation" after trying to
185   // allocate into dest. Previous_plab_refill_failed indicates whether previous
186   // PLAB refill for the original (source) object failed.
187   // Returns a non-NULL pointer if successful, and updates dest if required.
188   // Also determines whether we should continue to try to allocate into the various
189   // generations or just end trying to allocate.
190   HeapWord* allocate_in_next_plab(G1HeapRegionAttr* dest,
191                                   size_t word_sz,
192                                   bool previous_plab_refill_failed,
193                                   uint node_index);
194 
195   inline G1HeapRegionAttr next_region_attr(G1HeapRegionAttr const region_attr, markWord const m, uint& age);
196 
197   void report_promotion_event(G1HeapRegionAttr const dest_attr,
198                               oop const old, size_t word_sz, uint age,
199                               HeapWord * const obj_ptr, uint node_index) const;
200 
201   void trim_queue_to_threshold(uint threshold);
202 
203   inline bool needs_partial_trimming() const;
204 
205   // NUMA statistics related methods.
206   void initialize_numa_stats();
207   void flush_numa_stats();
208   inline void update_numa_stats(uint node_index);
209 
210 public:
211   oop copy_to_survivor_space(G1HeapRegionAttr region_attr, oop obj, markWord old_mark);
212 
213   inline void trim_queue();
214   inline void trim_queue_partially();
215   void steal_and_trim_queue(G1ScannerTasksQueueSet *task_queues);
216 
217   Tickspan trim_ticks() const;
218   void reset_trim_ticks();
219 
220   // An attempt to evacuate "obj" has failed; take necessary steps.
221   oop handle_evacuation_failure_par(oop obj, markWord m, size_t word_sz);
222 
223   template <typename T>
224   inline void remember_root_into_optional_region(T* p);
225   template <typename T>
226   inline void remember_reference_into_optional_region(T* p);
227 
228   inline G1OopStarChunkedList* oops_into_optional_region(const HeapRegion* hr);
229 };
230 
231 class G1ParScanThreadStateSet : public StackObj {
232   G1CollectedHeap* _g1h;
233   G1RedirtyCardsQueueSet _rdcqs;
234   PreservedMarksSet _preserved_marks_set;
235   G1ParScanThreadState** _states;
236   size_t* _surviving_young_words_total;
237   size_t _young_cset_length;
238   size_t _optional_cset_length;
239   uint _n_workers;
240   bool _flushed;
241   G1EvacFailureRegions* _evac_failure_regions;
242 
243  public:
244   G1ParScanThreadStateSet(G1CollectedHeap* g1h,
245                           uint n_workers,
246                           size_t young_cset_length,
247                           size_t optional_cset_length,
248                           G1EvacFailureRegions* evac_failure_regions);
249   ~G1ParScanThreadStateSet();
250 
251   G1RedirtyCardsQueueSet* rdcqs() { return &_rdcqs; }
252   PreservedMarksSet* preserved_marks_set() { return &_preserved_marks_set; }
253 
254   void flush_stats();
255   void record_unused_optional_region(HeapRegion* hr);
256 
257   G1ParScanThreadState* state_for_worker(uint worker_id);
258 
259   const size_t* surviving_young_words() const;
260 };
261 
262 #endif // SHARE_GC_G1_G1PARSCANTHREADSTATE_HPP