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/g1DirtyCardQueue.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 G1DirtyCardQueue _dcq; 50 G1CardTable* _ct; 51 G1EvacuationRootClosures* _closures; 52 53 G1PLABAllocator* _plab_allocator; 54 55 AgeTable _age_table; 56 InCSetState _dest[InCSetState::Num]; 57 // Local tenuring threshold. 58 uint _tenuring_threshold; 59 G1ScanEvacuatedObjClosure _scanner; 60 61 uint _worker_id; 62 63 // Upper and lower threshold to start and end work queue draining. 64 uint const _stack_trim_upper_threshold; 65 uint const _stack_trim_lower_threshold; 66 67 Tickspan _trim_ticks; 68 // Map from young-age-index (0 == not young, 1 is youngest) to 69 // surviving words. base is what we get back from the malloc call 70 size_t* _surviving_young_words_base; 71 // this points into the array, as we use the first few entries for padding 72 size_t* _surviving_young_words; 73 74 // Indicates whether in the last generation (old) there is no more space 75 // available for allocation. 76 bool _old_gen_is_full; 77 78 #define PADDING_ELEM_NUM (DEFAULT_CACHE_LINE_SIZE / sizeof(size_t)) 79 80 G1DirtyCardQueue& dirty_card_queue() { return _dcq; } 81 G1CardTable* ct() { return _ct; } 82 83 InCSetState dest(InCSetState original) const { 84 assert(original.is_valid(), 85 "Original state invalid: " CSETSTATE_FORMAT, original.value()); 86 assert(_dest[original.value()].is_valid_gen(), 87 "Dest state is invalid: " CSETSTATE_FORMAT, _dest[original.value()].value()); 88 return _dest[original.value()]; 89 } 90 91 size_t _num_optional_regions; 92 G1OopStarChunkedList* _oops_into_optional_regions; 93 94 public: 95 G1ParScanThreadState(G1CollectedHeap* g1h, 96 uint worker_id, 97 size_t young_cset_length, 98 size_t optional_cset_length); 99 virtual ~G1ParScanThreadState(); 100 101 void set_ref_discoverer(ReferenceDiscoverer* rd) { _scanner.set_ref_discoverer(rd); } 102 103 #ifdef ASSERT 104 bool queue_is_empty() const { return _refs->is_empty(); } 105 106 bool verify_ref(narrowOop* ref) const; 107 bool verify_ref(oop* ref) const; 108 bool verify_task(StarTask ref) const; 109 #endif // ASSERT 110 111 template <class T> void do_oop_ext(T* ref); 112 template <class T> void push_on_queue(T* ref); 113 114 template <class T> void enqueue_card_if_tracked(T* p, oop o) { 115 assert(!HeapRegion::is_in_same_region(p, o), "Should have filtered out cross-region references already."); 116 assert(!_g1h->heap_region_containing(p)->is_young(), "Should have filtered out from-young references already."); 117 if (!_g1h->heap_region_containing((HeapWord*)o)->rem_set()->is_tracked()) { 118 return; 119 } 120 size_t card_index = ct()->index_for(p); 121 // If the card hasn't been added to the buffer, do it. 122 if (ct()->mark_card_deferred(card_index)) { 123 dirty_card_queue().enqueue((jbyte*)ct()->byte_for_index(card_index)); 124 } 125 } 126 127 G1EvacuationRootClosures* closures() { return _closures; } 128 uint worker_id() { return _worker_id; } 129 130 size_t lab_waste() const; 131 size_t lab_undo_waste() const; 132 133 size_t* surviving_young_words() { 134 // We add one to hide entry 0 which accumulates surviving words for 135 // age -1 regions (i.e. non-young ones) 136 return _surviving_young_words + 1; 137 } 138 139 void flush(size_t* surviving_young_words); 140 141 private: 142 #define G1_PARTIAL_ARRAY_MASK 0x2 143 144 inline bool has_partial_array_mask(oop* ref) const { 145 return ((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) == G1_PARTIAL_ARRAY_MASK; 146 } 147 148 // We never encode partial array oops as narrowOop*, so return false immediately. 149 // This allows the compiler to create optimized code when popping references from 150 // the work queue. 151 inline bool has_partial_array_mask(narrowOop* ref) const { 152 assert(((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) != G1_PARTIAL_ARRAY_MASK, "Partial array oop reference encoded as narrowOop*"); 153 return false; 154 } 155 156 // Only implement set_partial_array_mask() for regular oops, not for narrowOops. 157 // We always encode partial arrays as regular oop, to allow the 158 // specialization for has_partial_array_mask() for narrowOops above. 159 // This means that unintentional use of this method with narrowOops are caught 160 // by the compiler. 161 inline oop* set_partial_array_mask(oop obj) const { 162 assert(((uintptr_t)(void *)obj & G1_PARTIAL_ARRAY_MASK) == 0, "Information loss!"); 163 return (oop*) ((uintptr_t)(void *)obj | G1_PARTIAL_ARRAY_MASK); 164 } 165 166 inline oop clear_partial_array_mask(oop* ref) const { 167 return cast_to_oop((intptr_t)ref & ~G1_PARTIAL_ARRAY_MASK); 168 } 169 170 inline void do_oop_partial_array(oop* p); 171 172 // This method is applied to the fields of the objects that have just been copied. 173 template <class T> inline void do_oop_evac(T* p); 174 175 inline void deal_with_reference(oop* ref_to_scan); 176 inline void deal_with_reference(narrowOop* ref_to_scan); 177 178 inline void dispatch_reference(StarTask ref); 179 180 // Tries to allocate word_sz in the PLAB of the next "generation" after trying to 181 // allocate into dest. State is the original (source) cset state for the object 182 // that is allocated for. Previous_plab_refill_failed indicates whether previously 183 // a PLAB refill into "state" 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(InCSetState const state, 188 InCSetState* dest, 189 size_t word_sz, 190 bool previous_plab_refill_failed); 191 192 inline InCSetState next_state(InCSetState const state, markOop const m, uint& age); 193 194 void report_promotion_event(InCSetState const dest_state, 195 oop const old, size_t word_sz, uint age, 196 HeapWord * const obj_ptr) const; 197 198 inline bool needs_partial_trimming() const; 199 inline bool is_partially_trimmed() const; 200 201 inline void trim_queue_to_threshold(uint threshold); 202 public: 203 oop copy_to_survivor_space(InCSetState const state, oop const obj, markOop const old_mark); 204 205 void trim_queue(); 206 void trim_queue_partially(); 207 208 Tickspan trim_ticks() const; 209 void reset_trim_ticks(); 210 211 inline void steal_and_trim_queue(RefToScanQueueSet *task_queues); 212 213 // An attempt to evacuate "obj" has failed; take necessary steps. 214 oop handle_evacuation_failure_par(oop obj, markOop m); 215 216 template <typename T> 217 inline void remember_root_into_optional_region(T* p); 218 template <typename T> 219 inline void remember_reference_into_optional_region(T* p); 220 221 inline G1OopStarChunkedList* oops_into_optional_region(const HeapRegion* hr); 222 }; 223 224 class G1ParScanThreadStateSet : public StackObj { 225 G1CollectedHeap* _g1h; 226 G1ParScanThreadState** _states; 227 size_t* _surviving_young_words_total; 228 size_t _young_cset_length; 229 size_t _optional_cset_length; 230 uint _n_workers; 231 bool _flushed; 232 233 public: 234 G1ParScanThreadStateSet(G1CollectedHeap* g1h, 235 uint n_workers, 236 size_t young_cset_length, 237 size_t optional_cset_length); 238 ~G1ParScanThreadStateSet(); 239 240 void flush(); 241 void record_unused_optional_region(HeapRegion* hr); 242 243 G1ParScanThreadState* state_for_worker(uint worker_id); 244 245 const size_t* surviving_young_words() const; 246 247 private: 248 G1ParScanThreadState* new_par_scan_state(uint worker_id, size_t young_cset_length); 249 }; 250 251 #endif // SHARE_GC_G1_G1PARSCANTHREADSTATE_HPP