1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ART_RUNTIME_GC_ROOT_H_
#define ART_RUNTIME_GC_ROOT_H_
#include "base/locks.h" // For Locks::mutator_lock_.
#include "base/macros.h"
#include "mirror/object_reference.h"
#include "read_barrier_option.h"
namespace art {
class ArtField;
class ArtMethod;
template<class MirrorType> class ObjPtr;
namespace mirror {
class Object;
} // namespace mirror
template <size_t kBufferSize>
class BufferedRootVisitor;
// Dependent on pointer size so that we don't have frames that are too big on 64 bit.
static const size_t kDefaultBufferedRootCount = 1024 / sizeof(void*);
enum RootType {
kRootUnknown = 0,
kRootJNIGlobal,
kRootJNILocal,
kRootJavaFrame,
kRootNativeStack,
kRootStickyClass,
kRootThreadBlock,
kRootMonitorUsed,
kRootThreadObject,
kRootInternedString,
kRootFinalizing, // used for HPROF's conversion to HprofHeapTag
kRootDebugger,
kRootReferenceCleanup, // used for HPROF's conversion to HprofHeapTag
kRootVMInternal,
kRootJNIMonitor,
};
std::ostream& operator<<(std::ostream& os, const RootType& root_type);
// Only used by hprof. thread_id_ and type_ are only used by hprof.
class RootInfo {
public:
// Thread id 0 is for non thread roots.
explicit RootInfo(RootType type, uint32_t thread_id = 0)
: type_(type), thread_id_(thread_id) {
}
RootInfo(const RootInfo&) = default;
virtual ~RootInfo() {
}
RootType GetType() const {
return type_;
}
uint32_t GetThreadId() const {
return thread_id_;
}
virtual void Describe(std::ostream& os) const {
os << "Type=" << type_ << " thread_id=" << thread_id_;
}
std::string ToString() const;
private:
const RootType type_;
const uint32_t thread_id_;
};
inline std::ostream& operator<<(std::ostream& os, const RootInfo& root_info) {
root_info.Describe(os);
return os;
}
// Not all combinations of flags are valid. You may not visit all roots as well as the new roots
// (no logical reason to do this). You also may not start logging new roots and stop logging new
// roots (also no logical reason to do this).
//
// The precise flag ensures that more metadata is supplied. An example is vreg data for compiled
// method frames.
enum VisitRootFlags : uint8_t {
kVisitRootFlagAllRoots = 0x1,
kVisitRootFlagNewRoots = 0x2,
kVisitRootFlagStartLoggingNewRoots = 0x4,
kVisitRootFlagStopLoggingNewRoots = 0x8,
kVisitRootFlagClearRootLog = 0x10,
kVisitRootFlagClassLoader = 0x20,
kVisitRootFlagPrecise = 0x80,
};
class RootVisitor {
public:
virtual ~RootVisitor() { }
// Single root version, not overridable.
ALWAYS_INLINE void VisitRoot(mirror::Object** root, const RootInfo& info)
REQUIRES_SHARED(Locks::mutator_lock_) {
VisitRoots(&root, 1, info);
}
// Single root version, not overridable.
ALWAYS_INLINE void VisitRootIfNonNull(mirror::Object** root, const RootInfo& info)
REQUIRES_SHARED(Locks::mutator_lock_) {
if (*root != nullptr) {
VisitRoot(root, info);
}
}
virtual void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info)
REQUIRES_SHARED(Locks::mutator_lock_) = 0;
virtual void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
const RootInfo& info)
REQUIRES_SHARED(Locks::mutator_lock_) = 0;
};
// Only visits roots one at a time, doesn't handle updating roots. Used when performance isn't
// critical.
class SingleRootVisitor : public RootVisitor {
private:
void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) override
REQUIRES_SHARED(Locks::mutator_lock_) {
for (size_t i = 0; i < count; ++i) {
VisitRoot(*roots[i], info);
}
}
void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
const RootInfo& info) override
REQUIRES_SHARED(Locks::mutator_lock_) {
for (size_t i = 0; i < count; ++i) {
VisitRoot(roots[i]->AsMirrorPtr(), info);
}
}
virtual void VisitRoot(mirror::Object* root, const RootInfo& info) = 0;
};
class GcRootSource {
public:
GcRootSource()
: field_(nullptr), method_(nullptr) {
}
explicit GcRootSource(ArtField* field)
: field_(field), method_(nullptr) {
}
explicit GcRootSource(ArtMethod* method)
: field_(nullptr), method_(method) {
}
ArtField* GetArtField() const {
return field_;
}
ArtMethod* GetArtMethod() const {
return method_;
}
bool HasArtField() const {
return field_ != nullptr;
}
bool HasArtMethod() const {
return method_ != nullptr;
}
private:
ArtField* const field_;
ArtMethod* const method_;
DISALLOW_COPY_AND_ASSIGN(GcRootSource);
};
template<class MirrorType>
class GcRoot {
public:
template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
ALWAYS_INLINE MirrorType* Read(GcRootSource* gc_root_source = nullptr) const
REQUIRES_SHARED(Locks::mutator_lock_);
void VisitRoot(RootVisitor* visitor, const RootInfo& info) const
REQUIRES_SHARED(Locks::mutator_lock_) {
DCHECK(!IsNull());
mirror::CompressedReference<mirror::Object>* roots[1] = { &root_ };
visitor->VisitRoots(roots, 1u, info);
DCHECK(!IsNull());
}
void VisitRootIfNonNull(RootVisitor* visitor, const RootInfo& info) const
REQUIRES_SHARED(Locks::mutator_lock_) {
if (!IsNull()) {
VisitRoot(visitor, info);
}
}
ALWAYS_INLINE mirror::CompressedReference<mirror::Object>* AddressWithoutBarrier() {
return &root_;
}
ALWAYS_INLINE bool IsNull() const {
// It's safe to null-check it without a read barrier.
return root_.IsNull();
}
ALWAYS_INLINE GcRoot() {}
explicit ALWAYS_INLINE GcRoot(MirrorType* ref)
REQUIRES_SHARED(Locks::mutator_lock_);
explicit ALWAYS_INLINE GcRoot(ObjPtr<MirrorType> ref)
REQUIRES_SHARED(Locks::mutator_lock_);
private:
// Root visitors take pointers to root_ and place them in CompressedReference** arrays. We use a
// CompressedReference<mirror::Object> here since it violates strict aliasing requirements to
// cast CompressedReference<MirrorType>* to CompressedReference<mirror::Object>*.
mutable mirror::CompressedReference<mirror::Object> root_;
template <size_t kBufferSize> friend class BufferedRootVisitor;
};
// Simple data structure for buffered root visiting to avoid virtual dispatch overhead. Currently
// only for CompressedReferences since these are more common than the Object** roots which are only
// for thread local roots.
template <size_t kBufferSize>
class BufferedRootVisitor {
public:
BufferedRootVisitor(RootVisitor* visitor, const RootInfo& root_info)
: visitor_(visitor), root_info_(root_info), buffer_pos_(0) {
}
~BufferedRootVisitor() {
Flush();
}
template <class MirrorType>
ALWAYS_INLINE void VisitRootIfNonNull(GcRoot<MirrorType>& root)
REQUIRES_SHARED(Locks::mutator_lock_) {
if (!root.IsNull()) {
VisitRoot(root);
}
}
template <class MirrorType>
ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<MirrorType>* root)
REQUIRES_SHARED(Locks::mutator_lock_) {
if (!root->IsNull()) {
VisitRoot(root);
}
}
template <class MirrorType>
void VisitRoot(GcRoot<MirrorType>& root) REQUIRES_SHARED(Locks::mutator_lock_) {
VisitRoot(root.AddressWithoutBarrier());
}
template <class MirrorType>
void VisitRoot(mirror::CompressedReference<MirrorType>* root)
REQUIRES_SHARED(Locks::mutator_lock_) {
if (UNLIKELY(buffer_pos_ >= kBufferSize)) {
Flush();
}
roots_[buffer_pos_++] = root;
}
void Flush() REQUIRES_SHARED(Locks::mutator_lock_) {
visitor_->VisitRoots(roots_, buffer_pos_, root_info_);
buffer_pos_ = 0;
}
private:
RootVisitor* const visitor_;
RootInfo root_info_;
mirror::CompressedReference<mirror::Object>* roots_[kBufferSize];
size_t buffer_pos_;
};
class UnbufferedRootVisitor {
public:
UnbufferedRootVisitor(RootVisitor* visitor, const RootInfo& root_info)
: visitor_(visitor), root_info_(root_info) {}
template <class MirrorType>
ALWAYS_INLINE void VisitRootIfNonNull(GcRoot<MirrorType>& root) const
REQUIRES_SHARED(Locks::mutator_lock_) {
if (!root.IsNull()) {
VisitRoot(root);
}
}
template <class MirrorType>
ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<MirrorType>* root) const
REQUIRES_SHARED(Locks::mutator_lock_) {
if (!root->IsNull()) {
VisitRoot(root);
}
}
template <class MirrorType>
void VisitRoot(GcRoot<MirrorType>& root) const REQUIRES_SHARED(Locks::mutator_lock_) {
VisitRoot(root.AddressWithoutBarrier());
}
template <class MirrorType>
void VisitRoot(mirror::CompressedReference<MirrorType>* root) const
REQUIRES_SHARED(Locks::mutator_lock_) {
visitor_->VisitRoots(&root, 1, root_info_);
}
private:
RootVisitor* const visitor_;
RootInfo root_info_;
};
} // namespace art
#endif // ART_RUNTIME_GC_ROOT_H_
|