115 _records[_sweep_index].invocation = _invocations;
116 _records[_sweep_index].compile_id = nm->compile_id();
117 _records[_sweep_index].kind = nm->compile_kind();
118 _records[_sweep_index].state = nm->_state;
119 _records[_sweep_index].vep = nm->verified_entry_point();
120 _records[_sweep_index].uep = nm->entry_point();
121 _records[_sweep_index].line = line;
122
123 _sweep_index = (_sweep_index + 1) % SweeperLogEntries;
124 }
125 }
126 #else
127 #define SWEEP(nm)
128 #endif
129
130
131 long NMethodSweeper::_traversals = 0; // No. of stack traversals performed
132 nmethod* NMethodSweeper::_current = NULL; // Current nmethod
133 int NMethodSweeper::_seen = 0 ; // No. of nmethods we have currently processed in current pass of CodeCache
134 int NMethodSweeper::_flushed_count = 0; // Nof. nmethods flushed in current sweep
135 int NMethodSweeper::_zombified_count = 0; // Nof. nmethods made zombie in current sweep
136 int NMethodSweeper::_marked_count = 0; // Nof. nmethods marked for reclaim in current sweep
137
138 volatile int NMethodSweeper::_invocations = 0; // No. of invocations left until we are completed with this pass
139 volatile int NMethodSweeper::_sweep_started = 0; // Whether a sweep is in progress.
140
141 jint NMethodSweeper::_locked_seen = 0;
142 jint NMethodSweeper::_not_entrant_seen_on_stack = 0;
143 bool NMethodSweeper::_resweep = false;
144 jint NMethodSweeper::_flush_token = 0;
145 jlong NMethodSweeper::_last_full_flush_time = 0;
146 int NMethodSweeper::_highest_marked = 0;
147 int NMethodSweeper::_dead_compile_ids = 0;
148 long NMethodSweeper::_last_flush_traversal_id = 0;
149
150 int NMethodSweeper::_number_of_flushes = 0; // Total of full traversals caused by full cache
151 int NMethodSweeper::_total_nof_methods_reclaimed = 0;
152 jlong NMethodSweeper::_total_time_sweeping = 0;
153 jlong NMethodSweeper::_total_time_this_sweep = 0;
154 jlong NMethodSweeper::_peak_sweep_time = 0;
155 jlong NMethodSweeper::_peak_sweep_fraction_time = 0;
156 jlong NMethodSweeper::_total_disconnect_time = 0;
157 jlong NMethodSweeper::_peak_disconnect_time = 0;
158
159 class MarkActivationClosure: public CodeBlobClosure {
160 public:
161 virtual void do_code_blob(CodeBlob* cb) {
162 // If we see an activation belonging to a non_entrant nmethod, we mark it.
163 if (cb->is_nmethod() && ((nmethod*)cb)->is_not_entrant()) {
164 ((nmethod*)cb)->mark_as_seen_on_stack();
165 }
166 }
167 };
168 static MarkActivationClosure mark_activation_closure;
169
170 bool NMethodSweeper::sweep_in_progress() {
171 return (_current != NULL);
231 #ifdef ASSERT
232 if (LogSweeper && _records == NULL) {
233 // Create the ring buffer for the logging code
234 _records = NEW_C_HEAP_ARRAY(SweeperRecord, SweeperLogEntries, mtGC);
235 memset(_records, 0, sizeof(SweeperRecord) * SweeperLogEntries);
236 }
237 #endif
238 if (_invocations > 0) {
239 sweep_code_cache();
240 _invocations--;
241 }
242 _sweep_started = 0;
243 }
244 }
245
246 void NMethodSweeper::sweep_code_cache() {
247
248 jlong sweep_start_counter = os::elapsed_counter();
249
250 _flushed_count = 0;
251 _zombified_count = 0;
252 _marked_count = 0;
253
254 if (PrintMethodFlushing && Verbose) {
255 tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_nmethods(), _invocations);
256 }
257
258 if (!CompileBroker::should_compile_new_jobs()) {
259 // If we have turned off compilations we might as well do full sweeps
260 // in order to reach the clean state faster. Otherwise the sleeping compiler
261 // threads will slow down sweeping. After a few iterations the cache
262 // will be clean and sweeping stops (_resweep will not be set)
263 _invocations = 1;
264 }
265
266 // We want to visit all nmethods after NmethodSweepFraction
267 // invocations so divide the remaining number of nmethods by the
268 // remaining number of invocations. This is only an estimate since
269 // the number of nmethods changes during the sweep so the final
270 // stage must iterate until it there are no more nmethods.
308
309 assert(_invocations > 1 || _current == NULL, "must have scanned the whole cache");
310
311 if (!sweep_in_progress() && !_resweep && (_locked_seen || _not_entrant_seen_on_stack)) {
312 // we've completed a scan without making progress but there were
313 // nmethods we were unable to process either because they were
314 // locked or were still on stack. We don't have to aggresively
315 // clean them up so just stop scanning. We could scan once more
316 // but that complicates the control logic and it's unlikely to
317 // matter much.
318 if (PrintMethodFlushing) {
319 tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep");
320 }
321 }
322
323 jlong sweep_end_counter = os::elapsed_counter();
324 jlong sweep_time = sweep_end_counter - sweep_start_counter;
325 _total_time_sweeping += sweep_time;
326 _total_time_this_sweep += sweep_time;
327 _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time);
328 _total_nof_methods_reclaimed += _flushed_count;
329
330 EventSweepCodeCache event(UNTIMED);
331 if (event.should_commit()) {
332 event.set_starttime(sweep_start_counter);
333 event.set_endtime(sweep_end_counter);
334 event.set_sweepIndex(_traversals);
335 event.set_sweepFractionIndex(NmethodSweepFraction - _invocations + 1);
336 event.set_sweptCount(swept_count);
337 event.set_flushedCount(_flushed_count);
338 event.set_markedCount(_marked_count);
339 event.set_zombifiedCount(_zombified_count);
340 event.commit();
341 }
342
343 #ifdef ASSERT
344 if(PrintMethodFlushing) {
345 tty->print_cr("### sweeper: sweep time(%d): " INT64_FORMAT, _invocations, (jlong)sweep_time);
346 }
347 #endif
408 // Clean-up all inline caches that points to zombie/non-reentrant methods
409 MutexLocker cl(CompiledIC_lock);
410 nm->cleanup_inline_caches();
411 SWEEP(nm);
412 } else {
413 _locked_seen++;
414 SWEEP(nm);
415 }
416 return;
417 }
418
419 if (nm->is_zombie()) {
420 // If it is first time, we see nmethod then we mark it. Otherwise,
421 // we reclame it. When we have seen a zombie method twice, we know that
422 // there are no inline caches that refer to it.
423 if (nm->is_marked_for_reclamation()) {
424 assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
425 if (PrintMethodFlushing && Verbose) {
426 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (marked for reclamation) being flushed", nm->compile_id(), nm);
427 }
428 release_nmethod(nm);
429 _flushed_count++;
430 } else {
431 if (PrintMethodFlushing && Verbose) {
432 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (zombie) being marked for reclamation", nm->compile_id(), nm);
433 }
434 nm->mark_for_reclamation();
435 _resweep = true;
436 _marked_count++;
437 SWEEP(nm);
438 }
439 } else if (nm->is_not_entrant()) {
440 // If there is no current activations of this method on the
441 // stack we can safely convert it to a zombie method
442 if (nm->can_not_entrant_be_converted()) {
443 if (PrintMethodFlushing && Verbose) {
444 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (not entrant) being made zombie", nm->compile_id(), nm);
445 }
446 nm->make_zombie();
447 _resweep = true;
448 _zombified_count++;
449 SWEEP(nm);
450 } else {
451 // Still alive, clean up its inline caches
452 MutexLocker cl(CompiledIC_lock);
453 nm->cleanup_inline_caches();
454 // we coudn't transition this nmethod so don't immediately
455 // request a rescan. If this method stays on the stack for a
456 // long time we don't want to keep rescanning the code cache.
457 _not_entrant_seen_on_stack++;
458 SWEEP(nm);
459 }
460 } else if (nm->is_unloaded()) {
461 // Unloaded code, just make it a zombie
462 if (PrintMethodFlushing && Verbose)
463 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (unloaded) being made zombie", nm->compile_id(), nm);
464
465 if (nm->is_osr_method()) {
466 SWEEP(nm);
467 // No inline caches will ever point to osr methods, so we can just remove it
468 release_nmethod(nm);
469 _flushed_count++;
470 } else {
471 nm->make_zombie();
472 _resweep = true;
473 _zombified_count++;
474 SWEEP(nm);
475 }
476 } else {
477 assert(nm->is_alive(), "should be alive");
478
479 if (UseCodeCacheFlushing) {
480 if (nm->is_speculatively_disconnected() && !nm->is_locked_by_vm() && !nm->is_osr_method() &&
481 (_traversals > _last_flush_traversal_id + 2) && (nm->compile_id() < _highest_marked)) {
482 // This method has not been called since the forced cleanup happened
483 nm->make_not_entrant();
484 }
485 }
486
487 // Clean-up all inline caches that points to zombie/non-reentrant methods
633 }
634
635 if (LogCompilation && (xtty != NULL)) {
636 stringStream s;
637 // Dump code cache state into a buffer before locking the tty,
638 // because log_state() will use locks causing lock conflicts.
639 CodeCache::log_state(&s);
640
641 ttyLocker ttyl;
642 xtty->begin_elem("sweeper state='%s' traversals='" INTX_FORMAT "' ", msg, (intx)traversal_count());
643 if (format != NULL) {
644 va_list ap;
645 va_start(ap, format);
646 xtty->vprint(format, ap);
647 va_end(ap);
648 }
649 xtty->print(s.as_string());
650 xtty->stamp();
651 xtty->end_elem();
652 }
653 }
|
115 _records[_sweep_index].invocation = _invocations;
116 _records[_sweep_index].compile_id = nm->compile_id();
117 _records[_sweep_index].kind = nm->compile_kind();
118 _records[_sweep_index].state = nm->_state;
119 _records[_sweep_index].vep = nm->verified_entry_point();
120 _records[_sweep_index].uep = nm->entry_point();
121 _records[_sweep_index].line = line;
122
123 _sweep_index = (_sweep_index + 1) % SweeperLogEntries;
124 }
125 }
126 #else
127 #define SWEEP(nm)
128 #endif
129
130
131 long NMethodSweeper::_traversals = 0; // No. of stack traversals performed
132 nmethod* NMethodSweeper::_current = NULL; // Current nmethod
133 int NMethodSweeper::_seen = 0 ; // No. of nmethods we have currently processed in current pass of CodeCache
134 int NMethodSweeper::_flushed_count = 0; // Nof. nmethods flushed in current sweep
135 size_t NMethodSweeper::_flushed_size = 0; // Total size of nmethods flushed in current sweep
136 int NMethodSweeper::_zombified_count = 0; // Nof. nmethods made zombie in current sweep
137 int NMethodSweeper::_marked_count = 0; // Nof. nmethods marked for reclaim in current sweep
138
139 volatile int NMethodSweeper::_invocations = 0; // No. of invocations left until we are completed with this pass
140 volatile int NMethodSweeper::_sweep_started = 0; // Whether a sweep is in progress.
141
142 jint NMethodSweeper::_locked_seen = 0;
143 jint NMethodSweeper::_not_entrant_seen_on_stack = 0;
144 bool NMethodSweeper::_resweep = false;
145 jint NMethodSweeper::_flush_token = 0;
146 jlong NMethodSweeper::_last_full_flush_time = 0;
147 int NMethodSweeper::_highest_marked = 0;
148 int NMethodSweeper::_dead_compile_ids = 0;
149 long NMethodSweeper::_last_flush_traversal_id = 0;
150
151 int NMethodSweeper::_number_of_flushes = 0; // Total of full traversals caused by full cache
152 int NMethodSweeper::_total_nof_methods_reclaimed = 0;
153 size_t NMethodSweeper::_total_flushed_size = 0;
154 jlong NMethodSweeper::_total_time_sweeping = 0;
155 jlong NMethodSweeper::_total_time_this_sweep = 0;
156 jlong NMethodSweeper::_peak_sweep_time = 0;
157 jlong NMethodSweeper::_peak_sweep_fraction_time = 0;
158 jlong NMethodSweeper::_total_disconnect_time = 0;
159 jlong NMethodSweeper::_peak_disconnect_time = 0;
160
161 class MarkActivationClosure: public CodeBlobClosure {
162 public:
163 virtual void do_code_blob(CodeBlob* cb) {
164 // If we see an activation belonging to a non_entrant nmethod, we mark it.
165 if (cb->is_nmethod() && ((nmethod*)cb)->is_not_entrant()) {
166 ((nmethod*)cb)->mark_as_seen_on_stack();
167 }
168 }
169 };
170 static MarkActivationClosure mark_activation_closure;
171
172 bool NMethodSweeper::sweep_in_progress() {
173 return (_current != NULL);
233 #ifdef ASSERT
234 if (LogSweeper && _records == NULL) {
235 // Create the ring buffer for the logging code
236 _records = NEW_C_HEAP_ARRAY(SweeperRecord, SweeperLogEntries, mtGC);
237 memset(_records, 0, sizeof(SweeperRecord) * SweeperLogEntries);
238 }
239 #endif
240 if (_invocations > 0) {
241 sweep_code_cache();
242 _invocations--;
243 }
244 _sweep_started = 0;
245 }
246 }
247
248 void NMethodSweeper::sweep_code_cache() {
249
250 jlong sweep_start_counter = os::elapsed_counter();
251
252 _flushed_count = 0;
253 _flushed_size = 0;
254 _zombified_count = 0;
255 _marked_count = 0;
256
257 if (PrintMethodFlushing && Verbose) {
258 tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_nmethods(), _invocations);
259 }
260
261 if (!CompileBroker::should_compile_new_jobs()) {
262 // If we have turned off compilations we might as well do full sweeps
263 // in order to reach the clean state faster. Otherwise the sleeping compiler
264 // threads will slow down sweeping. After a few iterations the cache
265 // will be clean and sweeping stops (_resweep will not be set)
266 _invocations = 1;
267 }
268
269 // We want to visit all nmethods after NmethodSweepFraction
270 // invocations so divide the remaining number of nmethods by the
271 // remaining number of invocations. This is only an estimate since
272 // the number of nmethods changes during the sweep so the final
273 // stage must iterate until it there are no more nmethods.
311
312 assert(_invocations > 1 || _current == NULL, "must have scanned the whole cache");
313
314 if (!sweep_in_progress() && !_resweep && (_locked_seen || _not_entrant_seen_on_stack)) {
315 // we've completed a scan without making progress but there were
316 // nmethods we were unable to process either because they were
317 // locked or were still on stack. We don't have to aggresively
318 // clean them up so just stop scanning. We could scan once more
319 // but that complicates the control logic and it's unlikely to
320 // matter much.
321 if (PrintMethodFlushing) {
322 tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep");
323 }
324 }
325
326 jlong sweep_end_counter = os::elapsed_counter();
327 jlong sweep_time = sweep_end_counter - sweep_start_counter;
328 _total_time_sweeping += sweep_time;
329 _total_time_this_sweep += sweep_time;
330 _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time);
331 _total_flushed_size += _flushed_size;
332 _total_nof_methods_reclaimed += _flushed_count;
333
334 EventSweepCodeCache event(UNTIMED);
335 if (event.should_commit()) {
336 event.set_starttime(sweep_start_counter);
337 event.set_endtime(sweep_end_counter);
338 event.set_sweepIndex(_traversals);
339 event.set_sweepFractionIndex(NmethodSweepFraction - _invocations + 1);
340 event.set_sweptCount(swept_count);
341 event.set_flushedCount(_flushed_count);
342 event.set_markedCount(_marked_count);
343 event.set_zombifiedCount(_zombified_count);
344 event.commit();
345 }
346
347 #ifdef ASSERT
348 if(PrintMethodFlushing) {
349 tty->print_cr("### sweeper: sweep time(%d): " INT64_FORMAT, _invocations, (jlong)sweep_time);
350 }
351 #endif
412 // Clean-up all inline caches that points to zombie/non-reentrant methods
413 MutexLocker cl(CompiledIC_lock);
414 nm->cleanup_inline_caches();
415 SWEEP(nm);
416 } else {
417 _locked_seen++;
418 SWEEP(nm);
419 }
420 return;
421 }
422
423 if (nm->is_zombie()) {
424 // If it is first time, we see nmethod then we mark it. Otherwise,
425 // we reclame it. When we have seen a zombie method twice, we know that
426 // there are no inline caches that refer to it.
427 if (nm->is_marked_for_reclamation()) {
428 assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
429 if (PrintMethodFlushing && Verbose) {
430 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (marked for reclamation) being flushed", nm->compile_id(), nm);
431 }
432 _flushed_size += nm->size();
433 release_nmethod(nm);
434 _flushed_count++;
435 } else {
436 if (PrintMethodFlushing && Verbose) {
437 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (zombie) being marked for reclamation", nm->compile_id(), nm);
438 }
439 nm->mark_for_reclamation();
440 _resweep = true;
441 _marked_count++;
442 SWEEP(nm);
443 }
444 } else if (nm->is_not_entrant()) {
445 // If there is no current activations of this method on the
446 // stack we can safely convert it to a zombie method
447 if (nm->can_not_entrant_be_converted()) {
448 if (PrintMethodFlushing && Verbose) {
449 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (not entrant) being made zombie", nm->compile_id(), nm);
450 }
451 nm->make_zombie();
452 _resweep = true;
453 _zombified_count++;
454 SWEEP(nm);
455 } else {
456 // Still alive, clean up its inline caches
457 MutexLocker cl(CompiledIC_lock);
458 nm->cleanup_inline_caches();
459 // we coudn't transition this nmethod so don't immediately
460 // request a rescan. If this method stays on the stack for a
461 // long time we don't want to keep rescanning the code cache.
462 _not_entrant_seen_on_stack++;
463 SWEEP(nm);
464 }
465 } else if (nm->is_unloaded()) {
466 // Unloaded code, just make it a zombie
467 if (PrintMethodFlushing && Verbose)
468 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (unloaded) being made zombie", nm->compile_id(), nm);
469
470 if (nm->is_osr_method()) {
471 SWEEP(nm);
472 // No inline caches will ever point to osr methods, so we can just remove it
473 _flushed_size += nm->size();
474 release_nmethod(nm);
475 _flushed_count++;
476 } else {
477 nm->make_zombie();
478 _resweep = true;
479 _zombified_count++;
480 SWEEP(nm);
481 }
482 } else {
483 assert(nm->is_alive(), "should be alive");
484
485 if (UseCodeCacheFlushing) {
486 if (nm->is_speculatively_disconnected() && !nm->is_locked_by_vm() && !nm->is_osr_method() &&
487 (_traversals > _last_flush_traversal_id + 2) && (nm->compile_id() < _highest_marked)) {
488 // This method has not been called since the forced cleanup happened
489 nm->make_not_entrant();
490 }
491 }
492
493 // Clean-up all inline caches that points to zombie/non-reentrant methods
639 }
640
641 if (LogCompilation && (xtty != NULL)) {
642 stringStream s;
643 // Dump code cache state into a buffer before locking the tty,
644 // because log_state() will use locks causing lock conflicts.
645 CodeCache::log_state(&s);
646
647 ttyLocker ttyl;
648 xtty->begin_elem("sweeper state='%s' traversals='" INTX_FORMAT "' ", msg, (intx)traversal_count());
649 if (format != NULL) {
650 va_list ap;
651 va_start(ap, format);
652 xtty->vprint(format, ap);
653 va_end(ap);
654 }
655 xtty->print(s.as_string());
656 xtty->stamp();
657 xtty->end_elem();
658 }
659 }
660
661 void NMethodSweeper::print() {
662 ttyLocker ttyl;
663 tty->print_cr("Method flushing statistics:");
664 tty->print_cr(" Total sweep time: %1.3lfms", (double)_total_time_sweeping/1000);
665 tty->print_cr(" Peak sweep time: %1.3lfms", (double)_peak_sweep_time/1000);
666 tty->print_cr(" Peak sweep fraction time: %1.3lfms", (double)_peak_sweep_fraction_time/1000);
667 tty->print_cr(" Total number of stack traversals: %d", _traversals);
668 tty->print_cr(" Total number of flushed methods: %d", _total_nof_methods_reclaimed);
669 tty->print_cr(" Total size of flushed methods: " SIZE_FORMAT "Kb", _total_flushed_size/K);
670 }
|