blob: 4164bbddc2e98b796b8ce0d8a0c44b9277b4005d [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 Chartiere34fa1d2015-01-14 14:55:47 -080033enum 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};
48std::ostream& operator<<(std::ostream& os, const RootType& root_type);
49
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070050// Only used by hprof. tid and root_type are only used by hprof.
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080051class 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 Chartierbb87e0f2015-04-03 11:21:55 -070068 std::string ToString() const;
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080069
70 private:
71 const RootType type_;
72 const uint32_t thread_id_;
73};
74
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070075inline std::ostream& operator<<(std::ostream& os, const RootInfo& root_info) {
76 root_info.Describe(os);
77 return os;
78}
79
80class 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.
107class 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 Chartiere34fa1d2015-01-14 14:55:47 -0800126
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700127template<class MirrorType>
Hiroshi Yamauchi9e47bfa2015-02-23 11:14:40 -0800128class GcRoot {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700129 public:
130 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800131 ALWAYS_INLINE MirrorType* Read() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700132
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700133 void VisitRoot(RootVisitor* visitor, const RootInfo& info) const
134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700135 DCHECK(!IsNull());
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700136 mirror::CompressedReference<mirror::Object>* roots[1] = { &root_ };
137 visitor->VisitRoots(roots, 1u, info);
Mathieu Chartiereb175f72014-10-31 11:49:27 -0700138 DCHECK(!IsNull());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700139 }
140
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700141 void VisitRootIfNonNull(RootVisitor* visitor, const RootInfo& info) const
142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800143 if (!IsNull()) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700144 VisitRoot(visitor, info);
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800145 }
146 }
147
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700148 ALWAYS_INLINE mirror::CompressedReference<mirror::Object>* AddressWithoutBarrier() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700149 return &root_;
150 }
151
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700152 ALWAYS_INLINE bool IsNull() const {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700153 // It's safe to null-check it without a read barrier.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700154 return root_.IsNull();
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700155 }
156
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700157 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.
168template <size_t kBufferSize>
169class BufferedRootVisitor {
170 public:
171 BufferedRootVisitor(RootVisitor* visitor, const RootInfo& root_info)
172 : visitor_(visitor), root_info_(root_info), buffer_pos_(0) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700173 }
174
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700175 ~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 Yamauchi94f7b492014-07-22 18:08:23 -0700212 }
213
214 private:
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700215 RootVisitor* const visitor_;
216 RootInfo root_info_;
217 mirror::CompressedReference<mirror::Object>* roots_[kBufferSize];
218 size_t buffer_pos_;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700219};
220
221} // namespace art
222
223#endif // ART_RUNTIME_GC_ROOT_H_