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