1 /* 2 * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 25 #include "incls/_precompiled.incl" 26 #include "incls/_concurrentG1Refine.cpp.incl" 27 28 bool ConcurrentG1Refine::_enabled = false; 29 30 ConcurrentG1Refine::ConcurrentG1Refine() : 31 _pya(PYA_continue), _last_pya(PYA_continue), 32 _last_cards_during(), _first_traversal(false), 33 _card_counts(NULL), _cur_card_count_histo(NULL), _cum_card_count_histo(NULL), 34 _hot_cache(NULL), 35 _def_use_cache(false), _use_cache(false), 36 _n_periods(0), _total_cards(0), _total_travs(0) 37 { 38 if (G1ConcRefine) { 39 _cg1rThread = new ConcurrentG1RefineThread(this); 40 assert(cg1rThread() != NULL, "Conc refine should have been created"); 41 assert(cg1rThread()->cg1r() == this, 42 "Conc refine thread should refer to this"); 43 } else { 44 _cg1rThread = NULL; 45 } 46 } 47 48 void ConcurrentG1Refine::init() { 49 if (G1ConcRSLogCacheSize > 0 || G1ConcRSCountTraversals) { 50 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 51 _n_card_counts = 52 (unsigned) (g1h->g1_reserved_obj_bytes() >> CardTableModRefBS::card_shift); 53 _card_counts = NEW_C_HEAP_ARRAY(unsigned char, _n_card_counts); 54 for (size_t i = 0; i < _n_card_counts; i++) _card_counts[i] = 0; 55 ModRefBarrierSet* bs = g1h->mr_bs(); 56 guarantee(bs->is_a(BarrierSet::CardTableModRef), "Precondition"); 57 CardTableModRefBS* ctbs = (CardTableModRefBS*)bs; 58 _ct_bot = ctbs->byte_for_const(g1h->reserved_region().start()); 59 if (G1ConcRSCountTraversals) { 60 _cur_card_count_histo = NEW_C_HEAP_ARRAY(unsigned, 256); 61 _cum_card_count_histo = NEW_C_HEAP_ARRAY(unsigned, 256); 62 for (int i = 0; i < 256; i++) { 63 _cur_card_count_histo[i] = 0; 64 _cum_card_count_histo[i] = 0; 65 } 66 } 67 } 68 if (G1ConcRSLogCacheSize > 0) { 69 _def_use_cache = true; 70 _use_cache = true; 71 _hot_cache_size = (1 << G1ConcRSLogCacheSize); 72 _hot_cache = NEW_C_HEAP_ARRAY(jbyte*, _hot_cache_size); 73 _n_hot = 0; 74 _hot_cache_idx = 0; 75 } 76 } 77 78 ConcurrentG1Refine::~ConcurrentG1Refine() { 79 if (G1ConcRSLogCacheSize > 0 || G1ConcRSCountTraversals) { 80 assert(_card_counts != NULL, "Logic"); 81 FREE_C_HEAP_ARRAY(unsigned char, _card_counts); 82 assert(_cur_card_count_histo != NULL, "Logic"); 83 FREE_C_HEAP_ARRAY(unsigned, _cur_card_count_histo); 84 assert(_cum_card_count_histo != NULL, "Logic"); 85 FREE_C_HEAP_ARRAY(unsigned, _cum_card_count_histo); 86 } 87 if (G1ConcRSLogCacheSize > 0) { 88 assert(_hot_cache != NULL, "Logic"); 89 FREE_C_HEAP_ARRAY(jbyte*, _hot_cache); 90 } 91 } 92 93 bool ConcurrentG1Refine::refine() { 94 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 95 unsigned cards_before = g1h->g1_rem_set()->conc_refine_cards(); 96 clear_hot_cache(); // Any previous values in this are now invalid. 97 g1h->g1_rem_set()->concurrentRefinementPass(this); 98 _traversals++; 99 unsigned cards_after = g1h->g1_rem_set()->conc_refine_cards(); 100 unsigned cards_during = cards_after-cards_before; 101 // If this is the first traversal in the current enabling 102 // and we did some cards, or if the number of cards found is decreasing 103 // sufficiently quickly, then keep going. Otherwise, sleep a while. 104 bool res = 105 (_first_traversal && cards_during > 0) 106 || 107 (!_first_traversal && cards_during * 3 < _last_cards_during * 2); 108 _last_cards_during = cards_during; 109 _first_traversal = false; 110 return res; 111 } 112 113 void ConcurrentG1Refine::enable() { 114 MutexLocker x(G1ConcRefine_mon); 115 if (!_enabled) { 116 _enabled = true; 117 _first_traversal = true; _last_cards_during = 0; 118 G1ConcRefine_mon->notify_all(); 119 } 120 } 121 122 unsigned ConcurrentG1Refine::disable() { 123 MutexLocker x(G1ConcRefine_mon); 124 if (_enabled) { 125 _enabled = false; 126 return _traversals; 127 } else { 128 return 0; 129 } 130 } 131 132 void ConcurrentG1Refine::wait_for_ConcurrentG1Refine_enabled() { 133 G1ConcRefine_mon->lock(); 134 while (!_enabled) { 135 G1ConcRefine_mon->wait(Mutex::_no_safepoint_check_flag); 136 } 137 G1ConcRefine_mon->unlock(); 138 _traversals = 0; 139 }; 140 141 void ConcurrentG1Refine::set_pya_restart() { 142 // If we're using the log-based RS barrier, the above will cause 143 // in-progress traversals of completed log buffers to quit early; we will 144 // also abandon all other buffers. 145 if (G1RSBarrierUseQueue) { 146 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set(); 147 dcqs.abandon_logs(); 148 // Reset the post-yield actions. 149 _pya = PYA_continue; 150 _last_pya = PYA_continue; 151 } else { 152 _pya = PYA_restart; 153 } 154 } 155 156 void ConcurrentG1Refine::set_pya_cancel() { 157 _pya = PYA_cancel; 158 } 159 160 PostYieldAction ConcurrentG1Refine::get_pya() { 161 if (_pya != PYA_continue) { 162 jint val = _pya; 163 while (true) { 164 jint val_read = Atomic::cmpxchg(PYA_continue, &_pya, val); 165 if (val_read == val) { 166 PostYieldAction res = (PostYieldAction)val; 167 assert(res != PYA_continue, "Only the refine thread should reset."); 168 _last_pya = res; 169 return res; 170 } else { 171 val = val_read; 172 } 173 } 174 } 175 // QQQ WELL WHAT DO WE RETURN HERE??? 176 // make up something! 177 return PYA_continue; 178 } 179 180 PostYieldAction ConcurrentG1Refine::get_last_pya() { 181 PostYieldAction res = _last_pya; 182 _last_pya = PYA_continue; 183 return res; 184 } 185 186 bool ConcurrentG1Refine::do_traversal() { 187 return _cg1rThread->do_traversal(); 188 } 189 190 int ConcurrentG1Refine::add_card_count(jbyte* card_ptr) { 191 size_t card_num = (card_ptr - _ct_bot); 192 guarantee(0 <= card_num && card_num < _n_card_counts, "Bounds"); 193 unsigned char cnt = _card_counts[card_num]; 194 if (cnt < 255) _card_counts[card_num]++; 195 return cnt; 196 _total_travs++; 197 } 198 199 jbyte* ConcurrentG1Refine::cache_insert(jbyte* card_ptr) { 200 int count = add_card_count(card_ptr); 201 // Count previously unvisited cards. 202 if (count == 0) _total_cards++; 203 // We'll assume a traversal unless we store it in the cache. 204 if (count < G1ConcRSHotCardLimit) { 205 _total_travs++; 206 return card_ptr; 207 } 208 // Otherwise, it's hot. 209 jbyte* res = NULL; 210 MutexLockerEx x(HotCardCache_lock, Mutex::_no_safepoint_check_flag); 211 if (_n_hot == _hot_cache_size) { 212 _total_travs++; 213 res = _hot_cache[_hot_cache_idx]; 214 _n_hot--; 215 } 216 // Now _n_hot < _hot_cache_size, and we can insert at _hot_cache_idx. 217 _hot_cache[_hot_cache_idx] = card_ptr; 218 _hot_cache_idx++; 219 if (_hot_cache_idx == _hot_cache_size) _hot_cache_idx = 0; 220 _n_hot++; 221 return res; 222 } 223 224 225 void ConcurrentG1Refine::clean_up_cache(int worker_i, G1RemSet* g1rs) { 226 assert(!use_cache(), "cache should be disabled"); 227 int start_ind = _hot_cache_idx-1; 228 for (int i = 0; i < _n_hot; i++) { 229 int ind = start_ind - i; 230 if (ind < 0) ind = ind + _hot_cache_size; 231 jbyte* entry = _hot_cache[ind]; 232 if (entry != NULL) { 233 g1rs->concurrentRefineOneCard(entry, worker_i); 234 } 235 } 236 _n_hot = 0; 237 _hot_cache_idx = 0; 238 } 239 240 void ConcurrentG1Refine::clear_and_record_card_counts() { 241 if (G1ConcRSLogCacheSize == 0 && !G1ConcRSCountTraversals) return; 242 _n_periods++; 243 if (G1ConcRSCountTraversals) { 244 for (size_t i = 0; i < _n_card_counts; i++) { 245 unsigned char bucket = _card_counts[i]; 246 _cur_card_count_histo[bucket]++; 247 _card_counts[i] = 0; 248 } 249 gclog_or_tty->print_cr("Card counts:"); 250 for (int i = 0; i < 256; i++) { 251 if (_cur_card_count_histo[i] > 0) { 252 gclog_or_tty->print_cr(" %3d: %9d", i, _cur_card_count_histo[i]); 253 _cum_card_count_histo[i] += _cur_card_count_histo[i]; 254 _cur_card_count_histo[i] = 0; 255 } 256 } 257 } else { 258 assert(G1ConcRSLogCacheSize > 0, "Logic"); 259 Copy::fill_to_words((HeapWord*)(&_card_counts[0]), 260 _n_card_counts / HeapWordSize); 261 } 262 } 263 264 void 265 ConcurrentG1Refine:: 266 print_card_count_histo_range(unsigned* histo, int from, int to, 267 float& cum_card_pct, 268 float& cum_travs_pct) { 269 unsigned cards = 0; 270 unsigned travs = 0; 271 guarantee(to <= 256, "Precondition"); 272 for (int i = from; i < to-1; i++) { 273 cards += histo[i]; 274 travs += histo[i] * i; 275 } 276 if (to == 256) { 277 unsigned histo_card_sum = 0; 278 unsigned histo_trav_sum = 0; 279 for (int i = 1; i < 255; i++) { 280 histo_trav_sum += histo[i] * i; 281 } 282 cards += histo[255]; 283 // correct traversals for the last one. 284 unsigned travs_255 = (unsigned) (_total_travs - histo_trav_sum); 285 travs += travs_255; 286 287 } else { 288 cards += histo[to-1]; 289 travs += histo[to-1] * (to-1); 290 } 291 float fperiods = (float)_n_periods; 292 float f_tot_cards = (float)_total_cards/fperiods; 293 float f_tot_travs = (float)_total_travs/fperiods; 294 if (cards > 0) { 295 float fcards = (float)cards/fperiods; 296 float ftravs = (float)travs/fperiods; 297 if (to == 256) { 298 gclog_or_tty->print(" %4d- %10.2f%10.2f", from, fcards, ftravs); 299 } else { 300 gclog_or_tty->print(" %4d-%4d %10.2f%10.2f", from, to-1, fcards, ftravs); 301 } 302 float pct_cards = fcards*100.0/f_tot_cards; 303 cum_card_pct += pct_cards; 304 float pct_travs = ftravs*100.0/f_tot_travs; 305 cum_travs_pct += pct_travs; 306 gclog_or_tty->print_cr("%10.2f%10.2f%10.2f%10.2f", 307 pct_cards, cum_card_pct, 308 pct_travs, cum_travs_pct); 309 } 310 } 311 312 void ConcurrentG1Refine::print_final_card_counts() { 313 if (!G1ConcRSCountTraversals) return; 314 315 gclog_or_tty->print_cr("Did %d total traversals of %d distinct cards.", 316 _total_travs, _total_cards); 317 float fperiods = (float)_n_periods; 318 gclog_or_tty->print_cr(" This is an average of %8.2f traversals, %8.2f cards, " 319 "per collection.", (float)_total_travs/fperiods, 320 (float)_total_cards/fperiods); 321 gclog_or_tty->print_cr(" This is an average of %8.2f traversals/distinct " 322 "dirty card.\n", 323 _total_cards > 0 ? 324 (float)_total_travs/(float)_total_cards : 0.0); 325 326 327 gclog_or_tty->print_cr("Histogram:\n\n%10s %10s%10s%10s%10s%10s%10s", 328 "range", "# cards", "# travs", "% cards", "(cum)", 329 "% travs", "(cum)"); 330 gclog_or_tty->print_cr("------------------------------------------------------------" 331 "-------------"); 332 float cum_cards_pct = 0.0; 333 float cum_travs_pct = 0.0; 334 for (int i = 1; i < 10; i++) { 335 print_card_count_histo_range(_cum_card_count_histo, i, i+1, 336 cum_cards_pct, cum_travs_pct); 337 } 338 for (int i = 10; i < 100; i += 10) { 339 print_card_count_histo_range(_cum_card_count_histo, i, i+10, 340 cum_cards_pct, cum_travs_pct); 341 } 342 print_card_count_histo_range(_cum_card_count_histo, 100, 150, 343 cum_cards_pct, cum_travs_pct); 344 print_card_count_histo_range(_cum_card_count_histo, 150, 200, 345 cum_cards_pct, cum_travs_pct); 346 print_card_count_histo_range(_cum_card_count_histo, 150, 255, 347 cum_cards_pct, cum_travs_pct); 348 print_card_count_histo_range(_cum_card_count_histo, 255, 256, 349 cum_cards_pct, cum_travs_pct); 350 }