1 /*
   2  * Copyright (c) 1997, 2013, 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  *
  23  */
  24 
  25 #ifndef SHARE_VM_RUNTIME_SWEEPER_HPP
  26 #define SHARE_VM_RUNTIME_SWEEPER_HPP
  27 
  28 // An NmethodSweeper is an incremental cleaner for:
  29 //    - cleanup inline caches
  30 //    - reclamation of nmethods
  31 // Removing nmethods from the code cache includes three operations
  32 //  1) mark active nmethods
  33 //     Is done in 'mark_active_nmethods()'. This function is called at a
  34 //     safepoint and marks all nmethods that are active on a thread's stack.
  35 //  2) sweep nmethods
  36 //     Is done in sweep_code_cache(). This function is the only place in the
  37 //     sweeper where memory is reclaimed. Note that sweep_code_cache() is not
  38 //     called at a safepoint. However, sweep_code_cache() stops executing if
  39 //     another thread requests a safepoint. Consequently, 'mark_active_nmethods()'
  40 //     and sweep_code_cache() cannot execute at the same time.
  41 //     To reclaim memory, nmethods are first marked as 'not-entrant'. Not-entrant
  42 //     nmethod cannot be called by Java threads, but they can still be active on the
  43 //     stack. To ensure that active nmethod are not reclaimed, we have to wait until the
  44 //     next marking phase has completed. If a not-entrant nmethod was NOT marked as active,
  45 //     it can be converted to 'zombie' state. To safely remove the nmethod, all inline caches
  46 //     (IC) that point to the the nmethod must be cleared. After that, the nmethod can be
  47 //     evicted from the code cache.
  48 //  3) code cache flushing
  49 //     Is done in 'speculative_disconnect_nmethods()' and performed as a VM operation.
  50 //     Code cache flushing is performed as a last resort to clean the code cache. Flushing
  51 //     is performed in two steps. First, a subset of nmethods is selected for eviction. The
  52 //     selected nmethods are 'speculatively disconnected, i.e., the methods are added to a
  53 //     linked list AND the method's code pointer is set to null (nm->method()->code() = NULL)
  54 //     If the compiler tries to compile the disconnected nmethods (the compiler has to, since
  55 //     method()->code() == NULL), the compiler first searches the linked list where speculatively
  56 //     disconnected nmethods are stored. If the compiler finds the nmethod, the compiler 're-animates'
  57 //     the nmethod (restores the code pointer). If not, the method is removed by normal sweeping.
  58 
  59 class NMethodSweeper : public AllStatic {
  60   static long      _traversals;      // Stack scan count, also sweep ID.
  61   static nmethod*  _current;         // Current nmethod
  62   static int       _seen;            // Nof. nmethod we have currently processed in current pass of CodeCache
  63   static int       _flushed_count;   // Nof. nmethods flushed in current sweep
  64   static int       _zombified_count; // Nof. nmethods made zombie in current sweep
  65   static int       _marked_count;    // Nof. nmethods marked for reclaim in current sweep
  66 
  67   static volatile int  _invocations;   // No. of invocations left until we are completed with this pass
  68   static volatile int  _sweep_started; // Flag to control conc sweeper
  69 
  70   //The following are reset in mark_active_nmethods and synchronized by the safepoint
  71   static bool      _request_mark_phase;        // Indicates that a change has happend and we need another mark pahse,
  72                                                // always checked and reset at a safepoint so memory will be in sync.
  73   static int       _locked_seen;               // Number of locked nmethods encountered during the scan
  74   static int       _not_entrant_seen_on_stack; // Number of not entrant nmethod were are still on stack
  75   static jint      _flush_token;               // token that guards method flushing, making sure it is executed only once.
  76 
  77   // These are set during a flush, a VM-operation
  78   static long      _last_flush_traversal_id; // trav number at last flush unloading
  79   static jlong     _last_full_flush_time;    // timestamp of last emergency unloading
  80 
  81   // Stat counters
  82   static int       _number_of_flushes;            // Total of full traversals caused by full cache
  83   static int       _total_nof_methods_reclaimed;  // Accumulated nof methods flushed
  84   static jlong     _total_time_sweeping;          // Accumulated time sweeping
  85   static jlong     _total_time_this_sweep;        // Total time this sweep
  86   static jlong     _peak_sweep_time;              // Peak time for a full sweep
  87   static jlong     _peak_sweep_fraction_time;     // Peak time sweeping one fraction
  88   static jlong     _total_disconnect_time;        // Total time cleaning code mem
  89   static jlong     _peak_disconnect_time;         // Peak time cleaning code mem
  90 
  91   static void process_nmethod(nmethod *nm);
  92   static void release_nmethod(nmethod* nm);
  93 
  94   static void log_sweep(const char* msg, const char* format = NULL, ...);
  95   static bool sweep_in_progress();
  96   static void sweep_code_cache();
  97   static void request_nmethod_marking() { _request_mark_phase = true; }
  98   static void reset_nmethod_marking()   { _request_mark_phase = false; }
  99   static bool need_marking_phase()      { return _request_mark_phase; }
 100 
 101   static int _hotness_counter_reset_val;
 102 
 103  public:
 104   static long traversal_count()              { return _traversals; }
 105   static int  number_of_flushes()            { return _number_of_flushes; }
 106   static int  total_nof_methods_reclaimed()  { return _total_nof_methods_reclaimed; }
 107   static jlong total_time_sweeping()         { return _total_time_sweeping; }
 108   static jlong peak_sweep_time()             { return _peak_sweep_time; }
 109   static jlong peak_sweep_fraction_time()    { return _peak_sweep_fraction_time; }
 110   static jlong total_disconnect_time()       { return _total_disconnect_time; }
 111   static jlong peak_disconnect_time()        { return _peak_disconnect_time; }
 112 
 113 #ifdef ASSERT
 114   static bool is_sweeping(nmethod* which) { return _current == which; }
 115   // Keep track of sweeper activity in the ring buffer
 116   static void record_sweep(nmethod* nm, int line);
 117   static void report_events(int id, address entry);
 118   static void report_events();
 119 #endif
 120 
 121   static void mark_active_nmethods();      // Invoked at the end of each safepoint
 122   static void possibly_sweep();            // Compiler threads call this to sweep
 123 
 124   static int sort_nmethods_by_hotness(nmethod** nm1, nmethod** nm2);
 125   static int get_hotness_counter_reset_val();
 126 
 127   static void notify() {
 128     // Request a new sweep of the code cache from the beginning. No
 129     // need to synchronize the setting of this flag since it only
 130     // changes to false at safepoint so we can never overwrite it with false.
 131      request_nmethod_marking();
 132   }
 133 
 134   static void handle_full_code_cache(bool is_full); // Called by compilers who fail to allocate
 135   static void speculative_disconnect_nmethods(bool was_full);   // Called by vm op to deal with alloc failure
 136 };
 137 
 138 #endif // SHARE_VM_RUNTIME_SWEEPER_HPP