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 private:
46 nmethod* _nmethod;
47 nmethodBucket* volatile _next;
48 nmethodBucket* volatile _purge_list_next;
49
50 public:
51 nmethodBucket(nmethod* nmethod, nmethodBucket* next) :
52 _nmethod(nmethod), _next(next), _purge_list_next(nullptr) {}
53
54 nmethodBucket* next();
55 nmethodBucket* next_not_unloading();
56 void set_next(nmethodBucket* b);
57 nmethodBucket* purge_list_next();
58 void set_purge_list_next(nmethodBucket* b);
59 nmethod* get_nmethod() { return _nmethod; }
60 };
61
62 //
63 // Utility class to manipulate nmethod dependency context.
64 // Dependency context can be attached either to an InstanceKlass (_dep_context field)
65 // or CallSite oop for call_site_target dependencies (see javaClasses.hpp).
66 // DependencyContext class operates on some location which holds a nmethodBucket* value
67 // and uint64_t integer recording the safepoint counter at the last cleanup.
68 //
69 class DependencyContext : public StackObj {
70 friend class TestDependencyContext;
71 private:
72 nmethodBucket* volatile* _dependency_context_addr;
73 volatile uint64_t* _last_cleanup_addr;
74
75 bool claim_cleanup();
76 static bool delete_on_release();
77 void set_dependencies(nmethodBucket* b);
78 nmethodBucket* dependencies();
79 nmethodBucket* dependencies_not_unloading();
80
81 static PerfCounter* _perf_total_buckets_allocated_count;
82 static PerfCounter* _perf_total_buckets_deallocated_count;
83 static PerfCounter* _perf_total_buckets_stale_count;
84 static PerfCounter* _perf_total_buckets_stale_acc_count;
85 static nmethodBucket* volatile _purge_list;
86 static uint64_t _cleaning_epoch_monotonic;
87 static volatile uint64_t _cleaning_epoch;
88
89 public:
90 #ifdef ASSERT
91 // Safepoints are forbidden during DC lifetime. GC can invalidate
92 // _dependency_context_addr if it relocates the holder
93 SafepointStateTracker _safepoint_tracker;
94
95 DependencyContext(nmethodBucket* volatile* bucket_addr, volatile uint64_t* last_cleanup_addr)
96 : _dependency_context_addr(bucket_addr),
97 _last_cleanup_addr(last_cleanup_addr),
98 _safepoint_tracker(SafepointSynchronize::safepoint_state_tracker()) {}
99
100 ~DependencyContext() {
101 assert(!_safepoint_tracker.safepoint_state_changed(), "must be the same safepoint");
102 }
103 #else
104 DependencyContext(nmethodBucket* volatile* bucket_addr, volatile uint64_t* last_cleanup_addr)
105 : _dependency_context_addr(bucket_addr),
106 _last_cleanup_addr(last_cleanup_addr) {}
107 #endif // ASSERT
108
109 static void init();
110
111 void mark_dependent_nmethods(DeoptimizationScope* deopt_scope, DepChange& changes, InstanceKlass* context = nullptr);
112 void add_dependent_nmethod(nmethod* nm);
113 void remove_all_dependents();
114 void clean_unloading_dependents();
115 static void purge_dependency_contexts();
116 static void release(nmethodBucket* b);
117 static void cleaning_start();
118 static void cleaning_end();
119
120 #ifndef PRODUCT
121 void print_dependent_nmethods(bool verbose);
122 bool is_empty();
123 #endif //PRODUCT
124 bool is_dependent_nmethod(nmethod* nm);
125 };
126 #endif // SHARE_CODE_DEPENDENCYCONTEXT_HPP