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