< prev index next >

src/share/vm/runtime/sweeper.cpp

Print this page


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. 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 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  *


 129    _records = NEW_C_HEAP_ARRAY(SweeperRecord, SweeperLogEntries, mtGC);
 130    memset(_records, 0, sizeof(SweeperRecord) * SweeperLogEntries);
 131   }
 132 }
 133 #else
 134 #define SWEEP(nm)
 135 #endif
 136 
 137 NMethodIterator NMethodSweeper::_current;                      // Current nmethod
 138 long     NMethodSweeper::_traversals                   = 0;    // Stack scan count, also sweep ID.
 139 long     NMethodSweeper::_total_nof_code_cache_sweeps  = 0;    // Total number of full sweeps of the code cache
 140 long     NMethodSweeper::_time_counter                 = 0;    // Virtual time used to periodically invoke sweeper
 141 long     NMethodSweeper::_last_sweep                   = 0;    // Value of _time_counter when the last sweep happened
 142 int      NMethodSweeper::_seen                         = 0;    // Nof. nmethod we have currently processed in current pass of CodeCache
 143 
 144 volatile bool NMethodSweeper::_should_sweep            = true; // Indicates if we should invoke the sweeper
 145 volatile bool NMethodSweeper::_force_sweep             = false;// Indicates if we should force a sweep
 146 volatile int  NMethodSweeper::_bytes_changed           = 0;    // Counts the total nmethod size if the nmethod changed from:
 147                                                                //   1) alive       -> not_entrant
 148                                                                //   2) not_entrant -> zombie
 149                                                                //   3) zombie      -> marked_for_reclamation
 150 int    NMethodSweeper::_hotness_counter_reset_val       = 0;
 151 
 152 long   NMethodSweeper::_total_nof_methods_reclaimed     = 0;   // Accumulated nof methods flushed
 153 long   NMethodSweeper::_total_nof_c2_methods_reclaimed  = 0;   // Accumulated nof methods flushed
 154 size_t NMethodSweeper::_total_flushed_size              = 0;   // Total number of bytes flushed from the code cache
 155 Tickspan NMethodSweeper::_total_time_sweeping;                 // Accumulated time sweeping
 156 Tickspan NMethodSweeper::_total_time_this_sweep;               // Total time this sweep
 157 Tickspan NMethodSweeper::_peak_sweep_time;                     // Peak time for a full sweep
 158 Tickspan NMethodSweeper::_peak_sweep_fraction_time;            // Peak time sweeping one fraction
 159 
 160 Monitor* NMethodSweeper::_stat_lock = new Monitor(Mutex::special, "Sweeper::Statistics", true, Monitor::_safepoint_check_sometimes);
 161 
 162 class MarkActivationClosure: public CodeBlobClosure {
 163 public:
 164   virtual void do_code_blob(CodeBlob* cb) {
 165     assert(cb->is_nmethod(), "CodeBlob should be nmethod");
 166     nmethod* nm = (nmethod*)cb;
 167     nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
 168     // If we see an activation belonging to a non_entrant nmethod, we mark it.
 169     if (nm->is_not_entrant()) {


 380   // can further increase by calls to 'report_state_change'.
 381   if (_should_sweep) {
 382     _bytes_changed = 0;
 383   }
 384 
 385   if (forced) {
 386     // Notify requester that forced sweep finished
 387     assert(_force_sweep, "Should be a forced sweep");
 388     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 389     _force_sweep = false;
 390     CodeCache_lock->notify();
 391   }
 392 }
 393 
 394 void NMethodSweeper::sweep_code_cache() {
 395   ResourceMark rm;
 396   Ticks sweep_start_counter = Ticks::now();
 397 
 398   int flushed_count                = 0;
 399   int zombified_count              = 0;
 400   int marked_for_reclamation_count = 0;
 401   int flushed_c2_count     = 0;
 402 
 403   if (PrintMethodFlushing && Verbose) {
 404     tty->print_cr("### Sweep at %d out of %d", _seen, CodeCache::nmethod_count());
 405   }
 406 
 407   int swept_count = 0;
 408   assert(!SafepointSynchronize::is_at_safepoint(), "should not be in safepoint when we get here");
 409   assert(!CodeCache_lock->owned_by_self(), "just checking");
 410 
 411   int freed_memory = 0;
 412   {
 413     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 414 
 415     while (!_current.end()) {
 416       swept_count++;
 417       // Since we will give up the CodeCache_lock, always skip ahead
 418       // to the next nmethod.  Other blobs can be deleted by other
 419       // threads but nmethods are only reclaimed by the sweeper.
 420       nmethod* nm = _current.method();


 425         MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 426         // Save information before potentially flushing the nmethod
 427         int size = nm->total_size();
 428         bool is_c2_method = nm->is_compiled_by_c2();
 429         bool is_osr = nm->is_osr_method();
 430         int compile_id = nm->compile_id();
 431         intptr_t address = p2i(nm);
 432         const char* state_before = nm->state();
 433         const char* state_after = "";
 434 
 435         MethodStateChange type = process_nmethod(nm);
 436         switch (type) {
 437           case Flushed:
 438             state_after = "flushed";
 439             freed_memory += size;
 440             ++flushed_count;
 441             if (is_c2_method) {
 442               ++flushed_c2_count;
 443             }
 444             break;
 445           case MarkedForReclamation:
 446             state_after = "marked for reclamation";
 447             ++marked_for_reclamation_count;
 448             break;
 449           case MadeZombie:
 450             state_after = "made zombie";
 451             ++zombified_count;
 452             break;
 453           case None:
 454             break;
 455           default:
 456            ShouldNotReachHere();
 457         }
 458         if (PrintMethodFlushing && Verbose && type != None) {
 459           tty->print_cr("### %s nmethod %3d/" PTR_FORMAT " (%s) %s", is_osr ? "osr" : "", compile_id, address, state_before, state_after);
 460         }
 461       }
 462 
 463       _seen++;
 464       handle_safepoint_request();
 465     }
 466   }
 467 
 468   assert(_current.end(), "must have scanned the whole cache");
 469 
 470   const Ticks sweep_end_counter = Ticks::now();
 471   const Tickspan sweep_time = sweep_end_counter - sweep_start_counter;
 472   {
 473     MutexLockerEx mu(_stat_lock, Mutex::_no_safepoint_check_flag);
 474     _total_time_sweeping  += sweep_time;
 475     _total_time_this_sweep += sweep_time;
 476     _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time);
 477     _total_flushed_size += freed_memory;
 478     _total_nof_methods_reclaimed += flushed_count;
 479     _total_nof_c2_methods_reclaimed += flushed_c2_count;
 480     _peak_sweep_time = MAX2(_peak_sweep_time, _total_time_this_sweep);
 481   }
 482   EventSweepCodeCache event(UNTIMED);
 483   if (event.should_commit()) {
 484     event.set_starttime(sweep_start_counter);
 485     event.set_endtime(sweep_end_counter);
 486     event.set_sweepIndex(_traversals);
 487     event.set_sweptCount(swept_count);
 488     event.set_flushedCount(flushed_count);
 489     event.set_markedCount(marked_for_reclamation_count);
 490     event.set_zombifiedCount(zombified_count);
 491     event.commit();
 492   }
 493 
 494 #ifdef ASSERT
 495   if(PrintMethodFlushing) {
 496     tty->print_cr("### sweeper:      sweep time(" JLONG_FORMAT "): ", sweep_time.value());
 497   }
 498 #endif
 499 
 500   log_sweep("finished");
 501 
 502   // Sweeper is the only case where memory is released, check here if it
 503   // is time to restart the compiler. Only checking if there is a certain
 504   // amount of free memory in the code cache might lead to re-enabling
 505   // compilation although no memory has been released. For example, there are
 506   // cases when compilation was disabled although there is 4MB (or more) free
 507   // memory in the code cache. The reason is code cache fragmentation. Therefore,
 508   // it only makes sense to re-enable compilation if we have actually freed memory.
 509   // Note that typically several kB are released for sweeping 16MB of the code


 584 
 585   MethodStateChange result = None;
 586   // Make sure this nmethod doesn't get unloaded during the scan,
 587   // since safepoints may happen during acquired below locks.
 588   NMethodMarker nmm(nm);
 589   SWEEP(nm);
 590 
 591   // Skip methods that are currently referenced by the VM
 592   if (nm->is_locked_by_vm()) {
 593     // But still remember to clean-up inline caches for alive nmethods
 594     if (nm->is_alive()) {
 595       // Clean inline caches that point to zombie/non-entrant/unloaded nmethods
 596       MutexLocker cl(CompiledIC_lock);
 597       nm->cleanup_inline_caches();
 598       SWEEP(nm);
 599     }
 600     return result;
 601   }
 602 
 603   if (nm->is_zombie()) {
 604     // If it is the first time we see nmethod then we mark it. Otherwise,
 605     // we reclaim it. When we have seen a zombie method twice, we know that
 606     // there are no inline caches that refer to it.
 607     if (nm->is_marked_for_reclamation()) {
 608       assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
 609       release_nmethod(nm);
 610       assert(result == None, "sanity");
 611       result = Flushed;
 612     } else {
 613       nm->mark_for_reclamation();
 614       // Keep track of code cache state change
 615       _bytes_changed += nm->total_size();
 616       SWEEP(nm);
 617       assert(result == None, "sanity");
 618       result = MarkedForReclamation;
 619       assert(nm->is_marked_for_reclamation(), "nmethod must be marked for reclamation");
 620     }
 621   } else if (nm->is_not_entrant()) {
 622     // If there are no current activations of this method on the
 623     // stack we can safely convert it to a zombie method
 624     if (nm->can_convert_to_zombie()) {
 625       // Clear ICStubs to prevent back patching stubs of zombie or flushed
 626       // nmethods during the next safepoint (see ICStub::finalize).
 627       {
 628         MutexLocker cl(CompiledIC_lock);
 629         nm->clear_ic_stubs();
 630       }
 631       // Code cache state change is tracked in make_zombie()
 632       nm->make_zombie();
 633       SWEEP(nm);
 634       if (nm->is_osr_method()) {
 635         // No inline caches will ever point to osr methods, so we can just remove it.
 636         // Make sure that we unregistered the nmethod with the heap and flushed all
 637         // dependencies before removing the nmethod (done in make_zombie()).
 638         assert(nm->is_zombie(), "nmethod must be unregistered");
 639         release_nmethod(nm);
 640         assert(result == None, "sanity");


   1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. 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 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  *


 129    _records = NEW_C_HEAP_ARRAY(SweeperRecord, SweeperLogEntries, mtGC);
 130    memset(_records, 0, sizeof(SweeperRecord) * SweeperLogEntries);
 131   }
 132 }
 133 #else
 134 #define SWEEP(nm)
 135 #endif
 136 
 137 NMethodIterator NMethodSweeper::_current;                      // Current nmethod
 138 long     NMethodSweeper::_traversals                   = 0;    // Stack scan count, also sweep ID.
 139 long     NMethodSweeper::_total_nof_code_cache_sweeps  = 0;    // Total number of full sweeps of the code cache
 140 long     NMethodSweeper::_time_counter                 = 0;    // Virtual time used to periodically invoke sweeper
 141 long     NMethodSweeper::_last_sweep                   = 0;    // Value of _time_counter when the last sweep happened
 142 int      NMethodSweeper::_seen                         = 0;    // Nof. nmethod we have currently processed in current pass of CodeCache
 143 
 144 volatile bool NMethodSweeper::_should_sweep            = true; // Indicates if we should invoke the sweeper
 145 volatile bool NMethodSweeper::_force_sweep             = false;// Indicates if we should force a sweep
 146 volatile int  NMethodSweeper::_bytes_changed           = 0;    // Counts the total nmethod size if the nmethod changed from:
 147                                                                //   1) alive       -> not_entrant
 148                                                                //   2) not_entrant -> zombie

 149 int    NMethodSweeper::_hotness_counter_reset_val       = 0;
 150 
 151 long   NMethodSweeper::_total_nof_methods_reclaimed     = 0;   // Accumulated nof methods flushed
 152 long   NMethodSweeper::_total_nof_c2_methods_reclaimed  = 0;   // Accumulated nof methods flushed
 153 size_t NMethodSweeper::_total_flushed_size              = 0;   // Total number of bytes flushed from the code cache
 154 Tickspan NMethodSweeper::_total_time_sweeping;                 // Accumulated time sweeping
 155 Tickspan NMethodSweeper::_total_time_this_sweep;               // Total time this sweep
 156 Tickspan NMethodSweeper::_peak_sweep_time;                     // Peak time for a full sweep
 157 Tickspan NMethodSweeper::_peak_sweep_fraction_time;            // Peak time sweeping one fraction
 158 
 159 Monitor* NMethodSweeper::_stat_lock = new Monitor(Mutex::special, "Sweeper::Statistics", true, Monitor::_safepoint_check_sometimes);
 160 
 161 class MarkActivationClosure: public CodeBlobClosure {
 162 public:
 163   virtual void do_code_blob(CodeBlob* cb) {
 164     assert(cb->is_nmethod(), "CodeBlob should be nmethod");
 165     nmethod* nm = (nmethod*)cb;
 166     nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
 167     // If we see an activation belonging to a non_entrant nmethod, we mark it.
 168     if (nm->is_not_entrant()) {


 379   // can further increase by calls to 'report_state_change'.
 380   if (_should_sweep) {
 381     _bytes_changed = 0;
 382   }
 383 
 384   if (forced) {
 385     // Notify requester that forced sweep finished
 386     assert(_force_sweep, "Should be a forced sweep");
 387     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 388     _force_sweep = false;
 389     CodeCache_lock->notify();
 390   }
 391 }
 392 
 393 void NMethodSweeper::sweep_code_cache() {
 394   ResourceMark rm;
 395   Ticks sweep_start_counter = Ticks::now();
 396 
 397   int flushed_count                = 0;
 398   int zombified_count              = 0;

 399   int flushed_c2_count     = 0;
 400 
 401   if (PrintMethodFlushing && Verbose) {
 402     tty->print_cr("### Sweep at %d out of %d", _seen, CodeCache::nmethod_count());
 403   }
 404 
 405   int swept_count = 0;
 406   assert(!SafepointSynchronize::is_at_safepoint(), "should not be in safepoint when we get here");
 407   assert(!CodeCache_lock->owned_by_self(), "just checking");
 408 
 409   int freed_memory = 0;
 410   {
 411     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 412 
 413     while (!_current.end()) {
 414       swept_count++;
 415       // Since we will give up the CodeCache_lock, always skip ahead
 416       // to the next nmethod.  Other blobs can be deleted by other
 417       // threads but nmethods are only reclaimed by the sweeper.
 418       nmethod* nm = _current.method();


 423         MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 424         // Save information before potentially flushing the nmethod
 425         int size = nm->total_size();
 426         bool is_c2_method = nm->is_compiled_by_c2();
 427         bool is_osr = nm->is_osr_method();
 428         int compile_id = nm->compile_id();
 429         intptr_t address = p2i(nm);
 430         const char* state_before = nm->state();
 431         const char* state_after = "";
 432 
 433         MethodStateChange type = process_nmethod(nm);
 434         switch (type) {
 435           case Flushed:
 436             state_after = "flushed";
 437             freed_memory += size;
 438             ++flushed_count;
 439             if (is_c2_method) {
 440               ++flushed_c2_count;
 441             }
 442             break;




 443           case MadeZombie:
 444             state_after = "made zombie";
 445             ++zombified_count;
 446             break;
 447           case None:
 448             break;
 449           default:
 450            ShouldNotReachHere();
 451         }
 452         if (PrintMethodFlushing && Verbose && type != None) {
 453           tty->print_cr("### %s nmethod %3d/" PTR_FORMAT " (%s) %s", is_osr ? "osr" : "", compile_id, address, state_before, state_after);
 454         }
 455       }
 456 
 457       _seen++;
 458       handle_safepoint_request();
 459     }
 460   }
 461 
 462   assert(_current.end(), "must have scanned the whole cache");
 463 
 464   const Ticks sweep_end_counter = Ticks::now();
 465   const Tickspan sweep_time = sweep_end_counter - sweep_start_counter;
 466   {
 467     MutexLockerEx mu(_stat_lock, Mutex::_no_safepoint_check_flag);
 468     _total_time_sweeping  += sweep_time;
 469     _total_time_this_sweep += sweep_time;
 470     _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time);
 471     _total_flushed_size += freed_memory;
 472     _total_nof_methods_reclaimed += flushed_count;
 473     _total_nof_c2_methods_reclaimed += flushed_c2_count;
 474     _peak_sweep_time = MAX2(_peak_sweep_time, _total_time_this_sweep);
 475   }
 476   EventSweepCodeCache event(UNTIMED);
 477   if (event.should_commit()) {
 478     event.set_starttime(sweep_start_counter);
 479     event.set_endtime(sweep_end_counter);
 480     event.set_sweepIndex(_traversals);
 481     event.set_sweptCount(swept_count);
 482     event.set_flushedCount(flushed_count);

 483     event.set_zombifiedCount(zombified_count);
 484     event.commit();
 485   }
 486 
 487 #ifdef ASSERT
 488   if(PrintMethodFlushing) {
 489     tty->print_cr("### sweeper:      sweep time(" JLONG_FORMAT "): ", sweep_time.value());
 490   }
 491 #endif
 492 
 493   log_sweep("finished");
 494 
 495   // Sweeper is the only case where memory is released, check here if it
 496   // is time to restart the compiler. Only checking if there is a certain
 497   // amount of free memory in the code cache might lead to re-enabling
 498   // compilation although no memory has been released. For example, there are
 499   // cases when compilation was disabled although there is 4MB (or more) free
 500   // memory in the code cache. The reason is code cache fragmentation. Therefore,
 501   // it only makes sense to re-enable compilation if we have actually freed memory.
 502   // Note that typically several kB are released for sweeping 16MB of the code


 577 
 578   MethodStateChange result = None;
 579   // Make sure this nmethod doesn't get unloaded during the scan,
 580   // since safepoints may happen during acquired below locks.
 581   NMethodMarker nmm(nm);
 582   SWEEP(nm);
 583 
 584   // Skip methods that are currently referenced by the VM
 585   if (nm->is_locked_by_vm()) {
 586     // But still remember to clean-up inline caches for alive nmethods
 587     if (nm->is_alive()) {
 588       // Clean inline caches that point to zombie/non-entrant/unloaded nmethods
 589       MutexLocker cl(CompiledIC_lock);
 590       nm->cleanup_inline_caches();
 591       SWEEP(nm);
 592     }
 593     return result;
 594   }
 595 
 596   if (nm->is_zombie()) {
 597     // All inline caches that referred to this nmethod were cleaned in the
 598     // previous sweeper cycle. Now flush the nmethod from the code cache.


 599     assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
 600     release_nmethod(nm);
 601     assert(result == None, "sanity");
 602     result = Flushed;









 603   } else if (nm->is_not_entrant()) {
 604     // If there are no current activations of this method on the
 605     // stack we can safely convert it to a zombie method
 606     if (nm->can_convert_to_zombie()) {
 607       // Clear ICStubs to prevent back patching stubs of zombie or flushed
 608       // nmethods during the next safepoint (see ICStub::finalize).
 609       {
 610         MutexLocker cl(CompiledIC_lock);
 611         nm->clear_ic_stubs();
 612       }
 613       // Code cache state change is tracked in make_zombie()
 614       nm->make_zombie();
 615       SWEEP(nm);
 616       if (nm->is_osr_method()) {
 617         // No inline caches will ever point to osr methods, so we can just remove it.
 618         // Make sure that we unregistered the nmethod with the heap and flushed all
 619         // dependencies before removing the nmethod (done in make_zombie()).
 620         assert(nm->is_zombie(), "nmethod must be unregistered");
 621         release_nmethod(nm);
 622         assert(result == None, "sanity");


< prev index next >