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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "code/codeCache.hpp" 27 #include "code/compiledIC.hpp" 28 #include "code/icBuffer.hpp" 29 #include "code/nmethod.hpp" 30 #include "compiler/compileBroker.hpp" 31 #include "jfr/jfrEvents.hpp" 32 #include "memory/resourceArea.hpp" 33 #include "oops/method.hpp" 34 #include "runtime/atomic.hpp" 35 #include "runtime/compilationPolicy.hpp" 36 #include "runtime/mutexLocker.hpp" 37 #include "runtime/orderAccess.inline.hpp" 38 #include "runtime/os.hpp" 39 #include "runtime/sweeper.hpp" 40 #include "runtime/thread.inline.hpp" 41 #include "runtime/vm_operations.hpp" 42 #include "utilities/events.hpp" 43 #include "utilities/ticks.hpp" 44 #include "utilities/xmlstream.hpp" 45 46 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 47 48 #ifdef ASSERT 49 50 #define SWEEP(nm) record_sweep(nm, __LINE__) 51 // Sweeper logging code 301 // We are done with sweeping the code cache once. 302 if (_sweep_fractions_left == 0) { 303 _total_nof_code_cache_sweeps++; 304 _last_sweep = _time_counter; 305 // Reset flag; temporarily disables sweeper 306 _should_sweep = false; 307 // If there was enough state change, 'possibly_enable_sweeper()' 308 // sets '_should_sweep' to true 309 possibly_enable_sweeper(); 310 // Reset _bytes_changed only if there was enough state change. _bytes_changed 311 // can further increase by calls to 'report_state_change'. 312 if (_should_sweep) { 313 _bytes_changed = 0; 314 } 315 } 316 // Release work, because another compiler thread could continue. 317 OrderAccess::release_store((int*)&_sweep_started, 0); 318 } 319 } 320 321 static void post_sweep_event(EventSweepCodeCache* event, 322 const Ticks& start, 323 const Ticks& end, 324 s4 traversals, 325 int swept, 326 int flushed, 327 int zombified) { 328 assert(event != NULL, "invariant"); 329 assert(event->should_commit(), "invariant"); 330 event->set_starttime(start); 331 event->set_endtime(end); 332 event->set_sweepId(traversals); 333 event->set_sweptCount(swept); 334 event->set_flushedCount(flushed); 335 event->set_zombifiedCount(zombified); 336 event->commit(); 337 } 338 339 void NMethodSweeper::sweep_code_cache() { 340 ResourceMark rm; 341 Ticks sweep_start_counter = Ticks::now(); 342 343 _flushed_count = 0; 344 _zombified_count = 0; 345 _marked_for_reclamation_count = 0; 346 347 if (PrintMethodFlushing && Verbose) { 348 tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_nmethods(), _sweep_fractions_left); 349 } 350 351 if (!CompileBroker::should_compile_new_jobs()) { 352 // If we have turned off compilations we might as well do full sweeps 353 // in order to reach the clean state faster. Otherwise the sleeping compiler 354 // threads will slow down sweeping. 355 _sweep_fractions_left = 1; 356 } 357 393 // Now ready to process nmethod and give up CodeCache_lock 394 { 395 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 396 freed_memory += process_nmethod(_current); 397 } 398 _seen++; 399 _current = next; 400 } 401 } 402 403 assert(_sweep_fractions_left > 1 || _current == NULL, "must have scanned the whole cache"); 404 405 const Ticks sweep_end_counter = Ticks::now(); 406 const Tickspan sweep_time = sweep_end_counter - sweep_start_counter; 407 _total_time_sweeping += sweep_time; 408 _total_time_this_sweep += sweep_time; 409 _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time); 410 _total_flushed_size += freed_memory; 411 _total_nof_methods_reclaimed += _flushed_count; 412 413 EventSweepCodeCache event(UNTIMED); 414 if (event.should_commit()) { 415 post_sweep_event(&event, sweep_start_counter, sweep_end_counter, (s4)_traversals, swept_count, _flushed_count, _zombified_count); 416 } 417 418 #ifdef ASSERT 419 if(PrintMethodFlushing) { 420 tty->print_cr("### sweeper: sweep time(%d): " 421 INT64_FORMAT, _sweep_fractions_left, (jlong)sweep_time.value()); 422 } 423 #endif 424 425 if (_sweep_fractions_left == 1) { 426 _peak_sweep_time = MAX2(_peak_sweep_time, _total_time_this_sweep); 427 log_sweep("finished"); 428 } 429 430 // Sweeper is the only case where memory is released, check here if it 431 // is time to restart the compiler. Only checking if there is a certain 432 // amount of free memory in the code cache might lead to re-enabling 433 // compilation although no memory has been released. For example, there are 434 // cases when compilation was disabled although there is 4MB (or more) free 435 // memory in the code cache. The reason is code cache fragmentation. Therefore, 436 // it only makes sense to re-enable compilation if we have actually freed memory. | 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "code/codeCache.hpp" 27 #include "code/compiledIC.hpp" 28 #include "code/icBuffer.hpp" 29 #include "code/nmethod.hpp" 30 #include "compiler/compileBroker.hpp" 31 #if INCLUDE_JFR 32 #include "jfr/jfrEvents.hpp" 33 #endif 34 #include "memory/resourceArea.hpp" 35 #include "oops/method.hpp" 36 #include "runtime/atomic.hpp" 37 #include "runtime/compilationPolicy.hpp" 38 #include "runtime/mutexLocker.hpp" 39 #include "runtime/orderAccess.inline.hpp" 40 #include "runtime/os.hpp" 41 #include "runtime/sweeper.hpp" 42 #include "runtime/thread.inline.hpp" 43 #include "runtime/vm_operations.hpp" 44 #include "utilities/events.hpp" 45 #include "utilities/ticks.hpp" 46 #include "utilities/xmlstream.hpp" 47 48 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 49 50 #ifdef ASSERT 51 52 #define SWEEP(nm) record_sweep(nm, __LINE__) 53 // Sweeper logging code 303 // We are done with sweeping the code cache once. 304 if (_sweep_fractions_left == 0) { 305 _total_nof_code_cache_sweeps++; 306 _last_sweep = _time_counter; 307 // Reset flag; temporarily disables sweeper 308 _should_sweep = false; 309 // If there was enough state change, 'possibly_enable_sweeper()' 310 // sets '_should_sweep' to true 311 possibly_enable_sweeper(); 312 // Reset _bytes_changed only if there was enough state change. _bytes_changed 313 // can further increase by calls to 'report_state_change'. 314 if (_should_sweep) { 315 _bytes_changed = 0; 316 } 317 } 318 // Release work, because another compiler thread could continue. 319 OrderAccess::release_store((int*)&_sweep_started, 0); 320 } 321 } 322 323 #if INCLUDE_JFR 324 static void post_sweep_event(EventSweepCodeCache* event, 325 const Ticks& start, 326 const Ticks& end, 327 s4 traversals, 328 int swept, 329 int flushed, 330 int zombified) { 331 assert(event != NULL, "invariant"); 332 assert(event->should_commit(), "invariant"); 333 event->set_starttime(start); 334 event->set_endtime(end); 335 event->set_sweepId(traversals); 336 event->set_sweptCount(swept); 337 event->set_flushedCount(flushed); 338 event->set_zombifiedCount(zombified); 339 event->commit(); 340 } 341 #endif 342 343 void NMethodSweeper::sweep_code_cache() { 344 ResourceMark rm; 345 Ticks sweep_start_counter = Ticks::now(); 346 347 _flushed_count = 0; 348 _zombified_count = 0; 349 _marked_for_reclamation_count = 0; 350 351 if (PrintMethodFlushing && Verbose) { 352 tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_nmethods(), _sweep_fractions_left); 353 } 354 355 if (!CompileBroker::should_compile_new_jobs()) { 356 // If we have turned off compilations we might as well do full sweeps 357 // in order to reach the clean state faster. Otherwise the sleeping compiler 358 // threads will slow down sweeping. 359 _sweep_fractions_left = 1; 360 } 361 397 // Now ready to process nmethod and give up CodeCache_lock 398 { 399 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 400 freed_memory += process_nmethod(_current); 401 } 402 _seen++; 403 _current = next; 404 } 405 } 406 407 assert(_sweep_fractions_left > 1 || _current == NULL, "must have scanned the whole cache"); 408 409 const Ticks sweep_end_counter = Ticks::now(); 410 const Tickspan sweep_time = sweep_end_counter - sweep_start_counter; 411 _total_time_sweeping += sweep_time; 412 _total_time_this_sweep += sweep_time; 413 _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time); 414 _total_flushed_size += freed_memory; 415 _total_nof_methods_reclaimed += _flushed_count; 416 417 #if INCLUDE_JFR 418 EventSweepCodeCache event(UNTIMED); 419 if (event.should_commit()) { 420 post_sweep_event(&event, sweep_start_counter, sweep_end_counter, (s4)_traversals, swept_count, _flushed_count, _zombified_count); 421 } 422 #endif 423 424 #ifdef ASSERT 425 if(PrintMethodFlushing) { 426 tty->print_cr("### sweeper: sweep time(%d): " 427 INT64_FORMAT, _sweep_fractions_left, (jlong)sweep_time.value()); 428 } 429 #endif 430 431 if (_sweep_fractions_left == 1) { 432 _peak_sweep_time = MAX2(_peak_sweep_time, _total_time_this_sweep); 433 log_sweep("finished"); 434 } 435 436 // Sweeper is the only case where memory is released, check here if it 437 // is time to restart the compiler. Only checking if there is a certain 438 // amount of free memory in the code cache might lead to re-enabling 439 // compilation although no memory has been released. For example, there are 440 // cases when compilation was disabled although there is 4MB (or more) free 441 // memory in the code cache. The reason is code cache fragmentation. Therefore, 442 // it only makes sense to re-enable compilation if we have actually freed memory. |