Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1 | /* 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 Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 35 | #include <atomic> |
Alex Light | b214694 | 2019-03-12 15:46:40 +0000 | [diff] [blame] | 36 | #include <iosfwd> |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 37 | #include <unordered_map> |
| 38 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 39 | #include "base/mutex.h" |
| 40 | #include "runtime_callbacks.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 41 | |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 42 | #include <jvmti.h> |
| 43 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 44 | namespace art { |
| 45 | class ArtMethod; |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 46 | class ScopedObjectAccessUnchecked; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 47 | namespace mirror { |
| 48 | class Class; |
| 49 | } // namespace mirror |
| 50 | } // namespace art |
| 51 | |
| 52 | namespace openjdkjvmti { |
| 53 | |
| 54 | class DeoptManager; |
| 55 | |
| 56 | struct JvmtiMethodInspectionCallback : public art::MethodInspectionCallback { |
| 57 | public: |
| 58 | explicit JvmtiMethodInspectionCallback(DeoptManager* manager) : manager_(manager) {} |
| 59 | |
| 60 | bool IsMethodBeingInspected(art::ArtMethod* method) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 61 | override REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 62 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 63 | private: |
| 64 | DeoptManager* manager_; |
| 65 | }; |
| 66 | |
| 67 | class ScopedDeoptimizationContext; |
| 68 | |
| 69 | class DeoptManager { |
| 70 | public: |
| 71 | DeoptManager(); |
| 72 | |
| 73 | void Setup(); |
| 74 | void Shutdown(); |
| 75 | |
Alex Light | b214694 | 2019-03-12 15:46:40 +0000 | [diff] [blame] | 76 | void DumpDeoptInfo(art::Thread* self, std::ostream& stream); |
| 77 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 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 Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 93 | void AddDeoptimizeAllMethods() |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 94 | REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_) |
| 95 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 96 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 97 | void RemoveDeoptimizeAllMethods() |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 98 | REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_) |
| 99 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 100 | |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 101 | jvmtiError AddDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked& soa, jthread thread) |
| 102 | REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_) |
| 103 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 104 | |
| 105 | jvmtiError RemoveDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked& soa, jthread thread) |
| 106 | REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_) |
| 107 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 108 | |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 109 | void DeoptimizeThread(art::Thread* target) |
| 110 | REQUIRES(!art::Locks::thread_list_lock_) |
| 111 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 112 | void DeoptimizeAllThreads() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 113 | |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 114 | void FinishSetup() |
| 115 | REQUIRES(!deoptimization_status_lock_, !art::Roles::uninterruptible_) |
Nicolas Geoffray | 226805d | 2018-12-14 10:59:02 +0000 | [diff] [blame] | 116 | REQUIRES(art::Locks::mutator_lock_); |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 117 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 118 | static DeoptManager* Get(); |
| 119 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 120 | bool HaveLocalsChanged() const { |
| 121 | return set_local_variable_called_.load(); |
| 122 | } |
| 123 | |
| 124 | void SetLocalsUpdated() { |
| 125 | set_local_variable_called_.store(true); |
| 126 | } |
| 127 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 128 | private: |
| 129 | bool MethodHasBreakpointsLocked(art::ArtMethod* method) |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 130 | REQUIRES(breakpoint_status_lock_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 131 | |
| 132 | // Wait until nothing is currently in the middle of deoptimizing/undeoptimizing something. This is |
| 133 | // needed to ensure that everything is synchronized since threads need to drop the |
| 134 | // deoptimization_status_lock_ while deoptimizing methods. |
| 135 | void WaitForDeoptimizationToFinish(art::Thread* self) |
| 136 | RELEASE(deoptimization_status_lock_) REQUIRES(!art::Locks::mutator_lock_); |
| 137 | |
| 138 | void WaitForDeoptimizationToFinishLocked(art::Thread* self) |
| 139 | REQUIRES(deoptimization_status_lock_, !art::Locks::mutator_lock_); |
| 140 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 141 | void AddDeoptimizeAllMethodsLocked(art::Thread* self) |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 142 | RELEASE(deoptimization_status_lock_) |
| 143 | REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_); |
| 144 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 145 | void RemoveDeoptimizeAllMethodsLocked(art::Thread* self) |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 146 | RELEASE(deoptimization_status_lock_) |
| 147 | REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_); |
| 148 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 149 | void PerformGlobalDeoptimization(art::Thread* self) |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 150 | RELEASE(deoptimization_status_lock_) |
| 151 | REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_); |
| 152 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 153 | void PerformGlobalUndeoptimization(art::Thread* self) |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 154 | RELEASE(deoptimization_status_lock_) |
| 155 | REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_); |
| 156 | |
| 157 | void PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method) |
| 158 | RELEASE(deoptimization_status_lock_) |
| 159 | REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_); |
| 160 | |
| 161 | void PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method) |
| 162 | RELEASE(deoptimization_status_lock_) |
| 163 | REQUIRES(!art::Roles::uninterruptible_, !art::Locks::mutator_lock_); |
| 164 | |
| 165 | static constexpr const char* kDeoptManagerInstrumentationKey = "JVMTI_DeoptManager"; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 166 | |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 167 | art::Mutex deoptimization_status_lock_ ACQUIRED_BEFORE(art::Locks::classlinker_classes_lock_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 168 | art::ConditionVariable deoptimization_condition_ GUARDED_BY(deoptimization_status_lock_); |
| 169 | bool performing_deoptimization_ GUARDED_BY(deoptimization_status_lock_); |
| 170 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 171 | // Number of times we have gotten requests to deopt everything. |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 172 | uint32_t global_deopt_count_ GUARDED_BY(deoptimization_status_lock_); |
| 173 | |
| 174 | // Number of users of deoptimization there currently are. |
| 175 | uint32_t deopter_count_ GUARDED_BY(deoptimization_status_lock_); |
| 176 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 177 | // A mutex that just protects the breakpoint-status map. This mutex should always be at the |
| 178 | // bottom of the lock hierarchy. Nothing more should be locked if we hold this. |
| 179 | art::Mutex breakpoint_status_lock_ ACQUIRED_BEFORE(art::Locks::abort_lock_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 180 | // A map from methods to the number of breakpoints in them from all envs. |
| 181 | std::unordered_map<art::ArtMethod*, uint32_t> breakpoint_status_ |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 182 | GUARDED_BY(breakpoint_status_lock_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 183 | |
| 184 | // The MethodInspectionCallback we use to tell the runtime if we care about particular methods. |
| 185 | JvmtiMethodInspectionCallback inspection_callback_; |
| 186 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 187 | // Set to true if anything calls SetLocalVariables on any thread since we need to be careful about |
| 188 | // OSR after this. |
| 189 | std::atomic<bool> set_local_variable_called_; |
| 190 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 191 | // Helper for setting up/tearing-down for deoptimization. |
| 192 | friend class ScopedDeoptimizationContext; |
| 193 | }; |
| 194 | |
| 195 | } // namespace openjdkjvmti |
| 196 | #endif // ART_OPENJDKJVMTI_DEOPT_MANAGER_H_ |