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 { |
| 25 | |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 26 | namespace mirror { |
| 27 | class Object; |
| 28 | } // namespace mirror |
| 29 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 30 | template <size_t kBufferSize> |
| 31 | class BufferedRootVisitor; |
| 32 | |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 33 | enum RootType { |
| 34 | kRootUnknown = 0, |
| 35 | kRootJNIGlobal, |
| 36 | kRootJNILocal, |
| 37 | kRootJavaFrame, |
| 38 | kRootNativeStack, |
| 39 | kRootStickyClass, |
| 40 | kRootThreadBlock, |
| 41 | kRootMonitorUsed, |
| 42 | kRootThreadObject, |
| 43 | kRootInternedString, |
| 44 | kRootDebugger, |
| 45 | kRootVMInternal, |
| 46 | kRootJNIMonitor, |
| 47 | }; |
| 48 | std::ostream& operator<<(std::ostream& os, const RootType& root_type); |
| 49 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 50 | // Only used by hprof. tid and root_type are only used by hprof. |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 51 | class RootInfo { |
| 52 | public: |
| 53 | // Thread id 0 is for non thread roots. |
| 54 | explicit RootInfo(RootType type, uint32_t thread_id = 0) |
| 55 | : type_(type), thread_id_(thread_id) { |
| 56 | } |
| 57 | virtual ~RootInfo() { |
| 58 | } |
| 59 | RootType GetType() const { |
| 60 | return type_; |
| 61 | } |
| 62 | uint32_t GetThreadId() const { |
| 63 | return thread_id_; |
| 64 | } |
| 65 | virtual void Describe(std::ostream& os) const { |
| 66 | os << "Type=" << type_ << " thread_id=" << thread_id_; |
| 67 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 68 | std::string ToString() const; |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | const RootType type_; |
| 72 | const uint32_t thread_id_; |
| 73 | }; |
| 74 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 75 | inline std::ostream& operator<<(std::ostream& os, const RootInfo& root_info) { |
| 76 | root_info.Describe(os); |
| 77 | return os; |
| 78 | } |
| 79 | |
| 80 | class RootVisitor { |
| 81 | public: |
| 82 | virtual ~RootVisitor() { } |
| 83 | |
| 84 | // Single root versions, not overridable. |
| 85 | ALWAYS_INLINE void VisitRoot(mirror::Object** roots, const RootInfo& info) |
| 86 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 87 | VisitRoots(&roots, 1, info); |
| 88 | } |
| 89 | |
| 90 | ALWAYS_INLINE void VisitRootIfNonNull(mirror::Object** roots, const RootInfo& info) |
| 91 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 92 | if (*roots != nullptr) { |
| 93 | VisitRoot(roots, info); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | virtual void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) |
| 98 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0; |
| 99 | |
| 100 | virtual void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 101 | const RootInfo& info) |
| 102 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0; |
| 103 | }; |
| 104 | |
| 105 | // Only visits roots one at a time, doesn't handle updating roots. Used when performance isn't |
| 106 | // critical. |
| 107 | class SingleRootVisitor : public RootVisitor { |
| 108 | private: |
| 109 | void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) OVERRIDE |
| 110 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 111 | for (size_t i = 0; i < count; ++i) { |
| 112 | VisitRoot(*roots[i], info); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 117 | const RootInfo& info) OVERRIDE |
| 118 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 119 | for (size_t i = 0; i < count; ++i) { |
| 120 | VisitRoot(roots[i]->AsMirrorPtr(), info); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | virtual void VisitRoot(mirror::Object* root, const RootInfo& info) = 0; |
| 125 | }; |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 126 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 127 | template<class MirrorType> |
Hiroshi Yamauchi | 9e47bfa | 2015-02-23 11:14:40 -0800 | [diff] [blame] | 128 | class GcRoot { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 129 | public: |
| 130 | template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 131 | ALWAYS_INLINE MirrorType* Read() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 132 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 133 | void VisitRoot(RootVisitor* visitor, const RootInfo& info) const |
| 134 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 135 | DCHECK(!IsNull()); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 136 | mirror::CompressedReference<mirror::Object>* roots[1] = { &root_ }; |
| 137 | visitor->VisitRoots(roots, 1u, info); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 138 | DCHECK(!IsNull()); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 141 | void VisitRootIfNonNull(RootVisitor* visitor, const RootInfo& info) const |
| 142 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 143 | if (!IsNull()) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 144 | VisitRoot(visitor, info); |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 148 | ALWAYS_INLINE mirror::CompressedReference<mirror::Object>* AddressWithoutBarrier() { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 149 | return &root_; |
| 150 | } |
| 151 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 152 | ALWAYS_INLINE bool IsNull() const { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 153 | // It's safe to null-check it without a read barrier. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 154 | return root_.IsNull(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 157 | ALWAYS_INLINE GcRoot(MirrorType* ref = nullptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 158 | |
| 159 | private: |
| 160 | mutable mirror::CompressedReference<mirror::Object> root_; |
| 161 | |
| 162 | template <size_t kBufferSize> friend class BufferedRootVisitor; |
| 163 | }; |
| 164 | |
| 165 | // Simple data structure for buffered root visiting to avoid virtual dispatch overhead. Currently |
| 166 | // only for CompressedReferences since these are more common than the Object** roots which are only |
| 167 | // for thread local roots. |
| 168 | template <size_t kBufferSize> |
| 169 | class BufferedRootVisitor { |
| 170 | public: |
| 171 | BufferedRootVisitor(RootVisitor* visitor, const RootInfo& root_info) |
| 172 | : visitor_(visitor), root_info_(root_info), buffer_pos_(0) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 175 | ~BufferedRootVisitor() { |
| 176 | Flush(); |
| 177 | } |
| 178 | |
| 179 | template <class MirrorType> |
| 180 | ALWAYS_INLINE void VisitRootIfNonNull(GcRoot<MirrorType>& root) |
| 181 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 182 | if (!root.IsNull()) { |
| 183 | VisitRoot(root); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | template <class MirrorType> |
| 188 | ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<MirrorType>* root) |
| 189 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 190 | if (!root->IsNull()) { |
| 191 | VisitRoot(root); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | template <class MirrorType> |
| 196 | void VisitRoot(GcRoot<MirrorType>& root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 197 | VisitRoot(root.AddressWithoutBarrier()); |
| 198 | } |
| 199 | |
| 200 | template <class MirrorType> |
| 201 | void VisitRoot(mirror::CompressedReference<MirrorType>* root) |
| 202 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 203 | if (UNLIKELY(buffer_pos_ >= kBufferSize)) { |
| 204 | Flush(); |
| 205 | } |
| 206 | roots_[buffer_pos_++] = root; |
| 207 | } |
| 208 | |
| 209 | void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 210 | visitor_->VisitRoots(roots_, buffer_pos_, root_info_); |
| 211 | buffer_pos_ = 0; |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | private: |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame^] | 215 | RootVisitor* const visitor_; |
| 216 | RootInfo root_info_; |
| 217 | mirror::CompressedReference<mirror::Object>* roots_[kBufferSize]; |
| 218 | size_t buffer_pos_; |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | } // namespace art |
| 222 | |
| 223 | #endif // ART_RUNTIME_GC_ROOT_H_ |