1 /*
   2  * Copyright (c) 1997, 2012, 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 unreferences zombie nmethods
  31 //
  32 
  33 class NMethodSweeper : public AllStatic {
  34   static long      _traversals;   // Stack traversal count
  35   static nmethod*  _current;      // Current nmethod
  36   static int       _seen;         // Nof. nmethod we have currently processed in current pass of CodeCache
  37 
  38   static volatile int  _invocations;   // No. of invocations left until we are completed with this pass
  39   static volatile int  _sweep_started; // Flag to control conc sweeper
  40 
  41   //The following are reset in scan_stacks and synchronized by the safepoint
  42   static bool      _resweep;           // Indicates that a change has happend and we want another sweep,
  43                                        // always checked and reset at a safepoint so memory will be in sync.
  44   static int       _locked_seen;       // Number of locked nmethods encountered during the scan
  45   static int       _not_entrant_seen_on_stack; // Number of not entrant nmethod were are still on stack
  46   static jint      _flush_token;       // token that guards method flushing, making sure it is executed only once.
  47 
  48   // These are set during a flush, a VM-operation
  49   static long      _last_flush_traversal_id; // trav number at last flush unloading
  50   static jlong     _last_full_flush_time;    // timestamp of last emergency unloading
  51 
  52   // These are synchronized by the _sweep_started token
  53   static int       _highest_marked;   // highest compile id dumped at last emergency unloading
  54   static int       _dead_compile_ids; // number of compile ids that where not in the cache last flush
  55 
  56   static void process_nmethod(nmethod *nm);
  57   static void release_nmethod(nmethod* nm);
  58 
  59   static void log_sweep(const char* msg, const char* format = NULL, ...);
  60   static bool sweep_in_progress();
  61 
  62  public:
  63   static long traversal_count() { return _traversals; }
  64 
  65 #ifdef ASSERT
  66   // Keep track of sweeper activity in the ring buffer
  67   static void record_sweep(nmethod* nm, int line);
  68   static void report_events(int id, address entry);
  69   static void report_events();
  70 #endif
  71 
  72   static void scan_stacks();      // Invoked at the end of each safepoint
  73   static void sweep_code_cache(); // Concurrent part of sweep job
  74   static void possibly_sweep();   // Compiler threads call this to sweep
  75 
  76   static void notify(nmethod* nm) {
  77     // Request a new sweep of the code cache from the beginning. No
  78     // need to synchronize the setting of this flag since it only
  79     // changes to false at safepoint so we can never overwrite it with false.
  80      _resweep = true;
  81   }
  82 
  83   static void handle_full_code_cache(bool is_full); // Called by compilers who fail to allocate
  84   static void speculative_disconnect_nmethods(bool was_full);   // Called by vm op to deal with alloc failure
  85 };
  86 
  87 #endif // SHARE_VM_RUNTIME_SWEEPER_HPP