8196341: Add JFR events for parallel phases of G1

0 /*                                                                                                                         
1  * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.                                            
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.                                                           
3  *                                                                                                                         
4  * This code is free software; you can redistribute it and/or modify it                                                    
5  * under the terms of the GNU General Public License version 2 only, as                                                    
6  * published by the Free Software Foundation.                                                                              
7  *                                                                                                                         
8  * This code is distributed in the hope that it will be useful, but WITHOUT                                                
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or                                                   
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License                                                   
11  * version 2 for more details (a copy is included in the LICENSE file that                                                 
12  * accompanied this code).                                                                                                 
13  *                                                                                                                         
14  * You should have received a copy of the GNU General Public License version                                               
15  * 2 along with this work; if not, write to the Free Software Foundation,                                                  
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.                                                           
17  *                                                                                                                         
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA                                                 
19  * or visit www.oracle.com if you need additional information or have any                                                  
20  * questions.                                                                                                              
21  *                                                                                                                         
22  */                                                                                                                        
23 
24 #include "precompiled.hpp"                                                                                                 
25 #include "gc/g1/dirtyCardQueue.hpp"                                                                                        
26 #include "gc/g1/g1BarrierSet.hpp"                                                                                          
27 #include "gc/g1/g1BlockOffsetTable.inline.hpp"                                                                             
28 #include "gc/g1/g1CardTable.inline.hpp"                                                                                    
29 #include "gc/g1/g1CollectedHeap.inline.hpp"                                                                                
30 #include "gc/g1/g1ConcurrentRefine.hpp"                                                                                    
31 #include "gc/g1/g1FromCardCache.hpp"                                                                                       
32 #include "gc/g1/g1GCPhaseTimes.hpp"                                                                                        
33 #include "gc/g1/g1HotCardCache.hpp"                                                                                        
34 #include "gc/g1/g1OopClosures.inline.hpp"                                                                                  
35 #include "gc/g1/g1RootClosures.hpp"                                                                                        
36 #include "gc/g1/g1RemSet.hpp"                                                                                              
37 #include "gc/g1/heapRegion.inline.hpp"                                                                                     
38 #include "gc/g1/heapRegionManager.inline.hpp"                                                                              
39 #include "gc/g1/heapRegionRemSet.hpp"                                                                                      
40 #include "gc/shared/gcTraceTime.inline.hpp"                                                                                
41 #include "gc/shared/suspendibleThreadSet.hpp"                                                                              
                                                                                                                           
42 #include "memory/iterator.hpp"                                                                                             
43 #include "memory/resourceArea.hpp"                                                                                         
44 #include "oops/access.inline.hpp"                                                                                          
45 #include "oops/oop.inline.hpp"                                                                                             
46 #include "runtime/os.hpp"                                                                                                  
47 #include "utilities/align.hpp"                                                                                             
48 #include "utilities/globalDefinitions.hpp"                                                                                 
49 #include "utilities/intHisto.hpp"                                                                                          
50 #include "utilities/stack.inline.hpp"                                                                                      
51 #include "utilities/ticks.hpp"                                                                                             
52 
53 // Collects information about the overall remembered set scan progress during an evacuation.                               
54 class G1RemSetScanState : public CHeapObj<mtGC> {                                                                          
55 private:                                                                                                                   
56   class G1ClearCardTableTask : public AbstractGangTask {                                                                   
57     G1CollectedHeap* _g1h;                                                                                                 
58     uint* _dirty_region_list;                                                                                              
59     size_t _num_dirty_regions;                                                                                             
60     size_t _chunk_length;                                                                                                  
61 
62     size_t volatile _cur_dirty_regions;                                                                                    
63   public:                                                                                                                  
64     G1ClearCardTableTask(G1CollectedHeap* g1h,                                                                             
65                          uint* dirty_region_list,                                                                          
66                          size_t num_dirty_regions,                                                                         
67                          size_t chunk_length) :                                                                            
68       AbstractGangTask("G1 Clear Card Table Task"),                                                                        
69       _g1h(g1h),                                                                                                           
70       _dirty_region_list(dirty_region_list),                                                                               
71       _num_dirty_regions(num_dirty_regions),                                                                               
72       _chunk_length(chunk_length),                                                                                         
73       _cur_dirty_regions(0) {                                                                                              
74 
75       assert(chunk_length > 0, "must be");                                                                                 
76     }                                                                                                                      
77 
78     static size_t chunk_size() { return M; }                                                                               
79 
80     void work(uint worker_id) {                                                                                            
81       while (_cur_dirty_regions < _num_dirty_regions) {                                                                    
82         size_t next = Atomic::add(_chunk_length, &_cur_dirty_regions) - _chunk_length;                                     
83         size_t max = MIN2(next + _chunk_length, _num_dirty_regions);                                                       
84 
85         for (size_t i = next; i < max; i++) {                                                                              
86           HeapRegion* r = _g1h->region_at(_dirty_region_list[i]);                                                          
87           if (!r->is_survivor()) {                                                                                         
88             r->clear_cardtable();                                                                                          
89           }                                                                                                                
90         }                                                                                                                  
91       }                                                                                                                    
92     }                                                                                                                      
93   };                                                                                                                       
94 
95   size_t _max_regions;                                                                                                     
96 
97   // Scan progress for the remembered set of a single region. Transitions from                                             
98   // Unclaimed -> Claimed -> Complete.                                                                                     
99   // At each of the transitions the thread that does the transition needs to perform                                       
100   // some special action once. This is the reason for the extra "Claimed" state.                                           
101   typedef jint G1RemsetIterState;                                                                                          
102 
103   static const G1RemsetIterState Unclaimed = 0; // The remembered set has not been scanned yet.                            
104   static const G1RemsetIterState Claimed = 1;   // The remembered set is currently being scanned.                          
105   static const G1RemsetIterState Complete = 2;  // The remembered set has been completely scanned.                         
106 
107   G1RemsetIterState volatile* _iter_states;                                                                                
108   // The current location where the next thread should continue scanning in a region's                                     
109   // remembered set.                                                                                                       
110   size_t volatile* _iter_claims;                                                                                           
111 
112   // Temporary buffer holding the regions we used to store remembered set scan duplicate                                   
113   // information. These are also called "dirty". Valid entries are from [0.._cur_dirty_region)                             
114   uint* _dirty_region_buffer;                                                                                              
115 
116   typedef jbyte IsDirtyRegionState;                                                                                        
117   static const IsDirtyRegionState Clean = 0;                                                                               
118   static const IsDirtyRegionState Dirty = 1;                                                                               
119   // Holds a flag for every region whether it is in the _dirty_region_buffer already                                       
120   // to avoid duplicates. Uses jbyte since there are no atomic instructions for bools.                                     
121   IsDirtyRegionState* _in_dirty_region_buffer;                                                                             
122   size_t _cur_dirty_region;                                                                                                
123 
124   // Creates a snapshot of the current _top values at the start of collection to                                           
125   // filter out card marks that we do not want to scan.                                                                    
126   class G1ResetScanTopClosure : public HeapRegionClosure {                                                                 
127   private:                                                                                                                 
128     HeapWord** _scan_top;                                                                                                  
129   public:                                                                                                                  
130     G1ResetScanTopClosure(HeapWord** scan_top) : _scan_top(scan_top) { }                                                   
131 
132     virtual bool do_heap_region(HeapRegion* r) {                                                                           
133       uint hrm_index = r->hrm_index();                                                                                     
134       if (!r->in_collection_set() && r->is_old_or_humongous_or_archive()) {                                                
135         _scan_top[hrm_index] = r->top();                                                                                   
136       } else {                                                                                                             
137         _scan_top[hrm_index] = r->bottom();                                                                                
138       }                                                                                                                    
139       return false;                                                                                                        
140     }                                                                                                                      
141   };                                                                                                                       
142 
143   // For each region, contains the maximum top() value to be used during this garbage                                      
144   // collection. Subsumes common checks like filtering out everything but old and                                          
145   // humongous regions outside the collection set.                                                                         
146   // This is valid because we are not interested in scanning stray remembered set                                          
147   // entries from free or archive regions.                                                                                 
148   HeapWord** _scan_top;                                                                                                    
149 public:                                                                                                                    
150   G1RemSetScanState() :                                                                                                    
151     _max_regions(0),                                                                                                       
152     _iter_states(NULL),                                                                                                    
153     _iter_claims(NULL),                                                                                                    
154     _dirty_region_buffer(NULL),                                                                                            
155     _in_dirty_region_buffer(NULL),                                                                                         
156     _cur_dirty_region(0),                                                                                                  
157     _scan_top(NULL) {                                                                                                      
158   }                                                                                                                        
159 
160   ~G1RemSetScanState() {                                                                                                   
161     if (_iter_states != NULL) {                                                                                            
162       FREE_C_HEAP_ARRAY(G1RemsetIterState, _iter_states);                                                                  
163     }                                                                                                                      
164     if (_iter_claims != NULL) {                                                                                            
165       FREE_C_HEAP_ARRAY(size_t, _iter_claims);                                                                             
166     }                                                                                                                      
167     if (_dirty_region_buffer != NULL) {                                                                                    
168       FREE_C_HEAP_ARRAY(uint, _dirty_region_buffer);                                                                       
169     }                                                                                                                      
170     if (_in_dirty_region_buffer != NULL) {                                                                                 
171       FREE_C_HEAP_ARRAY(IsDirtyRegionState, _in_dirty_region_buffer);                                                      
172     }                                                                                                                      
173     if (_scan_top != NULL) {                                                                                               
174       FREE_C_HEAP_ARRAY(HeapWord*, _scan_top);                                                                             
175     }                                                                                                                      
176   }                                                                                                                        
177 
178   void initialize(uint max_regions) {                                                                                      
179     assert(_iter_states == NULL, "Must not be initialized twice");                                                         
180     assert(_iter_claims == NULL, "Must not be initialized twice");                                                         
181     _max_regions = max_regions;                                                                                            
182     _iter_states = NEW_C_HEAP_ARRAY(G1RemsetIterState, max_regions, mtGC);                                                 
183     _iter_claims = NEW_C_HEAP_ARRAY(size_t, max_regions, mtGC);                                                            
184     _dirty_region_buffer = NEW_C_HEAP_ARRAY(uint, max_regions, mtGC);                                                      
185     _in_dirty_region_buffer = NEW_C_HEAP_ARRAY(IsDirtyRegionState, max_regions, mtGC);                                     
186     _scan_top = NEW_C_HEAP_ARRAY(HeapWord*, max_regions, mtGC);                                                            
187   }                                                                                                                        
188 
189   void reset() {                                                                                                           
190     for (uint i = 0; i < _max_regions; i++) {                                                                              
191       _iter_states[i] = Unclaimed;                                                                                         
192     }                                                                                                                      
193 
194     G1ResetScanTopClosure cl(_scan_top);                                                                                   
195     G1CollectedHeap::heap()->heap_region_iterate(&cl);                                                                     
196 
197     memset((void*)_iter_claims, 0, _max_regions * sizeof(size_t));                                                         
198     memset(_in_dirty_region_buffer, Clean, _max_regions * sizeof(IsDirtyRegionState));                                     
199     _cur_dirty_region = 0;                                                                                                 
200   }                                                                                                                        
201 
202   // Attempt to claim the remembered set of the region for iteration. Returns true                                         
203   // if this call caused the transition from Unclaimed to Claimed.                                                         
204   inline bool claim_iter(uint region) {                                                                                    
205     assert(region < _max_regions, "Tried to access invalid region %u", region);                                            
206     if (_iter_states[region] != Unclaimed) {                                                                               
207       return false;                                                                                                        
208     }                                                                                                                      
209     G1RemsetIterState res = Atomic::cmpxchg(Claimed, &_iter_states[region], Unclaimed);                                    
210     return (res == Unclaimed);                                                                                             
211   }                                                                                                                        
212 
213   // Try to atomically sets the iteration state to "complete". Returns true for the                                        
214   // thread that caused the transition.                                                                                    
215   inline bool set_iter_complete(uint region) {                                                                             
216     if (iter_is_complete(region)) {                                                                                        
217       return false;                                                                                                        
218     }                                                                                                                      
219     G1RemsetIterState res = Atomic::cmpxchg(Complete, &_iter_states[region], Claimed);                                     
220     return (res == Claimed);                                                                                               
221   }                                                                                                                        
222 
223   // Returns true if the region's iteration is complete.                                                                   
224   inline bool iter_is_complete(uint region) const {                                                                        
225     assert(region < _max_regions, "Tried to access invalid region %u", region);                                            
226     return _iter_states[region] == Complete;                                                                               
227   }                                                                                                                        
228 
229   // The current position within the remembered set of the given region.                                                   
230   inline size_t iter_claimed(uint region) const {                                                                          
231     assert(region < _max_regions, "Tried to access invalid region %u", region);                                            
232     return _iter_claims[region];                                                                                           
233   }                                                                                                                        
234 
235   // Claim the next block of cards within the remembered set of the region with                                            
236   // step size.                                                                                                            
237   inline size_t iter_claimed_next(uint region, size_t step) {                                                              
238     return Atomic::add(step, &_iter_claims[region]) - step;                                                                
239   }                                                                                                                        
240 
241   void add_dirty_region(uint region) {                                                                                     
242     if (_in_dirty_region_buffer[region] == Dirty) {                                                                        
243       return;                                                                                                              
244     }                                                                                                                      
245 
246     bool marked_as_dirty = Atomic::cmpxchg(Dirty, &_in_dirty_region_buffer[region], Clean) == Clean;                       
247     if (marked_as_dirty) {                                                                                                 
248       size_t allocated = Atomic::add(1u, &_cur_dirty_region) - 1;                                                          
249       _dirty_region_buffer[allocated] = region;                                                                            
250     }                                                                                                                      
251   }                                                                                                                        
252 
253   HeapWord* scan_top(uint region_idx) const {                                                                              
254     return _scan_top[region_idx];                                                                                          
255   }                                                                                                                        
256 
257   // Clear the card table of "dirty" regions.                                                                              
258   void clear_card_table(WorkGang* workers) {                                                                               
259     if (_cur_dirty_region == 0) {                                                                                          
260       return;                                                                                                              
261     }                                                                                                                      
262 
263     size_t const num_chunks = align_up(_cur_dirty_region * HeapRegion::CardsPerRegion, G1ClearCardTableTask::chunk_size()) 
264     uint const num_workers = (uint)MIN2(num_chunks, (size_t)workers->active_workers());                                    
265     size_t const chunk_length = G1ClearCardTableTask::chunk_size() / HeapRegion::CardsPerRegion;                           
266 
267     // Iterate over the dirty cards region list.                                                                           
268     G1ClearCardTableTask cl(G1CollectedHeap::heap(), _dirty_region_buffer, _cur_dirty_region, chunk_length);               
269 
270     log_debug(gc, ergo)("Running %s using %u workers for " SIZE_FORMAT " "                                                 
271                         "units of work for " SIZE_FORMAT " regions.",                                                      
272                         cl.name(), num_workers, num_chunks, _cur_dirty_region);                                            
273     workers->run_task(&cl, num_workers);                                                                                   
274 
275 #ifndef PRODUCT                                                                                                            
276     G1CollectedHeap::heap()->verifier()->verify_card_table_cleanup();                                                      
277 #endif                                                                                                                     
278   }                                                                                                                        
279 };                                                                                                                         
280 
281 G1RemSet::G1RemSet(G1CollectedHeap* g1h,                                                                                   
282                    G1CardTable* ct,                                                                                        
283                    G1HotCardCache* hot_card_cache) :                                                                       
284   _scan_state(new G1RemSetScanState()),                                                                                    
285   _prev_period_summary(),                                                                                                  
286   _g1h(g1h),                                                                                                               
287   _num_conc_refined_cards(0),                                                                                              
288   _ct(ct),                                                                                                                 
289   _g1p(_g1h->g1_policy()),                                                                                                 
290   _hot_card_cache(hot_card_cache) {                                                                                        
291 }                                                                                                                          
292 
293 G1RemSet::~G1RemSet() {                                                                                                    
294   if (_scan_state != NULL) {                                                                                               
295     delete _scan_state;                                                                                                    
296   }                                                                                                                        
297 }                                                                                                                          
298 
299 uint G1RemSet::num_par_rem_sets() {                                                                                        
300   return DirtyCardQueueSet::num_par_ids() + G1ConcurrentRefine::max_num_threads() + MAX2(ConcGCThreads, ParallelGCThreads);
301 }                                                                                                                          
302 
303 void G1RemSet::initialize(size_t capacity, uint max_regions) {                                                             
304   G1FromCardCache::initialize(num_par_rem_sets(), max_regions);                                                            
305   _scan_state->initialize(max_regions);                                                                                    
306 }                                                                                                                          
307 
308 G1ScanRSForRegionClosure::G1ScanRSForRegionClosure(G1RemSetScanState* scan_state,                                          
309                                                    G1ScanObjsDuringScanRSClosure* scan_obj_on_card,                        
310                                                    G1ParScanThreadState* pss,                                              
311                                                    uint worker_i) :                                                        
312   _g1h(G1CollectedHeap::heap()),                                                                                           
313   _ct(_g1h->card_table()),                                                                                                 
314   _pss(pss),                                                                                                               
315   _scan_objs_on_card_cl(scan_obj_on_card),                                                                                 
316   _scan_state(scan_state),                                                                                                 
317   _worker_i(worker_i),                                                                                                     
318   _cards_scanned(0),                                                                                                       
319   _cards_claimed(0),                                                                                                       
320   _cards_skipped(0),                                                                                                       
321   _rem_set_root_scan_time(),                                                                                               
322   _rem_set_trim_partially_time(),                                                                                          
323   _strong_code_root_scan_time(),                                                                                           
324   _strong_code_trim_partially_time() {                                                                                     
325 }                                                                                                                          
326 
327 void G1ScanRSForRegionClosure::claim_card(size_t card_index, const uint region_idx_for_card){                              
328   _ct->set_card_claimed(card_index);                                                                                       
329   _scan_state->add_dirty_region(region_idx_for_card);                                                                      
330 }                                                                                                                          
331 
332 void G1ScanRSForRegionClosure::scan_card(MemRegion mr, uint region_idx_for_card) {                                         
333   HeapRegion* const card_region = _g1h->region_at(region_idx_for_card);                                                    
334   _scan_objs_on_card_cl->set_region(card_region);                                                                          
335   card_region->oops_on_card_seq_iterate_careful<true>(mr, _scan_objs_on_card_cl);                                          
336   _scan_objs_on_card_cl->trim_queue_partially();                                                                           
337   _cards_scanned++;                                                                                                        
338 }                                                                                                                          
339 
340 void G1ScanRSForRegionClosure::scan_rem_set_roots(HeapRegion* r) {                                                         
                                                                                                                           
341   uint const region_idx = r->hrm_index();                                                                                  
342 
343   if (_scan_state->claim_iter(region_idx)) {                                                                               
344     // If we ever free the collection set concurrently, we should also                                                     
345     // clear the card table concurrently therefore we won't need to                                                        
346     // add regions of the collection set to the dirty cards region.                                                        
347     _scan_state->add_dirty_region(region_idx);                                                                             
348   }                                                                                                                        
349 
350   // We claim cards in blocks so as to reduce the contention.                                                              
351   size_t const block_size = G1RSetScanBlockSize;                                                                           
352 
353   HeapRegionRemSetIterator iter(r->rem_set());                                                                             
354   size_t card_index;                                                                                                       
355 
356   size_t claimed_card_block = _scan_state->iter_claimed_next(region_idx, block_size);                                      
357   for (size_t current_card = 0; iter.has_next(card_index); current_card++) {                                               
358     if (current_card >= claimed_card_block + block_size) {                                                                 
359       claimed_card_block = _scan_state->iter_claimed_next(region_idx, block_size);                                         
360     }                                                                                                                      
361     if (current_card < claimed_card_block) {                                                                               
362       _cards_skipped++;                                                                                                    
363       continue;                                                                                                            
364     }                                                                                                                      
365     _cards_claimed++;                                                                                                      
366 
367     // If the card is dirty, then G1 will scan it during Update RS.                                                        
368     if (_ct->is_card_claimed(card_index) || _ct->is_card_dirty(card_index)) {                                              
369       continue;                                                                                                            
370     }                                                                                                                      
371 
372     HeapWord* const card_start = _g1h->bot()->address_for_index(card_index);                                               
373     uint const region_idx_for_card = _g1h->addr_to_region(card_start);                                                     
374 
375     assert(_g1h->region_at(region_idx_for_card)->is_in_reserved(card_start),                                               
376            "Card start " PTR_FORMAT " to scan outside of region %u", p2i(card_start), _g1h->region_at(region_idx_for_card)-
377     HeapWord* const top = _scan_state->scan_top(region_idx_for_card);                                                      
378     if (card_start >= top) {                                                                                               
379       continue;                                                                                                            
380     }                                                                                                                      
381 
382     // We claim lazily (so races are possible but they're benign), which reduces the                                       
383     // number of duplicate scans (the rsets of the regions in the cset can intersect).                                     
384     // Claim the card after checking bounds above: the remembered set may contain                                          
385     // random cards into current survivor, and we would then have an incorrectly                                           
386     // claimed card in survivor space. Card table clear does not reset the card table                                      
387     // of survivor space regions.                                                                                          
388     claim_card(card_index, region_idx_for_card);                                                                           
389 
390     MemRegion const mr(card_start, MIN2(card_start + BOTConstants::N_words, top));                                         
391 
392     scan_card(mr, region_idx_for_card);                                                                                    
393   }                                                                                                                        
                                                                                                                           
394 }                                                                                                                          
395 
396 void G1ScanRSForRegionClosure::scan_strong_code_roots(HeapRegion* r) {                                                     
                                                                                                                           
397   r->strong_code_roots_do(_pss->closures()->weak_codeblobs());                                                             
                                                                                                                           
398 }                                                                                                                          
399 
400 bool G1ScanRSForRegionClosure::do_heap_region(HeapRegion* r) {                                                             
401   assert(r->in_collection_set(),                                                                                           
402          "Should only be called on elements of the collection set but region %u is not.",                                  
403          r->hrm_index());                                                                                                  
404   uint const region_idx = r->hrm_index();                                                                                  
405 
406   // Do an early out if we know we are complete.                                                                           
407   if (_scan_state->iter_is_complete(region_idx)) {                                                                         
408     return false;                                                                                                          
409   }                                                                                                                        
410 
411   {                                                                                                                        
412     G1EvacPhaseWithTrimTimeTracker timer(_pss, _rem_set_root_scan_time, _rem_set_trim_partially_time);                     
413     scan_rem_set_roots(r);                                                                                                 
414   }                                                                                                                        
415 
416   if (_scan_state->set_iter_complete(region_idx)) {                                                                        
417     G1EvacPhaseWithTrimTimeTracker timer(_pss, _strong_code_root_scan_time, _strong_code_trim_partially_time);             
418     // Scan the strong code root list attached to the current region                                                       
419     scan_strong_code_roots(r);                                                                                             
420   }                                                                                                                        
421   return false;                                                                                                            
422 }                                                                                                                          
423 
424 void G1RemSet::scan_rem_set(G1ParScanThreadState* pss, uint worker_i) {                                                    
425   G1ScanObjsDuringScanRSClosure scan_cl(_g1h, pss);                                                                        
426   G1ScanRSForRegionClosure cl(_scan_state, &scan_cl, pss, worker_i);                                                       
427   _g1h->collection_set_iterate_from(&cl, worker_i);                                                                        
428 
429   G1GCPhaseTimes* p = _g1p->phase_times();                                                                                 
430 
431   p->record_time_secs(G1GCPhaseTimes::ScanRS, worker_i, cl.rem_set_root_scan_time().seconds());                            
432   p->add_time_secs(G1GCPhaseTimes::ObjCopy, worker_i, cl.rem_set_trim_partially_time().seconds());                         
433 
434   p->record_thread_work_item(G1GCPhaseTimes::ScanRS, worker_i, cl.cards_scanned(), G1GCPhaseTimes::ScanRSScannedCards);    
435   p->record_thread_work_item(G1GCPhaseTimes::ScanRS, worker_i, cl.cards_claimed(), G1GCPhaseTimes::ScanRSClaimedCards);    
436   p->record_thread_work_item(G1GCPhaseTimes::ScanRS, worker_i, cl.cards_skipped(), G1GCPhaseTimes::ScanRSSkippedCards);    
437 
438   p->record_time_secs(G1GCPhaseTimes::CodeRoots, worker_i, cl.strong_code_root_scan_time().seconds());                     
439   p->add_time_secs(G1GCPhaseTimes::ObjCopy, worker_i, cl.strong_code_root_trim_partially_time().seconds());                
440 }                                                                                                                          
441 
442 // Closure used for updating rem sets. Only called during an evacuation pause.                                             
443 class G1RefineCardClosure: public CardTableEntryClosure {                                                                  
444   G1RemSet* _g1rs;                                                                                                         
445   G1ScanObjsDuringUpdateRSClosure* _update_rs_cl;                                                                          
446 
447   size_t _cards_scanned;                                                                                                   
448   size_t _cards_skipped;                                                                                                   
449 public:                                                                                                                    
450   G1RefineCardClosure(G1CollectedHeap* g1h, G1ScanObjsDuringUpdateRSClosure* update_rs_cl) :                               
451     _g1rs(g1h->g1_rem_set()), _update_rs_cl(update_rs_cl), _cards_scanned(0), _cards_skipped(0)                            
452   {}                                                                                                                       
453 
454   bool do_card_ptr(jbyte* card_ptr, uint worker_i) {                                                                       
455     // The only time we care about recording cards that                                                                    
456     // contain references that point into the collection set                                                               
457     // is during RSet updating within an evacuation pause.                                                                 
458     // In this case worker_i should be the id of a GC worker thread.                                                       
459     assert(SafepointSynchronize::is_at_safepoint(), "not during an evacuation pause");                                     
460 
461     bool card_scanned = _g1rs->refine_card_during_gc(card_ptr, _update_rs_cl);                                             
462 
463     if (card_scanned) {                                                                                                    
464       _update_rs_cl->trim_queue_partially();                                                                               
465       _cards_scanned++;                                                                                                    
466     } else {                                                                                                               
467       _cards_skipped++;                                                                                                    
468     }                                                                                                                      
469     return true;                                                                                                           
470   }                                                                                                                        
471 
472   size_t cards_scanned() const { return _cards_scanned; }                                                                  
473   size_t cards_skipped() const { return _cards_skipped; }                                                                  
474 };                                                                                                                         
475 
476 void G1RemSet::update_rem_set(G1ParScanThreadState* pss, uint worker_i) {                                                  
477   G1GCPhaseTimes* p = _g1p->phase_times();                                                                                 
478 
479   // Apply closure to log entries in the HCC.                                                                              
480   if (G1HotCardCache::default_use_cache()) {                                                                               
481     G1EvacPhaseTimesTracker x(p, pss, G1GCPhaseTimes::ScanHCC, worker_i);                                                  
482 
483     G1ScanObjsDuringUpdateRSClosure scan_hcc_cl(_g1h, pss, worker_i);                                                      
484     G1RefineCardClosure refine_card_cl(_g1h, &scan_hcc_cl);                                                                
485     _g1h->iterate_hcc_closure(&refine_card_cl, worker_i);                                                                  
486   }                                                                                                                        
487 
488   // Now apply the closure to all remaining log entries.                                                                   
489   {                                                                                                                        
490     G1EvacPhaseTimesTracker x(p, pss, G1GCPhaseTimes::UpdateRS, worker_i);                                                 
491 
492     G1ScanObjsDuringUpdateRSClosure update_rs_cl(_g1h, pss, worker_i);                                                     
493     G1RefineCardClosure refine_card_cl(_g1h, &update_rs_cl);                                                               
494     _g1h->iterate_dirty_card_closure(&refine_card_cl, worker_i);                                                           
495 
496     p->record_thread_work_item(G1GCPhaseTimes::UpdateRS, worker_i, refine_card_cl.cards_scanned(), G1GCPhaseTimes::UpdateRS
497     p->record_thread_work_item(G1GCPhaseTimes::UpdateRS, worker_i, refine_card_cl.cards_skipped(), G1GCPhaseTimes::UpdateRS
498   }                                                                                                                        
499 }                                                                                                                          
500 
501 void G1RemSet::cleanupHRRS() {                                                                                             
502   HeapRegionRemSet::cleanup();                                                                                             
503 }                                                                                                                          
504 
505 void G1RemSet::oops_into_collection_set_do(G1ParScanThreadState* pss, uint worker_i) {                                     
506   update_rem_set(pss, worker_i);                                                                                           
507   scan_rem_set(pss, worker_i);;                                                                                            
508 }                                                                                                                          
509 
510 void G1RemSet::prepare_for_oops_into_collection_set_do() {                                                                 
511   DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();                                                          
512   dcqs.concatenate_logs();                                                                                                 
513 
514   _scan_state->reset();                                                                                                    
515 }                                                                                                                          
516 
517 void G1RemSet::cleanup_after_oops_into_collection_set_do() {                                                               
518   G1GCPhaseTimes* phase_times = _g1h->g1_policy()->phase_times();                                                          
519 
520   // Set all cards back to clean.                                                                                          
521   double start = os::elapsedTime();                                                                                        
522   _scan_state->clear_card_table(_g1h->workers());                                                                          
523   phase_times->record_clear_ct_time((os::elapsedTime() - start) * 1000.0);                                                 
524 }                                                                                                                          
525 
526 inline void check_card_ptr(jbyte* card_ptr, G1CardTable* ct) {                                                             
527 #ifdef ASSERT                                                                                                              
528   G1CollectedHeap* g1h = G1CollectedHeap::heap();                                                                          
529   assert(g1h->is_in_exact(ct->addr_for(card_ptr)),                                                                         
530          "Card at " PTR_FORMAT " index " SIZE_FORMAT " representing heap at " PTR_FORMAT " (%u) must be in committed heap",
531          p2i(card_ptr),                                                                                                    
532          ct->index_for(ct->addr_for(card_ptr)),                                                                            
533          p2i(ct->addr_for(card_ptr)),                                                                                      
534          g1h->addr_to_region(ct->addr_for(card_ptr)));                                                                     
535 #endif                                                                                                                     
536 }                                                                                                                          
537 
538 void G1RemSet::refine_card_concurrently(jbyte* card_ptr,                                                                   
539                                         uint worker_i) {                                                                   
540   assert(!_g1h->is_gc_active(), "Only call concurrently");                                                                 
541 
542   check_card_ptr(card_ptr, _ct);                                                                                           
543 
544   // If the card is no longer dirty, nothing to do.                                                                        
545   if (*card_ptr != G1CardTable::dirty_card_val()) {                                                                        
546     return;                                                                                                                
547   }                                                                                                                        
548 
549   // Construct the region representing the card.                                                                           
550   HeapWord* start = _ct->addr_for(card_ptr);                                                                               
551   // And find the region containing it.                                                                                    
552   HeapRegion* r = _g1h->heap_region_containing(start);                                                                     
553 
554   // This check is needed for some uncommon cases where we should                                                          
555   // ignore the card.                                                                                                      
556   //                                                                                                                       
557   // The region could be young.  Cards for young regions are                                                               
558   // distinctly marked (set to g1_young_gen), so the post-barrier will                                                     
559   // filter them out.  However, that marking is performed                                                                  
560   // concurrently.  A write to a young object could occur before the                                                       
561   // card has been marked young, slipping past the filter.                                                                 
562   //                                                                                                                       
563   // The card could be stale, because the region has been freed since                                                      
564   // the card was recorded. In this case the region type could be                                                          
565   // anything.  If (still) free or (reallocated) young, just ignore                                                        
566   // it.  If (reallocated) old or humongous, the later card trimming                                                       
567   // and additional checks in iteration may detect staleness.  At                                                          
568   // worst, we end up processing a stale card unnecessarily.                                                               
569   //                                                                                                                       
570   // In the normal (non-stale) case, the synchronization between the                                                       
571   // enqueueing of the card and processing it here will have ensured                                                       
572   // we see the up-to-date region type here.                                                                               
573   if (!r->is_old_or_humongous_or_archive()) {                                                                              
574     return;                                                                                                                
575   }                                                                                                                        
576 
577   // The result from the hot card cache insert call is either:                                                             
578   //   * pointer to the current card                                                                                       
579   //     (implying that the current card is not 'hot'),                                                                    
580   //   * null                                                                                                              
581   //     (meaning we had inserted the card ptr into the "hot" card cache,                                                  
582   //     which had some headroom),                                                                                         
583   //   * a pointer to a "hot" card that was evicted from the "hot" cache.                                                  
584   //                                                                                                                       
585 
586   if (_hot_card_cache->use_cache()) {                                                                                      
587     assert(!SafepointSynchronize::is_at_safepoint(), "sanity");                                                            
588 
589     const jbyte* orig_card_ptr = card_ptr;                                                                                 
590     card_ptr = _hot_card_cache->insert(card_ptr);                                                                          
591     if (card_ptr == NULL) {                                                                                                
592       // There was no eviction. Nothing to do.                                                                             
593       return;                                                                                                              
594     } else if (card_ptr != orig_card_ptr) {                                                                                
595       // Original card was inserted and an old card was evicted.                                                           
596       start = _ct->addr_for(card_ptr);                                                                                     
597       r = _g1h->heap_region_containing(start);                                                                             
598 
599       // Check whether the region formerly in the cache should be                                                          
600       // ignored, as discussed earlier for the original card.  The                                                         
601       // region could have been freed while in the cache.                                                                  
602       if (!r->is_old_or_humongous_or_archive()) {                                                                          
603         return;                                                                                                            
604       }                                                                                                                    
605     } // Else we still have the original card.                                                                             
606   }                                                                                                                        
607 
608   // Trim the region designated by the card to what's been allocated                                                       
609   // in the region.  The card could be stale, or the card could cover                                                      
610   // (part of) an object at the end of the allocated space and extend                                                      
611   // beyond the end of allocation.                                                                                         
612 
613   // Non-humongous objects are only allocated in the old-gen during                                                        
614   // GC, so if region is old then top is stable.  Humongous object                                                         
615   // allocation sets top last; if top has not yet been set, this is                                                        
616   // a stale card and we'll end up with an empty intersection.  If                                                         
617   // this is not a stale card, the synchronization between the                                                             
618   // enqueuing of the card and processing it here will have ensured                                                        
619   // we see the up-to-date top here.                                                                                       
620   HeapWord* scan_limit = r->top();                                                                                         
621 
622   if (scan_limit <= start) {                                                                                               
623     // If the trimmed region is empty, the card must be stale.                                                             
624     return;                                                                                                                
625   }                                                                                                                        
626 
627   // Okay to clean and process the card now.  There are still some                                                         
628   // stale card cases that may be detected by iteration and dealt with                                                     
629   // as iteration failure.                                                                                                 
630   *const_cast<volatile jbyte*>(card_ptr) = G1CardTable::clean_card_val();                                                  
631 
632   // This fence serves two purposes.  First, the card must be cleaned                                                      
633   // before processing the contents.  Second, we can't proceed with                                                        
634   // processing until after the read of top, for synchronization with                                                      
635   // possibly concurrent humongous object allocation.  It's okay that                                                      
636   // reading top and reading type were racy wrto each other.  We need                                                      
637   // both set, in any order, to proceed.                                                                                   
638   OrderAccess::fence();                                                                                                    
639 
640   // Don't use addr_for(card_ptr + 1) which can ask for                                                                    
641   // a card beyond the heap.                                                                                               
642   HeapWord* end = start + G1CardTable::card_size_in_words;                                                                 
643   MemRegion dirty_region(start, MIN2(scan_limit, end));                                                                    
644   assert(!dirty_region.is_empty(), "sanity");                                                                              
645 
646   G1ConcurrentRefineOopClosure conc_refine_cl(_g1h, worker_i);                                                             
647 
648   bool card_processed =                                                                                                    
649     r->oops_on_card_seq_iterate_careful<false>(dirty_region, &conc_refine_cl);                                             
650 
651   // If unable to process the card then we encountered an unparsable                                                       
652   // part of the heap (e.g. a partially allocated object) while                                                            
653   // processing a stale card.  Despite the card being stale, redirty                                                       
654   // and re-enqueue, because we've already cleaned the card.  Without                                                      
655   // this we could incorrectly discard a non-stale card.                                                                   
656   if (!card_processed) {                                                                                                   
657     // The card might have gotten re-dirtied and re-enqueued while we                                                      
658     // worked.  (In fact, it's pretty likely.)                                                                             
659     if (*card_ptr != G1CardTable::dirty_card_val()) {                                                                      
660       *card_ptr = G1CardTable::dirty_card_val();                                                                           
661       MutexLockerEx x(Shared_DirtyCardQ_lock,                                                                              
662                       Mutex::_no_safepoint_check_flag);                                                                    
663       DirtyCardQueue* sdcq =                                                                                               
664         G1BarrierSet::dirty_card_queue_set().shared_dirty_card_queue();                                                    
665       sdcq->enqueue(card_ptr);                                                                                             
666     }                                                                                                                      
667   } else {                                                                                                                 
668     _num_conc_refined_cards++; // Unsynchronized update, only used for logging.                                            
669   }                                                                                                                        
670 }                                                                                                                          
671 
672 bool G1RemSet::refine_card_during_gc(jbyte* card_ptr,                                                                      
673                                      G1ScanObjsDuringUpdateRSClosure* update_rs_cl) {                                      
674   assert(_g1h->is_gc_active(), "Only call during GC");                                                                     
675 
676   check_card_ptr(card_ptr, _ct);                                                                                           
677 
678   // If the card is no longer dirty, nothing to do. This covers cards that were already                                    
679   // scanned as parts of the remembered sets.                                                                              
680   if (*card_ptr != G1CardTable::dirty_card_val()) {                                                                        
681     return false;                                                                                                          
682   }                                                                                                                        
683 
684   // We claim lazily (so races are possible but they're benign), which reduces the                                         
685   // number of potential duplicate scans (multiple threads may enqueue the same card twice).                               
686   *card_ptr = G1CardTable::clean_card_val() | G1CardTable::claimed_card_val();                                             
687 
688   // Construct the region representing the card.                                                                           
689   HeapWord* card_start = _ct->addr_for(card_ptr);                                                                          
690   // And find the region containing it.                                                                                    
691   uint const card_region_idx = _g1h->addr_to_region(card_start);                                                           
692 
693   _scan_state->add_dirty_region(card_region_idx);                                                                          
694   HeapWord* scan_limit = _scan_state->scan_top(card_region_idx);                                                           
695   if (scan_limit <= card_start) {                                                                                          
696     // If the card starts above the area in the region containing objects to scan, skip it.                                
697     return false;                                                                                                          
698   }                                                                                                                        
699 
700   // Don't use addr_for(card_ptr + 1) which can ask for                                                                    
701   // a card beyond the heap.                                                                                               
702   HeapWord* card_end = card_start + G1CardTable::card_size_in_words;                                                       
703   MemRegion dirty_region(card_start, MIN2(scan_limit, card_end));                                                          
704   assert(!dirty_region.is_empty(), "sanity");                                                                              
705 
706   HeapRegion* const card_region = _g1h->region_at(card_region_idx);                                                        
707   update_rs_cl->set_region(card_region);                                                                                   
708   bool card_processed = card_region->oops_on_card_seq_iterate_careful<true>(dirty_region, update_rs_cl);                   
709   assert(card_processed, "must be");                                                                                       
710   return true;                                                                                                             
711 }                                                                                                                          
712 
713 void G1RemSet::print_periodic_summary_info(const char* header, uint period_count) {                                        
714   if ((G1SummarizeRSetStatsPeriod > 0) && log_is_enabled(Trace, gc, remset) &&                                             
715       (period_count % G1SummarizeRSetStatsPeriod == 0)) {                                                                  
716 
717     G1RemSetSummary current(this);                                                                                         
718     _prev_period_summary.subtract_from(&current);                                                                          
719 
720     Log(gc, remset) log;                                                                                                   
721     log.trace("%s", header);                                                                                               
722     ResourceMark rm;                                                                                                       
723     LogStream ls(log.trace());                                                                                             
724     _prev_period_summary.print_on(&ls);                                                                                    
725 
726     _prev_period_summary.set(&current);                                                                                    
727   }                                                                                                                        
728 }                                                                                                                          
729 
730 void G1RemSet::print_summary_info() {                                                                                      
731   Log(gc, remset, exit) log;                                                                                               
732   if (log.is_trace()) {                                                                                                    
733     log.trace(" Cumulative RS summary");                                                                                   
734     G1RemSetSummary current(this);                                                                                         
735     ResourceMark rm;                                                                                                       
736     LogStream ls(log.trace());                                                                                             
737     current.print_on(&ls);                                                                                                 
738   }                                                                                                                        
739 }                                                                                                                          
740 
741 class G1RebuildRemSetTask: public AbstractGangTask {                                                                       
742   // Aggregate the counting data that was constructed concurrently                                                         
743   // with marking.                                                                                                         
744   class G1RebuildRemSetHeapRegionClosure : public HeapRegionClosure {                                                      
745     G1ConcurrentMark* _cm;                                                                                                 
746     G1RebuildRemSetClosure _update_cl;                                                                                     
747 
748     // Applies _update_cl to the references of the given object, limiting objArrays                                        
749     // to the given MemRegion. Returns the amount of words actually scanned.                                               
750     size_t scan_for_references(oop const obj, MemRegion mr) {                                                              
751       size_t const obj_size = obj->size();                                                                                 
752       // All non-objArrays and objArrays completely within the mr                                                          
753       // can be scanned without passing the mr.                                                                            
754       if (!obj->is_objArray() || mr.contains(MemRegion((HeapWord*)obj, obj_size))) {                                       
755         obj->oop_iterate(&_update_cl);                                                                                     
756         return obj_size;                                                                                                   
757       }                                                                                                                    
758       // This path is for objArrays crossing the given MemRegion. Only scan the                                            
759       // area within the MemRegion.                                                                                        
760       obj->oop_iterate(&_update_cl, mr);                                                                                   
761       return mr.intersection(MemRegion((HeapWord*)obj, obj_size)).word_size();                                             
762     }                                                                                                                      
763 
764     // A humongous object is live (with respect to the scanning) either                                                    
765     // a) it is marked on the bitmap as such                                                                               
766     // b) its TARS is larger than TAMS, i.e. has been allocated during marking.                                            
767     bool is_humongous_live(oop const humongous_obj, const G1CMBitMap* const bitmap, HeapWord* tams, HeapWord* tars) const {
768       return bitmap->is_marked(humongous_obj) || (tars > tams);                                                            
769     }                                                                                                                      
770 
771     // Iterator over the live objects within the given MemRegion.                                                          
772     class LiveObjIterator : public StackObj {                                                                              
773       const G1CMBitMap* const _bitmap;                                                                                     
774       const HeapWord* _tams;                                                                                               
775       const MemRegion _mr;                                                                                                 
776       HeapWord* _current;                                                                                                  
777 
778       bool is_below_tams() const {                                                                                         
779         return _current < _tams;                                                                                           
780       }                                                                                                                    
781 
782       bool is_live(HeapWord* obj) const {                                                                                  
783         return !is_below_tams() || _bitmap->is_marked(obj);                                                                
784       }                                                                                                                    
785 
786       HeapWord* bitmap_limit() const {                                                                                     
787         return MIN2(const_cast<HeapWord*>(_tams), _mr.end());                                                              
788       }                                                                                                                    
789 
790       void move_if_below_tams() {                                                                                          
791         if (is_below_tams() && has_next()) {                                                                               
792           _current = _bitmap->get_next_marked_addr(_current, bitmap_limit());                                              
793         }                                                                                                                  
794       }                                                                                                                    
795     public:                                                                                                                
796       LiveObjIterator(const G1CMBitMap* const bitmap, const HeapWord* tams, const MemRegion mr, HeapWord* first_oop_into_mr
797           _bitmap(bitmap),                                                                                                 
798           _tams(tams),                                                                                                     
799           _mr(mr),                                                                                                         
800           _current(first_oop_into_mr) {                                                                                    
801 
802         assert(_current <= _mr.start(),                                                                                    
803                "First oop " PTR_FORMAT " should extend into mr [" PTR_FORMAT ", " PTR_FORMAT ")",                          
804                p2i(first_oop_into_mr), p2i(mr.start()), p2i(mr.end()));                                                    
805 
806         // Step to the next live object within the MemRegion if needed.                                                    
807         if (is_live(_current)) {                                                                                           
808           // Non-objArrays were scanned by the previous part of that region.                                               
809           if (_current < mr.start() && !oop(_current)->is_objArray()) {                                                    
810             _current += oop(_current)->size();                                                                             
811             // We might have positioned _current on a non-live object. Reposition to the next                              
812             // live one if needed.                                                                                         
813             move_if_below_tams();                                                                                          
814           }                                                                                                                
815         } else {                                                                                                           
816           // The object at _current can only be dead if below TAMS, so we can use the bitmap.                              
817           // immediately.                                                                                                  
818           _current = _bitmap->get_next_marked_addr(_current, bitmap_limit());                                              
819           assert(_current == _mr.end() || is_live(_current),                                                               
820                  "Current " PTR_FORMAT " should be live (%s) or beyond the end of the MemRegion (" PTR_FORMAT ")",         
821                  p2i(_current), BOOL_TO_STR(is_live(_current)), p2i(_mr.end()));                                           
822         }                                                                                                                  
823       }                                                                                                                    
824 
825       void move_to_next() {                                                                                                
826         _current += next()->size();                                                                                        
827         move_if_below_tams();                                                                                              
828       }                                                                                                                    
829 
830       oop next() const {                                                                                                   
831         oop result = oop(_current);                                                                                        
832         assert(is_live(_current),                                                                                          
833                "Object " PTR_FORMAT " must be live TAMS " PTR_FORMAT " below %d mr " PTR_FORMAT " " PTR_FORMAT " outside %d
834                p2i(_current), p2i(_tams), _tams > _current, p2i(_mr.start()), p2i(_mr.end()), _mr.contains(result));       
835         return result;                                                                                                     
836       }                                                                                                                    
837 
838       bool has_next() const {                                                                                              
839         return _current < _mr.end();                                                                                       
840       }                                                                                                                    
841     };                                                                                                                     
842 
843     // Rebuild remembered sets in the part of the region specified by mr and hr.                                           
844     // Objects between the bottom of the region and the TAMS are checked for liveness                                      
845     // using the given bitmap. Objects between TAMS and TARS are assumed to be live.                                       
846     // Returns the number of live words between bottom and TAMS.                                                           
847     size_t rebuild_rem_set_in_region(const G1CMBitMap* const bitmap,                                                       
848                                      HeapWord* const top_at_mark_start,                                                    
849                                      HeapWord* const top_at_rebuild_start,                                                 
850                                      HeapRegion* hr,                                                                       
851                                      MemRegion mr) {                                                                       
852       size_t marked_words = 0;                                                                                             
853 
854       if (hr->is_humongous()) {                                                                                            
855         oop const humongous_obj = oop(hr->humongous_start_region()->bottom());                                             
856         if (is_humongous_live(humongous_obj, bitmap, top_at_mark_start, top_at_rebuild_start)) {                           
857           // We need to scan both [bottom, TAMS) and [TAMS, top_at_rebuild_start);                                         
858           // however in case of humongous objects it is sufficient to scan the encompassing                                
859           // area (top_at_rebuild_start is always larger or equal to TAMS) as one of the                                   
860           // two areas will be zero sized. I.e. TAMS is either                                                             
861           // the same as bottom or top(_at_rebuild_start). There is no way TAMS has a different                            
862           // value: this would mean that TAMS points somewhere into the object.                                            
863           assert(hr->top() == top_at_mark_start || hr->top() == top_at_rebuild_start,                                      
864                  "More than one object in the humongous region?");                                                         
865           humongous_obj->oop_iterate(&_update_cl, mr);                                                                     
866           return top_at_mark_start != hr->bottom() ? mr.intersection(MemRegion((HeapWord*)humongous_obj, humongous_obj->siz
867         } else {                                                                                                           
868           return 0;                                                                                                        
869         }                                                                                                                  
870       }                                                                                                                    
871 
872       for (LiveObjIterator it(bitmap, top_at_mark_start, mr, hr->block_start(mr.start())); it.has_next(); it.move_to_next()
873         oop obj = it.next();                                                                                               
874         size_t scanned_size = scan_for_references(obj, mr);                                                                
875         if ((HeapWord*)obj < top_at_mark_start) {                                                                          
876           marked_words += scanned_size;                                                                                    
877         }                                                                                                                  
878       }                                                                                                                    
879 
880       return marked_words * HeapWordSize;                                                                                  
881     }                                                                                                                      
882 public:                                                                                                                    
883   G1RebuildRemSetHeapRegionClosure(G1CollectedHeap* g1h,                                                                   
884                                    G1ConcurrentMark* cm,                                                                   
885                                    uint worker_id) :                                                                       
886     HeapRegionClosure(),                                                                                                   
887     _cm(cm),                                                                                                               
888     _update_cl(g1h, worker_id) { }                                                                                         
889 
890     bool do_heap_region(HeapRegion* hr) {                                                                                  
891       if (_cm->has_aborted()) {                                                                                            
892         return true;                                                                                                       
893       }                                                                                                                    
894 
895       uint const region_idx = hr->hrm_index();                                                                             
896       DEBUG_ONLY(HeapWord* const top_at_rebuild_start_check = _cm->top_at_rebuild_start(region_idx);)                      
897       assert(top_at_rebuild_start_check == NULL ||                                                                         
898              top_at_rebuild_start_check > hr->bottom(),                                                                    
899              "A TARS (" PTR_FORMAT ") == bottom() (" PTR_FORMAT ") indicates the old region %u is empty (%s)",             
900              p2i(top_at_rebuild_start_check), p2i(hr->bottom()),  region_idx, hr->get_type_str());                         
901 
902       size_t total_marked_bytes = 0;                                                                                       
903       size_t const chunk_size_in_words = G1RebuildRemSetChunkSize / HeapWordSize;                                          
904 
905       HeapWord* const top_at_mark_start = hr->prev_top_at_mark_start();                                                    
906 
907       HeapWord* cur = hr->bottom();                                                                                        
908       while (cur < hr->end()) {                                                                                            
909         // After every iteration (yield point) we need to check whether the region's                                       
910         // TARS changed due to e.g. eager reclaim.                                                                         
911         HeapWord* const top_at_rebuild_start = _cm->top_at_rebuild_start(region_idx);                                      
912         if (top_at_rebuild_start == NULL) {                                                                                
913           return false;                                                                                                    
914         }                                                                                                                  
915 
916         MemRegion next_chunk = MemRegion(hr->bottom(), top_at_rebuild_start).intersection(MemRegion(cur, chunk_size_in_word
917         if (next_chunk.is_empty()) {                                                                                       
918           break;                                                                                                           
919         }                                                                                                                  
920 
921         const Ticks start = Ticks::now();                                                                                  
922         size_t marked_bytes = rebuild_rem_set_in_region(_cm->prev_mark_bitmap(),                                           
923                                                         top_at_mark_start,                                                 
924                                                         top_at_rebuild_start,                                              
925                                                         hr,                                                                
926                                                         next_chunk);                                                       
927         Tickspan time = Ticks::now() - start;                                                                              
928 
929         log_trace(gc, remset, tracking)("Rebuilt region %u "                                                               
930                                         "live " SIZE_FORMAT " "                                                            
931                                         "time %.3fms "                                                                     
932                                         "marked bytes " SIZE_FORMAT " "                                                    
933                                         "bot " PTR_FORMAT " "                                                              
934                                         "TAMS " PTR_FORMAT " "                                                             
935                                         "TARS " PTR_FORMAT,                                                                
936                                         region_idx,                                                                        
937                                         _cm->liveness(region_idx) * HeapWordSize,                                          
938                                         time.seconds() * 1000.0,                                                           
939                                         marked_bytes,                                                                      
940                                         p2i(hr->bottom()),                                                                 
941                                         p2i(top_at_mark_start),                                                            
942                                         p2i(top_at_rebuild_start));                                                        
943 
944         if (marked_bytes > 0) {                                                                                            
945           total_marked_bytes += marked_bytes;                                                                              
946         }                                                                                                                  
947         cur += chunk_size_in_words;                                                                                        
948 
949         _cm->do_yield_check();                                                                                             
950         if (_cm->has_aborted()) {                                                                                          
951           return true;                                                                                                     
952         }                                                                                                                  
953       }                                                                                                                    
954       // In the final iteration of the loop the region might have been eagerly reclaimed.                                  
955       // Simply filter out those regions. We can not just use region type because there                                    
956       // might have already been new allocations into these regions.                                                       
957       DEBUG_ONLY(HeapWord* const top_at_rebuild_start = _cm->top_at_rebuild_start(region_idx);)                            
958       assert(top_at_rebuild_start == NULL ||                                                                               
959              total_marked_bytes == hr->marked_bytes(),                                                                     
960              "Marked bytes " SIZE_FORMAT " for region %u (%s) in [bottom, TAMS) do not match calculated marked bytes " SIZE
961              "(" PTR_FORMAT " " PTR_FORMAT " " PTR_FORMAT ")",                                                             
962              total_marked_bytes, hr->hrm_index(), hr->get_type_str(), hr->marked_bytes(),                                  
963              p2i(hr->bottom()), p2i(top_at_mark_start), p2i(top_at_rebuild_start));                                        
964        // Abort state may have changed after the yield check.                                                              
965       return _cm->has_aborted();                                                                                           
966     }                                                                                                                      
967   };                                                                                                                       
968 
969   HeapRegionClaimer _hr_claimer;                                                                                           
970   G1ConcurrentMark* _cm;                                                                                                   
971 
972   uint _worker_id_offset;                                                                                                  
973 public:                                                                                                                    
974   G1RebuildRemSetTask(G1ConcurrentMark* cm,                                                                                
975                       uint n_workers,                                                                                      
976                       uint worker_id_offset) :                                                                             
977       AbstractGangTask("G1 Rebuild Remembered Set"),                                                                       
978       _hr_claimer(n_workers),                                                                                              
979       _cm(cm),                                                                                                             
980       _worker_id_offset(worker_id_offset) {                                                                                
981   }                                                                                                                        
982 
983   void work(uint worker_id) {                                                                                              
984     SuspendibleThreadSetJoiner sts_join;                                                                                   
985 
986     G1CollectedHeap* g1h = G1CollectedHeap::heap();                                                                        
987 
988     G1RebuildRemSetHeapRegionClosure cl(g1h, _cm, _worker_id_offset + worker_id);                                          
989     g1h->heap_region_par_iterate_from_worker_offset(&cl, &_hr_claimer, worker_id);                                         
990   }                                                                                                                        
991 };                                                                                                                         
992 
993 void G1RemSet::rebuild_rem_set(G1ConcurrentMark* cm,                                                                       
994                                WorkGang* workers,                                                                          
995                                uint worker_id_offset) {                                                                    
996   uint num_workers = workers->active_workers();                                                                            
997 
998   G1RebuildRemSetTask cl(cm,                                                                                               
999                          num_workers,                                                                                      
1000                          worker_id_offset);                                                                                
1001   workers->run_task(&cl, num_workers);                                                                                     
1002 }                                                                                                                          
--- EOF ---