blob: 9789c5ed919a561ff38be69d44652041a8b64bb0 [file] [log] [blame]
Alex Light0fa17862017-10-24 13:43:05 -07001/* Copyright (C) 2017 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
32#ifndef ART_OPENJDKJVMTI_DEOPT_MANAGER_H_
33#define ART_OPENJDKJVMTI_DEOPT_MANAGER_H_
34
Alex Lightf2858632018-04-02 11:28:50 -070035#include <atomic>
Alex Lightb2146942019-03-12 15:46:40 +000036#include <iosfwd>
Alex Light0fa17862017-10-24 13:43:05 -070037#include <unordered_map>
38
Alex Light0fa17862017-10-24 13:43:05 -070039#include "base/mutex.h"
40#include "runtime_callbacks.h"
Alex Light0fa17862017-10-24 13:43:05 -070041
Alex Light3dacdd62019-03-12 15:45:47 +000042#include <jvmti.h>
43
Alex Light0fa17862017-10-24 13:43:05 -070044namespace art {
45class ArtMethod;
Alex Light3dacdd62019-03-12 15:45:47 +000046class ScopedObjectAccessUnchecked;
Alex Light0fa17862017-10-24 13:43:05 -070047namespace mirror {
48class Class;
49} // namespace mirror
50} // namespace art
51
52namespace openjdkjvmti {
53
54class DeoptManager;
55
56struct JvmtiMethodInspectionCallback : public art::MethodInspectionCallback {
57 public:
58 explicit JvmtiMethodInspectionCallback(DeoptManager* manager) : manager_(manager) {}
59
60 bool IsMethodBeingInspected(art::ArtMethod* method)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010061 override REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0fa17862017-10-24 13:43:05 -070062
63 bool IsMethodSafeToJit(art::ArtMethod* method)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010064 override REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0fa17862017-10-24 13:43:05 -070065
Alex Lightf2858632018-04-02 11:28:50 -070066 bool MethodNeedsDebugVersion(art::ArtMethod* method)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010067 override REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lightf2858632018-04-02 11:28:50 -070068
Alex Light0fa17862017-10-24 13:43:05 -070069 private:
70 DeoptManager* manager_;
71};
72
73class ScopedDeoptimizationContext;
74
75class DeoptManager {
76 public:
77 DeoptManager();
78
79 void Setup();
80 void Shutdown();
81
Alex Lightb2146942019-03-12 15:46:40 +000082 void DumpDeoptInfo(art::Thread* self, std::ostream& stream);
83
Alex Light0fa17862017-10-24 13:43:05 -070084 void RemoveDeoptimizationRequester() REQUIRES(!deoptimization_status_lock_,
85 !art::Roles::uninterruptible_);
86 void AddDeoptimizationRequester() REQUIRES(!deoptimization_status_lock_,
87 !art::Roles::uninterruptible_);
88 bool MethodHasBreakpoints(art::ArtMethod* method)
89 REQUIRES(!deoptimization_status_lock_);
90
91 void RemoveMethodBreakpoint(art::ArtMethod* method)
92 REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_)
93 REQUIRES_SHARED(art::Locks::mutator_lock_);
94
95 void AddMethodBreakpoint(art::ArtMethod* method)
96 REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_)
97 REQUIRES_SHARED(art::Locks::mutator_lock_);
98
David Srbeckyd25eb2c2018-07-19 12:17:04 +000099 void AddDeoptimizeAllMethods()
Alex Light0fa17862017-10-24 13:43:05 -0700100 REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_)
101 REQUIRES_SHARED(art::Locks::mutator_lock_);
102
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000103 void RemoveDeoptimizeAllMethods()
Alex Light0fa17862017-10-24 13:43:05 -0700104 REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_)
105 REQUIRES_SHARED(art::Locks::mutator_lock_);
106
Alex Light3dacdd62019-03-12 15:45:47 +0000107 jvmtiError AddDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked& soa, jthread thread)
108 REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_)
109 REQUIRES_SHARED(art::Locks::mutator_lock_);
110
111 jvmtiError RemoveDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked& soa, jthread thread)
112 REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_)
113 REQUIRES_SHARED(art::Locks::mutator_lock_);
114
Alex Lighta4cdd362019-04-18 09:17:10 -0700115 void DeoptimizeThread(art::Thread* target)
116 REQUIRES(!art::Locks::thread_list_lock_)
117 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700118 void DeoptimizeAllThreads() REQUIRES_SHARED(art::Locks::mutator_lock_);
119
Alex Light2ce6fc82017-12-18 16:42:36 -0800120 void FinishSetup()
121 REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_)
Nicolas Geoffray226805d2018-12-14 10:59:02 +0000122 REQUIRES(art::Locks::mutator_lock_);
Alex Light2ce6fc82017-12-18 16:42:36 -0800123
Alex Light0fa17862017-10-24 13:43:05 -0700124 static DeoptManager* Get();
125
Alex Lightf2858632018-04-02 11:28:50 -0700126 bool HaveLocalsChanged() const {
127 return set_local_variable_called_.load();
128 }
129
130 void SetLocalsUpdated() {
131 set_local_variable_called_.store(true);
132 }
133
Alex Light0fa17862017-10-24 13:43:05 -0700134 private:
135 bool MethodHasBreakpointsLocked(art::ArtMethod* method)
Alex Lightf2858632018-04-02 11:28:50 -0700136 REQUIRES(breakpoint_status_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700137
138 // Wait until nothing is currently in the middle of deoptimizing/undeoptimizing something. This is
139 // needed to ensure that everything is synchronized since threads need to drop the
140 // deoptimization_status_lock_ while deoptimizing methods.
141 void WaitForDeoptimizationToFinish(art::Thread* self)
142 RELEASE(deoptimization_status_lock_) REQUIRES(!art::Locks::mutator_lock_);
143
144 void WaitForDeoptimizationToFinishLocked(art::Thread* self)
145 REQUIRES(deoptimization_status_lock_, !art::Locks::mutator_lock_);
146
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000147 void AddDeoptimizeAllMethodsLocked(art::Thread* self)
Alex Light0fa17862017-10-24 13:43:05 -0700148 RELEASE(deoptimization_status_lock_)
149 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
150
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000151 void RemoveDeoptimizeAllMethodsLocked(art::Thread* self)
Alex Light0fa17862017-10-24 13:43:05 -0700152 RELEASE(deoptimization_status_lock_)
153 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
154
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000155 void PerformGlobalDeoptimization(art::Thread* self)
Alex Light0fa17862017-10-24 13:43:05 -0700156 RELEASE(deoptimization_status_lock_)
157 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
158
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000159 void PerformGlobalUndeoptimization(art::Thread* self)
Alex Light0fa17862017-10-24 13:43:05 -0700160 RELEASE(deoptimization_status_lock_)
161 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
162
163 void PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method)
164 RELEASE(deoptimization_status_lock_)
165 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
166
167 void PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method)
168 RELEASE(deoptimization_status_lock_)
169 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
170
171 static constexpr const char* kDeoptManagerInstrumentationKey = "JVMTI_DeoptManager";
Alex Light0fa17862017-10-24 13:43:05 -0700172
Alex Light2ce6fc82017-12-18 16:42:36 -0800173 art::Mutex deoptimization_status_lock_ ACQUIRED_BEFORE(art::Locks::classlinker_classes_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700174 art::ConditionVariable deoptimization_condition_ GUARDED_BY(deoptimization_status_lock_);
175 bool performing_deoptimization_ GUARDED_BY(deoptimization_status_lock_);
176
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000177 // Number of times we have gotten requests to deopt everything.
Alex Light0fa17862017-10-24 13:43:05 -0700178 uint32_t global_deopt_count_ GUARDED_BY(deoptimization_status_lock_);
179
180 // Number of users of deoptimization there currently are.
181 uint32_t deopter_count_ GUARDED_BY(deoptimization_status_lock_);
182
Alex Lightf2858632018-04-02 11:28:50 -0700183 // A mutex that just protects the breakpoint-status map. This mutex should always be at the
184 // bottom of the lock hierarchy. Nothing more should be locked if we hold this.
185 art::Mutex breakpoint_status_lock_ ACQUIRED_BEFORE(art::Locks::abort_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700186 // A map from methods to the number of breakpoints in them from all envs.
187 std::unordered_map<art::ArtMethod*, uint32_t> breakpoint_status_
Alex Lightf2858632018-04-02 11:28:50 -0700188 GUARDED_BY(breakpoint_status_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700189
190 // The MethodInspectionCallback we use to tell the runtime if we care about particular methods.
191 JvmtiMethodInspectionCallback inspection_callback_;
192
Alex Lightf2858632018-04-02 11:28:50 -0700193 // Set to true if anything calls SetLocalVariables on any thread since we need to be careful about
194 // OSR after this.
195 std::atomic<bool> set_local_variable_called_;
196
Alex Light0fa17862017-10-24 13:43:05 -0700197 // Helper for setting up/tearing-down for deoptimization.
198 friend class ScopedDeoptimizationContext;
199};
200
201} // namespace openjdkjvmti
202#endif // ART_OPENJDKJVMTI_DEOPT_MANAGER_H_