1 /*
  2  * Copyright (c) 2015, 2024, 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_CODE_DEPENDENCYCONTEXT_HPP
 26 #define SHARE_CODE_DEPENDENCYCONTEXT_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "oops/oop.hpp"
 30 #include "runtime/handles.hpp"
 31 #include "runtime/perfData.hpp"
 32 #include "runtime/safepoint.hpp"
 33 
 34 class nmethod;
 35 class DeoptimizationScope;
 36 class DepChange;
 37 
 38 //
 39 // nmethodBucket is used to record dependent nmethods for
 40 // deoptimization.  nmethod dependencies are actually <klass, method>
 41 // pairs but we really only care about the klass part for purposes of
 42 // finding nmethods which might need to be deoptimized.
 43 //
 44 class nmethodBucket: public CHeapObj<mtClass> {
 45   friend class VMStructs;
 46  private:
 47   nmethod*       _nmethod;
 48   nmethodBucket* volatile _next;
 49   nmethodBucket* volatile _purge_list_next;
 50 
 51  public:
 52   nmethodBucket(nmethod* nmethod, nmethodBucket* next) :
 53     _nmethod(nmethod), _next(next), _purge_list_next(nullptr) {}
 54 
 55   nmethodBucket* next();
 56   nmethodBucket* next_not_unloading();
 57   void set_next(nmethodBucket* b);
 58   nmethodBucket* purge_list_next();
 59   void set_purge_list_next(nmethodBucket* b);
 60   nmethod* get_nmethod()                     { return _nmethod; }
 61 };
 62 
 63 //
 64 // Utility class to manipulate nmethod dependency context.
 65 // Dependency context can be attached either to an InstanceKlass (_dep_context field)
 66 // or CallSiteContext oop for call_site_target dependencies (see javaClasses.hpp).
 67 // DependencyContext class operates on some location which holds a nmethodBucket* value
 68 // and uint64_t integer recording the safepoint counter at the last cleanup.
 69 //
 70 class DependencyContext : public StackObj {
 71   friend class VMStructs;
 72   friend class TestDependencyContext;
 73  private:
 74   nmethodBucket* volatile* _dependency_context_addr;
 75   volatile uint64_t*       _last_cleanup_addr;
 76 
 77   bool claim_cleanup();
 78   static bool delete_on_release();
 79   void set_dependencies(nmethodBucket* b);
 80   nmethodBucket* dependencies();
 81   nmethodBucket* dependencies_not_unloading();
 82 
 83   static PerfCounter*            _perf_total_buckets_allocated_count;
 84   static PerfCounter*            _perf_total_buckets_deallocated_count;
 85   static PerfCounter*            _perf_total_buckets_stale_count;
 86   static PerfCounter*            _perf_total_buckets_stale_acc_count;
 87   static nmethodBucket* volatile _purge_list;
 88   static uint64_t                _cleaning_epoch_monotonic;
 89   static volatile uint64_t       _cleaning_epoch;
 90 
 91  public:
 92 #ifdef ASSERT
 93   // Safepoints are forbidden during DC lifetime. GC can invalidate
 94   // _dependency_context_addr if it relocates the holder
 95   // (e.g. CallSiteContext Java object).
 96   SafepointStateTracker _safepoint_tracker;
 97 
 98   DependencyContext(nmethodBucket* volatile* bucket_addr, volatile uint64_t* last_cleanup_addr)
 99     : _dependency_context_addr(bucket_addr),
100       _last_cleanup_addr(last_cleanup_addr),
101       _safepoint_tracker(SafepointSynchronize::safepoint_state_tracker()) {}
102 
103   ~DependencyContext() {
104     assert(!_safepoint_tracker.safepoint_state_changed(), "must be the same safepoint");
105   }
106 #else
107   DependencyContext(nmethodBucket* volatile* bucket_addr, volatile uint64_t* last_cleanup_addr)
108     : _dependency_context_addr(bucket_addr),
109       _last_cleanup_addr(last_cleanup_addr) {}
110 #endif // ASSERT
111 
112   static void init();
113 
114   void mark_dependent_nmethods(DeoptimizationScope* deopt_scope, DepChange& changes, InstanceKlass* context = nullptr);
115   void add_dependent_nmethod(nmethod* nm);
116   void remove_all_dependents();
117   void remove_and_mark_for_deoptimization_all_dependents(DeoptimizationScope* deopt_scope);
118   void clean_unloading_dependents();
119   static nmethodBucket* release_and_get_next_not_unloading(nmethodBucket* b);
120   static void purge_dependency_contexts();
121   static void release(nmethodBucket* b);
122   static void cleaning_start();
123   static void cleaning_end();
124 
125 #ifndef PRODUCT
126   void print_dependent_nmethods(bool verbose);
127   bool is_empty();
128 #endif //PRODUCT
129   bool is_dependent_nmethod(nmethod* nm);
130 };
131 #endif // SHARE_CODE_DEPENDENCYCONTEXT_HPP