1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/g1/heapRegionRemSet.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,511 @@ 1.4 +/* 1.5 + * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONREMSET_HPP 1.29 +#define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONREMSET_HPP 1.30 + 1.31 +#include "gc_implementation/g1/g1CodeCacheRemSet.hpp" 1.32 +#include "gc_implementation/g1/sparsePRT.hpp" 1.33 + 1.34 +// Remembered set for a heap region. Represent a set of "cards" that 1.35 +// contain pointers into the owner heap region. Cards are defined somewhat 1.36 +// abstractly, in terms of what the "BlockOffsetTable" in use can parse. 1.37 + 1.38 +class G1CollectedHeap; 1.39 +class G1BlockOffsetSharedArray; 1.40 +class HeapRegion; 1.41 +class HeapRegionRemSetIterator; 1.42 +class PerRegionTable; 1.43 +class SparsePRT; 1.44 +class nmethod; 1.45 + 1.46 +// Essentially a wrapper around SparsePRTCleanupTask. See 1.47 +// sparsePRT.hpp for more details. 1.48 +class HRRSCleanupTask : public SparsePRTCleanupTask { 1.49 +}; 1.50 + 1.51 +// The FromCardCache remembers the most recently processed card on the heap on 1.52 +// a per-region and per-thread basis. 1.53 +class FromCardCache : public AllStatic { 1.54 + private: 1.55 + // Array of card indices. Indexed by thread X and heap region to minimize 1.56 + // thread contention. 1.57 + static int** _cache; 1.58 + static uint _max_regions; 1.59 + static size_t _static_mem_size; 1.60 + 1.61 + public: 1.62 + enum { 1.63 + InvalidCard = -1 // Card value of an invalid card, i.e. a card index not otherwise used. 1.64 + }; 1.65 + 1.66 + static void clear(uint region_idx); 1.67 + 1.68 + // Returns true if the given card is in the cache at the given location, or 1.69 + // replaces the card at that location and returns false. 1.70 + static bool contains_or_replace(uint worker_id, uint region_idx, int card) { 1.71 + int card_in_cache = at(worker_id, region_idx); 1.72 + if (card_in_cache == card) { 1.73 + return true; 1.74 + } else { 1.75 + set(worker_id, region_idx, card); 1.76 + return false; 1.77 + } 1.78 + } 1.79 + 1.80 + static int at(uint worker_id, uint region_idx) { 1.81 + return _cache[worker_id][region_idx]; 1.82 + } 1.83 + 1.84 + static void set(uint worker_id, uint region_idx, int val) { 1.85 + _cache[worker_id][region_idx] = val; 1.86 + } 1.87 + 1.88 + static void initialize(uint n_par_rs, uint max_num_regions); 1.89 + 1.90 + static void shrink(uint new_num_regions); 1.91 + 1.92 + static void print(outputStream* out = gclog_or_tty) PRODUCT_RETURN; 1.93 + 1.94 + static size_t static_mem_size() { 1.95 + return _static_mem_size; 1.96 + } 1.97 +}; 1.98 + 1.99 +// The "_coarse_map" is a bitmap with one bit for each region, where set 1.100 +// bits indicate that the corresponding region may contain some pointer 1.101 +// into the owning region. 1.102 + 1.103 +// The "_fine_grain_entries" array is an open hash table of PerRegionTables 1.104 +// (PRTs), indicating regions for which we're keeping the RS as a set of 1.105 +// cards. The strategy is to cap the size of the fine-grain table, 1.106 +// deleting an entry and setting the corresponding coarse-grained bit when 1.107 +// we would overflow this cap. 1.108 + 1.109 +// We use a mixture of locking and lock-free techniques here. We allow 1.110 +// threads to locate PRTs without locking, but threads attempting to alter 1.111 +// a bucket list obtain a lock. This means that any failing attempt to 1.112 +// find a PRT must be retried with the lock. It might seem dangerous that 1.113 +// a read can find a PRT that is concurrently deleted. This is all right, 1.114 +// because: 1.115 +// 1.116 +// 1) We only actually free PRT's at safe points (though we reuse them at 1.117 +// other times). 1.118 +// 2) We find PRT's in an attempt to add entries. If a PRT is deleted, 1.119 +// it's _coarse_map bit is set, so the that we were attempting to add 1.120 +// is represented. If a deleted PRT is re-used, a thread adding a bit, 1.121 +// thinking the PRT is for a different region, does no harm. 1.122 + 1.123 +class OtherRegionsTable VALUE_OBJ_CLASS_SPEC { 1.124 + friend class HeapRegionRemSetIterator; 1.125 + 1.126 + G1CollectedHeap* _g1h; 1.127 + Mutex* _m; 1.128 + HeapRegion* _hr; 1.129 + 1.130 + // These are protected by "_m". 1.131 + BitMap _coarse_map; 1.132 + size_t _n_coarse_entries; 1.133 + static jint _n_coarsenings; 1.134 + 1.135 + PerRegionTable** _fine_grain_regions; 1.136 + size_t _n_fine_entries; 1.137 + 1.138 + // The fine grain remembered sets are doubly linked together using 1.139 + // their 'next' and 'prev' fields. 1.140 + // This allows fast bulk freeing of all the fine grain remembered 1.141 + // set entries, and fast finding of all of them without iterating 1.142 + // over the _fine_grain_regions table. 1.143 + PerRegionTable * _first_all_fine_prts; 1.144 + PerRegionTable * _last_all_fine_prts; 1.145 + 1.146 + // Used to sample a subset of the fine grain PRTs to determine which 1.147 + // PRT to evict and coarsen. 1.148 + size_t _fine_eviction_start; 1.149 + static size_t _fine_eviction_stride; 1.150 + static size_t _fine_eviction_sample_size; 1.151 + 1.152 + SparsePRT _sparse_table; 1.153 + 1.154 + // These are static after init. 1.155 + static size_t _max_fine_entries; 1.156 + static size_t _mod_max_fine_entries_mask; 1.157 + 1.158 + // Requires "prt" to be the first element of the bucket list appropriate 1.159 + // for "hr". If this list contains an entry for "hr", return it, 1.160 + // otherwise return "NULL". 1.161 + PerRegionTable* find_region_table(size_t ind, HeapRegion* hr) const; 1.162 + 1.163 + // Find, delete, and return a candidate PerRegionTable, if any exists, 1.164 + // adding the deleted region to the coarse bitmap. Requires the caller 1.165 + // to hold _m, and the fine-grain table to be full. 1.166 + PerRegionTable* delete_region_table(); 1.167 + 1.168 + // If a PRT for "hr" is in the bucket list indicated by "ind" (which must 1.169 + // be the correct index for "hr"), delete it and return true; else return 1.170 + // false. 1.171 + bool del_single_region_table(size_t ind, HeapRegion* hr); 1.172 + 1.173 + // link/add the given fine grain remembered set into the "all" list 1.174 + void link_to_all(PerRegionTable * prt); 1.175 + // unlink/remove the given fine grain remembered set into the "all" list 1.176 + void unlink_from_all(PerRegionTable * prt); 1.177 + 1.178 +public: 1.179 + OtherRegionsTable(HeapRegion* hr, Mutex* m); 1.180 + 1.181 + HeapRegion* hr() const { return _hr; } 1.182 + 1.183 + // For now. Could "expand" some tables in the future, so that this made 1.184 + // sense. 1.185 + void add_reference(OopOrNarrowOopStar from, int tid); 1.186 + 1.187 + // Removes any entries shown by the given bitmaps to contain only dead 1.188 + // objects. 1.189 + void scrub(CardTableModRefBS* ctbs, BitMap* region_bm, BitMap* card_bm); 1.190 + 1.191 + size_t occupied() const; 1.192 + size_t occ_fine() const; 1.193 + size_t occ_coarse() const; 1.194 + size_t occ_sparse() const; 1.195 + 1.196 + static jint n_coarsenings() { return _n_coarsenings; } 1.197 + 1.198 + // Returns size in bytes. 1.199 + // Not const because it takes a lock. 1.200 + size_t mem_size() const; 1.201 + static size_t static_mem_size(); 1.202 + static size_t fl_mem_size(); 1.203 + 1.204 + bool contains_reference(OopOrNarrowOopStar from) const; 1.205 + bool contains_reference_locked(OopOrNarrowOopStar from) const; 1.206 + 1.207 + void clear(); 1.208 + 1.209 + // Specifically clear the from_card_cache. 1.210 + void clear_fcc(); 1.211 + 1.212 + // "from_hr" is being cleared; remove any entries from it. 1.213 + void clear_incoming_entry(HeapRegion* from_hr); 1.214 + 1.215 + void do_cleanup_work(HRRSCleanupTask* hrrs_cleanup_task); 1.216 + 1.217 + // Declare the heap size (in # of regions) to the OtherRegionsTable. 1.218 + // (Uses it to initialize from_card_cache). 1.219 + static void init_from_card_cache(uint max_regions); 1.220 + 1.221 + // Declares that only regions i s.t. 0 <= i < new_n_regs are in use. 1.222 + // Make sure any entries for higher regions are invalid. 1.223 + static void shrink_from_card_cache(uint new_num_regions); 1.224 + 1.225 + static void print_from_card_cache(); 1.226 +}; 1.227 + 1.228 +class HeapRegionRemSet : public CHeapObj<mtGC> { 1.229 + friend class VMStructs; 1.230 + friend class HeapRegionRemSetIterator; 1.231 + 1.232 +public: 1.233 + enum Event { 1.234 + Event_EvacStart, Event_EvacEnd, Event_RSUpdateEnd 1.235 + }; 1.236 + 1.237 +private: 1.238 + G1BlockOffsetSharedArray* _bosa; 1.239 + G1BlockOffsetSharedArray* bosa() const { return _bosa; } 1.240 + 1.241 + // A set of code blobs (nmethods) whose code contains pointers into 1.242 + // the region that owns this RSet. 1.243 + G1CodeRootSet _code_roots; 1.244 + 1.245 + Mutex _m; 1.246 + 1.247 + OtherRegionsTable _other_regions; 1.248 + 1.249 + enum ParIterState { Unclaimed, Claimed, Complete }; 1.250 + volatile ParIterState _iter_state; 1.251 + volatile jlong _iter_claimed; 1.252 + 1.253 + // Unused unless G1RecordHRRSOops is true. 1.254 + 1.255 + static const int MaxRecorded = 1000000; 1.256 + static OopOrNarrowOopStar* _recorded_oops; 1.257 + static HeapWord** _recorded_cards; 1.258 + static HeapRegion** _recorded_regions; 1.259 + static int _n_recorded; 1.260 + 1.261 + static const int MaxRecordedEvents = 1000; 1.262 + static Event* _recorded_events; 1.263 + static int* _recorded_event_index; 1.264 + static int _n_recorded_events; 1.265 + 1.266 + static void print_event(outputStream* str, Event evnt); 1.267 + 1.268 +public: 1.269 + HeapRegionRemSet(G1BlockOffsetSharedArray* bosa, HeapRegion* hr); 1.270 + 1.271 + static uint num_par_rem_sets(); 1.272 + static void setup_remset_size(); 1.273 + 1.274 + HeapRegion* hr() const { 1.275 + return _other_regions.hr(); 1.276 + } 1.277 + 1.278 + size_t occupied() { 1.279 + MutexLockerEx x(&_m, Mutex::_no_safepoint_check_flag); 1.280 + return occupied_locked(); 1.281 + } 1.282 + size_t occupied_locked() { 1.283 + return _other_regions.occupied(); 1.284 + } 1.285 + size_t occ_fine() const { 1.286 + return _other_regions.occ_fine(); 1.287 + } 1.288 + size_t occ_coarse() const { 1.289 + return _other_regions.occ_coarse(); 1.290 + } 1.291 + size_t occ_sparse() const { 1.292 + return _other_regions.occ_sparse(); 1.293 + } 1.294 + 1.295 + static jint n_coarsenings() { return OtherRegionsTable::n_coarsenings(); } 1.296 + 1.297 + // Used in the sequential case. 1.298 + void add_reference(OopOrNarrowOopStar from) { 1.299 + _other_regions.add_reference(from, 0); 1.300 + } 1.301 + 1.302 + // Used in the parallel case. 1.303 + void add_reference(OopOrNarrowOopStar from, int tid) { 1.304 + _other_regions.add_reference(from, tid); 1.305 + } 1.306 + 1.307 + // Removes any entries shown by the given bitmaps to contain only dead 1.308 + // objects. 1.309 + void scrub(CardTableModRefBS* ctbs, BitMap* region_bm, BitMap* card_bm); 1.310 + 1.311 + // The region is being reclaimed; clear its remset, and any mention of 1.312 + // entries for this region in other remsets. 1.313 + void clear(); 1.314 + void clear_locked(); 1.315 + 1.316 + // Attempt to claim the region. Returns true iff this call caused an 1.317 + // atomic transition from Unclaimed to Claimed. 1.318 + bool claim_iter(); 1.319 + // Sets the iteration state to "complete". 1.320 + void set_iter_complete(); 1.321 + // Returns "true" iff the region's iteration is complete. 1.322 + bool iter_is_complete(); 1.323 + 1.324 + // Support for claiming blocks of cards during iteration 1.325 + size_t iter_claimed() const { return (size_t)_iter_claimed; } 1.326 + // Claim the next block of cards 1.327 + size_t iter_claimed_next(size_t step) { 1.328 + size_t current, next; 1.329 + do { 1.330 + current = iter_claimed(); 1.331 + next = current + step; 1.332 + } while (Atomic::cmpxchg((jlong)next, &_iter_claimed, (jlong)current) != (jlong)current); 1.333 + return current; 1.334 + } 1.335 + void reset_for_par_iteration(); 1.336 + 1.337 + bool verify_ready_for_par_iteration() { 1.338 + return (_iter_state == Unclaimed) && (_iter_claimed == 0); 1.339 + } 1.340 + 1.341 + // The actual # of bytes this hr_remset takes up. 1.342 + // Note also includes the strong code root set. 1.343 + size_t mem_size() { 1.344 + MutexLockerEx x(&_m, Mutex::_no_safepoint_check_flag); 1.345 + return _other_regions.mem_size() 1.346 + // This correction is necessary because the above includes the second 1.347 + // part. 1.348 + + (sizeof(this) - sizeof(OtherRegionsTable)) 1.349 + + strong_code_roots_mem_size(); 1.350 + } 1.351 + 1.352 + // Returns the memory occupancy of all static data structures associated 1.353 + // with remembered sets. 1.354 + static size_t static_mem_size() { 1.355 + return OtherRegionsTable::static_mem_size() + G1CodeRootSet::static_mem_size(); 1.356 + } 1.357 + 1.358 + // Returns the memory occupancy of all free_list data structures associated 1.359 + // with remembered sets. 1.360 + static size_t fl_mem_size() { 1.361 + return OtherRegionsTable::fl_mem_size() + G1CodeRootSet::fl_mem_size(); 1.362 + } 1.363 + 1.364 + bool contains_reference(OopOrNarrowOopStar from) const { 1.365 + return _other_regions.contains_reference(from); 1.366 + } 1.367 + 1.368 + // Routines for managing the list of code roots that point into 1.369 + // the heap region that owns this RSet. 1.370 + void add_strong_code_root(nmethod* nm); 1.371 + void remove_strong_code_root(nmethod* nm); 1.372 + 1.373 + // During a collection, migrate the successfully evacuated strong 1.374 + // code roots that referenced into the region that owns this RSet 1.375 + // to the RSets of the new regions that they now point into. 1.376 + // Unsuccessfully evacuated code roots are not migrated. 1.377 + void migrate_strong_code_roots(); 1.378 + 1.379 + // Applies blk->do_code_blob() to each of the entries in 1.380 + // the strong code roots list 1.381 + void strong_code_roots_do(CodeBlobClosure* blk) const; 1.382 + 1.383 + // Returns the number of elements in the strong code roots list 1.384 + size_t strong_code_roots_list_length() { 1.385 + return _code_roots.length(); 1.386 + } 1.387 + 1.388 + // Returns true if the strong code roots contains the given 1.389 + // nmethod. 1.390 + bool strong_code_roots_list_contains(nmethod* nm) { 1.391 + return _code_roots.contains(nm); 1.392 + } 1.393 + 1.394 + // Returns the amount of memory, in bytes, currently 1.395 + // consumed by the strong code roots. 1.396 + size_t strong_code_roots_mem_size(); 1.397 + 1.398 + void print() PRODUCT_RETURN; 1.399 + 1.400 + // Called during a stop-world phase to perform any deferred cleanups. 1.401 + static void cleanup(); 1.402 + 1.403 + // Declare the heap size (in # of regions) to the HeapRegionRemSet(s). 1.404 + // (Uses it to initialize from_card_cache). 1.405 + static void init_heap(uint max_regions) { 1.406 + G1CodeRootSet::initialize(); 1.407 + OtherRegionsTable::init_from_card_cache(max_regions); 1.408 + } 1.409 + 1.410 + // Declares that only regions i s.t. 0 <= i < new_n_regs are in use. 1.411 + static void shrink_heap(uint new_n_regs) { 1.412 + OtherRegionsTable::shrink_from_card_cache(new_n_regs); 1.413 + } 1.414 + 1.415 +#ifndef PRODUCT 1.416 + static void print_from_card_cache() { 1.417 + OtherRegionsTable::print_from_card_cache(); 1.418 + } 1.419 +#endif 1.420 + 1.421 + static void record(HeapRegion* hr, OopOrNarrowOopStar f); 1.422 + static void print_recorded(); 1.423 + static void record_event(Event evnt); 1.424 + 1.425 + // These are wrappers for the similarly-named methods on 1.426 + // SparsePRT. Look at sparsePRT.hpp for more details. 1.427 + static void reset_for_cleanup_tasks(); 1.428 + void do_cleanup_work(HRRSCleanupTask* hrrs_cleanup_task); 1.429 + static void finish_cleanup_task(HRRSCleanupTask* hrrs_cleanup_task); 1.430 + 1.431 + // Run unit tests. 1.432 +#ifndef PRODUCT 1.433 + static void test_prt(); 1.434 + static void test(); 1.435 +#endif 1.436 +}; 1.437 + 1.438 +class HeapRegionRemSetIterator : public StackObj { 1.439 + 1.440 + // The region RSet over which we're iterating. 1.441 + HeapRegionRemSet* _hrrs; 1.442 + 1.443 + // Local caching of HRRS fields. 1.444 + const BitMap* _coarse_map; 1.445 + PerRegionTable** _fine_grain_regions; 1.446 + 1.447 + G1BlockOffsetSharedArray* _bosa; 1.448 + G1CollectedHeap* _g1h; 1.449 + 1.450 + // The number yielded since initialization. 1.451 + size_t _n_yielded_fine; 1.452 + size_t _n_yielded_coarse; 1.453 + size_t _n_yielded_sparse; 1.454 + 1.455 + // Indicates what granularity of table that we're currently iterating over. 1.456 + // We start iterating over the sparse table, progress to the fine grain 1.457 + // table, and then finish with the coarse table. 1.458 + // See HeapRegionRemSetIterator::has_next(). 1.459 + enum IterState { 1.460 + Sparse, 1.461 + Fine, 1.462 + Coarse 1.463 + }; 1.464 + IterState _is; 1.465 + 1.466 + // In both kinds of iteration, heap offset of first card of current 1.467 + // region. 1.468 + size_t _cur_region_card_offset; 1.469 + // Card offset within cur region. 1.470 + size_t _cur_region_cur_card; 1.471 + 1.472 + // Coarse table iteration fields: 1.473 + 1.474 + // Current region index; 1.475 + int _coarse_cur_region_index; 1.476 + size_t _coarse_cur_region_cur_card; 1.477 + 1.478 + bool coarse_has_next(size_t& card_index); 1.479 + 1.480 + // Fine table iteration fields: 1.481 + 1.482 + // Index of bucket-list we're working on. 1.483 + int _fine_array_index; 1.484 + 1.485 + // Per Region Table we're doing within current bucket list. 1.486 + PerRegionTable* _fine_cur_prt; 1.487 + 1.488 + /* SparsePRT::*/ SparsePRTIter _sparse_iter; 1.489 + 1.490 + void fine_find_next_non_null_prt(); 1.491 + 1.492 + bool fine_has_next(); 1.493 + bool fine_has_next(size_t& card_index); 1.494 + 1.495 +public: 1.496 + // We require an iterator to be initialized before use, so the 1.497 + // constructor does little. 1.498 + HeapRegionRemSetIterator(HeapRegionRemSet* hrrs); 1.499 + 1.500 + // If there remains one or more cards to be yielded, returns true and 1.501 + // sets "card_index" to one of those cards (which is then considered 1.502 + // yielded.) Otherwise, returns false (and leaves "card_index" 1.503 + // undefined.) 1.504 + bool has_next(size_t& card_index); 1.505 + 1.506 + size_t n_yielded_fine() { return _n_yielded_fine; } 1.507 + size_t n_yielded_coarse() { return _n_yielded_coarse; } 1.508 + size_t n_yielded_sparse() { return _n_yielded_sparse; } 1.509 + size_t n_yielded() { 1.510 + return n_yielded_fine() + n_yielded_coarse() + n_yielded_sparse(); 1.511 + } 1.512 +}; 1.513 + 1.514 +#endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONREMSET_HPP