blob: 0d3c93b83da79d285a523bfde3475417857660e0 [file] [log] [blame]
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -07001/*
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 Chartierbad02672014-08-25 13:08:22 -070020#include "base/macros.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070021#include "base/mutex.h" // For Locks::mutator_lock_.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070022#include "mirror/object_reference.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070023
24namespace art {
25
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080026namespace mirror {
27class Object;
28} // namespace mirror
29
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070030template <size_t kBufferSize>
31class BufferedRootVisitor;
32
Mathieu Chartier4809d0a2015-04-07 10:39:04 -070033// Dependent on pointer size so that we don't have frames that are too big on 64 bit.
34static const size_t kDefaultBufferedRootCount = 1024 / sizeof(void*);
35
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080036enum RootType {
37 kRootUnknown = 0,
38 kRootJNIGlobal,
39 kRootJNILocal,
40 kRootJavaFrame,
41 kRootNativeStack,
42 kRootStickyClass,
43 kRootThreadBlock,
44 kRootMonitorUsed,
45 kRootThreadObject,
46 kRootInternedString,
47 kRootDebugger,
48 kRootVMInternal,
49 kRootJNIMonitor,
50};
51std::ostream& operator<<(std::ostream& os, const RootType& root_type);
52
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070053// Only used by hprof. tid and root_type are only used by hprof.
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080054class RootInfo {
55 public:
56 // Thread id 0 is for non thread roots.
57 explicit RootInfo(RootType type, uint32_t thread_id = 0)
58 : type_(type), thread_id_(thread_id) {
59 }
Andreas Gampe758a8012015-04-03 21:28:42 -070060 RootInfo(const RootInfo&) = default;
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080061 virtual ~RootInfo() {
62 }
63 RootType GetType() const {
64 return type_;
65 }
66 uint32_t GetThreadId() const {
67 return thread_id_;
68 }
69 virtual void Describe(std::ostream& os) const {
70 os << "Type=" << type_ << " thread_id=" << thread_id_;
71 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070072 std::string ToString() const;
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080073
74 private:
75 const RootType type_;
76 const uint32_t thread_id_;
77};
78
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070079inline std::ostream& operator<<(std::ostream& os, const RootInfo& root_info) {
80 root_info.Describe(os);
81 return os;
82}
83
84class RootVisitor {
85 public:
86 virtual ~RootVisitor() { }
87
88 // Single root versions, not overridable.
89 ALWAYS_INLINE void VisitRoot(mirror::Object** roots, const RootInfo& info)
90 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
91 VisitRoots(&roots, 1, info);
92 }
93
94 ALWAYS_INLINE void VisitRootIfNonNull(mirror::Object** roots, const RootInfo& info)
95 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
96 if (*roots != nullptr) {
97 VisitRoot(roots, info);
98 }
99 }
100
101 virtual void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info)
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
103
104 virtual void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
105 const RootInfo& info)
106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
107};
108
109// Only visits roots one at a time, doesn't handle updating roots. Used when performance isn't
110// critical.
111class SingleRootVisitor : public RootVisitor {
112 private:
113 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) OVERRIDE
114 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
115 for (size_t i = 0; i < count; ++i) {
116 VisitRoot(*roots[i], info);
117 }
118 }
119
120 void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
121 const RootInfo& info) OVERRIDE
122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
123 for (size_t i = 0; i < count; ++i) {
124 VisitRoot(roots[i]->AsMirrorPtr(), info);
125 }
126 }
127
128 virtual void VisitRoot(mirror::Object* root, const RootInfo& info) = 0;
129};
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800130
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700131template<class MirrorType>
Hiroshi Yamauchi9e47bfa2015-02-23 11:14:40 -0800132class GcRoot {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700133 public:
134 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800135 ALWAYS_INLINE MirrorType* Read() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700136
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700137 void VisitRoot(RootVisitor* visitor, const RootInfo& info) const
138 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700139 DCHECK(!IsNull());
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700140 mirror::CompressedReference<mirror::Object>* roots[1] = { &root_ };
141 visitor->VisitRoots(roots, 1u, info);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700142 DCHECK(!IsNull());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700143 }
144
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700145 void VisitRootIfNonNull(RootVisitor* visitor, const RootInfo& info) const
146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800147 if (!IsNull()) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700148 VisitRoot(visitor, info);
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800149 }
150 }
151
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700152 ALWAYS_INLINE mirror::CompressedReference<mirror::Object>* AddressWithoutBarrier() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700153 return &root_;
154 }
155
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700156 ALWAYS_INLINE bool IsNull() const {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700157 // It's safe to null-check it without a read barrier.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700158 return root_.IsNull();
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700159 }
160
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700161 ALWAYS_INLINE GcRoot(MirrorType* ref = nullptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
162
163 private:
164 mutable mirror::CompressedReference<mirror::Object> root_;
165
166 template <size_t kBufferSize> friend class BufferedRootVisitor;
167};
168
169// Simple data structure for buffered root visiting to avoid virtual dispatch overhead. Currently
170// only for CompressedReferences since these are more common than the Object** roots which are only
171// for thread local roots.
172template <size_t kBufferSize>
173class BufferedRootVisitor {
174 public:
175 BufferedRootVisitor(RootVisitor* visitor, const RootInfo& root_info)
176 : visitor_(visitor), root_info_(root_info), buffer_pos_(0) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700177 }
178
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700179 ~BufferedRootVisitor() {
180 Flush();
181 }
182
183 template <class MirrorType>
184 ALWAYS_INLINE void VisitRootIfNonNull(GcRoot<MirrorType>& root)
185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
186 if (!root.IsNull()) {
187 VisitRoot(root);
188 }
189 }
190
191 template <class MirrorType>
192 ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<MirrorType>* root)
193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
194 if (!root->IsNull()) {
195 VisitRoot(root);
196 }
197 }
198
199 template <class MirrorType>
200 void VisitRoot(GcRoot<MirrorType>& root) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
201 VisitRoot(root.AddressWithoutBarrier());
202 }
203
204 template <class MirrorType>
205 void VisitRoot(mirror::CompressedReference<MirrorType>* root)
206 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
207 if (UNLIKELY(buffer_pos_ >= kBufferSize)) {
208 Flush();
209 }
210 roots_[buffer_pos_++] = root;
211 }
212
213 void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
214 visitor_->VisitRoots(roots_, buffer_pos_, root_info_);
215 buffer_pos_ = 0;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700216 }
217
218 private:
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700219 RootVisitor* const visitor_;
220 RootInfo root_info_;
221 mirror::CompressedReference<mirror::Object>* roots_[kBufferSize];
222 size_t buffer_pos_;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700223};
224
225} // namespace art
226
227#endif // ART_RUNTIME_GC_ROOT_H_