Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_GC_ROOT_H_ |
| 18 | #define ART_RUNTIME_GC_ROOT_H_ |
| 19 | |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 20 | #include "base/macros.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 21 | #include "base/mutex.h" // For Locks::mutator_lock_. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 22 | #include "mirror/object_reference.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 25 | class ArtField; |
| 26 | class ArtMethod; |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 27 | |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 28 | namespace mirror { |
| 29 | class Object; |
| 30 | } // namespace mirror |
| 31 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 32 | template <size_t kBufferSize> |
| 33 | class BufferedRootVisitor; |
| 34 | |
Mathieu Chartier | 4809d0a | 2015-04-07 10:39:04 -0700 | [diff] [blame] | 35 | // Dependent on pointer size so that we don't have frames that are too big on 64 bit. |
| 36 | static const size_t kDefaultBufferedRootCount = 1024 / sizeof(void*); |
| 37 | |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 38 | enum RootType { |
| 39 | kRootUnknown = 0, |
| 40 | kRootJNIGlobal, |
| 41 | kRootJNILocal, |
| 42 | kRootJavaFrame, |
| 43 | kRootNativeStack, |
| 44 | kRootStickyClass, |
| 45 | kRootThreadBlock, |
| 46 | kRootMonitorUsed, |
| 47 | kRootThreadObject, |
| 48 | kRootInternedString, |
Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 49 | kRootFinalizing, // used for HPROF's conversion to HprofHeapTag |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 50 | kRootDebugger, |
Man Cao | 1ed11b9 | 2015-06-11 22:47:35 -0700 | [diff] [blame] | 51 | kRootReferenceCleanup, // used for HPROF's conversion to HprofHeapTag |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 52 | kRootVMInternal, |
| 53 | kRootJNIMonitor, |
| 54 | }; |
| 55 | std::ostream& operator<<(std::ostream& os, const RootType& root_type); |
| 56 | |
Mathieu Chartier | d3ed9a3 | 2015-04-10 14:23:35 -0700 | [diff] [blame] | 57 | // Only used by hprof. thread_id_ and type_ are only used by hprof. |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 58 | class RootInfo { |
| 59 | public: |
| 60 | // Thread id 0 is for non thread roots. |
| 61 | explicit RootInfo(RootType type, uint32_t thread_id = 0) |
| 62 | : type_(type), thread_id_(thread_id) { |
| 63 | } |
Andreas Gampe | 758a801 | 2015-04-03 21:28:42 -0700 | [diff] [blame] | 64 | RootInfo(const RootInfo&) = default; |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 65 | virtual ~RootInfo() { |
| 66 | } |
| 67 | RootType GetType() const { |
| 68 | return type_; |
| 69 | } |
| 70 | uint32_t GetThreadId() const { |
| 71 | return thread_id_; |
| 72 | } |
| 73 | virtual void Describe(std::ostream& os) const { |
| 74 | os << "Type=" << type_ << " thread_id=" << thread_id_; |
| 75 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 76 | std::string ToString() const; |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 77 | |
| 78 | private: |
| 79 | const RootType type_; |
| 80 | const uint32_t thread_id_; |
| 81 | }; |
| 82 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 83 | inline std::ostream& operator<<(std::ostream& os, const RootInfo& root_info) { |
| 84 | root_info.Describe(os); |
| 85 | return os; |
| 86 | } |
| 87 | |
| 88 | class RootVisitor { |
| 89 | public: |
| 90 | virtual ~RootVisitor() { } |
| 91 | |
Mathieu Chartier | d3ed9a3 | 2015-04-10 14:23:35 -0700 | [diff] [blame] | 92 | // Single root version, not overridable. |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 93 | ALWAYS_INLINE void VisitRoot(mirror::Object** root, const RootInfo& info) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 94 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 95 | VisitRoots(&root, 1, info); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Mathieu Chartier | d3ed9a3 | 2015-04-10 14:23:35 -0700 | [diff] [blame] | 98 | // Single root version, not overridable. |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 99 | ALWAYS_INLINE void VisitRootIfNonNull(mirror::Object** root, const RootInfo& info) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 100 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 101 | if (*root != nullptr) { |
| 102 | VisitRoot(root, info); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | virtual void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 107 | SHARED_REQUIRES(Locks::mutator_lock_) = 0; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 108 | |
| 109 | virtual void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 110 | const RootInfo& info) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 111 | SHARED_REQUIRES(Locks::mutator_lock_) = 0; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | // Only visits roots one at a time, doesn't handle updating roots. Used when performance isn't |
| 115 | // critical. |
| 116 | class SingleRootVisitor : public RootVisitor { |
| 117 | private: |
| 118 | void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) OVERRIDE |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 119 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 120 | for (size_t i = 0; i < count; ++i) { |
| 121 | VisitRoot(*roots[i], info); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 126 | const RootInfo& info) OVERRIDE |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 127 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 128 | for (size_t i = 0; i < count; ++i) { |
| 129 | VisitRoot(roots[i]->AsMirrorPtr(), info); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | virtual void VisitRoot(mirror::Object* root, const RootInfo& info) = 0; |
| 134 | }; |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 135 | |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 136 | class GcRootSource { |
| 137 | public: |
| 138 | GcRootSource() |
| 139 | : field_(nullptr), method_(nullptr) { |
| 140 | } |
| 141 | explicit GcRootSource(ArtField* field) |
| 142 | : field_(field), method_(nullptr) { |
| 143 | } |
| 144 | explicit GcRootSource(ArtMethod* method) |
| 145 | : field_(nullptr), method_(method) { |
| 146 | } |
| 147 | ArtField* GetArtField() const { |
| 148 | return field_; |
| 149 | } |
| 150 | ArtMethod* GetArtMethod() const { |
| 151 | return method_; |
| 152 | } |
| 153 | bool HasArtField() const { |
| 154 | return field_ != nullptr; |
| 155 | } |
| 156 | bool HasArtMethod() const { |
| 157 | return method_ != nullptr; |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | ArtField* const field_; |
| 162 | ArtMethod* const method_; |
| 163 | |
| 164 | DISALLOW_COPY_AND_ASSIGN(GcRootSource); |
| 165 | }; |
| 166 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 167 | template<class MirrorType> |
Hiroshi Yamauchi | 9e47bfa | 2015-02-23 11:14:40 -0800 | [diff] [blame] | 168 | class GcRoot { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 169 | public: |
| 170 | template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 171 | ALWAYS_INLINE MirrorType* Read(GcRootSource* gc_root_source = nullptr) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 172 | SHARED_REQUIRES(Locks::mutator_lock_); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 173 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 174 | void VisitRoot(RootVisitor* visitor, const RootInfo& info) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 175 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 176 | DCHECK(!IsNull()); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 177 | mirror::CompressedReference<mirror::Object>* roots[1] = { &root_ }; |
| 178 | visitor->VisitRoots(roots, 1u, info); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 179 | DCHECK(!IsNull()); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 182 | void VisitRootIfNonNull(RootVisitor* visitor, const RootInfo& info) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 183 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 184 | if (!IsNull()) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 185 | VisitRoot(visitor, info); |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 189 | ALWAYS_INLINE mirror::CompressedReference<mirror::Object>* AddressWithoutBarrier() { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 190 | return &root_; |
| 191 | } |
| 192 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 193 | ALWAYS_INLINE bool IsNull() const { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 194 | // It's safe to null-check it without a read barrier. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 195 | return root_.IsNull(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 198 | ALWAYS_INLINE GcRoot(MirrorType* ref = nullptr) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 199 | |
| 200 | private: |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 201 | // Root visitors take pointers to root_ and place them in CompressedReference** arrays. We use a |
Mathieu Chartier | 9086b65 | 2015-04-14 09:35:18 -0700 | [diff] [blame] | 202 | // CompressedReference<mirror::Object> here since it violates strict aliasing requirements to |
| 203 | // cast CompressedReference<MirrorType>* to CompressedReference<mirror::Object>*. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 204 | mutable mirror::CompressedReference<mirror::Object> root_; |
| 205 | |
| 206 | template <size_t kBufferSize> friend class BufferedRootVisitor; |
| 207 | }; |
| 208 | |
| 209 | // Simple data structure for buffered root visiting to avoid virtual dispatch overhead. Currently |
| 210 | // only for CompressedReferences since these are more common than the Object** roots which are only |
| 211 | // for thread local roots. |
| 212 | template <size_t kBufferSize> |
| 213 | class BufferedRootVisitor { |
| 214 | public: |
| 215 | BufferedRootVisitor(RootVisitor* visitor, const RootInfo& root_info) |
| 216 | : visitor_(visitor), root_info_(root_info), buffer_pos_(0) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 219 | ~BufferedRootVisitor() { |
| 220 | Flush(); |
| 221 | } |
| 222 | |
| 223 | template <class MirrorType> |
| 224 | ALWAYS_INLINE void VisitRootIfNonNull(GcRoot<MirrorType>& root) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 225 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 226 | if (!root.IsNull()) { |
| 227 | VisitRoot(root); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | template <class MirrorType> |
| 232 | ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<MirrorType>* root) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 233 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 234 | if (!root->IsNull()) { |
| 235 | VisitRoot(root); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | template <class MirrorType> |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 240 | void VisitRoot(GcRoot<MirrorType>& root) SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 241 | VisitRoot(root.AddressWithoutBarrier()); |
| 242 | } |
| 243 | |
| 244 | template <class MirrorType> |
| 245 | void VisitRoot(mirror::CompressedReference<MirrorType>* root) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 246 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 247 | if (UNLIKELY(buffer_pos_ >= kBufferSize)) { |
| 248 | Flush(); |
| 249 | } |
| 250 | roots_[buffer_pos_++] = root; |
| 251 | } |
| 252 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 253 | void Flush() SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 254 | visitor_->VisitRoots(roots_, buffer_pos_, root_info_); |
| 255 | buffer_pos_ = 0; |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | private: |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 259 | RootVisitor* const visitor_; |
| 260 | RootInfo root_info_; |
| 261 | mirror::CompressedReference<mirror::Object>* roots_[kBufferSize]; |
| 262 | size_t buffer_pos_; |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | } // namespace art |
| 266 | |
| 267 | #endif // ART_RUNTIME_GC_ROOT_H_ |