|
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 if (_cg1rThread->do_traversal()) { |
|
149 _pya = PYA_restart; |
|
150 } else { |
|
151 _cg1rThread->set_do_traversal(true); |
|
152 // Reset the post-yield actions. |
|
153 _pya = PYA_continue; |
|
154 _last_pya = PYA_continue; |
|
155 } |
|
156 } else { |
|
157 _pya = PYA_restart; |
|
158 } |
|
159 } |
|
160 |
|
161 void ConcurrentG1Refine::set_pya_cancel() { |
|
162 _pya = PYA_cancel; |
|
163 } |
|
164 |
|
165 PostYieldAction ConcurrentG1Refine::get_pya() { |
|
166 if (_pya != PYA_continue) { |
|
167 jint val = _pya; |
|
168 while (true) { |
|
169 jint val_read = Atomic::cmpxchg(PYA_continue, &_pya, val); |
|
170 if (val_read == val) { |
|
171 PostYieldAction res = (PostYieldAction)val; |
|
172 assert(res != PYA_continue, "Only the refine thread should reset."); |
|
173 _last_pya = res; |
|
174 return res; |
|
175 } else { |
|
176 val = val_read; |
|
177 } |
|
178 } |
|
179 } |
|
180 // QQQ WELL WHAT DO WE RETURN HERE??? |
|
181 // make up something! |
|
182 return PYA_continue; |
|
183 } |
|
184 |
|
185 PostYieldAction ConcurrentG1Refine::get_last_pya() { |
|
186 PostYieldAction res = _last_pya; |
|
187 _last_pya = PYA_continue; |
|
188 return res; |
|
189 } |
|
190 |
|
191 bool ConcurrentG1Refine::do_traversal() { |
|
192 return _cg1rThread->do_traversal(); |
|
193 } |
|
194 |
|
195 int ConcurrentG1Refine::add_card_count(jbyte* card_ptr) { |
|
196 size_t card_num = (card_ptr - _ct_bot); |
|
197 guarantee(0 <= card_num && card_num < _n_card_counts, "Bounds"); |
|
198 unsigned char cnt = _card_counts[card_num]; |
|
199 if (cnt < 255) _card_counts[card_num]++; |
|
200 return cnt; |
|
201 _total_travs++; |
|
202 } |
|
203 |
|
204 jbyte* ConcurrentG1Refine::cache_insert(jbyte* card_ptr) { |
|
205 int count = add_card_count(card_ptr); |
|
206 // Count previously unvisited cards. |
|
207 if (count == 0) _total_cards++; |
|
208 // We'll assume a traversal unless we store it in the cache. |
|
209 if (count < G1ConcRSHotCardLimit) { |
|
210 _total_travs++; |
|
211 return card_ptr; |
|
212 } |
|
213 // Otherwise, it's hot. |
|
214 jbyte* res = NULL; |
|
215 MutexLockerEx x(HotCardCache_lock, Mutex::_no_safepoint_check_flag); |
|
216 if (_n_hot == _hot_cache_size) { |
|
217 _total_travs++; |
|
218 res = _hot_cache[_hot_cache_idx]; |
|
219 _n_hot--; |
|
220 } |
|
221 // Now _n_hot < _hot_cache_size, and we can insert at _hot_cache_idx. |
|
222 _hot_cache[_hot_cache_idx] = card_ptr; |
|
223 _hot_cache_idx++; |
|
224 if (_hot_cache_idx == _hot_cache_size) _hot_cache_idx = 0; |
|
225 _n_hot++; |
|
226 return res; |
|
227 } |
|
228 |
|
229 |
|
230 void ConcurrentG1Refine::clean_up_cache(int worker_i, G1RemSet* g1rs) { |
|
231 assert(!use_cache(), "cache should be disabled"); |
|
232 int start_ind = _hot_cache_idx-1; |
|
233 for (int i = 0; i < _n_hot; i++) { |
|
234 int ind = start_ind - i; |
|
235 if (ind < 0) ind = ind + _hot_cache_size; |
|
236 jbyte* entry = _hot_cache[ind]; |
|
237 if (entry != NULL) { |
|
238 g1rs->concurrentRefineOneCard(entry, worker_i); |
|
239 } |
|
240 } |
|
241 _n_hot = 0; |
|
242 _hot_cache_idx = 0; |
|
243 } |
|
244 |
|
245 void ConcurrentG1Refine::clear_and_record_card_counts() { |
|
246 if (G1ConcRSLogCacheSize == 0 && !G1ConcRSCountTraversals) return; |
|
247 _n_periods++; |
|
248 if (G1ConcRSCountTraversals) { |
|
249 for (size_t i = 0; i < _n_card_counts; i++) { |
|
250 unsigned char bucket = _card_counts[i]; |
|
251 _cur_card_count_histo[bucket]++; |
|
252 _card_counts[i] = 0; |
|
253 } |
|
254 gclog_or_tty->print_cr("Card counts:"); |
|
255 for (int i = 0; i < 256; i++) { |
|
256 if (_cur_card_count_histo[i] > 0) { |
|
257 gclog_or_tty->print_cr(" %3d: %9d", i, _cur_card_count_histo[i]); |
|
258 _cum_card_count_histo[i] += _cur_card_count_histo[i]; |
|
259 _cur_card_count_histo[i] = 0; |
|
260 } |
|
261 } |
|
262 } else { |
|
263 assert(G1ConcRSLogCacheSize > 0, "Logic"); |
|
264 Copy::fill_to_words((HeapWord*)(&_card_counts[0]), |
|
265 _n_card_counts / HeapWordSize); |
|
266 } |
|
267 } |
|
268 |
|
269 void |
|
270 ConcurrentG1Refine:: |
|
271 print_card_count_histo_range(unsigned* histo, int from, int to, |
|
272 float& cum_card_pct, |
|
273 float& cum_travs_pct) { |
|
274 unsigned cards = 0; |
|
275 unsigned travs = 0; |
|
276 guarantee(to <= 256, "Precondition"); |
|
277 for (int i = from; i < to-1; i++) { |
|
278 cards += histo[i]; |
|
279 travs += histo[i] * i; |
|
280 } |
|
281 if (to == 256) { |
|
282 unsigned histo_card_sum = 0; |
|
283 unsigned histo_trav_sum = 0; |
|
284 for (int i = 1; i < 255; i++) { |
|
285 histo_trav_sum += histo[i] * i; |
|
286 } |
|
287 cards += histo[255]; |
|
288 // correct traversals for the last one. |
|
289 unsigned travs_255 = (unsigned) (_total_travs - histo_trav_sum); |
|
290 travs += travs_255; |
|
291 |
|
292 } else { |
|
293 cards += histo[to-1]; |
|
294 travs += histo[to-1] * (to-1); |
|
295 } |
|
296 float fperiods = (float)_n_periods; |
|
297 float f_tot_cards = (float)_total_cards/fperiods; |
|
298 float f_tot_travs = (float)_total_travs/fperiods; |
|
299 if (cards > 0) { |
|
300 float fcards = (float)cards/fperiods; |
|
301 float ftravs = (float)travs/fperiods; |
|
302 if (to == 256) { |
|
303 gclog_or_tty->print(" %4d- %10.2f%10.2f", from, fcards, ftravs); |
|
304 } else { |
|
305 gclog_or_tty->print(" %4d-%4d %10.2f%10.2f", from, to-1, fcards, ftravs); |
|
306 } |
|
307 float pct_cards = fcards*100.0/f_tot_cards; |
|
308 cum_card_pct += pct_cards; |
|
309 float pct_travs = ftravs*100.0/f_tot_travs; |
|
310 cum_travs_pct += pct_travs; |
|
311 gclog_or_tty->print_cr("%10.2f%10.2f%10.2f%10.2f", |
|
312 pct_cards, cum_card_pct, |
|
313 pct_travs, cum_travs_pct); |
|
314 } |
|
315 } |
|
316 |
|
317 void ConcurrentG1Refine::print_final_card_counts() { |
|
318 if (!G1ConcRSCountTraversals) return; |
|
319 |
|
320 gclog_or_tty->print_cr("Did %d total traversals of %d distinct cards.", |
|
321 _total_travs, _total_cards); |
|
322 float fperiods = (float)_n_periods; |
|
323 gclog_or_tty->print_cr(" This is an average of %8.2f traversals, %8.2f cards, " |
|
324 "per collection.", (float)_total_travs/fperiods, |
|
325 (float)_total_cards/fperiods); |
|
326 gclog_or_tty->print_cr(" This is an average of %8.2f traversals/distinct " |
|
327 "dirty card.\n", |
|
328 _total_cards > 0 ? |
|
329 (float)_total_travs/(float)_total_cards : 0.0); |
|
330 |
|
331 |
|
332 gclog_or_tty->print_cr("Histogram:\n\n%10s %10s%10s%10s%10s%10s%10s", |
|
333 "range", "# cards", "# travs", "% cards", "(cum)", |
|
334 "% travs", "(cum)"); |
|
335 gclog_or_tty->print_cr("------------------------------------------------------------" |
|
336 "-------------"); |
|
337 float cum_cards_pct = 0.0; |
|
338 float cum_travs_pct = 0.0; |
|
339 for (int i = 1; i < 10; i++) { |
|
340 print_card_count_histo_range(_cum_card_count_histo, i, i+1, |
|
341 cum_cards_pct, cum_travs_pct); |
|
342 } |
|
343 for (int i = 10; i < 100; i += 10) { |
|
344 print_card_count_histo_range(_cum_card_count_histo, i, i+10, |
|
345 cum_cards_pct, cum_travs_pct); |
|
346 } |
|
347 print_card_count_histo_range(_cum_card_count_histo, 100, 150, |
|
348 cum_cards_pct, cum_travs_pct); |
|
349 print_card_count_histo_range(_cum_card_count_histo, 150, 200, |
|
350 cum_cards_pct, cum_travs_pct); |
|
351 print_card_count_histo_range(_cum_card_count_histo, 150, 255, |
|
352 cum_cards_pct, cum_travs_pct); |
|
353 print_card_count_histo_range(_cum_card_count_histo, 255, 256, |
|
354 cum_cards_pct, cum_travs_pct); |
|
355 } |