blob: a495b6835c302fd523eca496fced75ccd9954c27 [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
35#include <unordered_map>
36
37#include "jni.h"
38#include "jvmti.h"
39
40#include "base/mutex.h"
41#include "runtime_callbacks.h"
42#include "ti_breakpoint.h"
43
44namespace art {
45class ArtMethod;
46namespace mirror {
47class Class;
48} // namespace mirror
49} // namespace art
50
51namespace openjdkjvmti {
52
53class DeoptManager;
54
55struct JvmtiMethodInspectionCallback : public art::MethodInspectionCallback {
56 public:
57 explicit JvmtiMethodInspectionCallback(DeoptManager* manager) : manager_(manager) {}
58
59 bool IsMethodBeingInspected(art::ArtMethod* method)
60 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_);
61
62 bool IsMethodSafeToJit(art::ArtMethod* method)
63 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_);
64
65 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
93 void AddDeoptimizeAllMethods()
94 REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_)
95 REQUIRES_SHARED(art::Locks::mutator_lock_);
96
97 void RemoveDeoptimizeAllMethods()
98 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_)
106 REQUIRES_SHARED(art::Locks::mutator_lock_);
107
Alex Light0fa17862017-10-24 13:43:05 -0700108 static DeoptManager* Get();
109
110 private:
111 bool MethodHasBreakpointsLocked(art::ArtMethod* method)
112 REQUIRES(deoptimization_status_lock_);
113
114 // Wait until nothing is currently in the middle of deoptimizing/undeoptimizing something. This is
115 // needed to ensure that everything is synchronized since threads need to drop the
116 // deoptimization_status_lock_ while deoptimizing methods.
117 void WaitForDeoptimizationToFinish(art::Thread* self)
118 RELEASE(deoptimization_status_lock_) REQUIRES(!art::Locks::mutator_lock_);
119
120 void WaitForDeoptimizationToFinishLocked(art::Thread* self)
121 REQUIRES(deoptimization_status_lock_, !art::Locks::mutator_lock_);
122
123 void AddDeoptimizeAllMethodsLocked(art::Thread* self)
124 RELEASE(deoptimization_status_lock_)
125 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
126
127 void RemoveDeoptimizeAllMethodsLocked(art::Thread* self)
128 RELEASE(deoptimization_status_lock_)
129 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
130
131 void PerformGlobalDeoptimization(art::Thread* self)
132 RELEASE(deoptimization_status_lock_)
133 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
134
135 void PerformGlobalUndeoptimization(art::Thread* self)
136 RELEASE(deoptimization_status_lock_)
137 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
138
139 void PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method)
140 RELEASE(deoptimization_status_lock_)
141 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
142
143 void PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method)
144 RELEASE(deoptimization_status_lock_)
145 REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_);
146
147 static constexpr const char* kDeoptManagerInstrumentationKey = "JVMTI_DeoptManager";
Alex Light0fa17862017-10-24 13:43:05 -0700148
Alex Light2ce6fc82017-12-18 16:42:36 -0800149 art::Mutex deoptimization_status_lock_ ACQUIRED_BEFORE(art::Locks::classlinker_classes_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700150 art::ConditionVariable deoptimization_condition_ GUARDED_BY(deoptimization_status_lock_);
151 bool performing_deoptimization_ GUARDED_BY(deoptimization_status_lock_);
152
153 // Number of times we have gotten requests to deopt everything.
154 uint32_t global_deopt_count_ GUARDED_BY(deoptimization_status_lock_);
155
156 // Number of users of deoptimization there currently are.
157 uint32_t deopter_count_ GUARDED_BY(deoptimization_status_lock_);
158
159 // A map from methods to the number of breakpoints in them from all envs.
160 std::unordered_map<art::ArtMethod*, uint32_t> breakpoint_status_
161 GUARDED_BY(deoptimization_status_lock_);
162
163 // The MethodInspectionCallback we use to tell the runtime if we care about particular methods.
164 JvmtiMethodInspectionCallback inspection_callback_;
165
166 // Helper for setting up/tearing-down for deoptimization.
167 friend class ScopedDeoptimizationContext;
168};
169
170} // namespace openjdkjvmti
171#endif // ART_OPENJDKJVMTI_DEOPT_MANAGER_H_