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_CONCURRENTMARK_HPP 26 #define SHARE_VM_GC_G1_CONCURRENTMARK_HPP 27 28 #include "classfile/javaClasses.hpp" 29 #include "gc/g1/g1RegionToSpaceMapper.hpp" 30 #include "gc/g1/heapRegionSet.hpp" 31 #include "gc/shared/gcId.hpp" 32 #include "gc/shared/taskqueue.hpp" 33 34 class G1CollectedHeap; 35 class CMBitMap; 36 class CMTask; 37 class ConcurrentMark; 38 typedef GenericTaskQueue<oop, mtGC> CMTaskQueue; 39 typedef GenericTaskQueueSet<CMTaskQueue, mtGC> CMTaskQueueSet; 40 41 // Closure used by CM during concurrent reference discovery 42 // and reference processing (during remarking) to determine 43 // if a particular object is alive. It is primarily used 44 // to determine if referents of discovered reference objects 45 // are alive. An instance is also embedded into the 46 // reference processor as the _is_alive_non_header field 47 class G1CMIsAliveClosure: public BoolObjectClosure { 48 G1CollectedHeap* _g1; 49 public: 50 G1CMIsAliveClosure(G1CollectedHeap* g1) : _g1(g1) { } 51 52 bool do_object_b(oop obj); 53 }; 54 55 // A generic CM bit map. This is essentially a wrapper around the BitMap 56 // class, with one bit per (1<<_shifter) HeapWords. 57 58 class CMBitMapRO VALUE_OBJ_CLASS_SPEC { 59 protected: 60 HeapWord* _bmStartWord; // base address of range covered by map 61 size_t _bmWordSize; // map size (in #HeapWords covered) 62 const int _shifter; // map to char or bit 63 BitMap _bm; // the bit map itself 64 65 public: 66 // constructor 67 CMBitMapRO(int shifter); 68 69 enum { do_yield = true }; 70 71 // inquiries 72 HeapWord* startWord() const { return _bmStartWord; } 73 size_t sizeInWords() const { return _bmWordSize; } 74 // the following is one past the last word in space 75 HeapWord* endWord() const { return _bmStartWord + _bmWordSize; } 76 77 // read marks 78 79 bool isMarked(HeapWord* addr) const { 80 assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize), 81 "outside underlying space?"); 82 return _bm.at(heapWordToOffset(addr)); 83 } 84 85 // iteration 86 inline bool iterate(BitMapClosure* cl, MemRegion mr); 87 inline bool iterate(BitMapClosure* cl); 88 89 // Return the address corresponding to the next marked bit at or after 90 // "addr", and before "limit", if "limit" is non-NULL. If there is no 91 // such bit, returns "limit" if that is non-NULL, or else "endWord()". 92 HeapWord* getNextMarkedWordAddress(const HeapWord* addr, 93 const HeapWord* limit = NULL) const; 94 // Return the address corresponding to the next unmarked bit at or after 95 // "addr", and before "limit", if "limit" is non-NULL. If there is no 96 // such bit, returns "limit" if that is non-NULL, or else "endWord()". 97 HeapWord* getNextUnmarkedWordAddress(const HeapWord* addr, 98 const HeapWord* limit = NULL) const; 99 100 // conversion utilities 101 HeapWord* offsetToHeapWord(size_t offset) const { 102 return _bmStartWord + (offset << _shifter); 103 } 104 size_t heapWordToOffset(const HeapWord* addr) const { 105 return pointer_delta(addr, _bmStartWord) >> _shifter; 106 } 107 int heapWordDiffToOffsetDiff(size_t diff) const; 108 109 // The argument addr should be the start address of a valid object 110 HeapWord* nextObject(HeapWord* addr) { 111 oop obj = (oop) addr; 112 HeapWord* res = addr + obj->size(); 113 assert(offsetToHeapWord(heapWordToOffset(res)) == res, "sanity"); 114 return res; 115 } 116 117 void print_on_error(outputStream* st, const char* prefix) const; 118 119 // debugging 120 NOT_PRODUCT(bool covers(MemRegion rs) const;) 121 }; 122 123 class CMBitMapMappingChangedListener : public G1MappingChangedListener { 124 private: 125 CMBitMap* _bm; 126 public: 127 CMBitMapMappingChangedListener() : _bm(NULL) {} 128 129 void set_bitmap(CMBitMap* bm) { _bm = bm; } 130 131 virtual void on_commit(uint start_idx, size_t num_regions, bool zero_filled); 132 }; 133 134 class CMBitMap : public CMBitMapRO { 135 private: 136 CMBitMapMappingChangedListener _listener; 137 138 public: 139 static size_t compute_size(size_t heap_size); 140 // Returns the amount of bytes on the heap between two marks in the bitmap. 141 static size_t mark_distance(); 142 // Returns how many bytes (or bits) of the heap a single byte (or bit) of the 143 // mark bitmap corresponds to. This is the same as the mark distance above. 144 static size_t heap_map_factor() { 145 return mark_distance(); 146 } 147 148 CMBitMap() : CMBitMapRO(LogMinObjAlignment), _listener() { _listener.set_bitmap(this); } 149 150 // Initializes the underlying BitMap to cover the given area. 151 void initialize(MemRegion heap, G1RegionToSpaceMapper* storage); 152 153 // Write marks. 154 inline void mark(HeapWord* addr); 155 inline void clear(HeapWord* addr); 156 inline bool parMark(HeapWord* addr); 157 inline bool parClear(HeapWord* addr); 158 159 void markRange(MemRegion mr); 160 void clearRange(MemRegion mr); 161 162 // Starting at the bit corresponding to "addr" (inclusive), find the next 163 // "1" bit, if any. This bit starts some run of consecutive "1"'s; find 164 // the end of this run (stopping at "end_addr"). Return the MemRegion 165 // covering from the start of the region corresponding to the first bit 166 // of the run to the end of the region corresponding to the last bit of 167 // the run. If there is no "1" bit at or after "addr", return an empty 168 // MemRegion. 169 MemRegion getAndClearMarkedRegion(HeapWord* addr, HeapWord* end_addr); 170 171 // Clear the whole mark bitmap. 172 void clearAll(); 173 }; 174 175 // Represents a marking stack used by ConcurrentMarking in the G1 collector. 176 class CMMarkStack VALUE_OBJ_CLASS_SPEC { 177 VirtualSpace _virtual_space; // Underlying backing store for actual stack 178 ConcurrentMark* _cm; 179 oop* _base; // bottom of stack 180 jint _index; // one more than last occupied index 181 jint _capacity; // max #elements 182 jint _saved_index; // value of _index saved at start of GC 183 184 bool _overflow; 185 bool _should_expand; 186 DEBUG_ONLY(bool _drain_in_progress;) 187 DEBUG_ONLY(bool _drain_in_progress_yields;) 188 189 oop pop() { 190 if (!isEmpty()) { 361 protected: 362 ConcurrentMarkThread* _cmThread; // The thread doing the work 363 G1CollectedHeap* _g1h; // The heap 364 uint _parallel_marking_threads; // The number of marking 365 // threads we're using 366 uint _max_parallel_marking_threads; // Max number of marking 367 // threads we'll ever use 368 double _sleep_factor; // How much we have to sleep, with 369 // respect to the work we just did, to 370 // meet the marking overhead goal 371 double _marking_task_overhead; // Marking target overhead for 372 // a single task 373 374 // Same as the two above, but for the cleanup task 375 double _cleanup_sleep_factor; 376 double _cleanup_task_overhead; 377 378 FreeRegionList _cleanup_list; 379 380 // Concurrent marking support structures 381 CMBitMap _markBitMap1; 382 CMBitMap _markBitMap2; 383 CMBitMapRO* _prevMarkBitMap; // Completed mark bitmap 384 CMBitMap* _nextMarkBitMap; // Under-construction mark bitmap 385 386 BitMap _region_bm; 387 BitMap _card_bm; 388 389 // Heap bounds 390 HeapWord* _heap_start; 391 HeapWord* _heap_end; 392 393 // Root region tracking and claiming 394 CMRootRegions _root_regions; 395 396 // For gray objects 397 CMMarkStack _markStack; // Grey objects behind global finger 398 HeapWord* volatile _finger; // The global finger, region aligned, 399 // always points to the end of the 400 // last claimed region 401 402 // Marking tasks | 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_CONCURRENTMARK_HPP 26 #define SHARE_VM_GC_G1_CONCURRENTMARK_HPP 27 28 #include "classfile/javaClasses.hpp" 29 #include "gc/g1/g1RegionToSpaceMapper.hpp" 30 #include "gc/g1/heapRegionSet.hpp" 31 #include "gc/shared/cmBitMap.inline.hpp" 32 #include "gc/shared/gcId.hpp" 33 #include "gc/shared/taskqueue.hpp" 34 35 class G1CollectedHeap; 36 class CMBitMap; 37 class CMTask; 38 class ConcurrentMark; 39 typedef GenericTaskQueue<oop, mtGC> CMTaskQueue; 40 typedef GenericTaskQueueSet<CMTaskQueue, mtGC> CMTaskQueueSet; 41 42 // Closure used by CM during concurrent reference discovery 43 // and reference processing (during remarking) to determine 44 // if a particular object is alive. It is primarily used 45 // to determine if referents of discovered reference objects 46 // are alive. An instance is also embedded into the 47 // reference processor as the _is_alive_non_header field 48 class G1CMIsAliveClosure: public BoolObjectClosure { 49 G1CollectedHeap* _g1; 50 public: 51 G1CMIsAliveClosure(G1CollectedHeap* g1) : _g1(g1) { } 52 53 bool do_object_b(oop obj); 54 }; 55 56 class CMBitMapMappingChangedListener : public G1MappingChangedListener { 57 private: 58 CMBitMap* _bm; 59 public: 60 CMBitMapMappingChangedListener() : _bm(NULL) {} 61 62 void set_bitmap(CMBitMap* bm) { _bm = bm; } 63 64 virtual void on_commit(uint start_idx, size_t num_regions, bool zero_filled); 65 }; 66 67 class G1CMBitMap : public CMBitMap { 68 private: 69 CMBitMapMappingChangedListener _listener; 70 71 public: 72 G1CMBitMap() : CMBitMap(), _listener() { _listener.set_bitmap(this); } 73 static size_t compute_size(size_t heap_size); 74 // Returns the amount of bytes on the heap between two marks in the bitmap. 75 static size_t mark_distance(); 76 // Returns how many bytes (or bits) of the heap a single byte (or bit) of the 77 // mark bitmap corresponds to. This is the same as the mark distance above. 78 static size_t heap_map_factor() { 79 return mark_distance(); 80 } 81 82 // Initializes the underlying BitMap to cover the given area. 83 void initialize(MemRegion heap, G1RegionToSpaceMapper* storage); 84 85 // Clear the whole mark bitmap. 86 void clearAll(); 87 }; 88 89 // Represents a marking stack used by ConcurrentMarking in the G1 collector. 90 class CMMarkStack VALUE_OBJ_CLASS_SPEC { 91 VirtualSpace _virtual_space; // Underlying backing store for actual stack 92 ConcurrentMark* _cm; 93 oop* _base; // bottom of stack 94 jint _index; // one more than last occupied index 95 jint _capacity; // max #elements 96 jint _saved_index; // value of _index saved at start of GC 97 98 bool _overflow; 99 bool _should_expand; 100 DEBUG_ONLY(bool _drain_in_progress;) 101 DEBUG_ONLY(bool _drain_in_progress_yields;) 102 103 oop pop() { 104 if (!isEmpty()) { 275 protected: 276 ConcurrentMarkThread* _cmThread; // The thread doing the work 277 G1CollectedHeap* _g1h; // The heap 278 uint _parallel_marking_threads; // The number of marking 279 // threads we're using 280 uint _max_parallel_marking_threads; // Max number of marking 281 // threads we'll ever use 282 double _sleep_factor; // How much we have to sleep, with 283 // respect to the work we just did, to 284 // meet the marking overhead goal 285 double _marking_task_overhead; // Marking target overhead for 286 // a single task 287 288 // Same as the two above, but for the cleanup task 289 double _cleanup_sleep_factor; 290 double _cleanup_task_overhead; 291 292 FreeRegionList _cleanup_list; 293 294 // Concurrent marking support structures 295 G1CMBitMap _markBitMap1; 296 G1CMBitMap _markBitMap2; 297 CMBitMapRO* _prevMarkBitMap; // Completed mark bitmap 298 CMBitMap* _nextMarkBitMap; // Under-construction mark bitmap 299 300 BitMap _region_bm; 301 BitMap _card_bm; 302 303 // Heap bounds 304 HeapWord* _heap_start; 305 HeapWord* _heap_end; 306 307 // Root region tracking and claiming 308 CMRootRegions _root_regions; 309 310 // For gray objects 311 CMMarkStack _markStack; // Grey objects behind global finger 312 HeapWord* volatile _finger; // The global finger, region aligned, 313 // always points to the end of the 314 // last claimed region 315 316 // Marking tasks |