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