blob: 856f3f4931abc48ef25b2e4700ef204dc44ad861 [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 Light0fa17862017-10-24 13:43:05 -070036#include <unordered_map>
37
Alex Light0fa17862017-10-24 13:43:05 -070038#include "base/mutex.h"
39#include "runtime_callbacks.h"
Alex Light0fa17862017-10-24 13:43:05 -070040
41namespace art {
42class ArtMethod;
43namespace mirror {
44class Class;
45} // namespace mirror
46} // namespace art
47
48namespace openjdkjvmti {
49
50class DeoptManager;
51
52struct JvmtiMethodInspectionCallback : public art::MethodInspectionCallback {
53 public:
54 explicit JvmtiMethodInspectionCallback(DeoptManager* manager) : manager_(manager) {}
55
56 bool IsMethodBeingInspected(art::ArtMethod* method)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010057 override REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0fa17862017-10-24 13:43:05 -070058
59 bool IsMethodSafeToJit(art::ArtMethod* method)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010060 override REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0fa17862017-10-24 13:43:05 -070061
Alex Lightf2858632018-04-02 11:28:50 -070062 bool MethodNeedsDebugVersion(art::ArtMethod* method)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010063 override REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lightf2858632018-04-02 11:28:50 -070064
Alex Light0fa17862017-10-24 13:43:05 -070065 private:
66 DeoptManager* manager_;
67};
68
69class ScopedDeoptimizationContext;
70
71class DeoptManager {
72 public:
73 DeoptManager();
74
75 void Setup();
76 void Shutdown();
77
78 void RemoveDeoptimizationRequester() REQUIRES(!deoptimization_status_lock_,
79 !art::Roles::uninterruptible_);
80 void AddDeoptimizationRequester() REQUIRES(!deoptimization_status_lock_,
81 !art::Roles::uninterruptible_);
82 bool MethodHasBreakpoints(art::ArtMethod* method)
83 REQUIRES(!deoptimization_status_lock_);
84
85 void RemoveMethodBreakpoint(art::ArtMethod* method)
86 REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_)
87 REQUIRES_SHARED(art::Locks::mutator_lock_);
88
89 void AddMethodBreakpoint(art::ArtMethod* method)
90 REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_)
91 REQUIRES_SHARED(art::Locks::mutator_lock_);
92
David Srbeckyd25eb2c2018-07-19 12:17:04 +000093 void AddDeoptimizeAllMethods()
Alex Light0fa17862017-10-24 13:43:05 -070094 REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_)
95 REQUIRES_SHARED(art::Locks::mutator_lock_);
96
David Srbeckyd25eb2c2018-07-19 12:17:04 +000097 void RemoveDeoptimizeAllMethods()
Alex Light0fa17862017-10-24 13:43:05 -070098 REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_)
99 REQUIRES_SHARED(art::Locks::mutator_lock_);
100
101 void DeoptimizeThread(art::Thread* target) REQUIRES_SHARED(art::Locks::mutator_lock_);
102 void DeoptimizeAllThreads() REQUIRES_SHARED(art::Locks::mutator_lock_);
103
Alex Light2ce6fc82017-12-18 16:42:36 -0800104 void FinishSetup()
105 REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_)
Nicolas Geoffray226805d2018-12-14 10:59:02 +0000106 REQUIRES(art::Locks::mutator_lock_);
Alex Light2ce6fc82017-12-18 16:42:36 -0800107
Alex Light0fa17862017-10-24 13:43:05 -0700108 static DeoptManager* Get();
109
Alex Lightf2858632018-04-02 11:28:50 -0700110 bool HaveLocalsChanged() const {
111 return set_local_variable_called_.load();
112 }
113
114 void SetLocalsUpdated() {
115 set_local_variable_called_.store(true);
116 }
117
Alex Light0fa17862017-10-24 13:43:05 -0700118 private:
119 bool MethodHasBreakpointsLocked(art::ArtMethod* method)
Alex Lightf2858632018-04-02 11:28:50 -0700120 REQUIRES(breakpoint_status_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700121
122 // Wait until nothing is currently in the middle of deoptimizing/undeoptimizing something. This is
123 // needed to ensure that everything is synchronized since threads need to drop the
124 // deoptimization_status_lock_ while deoptimizing methods.
125 void WaitForDeoptimizationToFinish(art::Thread* self)
126 RELEASE(deoptimization_status_lock_) REQUIRES(!art::Locks::mutator_lock_);
127
128 void WaitForDeoptimizationToFinishLocked(art::Thread* self)
129 REQUIRES(deoptimization_status_lock_, !art::Locks::mutator_lock_);
130
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000131 void AddDeoptimizeAllMethodsLocked(art::Thread* self)
Alex Light0fa17862017-10-24 13:43:05 -0700132 RELEASE(deoptimization_status_lock_)
133 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
134
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000135 void RemoveDeoptimizeAllMethodsLocked(art::Thread* self)
Alex Light0fa17862017-10-24 13:43:05 -0700136 RELEASE(deoptimization_status_lock_)
137 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
138
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000139 void PerformGlobalDeoptimization(art::Thread* self)
Alex Light0fa17862017-10-24 13:43:05 -0700140 RELEASE(deoptimization_status_lock_)
141 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
142
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000143 void PerformGlobalUndeoptimization(art::Thread* self)
Alex Light0fa17862017-10-24 13:43:05 -0700144 RELEASE(deoptimization_status_lock_)
145 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
146
147 void PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method)
148 RELEASE(deoptimization_status_lock_)
149 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
150
151 void PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method)
152 RELEASE(deoptimization_status_lock_)
153 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
154
155 static constexpr const char* kDeoptManagerInstrumentationKey = "JVMTI_DeoptManager";
Alex Light0fa17862017-10-24 13:43:05 -0700156
Alex Light2ce6fc82017-12-18 16:42:36 -0800157 art::Mutex deoptimization_status_lock_ ACQUIRED_BEFORE(art::Locks::classlinker_classes_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700158 art::ConditionVariable deoptimization_condition_ GUARDED_BY(deoptimization_status_lock_);
159 bool performing_deoptimization_ GUARDED_BY(deoptimization_status_lock_);
160
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000161 // Number of times we have gotten requests to deopt everything.
Alex Light0fa17862017-10-24 13:43:05 -0700162 uint32_t global_deopt_count_ GUARDED_BY(deoptimization_status_lock_);
163
164 // Number of users of deoptimization there currently are.
165 uint32_t deopter_count_ GUARDED_BY(deoptimization_status_lock_);
166
Alex Lightf2858632018-04-02 11:28:50 -0700167 // A mutex that just protects the breakpoint-status map. This mutex should always be at the
168 // bottom of the lock hierarchy. Nothing more should be locked if we hold this.
169 art::Mutex breakpoint_status_lock_ ACQUIRED_BEFORE(art::Locks::abort_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700170 // A map from methods to the number of breakpoints in them from all envs.
171 std::unordered_map<art::ArtMethod*, uint32_t> breakpoint_status_
Alex Lightf2858632018-04-02 11:28:50 -0700172 GUARDED_BY(breakpoint_status_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700173
174 // The MethodInspectionCallback we use to tell the runtime if we care about particular methods.
175 JvmtiMethodInspectionCallback inspection_callback_;
176
Alex Lightf2858632018-04-02 11:28:50 -0700177 // Set to true if anything calls SetLocalVariables on any thread since we need to be careful about
178 // OSR after this.
179 std::atomic<bool> set_local_variable_called_;
180
Alex Light0fa17862017-10-24 13:43:05 -0700181 // Helper for setting up/tearing-down for deoptimization.
182 friend class ScopedDeoptimizationContext;
183};
184
185} // namespace openjdkjvmti
186#endif // ART_OPENJDKJVMTI_DEOPT_MANAGER_H_