1 /* 2 * Copyright (c) 2014, 2019, 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/g1CardTable.hpp" 29 #include "gc/g1/g1CollectedHeap.hpp" 30 #include "gc/g1/g1RedirtyCardsQueue.hpp" 31 #include "gc/g1/g1OopClosures.hpp" 32 #include "gc/g1/g1Policy.hpp" 33 #include "gc/g1/g1RemSet.hpp" 34 #include "gc/g1/heapRegionRemSet.hpp" 35 #include "gc/shared/ageTable.hpp" 36 #include "memory/allocation.hpp" 37 #include "oops/oop.hpp" 38 #include "utilities/ticks.hpp" 39 40 class G1OopStarChunkedList; 41 class G1PLABAllocator; 42 class G1EvacuationRootClosures; 43 class HeapRegion; 44 class outputStream; 45 46 class G1ParScanThreadState : public CHeapObj<mtGC> { 47 G1CollectedHeap* _g1h; 48 RefToScanQueue* _refs; 49 G1RedirtyCardsQueue _rdcq; 50 G1CardTable* _ct; 51 G1EvacuationRootClosures* _closures; 52 53 G1PLABAllocator* _plab_allocator; 54 55 AgeTable _age_table; 56 G1HeapRegionAttr _dest[G1HeapRegionAttr::Num]; 57 // Local tenuring threshold. 58 uint _tenuring_threshold; 59 G1ScanEvacuatedObjClosure _scanner; 60 61 uint _worker_id; 62 63 // Remember the last enqueued card to avoid enqueuing the same card over and over; 64 // since we only ever scan a card once, this is sufficient. 65 size_t _last_enqueued_card; 66 67 // Upper and lower threshold to start and end work queue draining. 68 uint const _stack_trim_upper_threshold; 69 uint const _stack_trim_lower_threshold; 70 71 Tickspan _trim_ticks; 72 73 // The amount of actually copied words into the survivor regions during evacuation. 74 size_t _surviving_survivor_words; 75 // Map from young-age-index (0 == not young, 1 is youngest) to 76 // surviving words. base is what we get back from the malloc call 77 size_t* _surviving_young_words_base; 78 // this points into the array, as we use the first few entries for padding 79 size_t* _surviving_young_words; 80 // Number of elements in the array above. 81 size_t _surviving_words_length; 82 // Indicates whether in the last generation (old) there is no more space 83 // available for allocation. 84 bool _old_gen_is_full; 85 86 #define PADDING_ELEM_NUM (DEFAULT_CACHE_LINE_SIZE / sizeof(size_t)) 87 88 G1RedirtyCardsQueue& redirty_cards_queue() { return _rdcq; } 89 G1CardTable* ct() { return _ct; } 90 91 G1HeapRegionAttr dest(G1HeapRegionAttr original) const { 92 assert(original.is_valid(), 93 "Original region attr invalid: %s", original.get_type_str()); 94 assert(_dest[original.type()].is_valid_gen(), 95 "Dest region attr is invalid: %s", _dest[original.type()].get_type_str()); 96 return _dest[original.type()]; 97 } 98 99 size_t _num_optional_regions; 100 G1OopStarChunkedList* _oops_into_optional_regions; 101 102 G1NUMA* _numa; 103 104 // Records how many object allocations happened at each node during copy to survivor. 105 // Only starts recording when log of gc+heap+numa is enabled and its data is 106 // transferred when flushed. 107 size_t* _obj_alloc_stat; 108 109 public: 110 G1ParScanThreadState(G1CollectedHeap* g1h, 111 G1RedirtyCardsQueueSet* rdcqs, 112 uint worker_id, 113 size_t young_cset_length, 114 size_t optional_cset_length); 115 virtual ~G1ParScanThreadState(); 116 117 void set_ref_discoverer(ReferenceDiscoverer* rd) { _scanner.set_ref_discoverer(rd); } 118 119 #ifdef ASSERT 120 bool queue_is_empty() const { return _refs->is_empty(); } 121 122 bool verify_ref(narrowOop* ref) const; 123 bool verify_ref(oop* ref) const; 124 bool verify_task(StarTask ref) const; 125 #endif // ASSERT 126 127 template <class T> void do_oop_ext(T* ref); 128 template <class T> void push_on_queue(T* ref); 129 130 template <class T> void enqueue_card_if_tracked(G1HeapRegionAttr region_attr, T* p, oop o) { 131 assert(!HeapRegion::is_in_same_region(p, o), "Should have filtered out cross-region references already."); 132 assert(!_g1h->heap_region_containing(p)->is_young(), "Should have filtered out from-young references already."); 133 134 #ifdef ASSERT 135 HeapRegion* const hr_obj = _g1h->heap_region_containing((HeapWord*)o); 136 assert(region_attr.needs_remset_update() == hr_obj->rem_set()->is_tracked(), 137 "State flag indicating remset tracking disagrees (%s) with actual remembered set (%s) for region %u", 138 BOOL_TO_STR(region_attr.needs_remset_update()), 139 BOOL_TO_STR(hr_obj->rem_set()->is_tracked()), 140 hr_obj->hrm_index()); 141 #endif 142 if (!region_attr.needs_remset_update()) { 143 return; 144 } 145 size_t card_index = ct()->index_for(p); 146 // If the card hasn't been added to the buffer, do it. 147 if (_last_enqueued_card != card_index) { 148 redirty_cards_queue().enqueue(ct()->byte_for_index(card_index)); 149 _last_enqueued_card = card_index; 150 } 151 } 152 153 G1EvacuationRootClosures* closures() { return _closures; } 154 uint worker_id() { return _worker_id; } 155 156 size_t lab_waste_words() const; 157 size_t lab_undo_waste_words() const; 158 159 // Pass locally gathered statistics to global state. Returns the total number of 160 // HeapWords copied. 161 size_t flush(size_t* surviving_young_words); 162 163 private: 164 #define G1_PARTIAL_ARRAY_MASK 0x2 165 166 inline bool has_partial_array_mask(oop* ref) const { 167 return ((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) == G1_PARTIAL_ARRAY_MASK; 168 } 169 170 // We never encode partial array oops as narrowOop*, so return false immediately. 171 // This allows the compiler to create optimized code when popping references from 172 // the work queue. 173 inline bool has_partial_array_mask(narrowOop* ref) const { 174 assert(((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) != G1_PARTIAL_ARRAY_MASK, "Partial array oop reference encoded as narrowOop*"); 175 return false; 176 } 177 178 // Only implement set_partial_array_mask() for regular oops, not for narrowOops. 179 // We always encode partial arrays as regular oop, to allow the 180 // specialization for has_partial_array_mask() for narrowOops above. 181 // This means that unintentional use of this method with narrowOops are caught 182 // by the compiler. 183 inline oop* set_partial_array_mask(oop obj) const { 184 assert(((uintptr_t)(void *)obj & G1_PARTIAL_ARRAY_MASK) == 0, "Information loss!"); 185 return (oop*) ((uintptr_t)(void *)obj | G1_PARTIAL_ARRAY_MASK); 186 } 187 188 inline oop clear_partial_array_mask(oop* ref) const { 189 return cast_to_oop((intptr_t)ref & ~G1_PARTIAL_ARRAY_MASK); 190 } 191 192 inline void do_oop_partial_array(oop* p); 193 194 // This method is applied to the fields of the objects that have just been copied. 195 template <class T> inline void do_oop_evac(T* p); 196 197 inline void deal_with_reference(oop* ref_to_scan); 198 inline void deal_with_reference(narrowOop* ref_to_scan); 199 200 inline void dispatch_reference(StarTask ref); 201 202 // Tries to allocate word_sz in the PLAB of the next "generation" after trying to 203 // allocate into dest. Previous_plab_refill_failed indicates whether previous 204 // PLAB refill for the original (source) object failed. 205 // Returns a non-NULL pointer if successful, and updates dest if required. 206 // Also determines whether we should continue to try to allocate into the various 207 // generations or just end trying to allocate. 208 HeapWord* allocate_in_next_plab(G1HeapRegionAttr* dest, 209 size_t word_sz, 210 bool previous_plab_refill_failed, 211 uint node_index); 212 213 inline G1HeapRegionAttr next_region_attr(G1HeapRegionAttr const region_attr, markWord const m, uint& age); 214 215 void report_promotion_event(G1HeapRegionAttr const dest_attr, 216 oop const old, size_t word_sz, uint age, 217 HeapWord * const obj_ptr, uint node_index) const; 218 219 inline bool needs_partial_trimming() const; 220 inline bool is_partially_trimmed() const; 221 222 inline void trim_queue_to_threshold(uint threshold); 223 224 // NUMA statistics related methods. 225 inline void initialize_numa_stats(); 226 inline void flush_numa_stats(); 227 inline void update_numa_stats(uint node_index); 228 229 public: 230 oop copy_to_survivor_space(G1HeapRegionAttr const region_attr, oop const obj, markWord const old_mark); 231 232 void trim_queue(); 233 void trim_queue_partially(); 234 235 Tickspan trim_ticks() const; 236 void reset_trim_ticks(); 237 238 inline void steal_and_trim_queue(RefToScanQueueSet *task_queues); 239 240 // An attempt to evacuate "obj" has failed; take necessary steps. 241 oop handle_evacuation_failure_par(oop obj, markWord m); 242 243 template <typename T> 244 inline void remember_root_into_optional_region(T* p); 245 template <typename T> 246 inline void remember_reference_into_optional_region(T* p); 247 248 inline G1OopStarChunkedList* oops_into_optional_region(const HeapRegion* hr); 249 }; 250 251 class G1ParScanThreadStateSet : public StackObj { 252 G1CollectedHeap* _g1h; 253 G1RedirtyCardsQueueSet* _rdcqs; 254 G1ParScanThreadState** _states; 255 size_t* _surviving_young_words_total; 256 size_t _young_cset_length; 257 size_t _optional_cset_length; 258 uint _n_workers; 259 bool _flushed; 260 261 public: 262 G1ParScanThreadStateSet(G1CollectedHeap* g1h, 263 G1RedirtyCardsQueueSet* rdcqs, 264 uint n_workers, 265 size_t young_cset_length, 266 size_t optional_cset_length); 267 ~G1ParScanThreadStateSet(); 268 269 void flush(); 270 void record_unused_optional_region(HeapRegion* hr); 271 272 G1ParScanThreadState* state_for_worker(uint worker_id); 273 274 const size_t* surviving_young_words() const; 275 276 private: 277 G1ParScanThreadState* new_par_scan_state(uint worker_id, size_t young_cset_length); 278 }; 279 280 #endif // SHARE_GC_G1_G1PARSCANTHREADSTATE_HPP