blob: d3641d196f42b95ab3e1ad15ed01c3cea6821f1e [file] [log] [blame]
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -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#include "reference_processor.h"
18
19#include "mirror/object-inl.h"
Fred Shih4ee7a662014-07-11 09:59:27 -070020#include "mirror/reference.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070021#include "mirror/reference-inl.h"
Fred Shih4ee7a662014-07-11 09:59:27 -070022#include "reference_processor-inl.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070023#include "reflection.h"
24#include "ScopedLocalRef.h"
25#include "scoped_thread_state_change.h"
26#include "well_known_classes.h"
27
28namespace art {
29namespace gc {
30
31ReferenceProcessor::ReferenceProcessor()
Fred Shih4ee7a662014-07-11 09:59:27 -070032 : process_references_args_(nullptr, nullptr, nullptr),
Mathieu Chartier2d1ab0a2014-05-08 15:27:31 -070033 preserving_references_(false), lock_("reference processor lock", kReferenceProcessorLock),
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070034 condition_("reference processor condition", lock_) {
35}
36
37void ReferenceProcessor::EnableSlowPath() {
Fred Shih4ee7a662014-07-11 09:59:27 -070038 mirror::Reference::GetJavaLangRefReference()->SetSlowPath(true);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070039}
40
41void ReferenceProcessor::DisableSlowPath(Thread* self) {
Fred Shih4ee7a662014-07-11 09:59:27 -070042 mirror::Reference::GetJavaLangRefReference()->SetSlowPath(false);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070043 condition_.Broadcast(self);
44}
45
46mirror::Object* ReferenceProcessor::GetReferent(Thread* self, mirror::Reference* reference) {
47 mirror::Object* const referent = reference->GetReferent();
Mathieu Chartier308351a2014-06-15 12:39:02 -070048 // If the referent is null then it is already cleared, we can just return null since there is no
49 // scenario where it becomes non-null during the reference processing phase.
Fred Shih4ee7a662014-07-11 09:59:27 -070050 if (UNLIKELY(!SlowPathEnabled()) || referent == nullptr) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070051 return referent;
52 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070053 MutexLock mu(self, lock_);
Fred Shih4ee7a662014-07-11 09:59:27 -070054 while (SlowPathEnabled()) {
Mathieu Chartier308351a2014-06-15 12:39:02 -070055 mirror::HeapReference<mirror::Object>* const referent_addr =
56 reference->GetReferentReferenceAddr();
57 // If the referent became cleared, return it. Don't need barrier since thread roots can't get
58 // updated until after we leave the function due to holding the mutator lock.
59 if (referent_addr->AsMirrorPtr() == nullptr) {
Mathieu Chartier2175f522014-05-09 11:01:06 -070060 return nullptr;
61 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070062 // Try to see if the referent is already marked by using the is_marked_callback. We can return
Mathieu Chartier308351a2014-06-15 12:39:02 -070063 // it to the mutator as long as the GC is not preserving references.
64 IsHeapReferenceMarkedCallback* const is_marked_callback =
65 process_references_args_.is_marked_callback_;
Fred Shih530e1b52014-06-09 15:19:54 -070066 if (LIKELY(is_marked_callback != nullptr)) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070067 // If it's null it means not marked, but it could become marked if the referent is reachable
Fred Shih530e1b52014-06-09 15:19:54 -070068 // by finalizer referents. So we can not return in this case and must block. Otherwise, we
69 // can return it to the mutator as long as the GC is not preserving references, in which
70 // case only black nodes can be safely returned. If the GC is preserving references, the
71 // mutator could take a white field from a grey or white node and move it somewhere else
72 // in the heap causing corruption since this field would get swept.
Mathieu Chartier308351a2014-06-15 12:39:02 -070073 if (is_marked_callback(referent_addr, process_references_args_.arg_)) {
Fred Shih530e1b52014-06-09 15:19:54 -070074 if (!preserving_references_ ||
75 (LIKELY(!reference->IsFinalizerReferenceInstance()) && !reference->IsEnqueued())) {
Mathieu Chartier308351a2014-06-15 12:39:02 -070076 return referent_addr->AsMirrorPtr();
Fred Shih530e1b52014-06-09 15:19:54 -070077 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070078 }
79 }
Mathieu Chartier2d1ab0a2014-05-08 15:27:31 -070080 condition_.WaitHoldingLocks(self);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070081 }
82 return reference->GetReferent();
83}
84
Mathieu Chartier308351a2014-06-15 12:39:02 -070085bool ReferenceProcessor::PreserveSoftReferenceCallback(mirror::HeapReference<mirror::Object>* obj,
86 void* arg) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070087 auto* const args = reinterpret_cast<ProcessReferencesArgs*>(arg);
Mathieu Chartier308351a2014-06-15 12:39:02 -070088 // TODO: Add smarter logic for preserving soft references.
89 mirror::Object* new_obj = args->mark_callback_(obj->AsMirrorPtr(), args->arg_);
90 DCHECK(new_obj != nullptr);
91 obj->Assign(new_obj);
92 return true;
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070093}
94
95void ReferenceProcessor::StartPreservingReferences(Thread* self) {
96 MutexLock mu(self, lock_);
97 preserving_references_ = true;
98}
99
100void ReferenceProcessor::StopPreservingReferences(Thread* self) {
101 MutexLock mu(self, lock_);
102 preserving_references_ = false;
103 // We are done preserving references, some people who are blocked may see a marked referent.
104 condition_.Broadcast(self);
105}
106
107// Process reference class instances and schedule finalizations.
108void ReferenceProcessor::ProcessReferences(bool concurrent, TimingLogger* timings,
109 bool clear_soft_references,
Mathieu Chartier308351a2014-06-15 12:39:02 -0700110 IsHeapReferenceMarkedCallback* is_marked_callback,
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700111 MarkObjectCallback* mark_object_callback,
112 ProcessMarkStackCallback* process_mark_stack_callback,
113 void* arg) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700114 TimingLogger::ScopedTiming t(concurrent ? __FUNCTION__ : "(Paused)ProcessReferences", timings);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700115 Thread* self = Thread::Current();
116 {
117 MutexLock mu(self, lock_);
118 process_references_args_.is_marked_callback_ = is_marked_callback;
119 process_references_args_.mark_callback_ = mark_object_callback;
120 process_references_args_.arg_ = arg;
Fred Shih4ee7a662014-07-11 09:59:27 -0700121 CHECK_EQ(SlowPathEnabled(), concurrent) << "Slow path must be enabled iff concurrent";
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700122 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700123 // Unless required to clear soft references with white references, preserve some white referents.
124 if (!clear_soft_references) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700125 TimingLogger::ScopedTiming split(concurrent ? "ForwardSoftReferences" :
Fred Shih530e1b52014-06-09 15:19:54 -0700126 "(Paused)ForwardSoftReferences", timings);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700127 if (concurrent) {
128 StartPreservingReferences(self);
129 }
Fred Shih530e1b52014-06-09 15:19:54 -0700130
131 soft_reference_queue_.ForwardSoftReferences(&PreserveSoftReferenceCallback,
132 &process_references_args_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700133 process_mark_stack_callback(arg);
134 if (concurrent) {
135 StopPreservingReferences(self);
136 }
137 }
138 // Clear all remaining soft and weak references with white referents.
Mathieu Chartier308351a2014-06-15 12:39:02 -0700139 soft_reference_queue_.ClearWhiteReferences(&cleared_references_, is_marked_callback, arg);
140 weak_reference_queue_.ClearWhiteReferences(&cleared_references_, is_marked_callback, arg);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700141 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700142 TimingLogger::ScopedTiming t(concurrent ? "EnqueueFinalizerReferences" :
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700143 "(Paused)EnqueueFinalizerReferences", timings);
144 if (concurrent) {
145 StartPreservingReferences(self);
146 }
147 // Preserve all white objects with finalize methods and schedule them for finalization.
Mathieu Chartier308351a2014-06-15 12:39:02 -0700148 finalizer_reference_queue_.EnqueueFinalizerReferences(&cleared_references_, is_marked_callback,
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700149 mark_object_callback, arg);
150 process_mark_stack_callback(arg);
151 if (concurrent) {
152 StopPreservingReferences(self);
153 }
154 }
155 // Clear all finalizer referent reachable soft and weak references with white referents.
Mathieu Chartier308351a2014-06-15 12:39:02 -0700156 soft_reference_queue_.ClearWhiteReferences(&cleared_references_, is_marked_callback, arg);
157 weak_reference_queue_.ClearWhiteReferences(&cleared_references_, is_marked_callback, arg);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700158 // Clear all phantom references with white referents.
Mathieu Chartier308351a2014-06-15 12:39:02 -0700159 phantom_reference_queue_.ClearWhiteReferences(&cleared_references_, is_marked_callback, arg);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700160 // At this point all reference queues other than the cleared references should be empty.
161 DCHECK(soft_reference_queue_.IsEmpty());
162 DCHECK(weak_reference_queue_.IsEmpty());
163 DCHECK(finalizer_reference_queue_.IsEmpty());
164 DCHECK(phantom_reference_queue_.IsEmpty());
Mathieu Chartier2175f522014-05-09 11:01:06 -0700165 {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700166 MutexLock mu(self, lock_);
Mathieu Chartier2175f522014-05-09 11:01:06 -0700167 // Need to always do this since the next GC may be concurrent. Doing this for only concurrent
168 // could result in a stale is_marked_callback_ being called before the reference processing
169 // starts since there is a small window of time where slow_path_enabled_ is enabled but the
170 // callback isn't yet set.
171 process_references_args_.is_marked_callback_ = nullptr;
172 if (concurrent) {
173 // Done processing, disable the slow path and broadcast to the waiters.
174 DisableSlowPath(self);
175 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700176 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700177}
178
179// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
180// marked, put it on the appropriate list in the heap for later processing.
181void ReferenceProcessor::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* ref,
Mathieu Chartier308351a2014-06-15 12:39:02 -0700182 IsHeapReferenceMarkedCallback* is_marked_callback,
183 void* arg) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700184 // klass can be the class of the old object if the visitor already updated the class of ref.
Mathieu Chartier308351a2014-06-15 12:39:02 -0700185 DCHECK(klass != nullptr);
Fred Shih4ee7a662014-07-11 09:59:27 -0700186 DCHECK(klass->IsTypeOfReferenceClass());
Mathieu Chartier308351a2014-06-15 12:39:02 -0700187 mirror::HeapReference<mirror::Object>* referent = ref->GetReferentReferenceAddr();
188 if (referent->AsMirrorPtr() != nullptr && !is_marked_callback(referent, arg)) {
189 Thread* self = Thread::Current();
190 // TODO: Remove these locks, and use atomic stacks for storing references?
191 // We need to check that the references haven't already been enqueued since we can end up
192 // scanning the same reference multiple times due to dirty cards.
193 if (klass->IsSoftReferenceClass()) {
194 soft_reference_queue_.AtomicEnqueueIfNotEnqueued(self, ref);
195 } else if (klass->IsWeakReferenceClass()) {
196 weak_reference_queue_.AtomicEnqueueIfNotEnqueued(self, ref);
197 } else if (klass->IsFinalizerReferenceClass()) {
198 finalizer_reference_queue_.AtomicEnqueueIfNotEnqueued(self, ref);
199 } else if (klass->IsPhantomReferenceClass()) {
200 phantom_reference_queue_.AtomicEnqueueIfNotEnqueued(self, ref);
201 } else {
202 LOG(FATAL) << "Invalid reference type " << PrettyClass(klass) << " " << std::hex
203 << klass->GetAccessFlags();
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700204 }
205 }
206}
207
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700208void ReferenceProcessor::UpdateRoots(IsMarkedCallback* callback, void* arg) {
209 cleared_references_.UpdateRoots(callback, arg);
210}
211
Mathieu Chartier308351a2014-06-15 12:39:02 -0700212void ReferenceProcessor::EnqueueClearedReferences(Thread* self) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700213 Locks::mutator_lock_->AssertNotHeld(self);
214 if (!cleared_references_.IsEmpty()) {
215 // When a runtime isn't started there are no reference queues to care about so ignore.
216 if (LIKELY(Runtime::Current()->IsStarted())) {
217 ScopedObjectAccess soa(self);
218 ScopedLocalRef<jobject> arg(self->GetJniEnv(),
219 soa.AddLocalReference<jobject>(cleared_references_.GetList()));
220 jvalue args[1];
221 args[0].l = arg.get();
222 InvokeWithJValues(soa, nullptr, WellKnownClasses::java_lang_ref_ReferenceQueue_add, args);
223 }
224 cleared_references_.Clear();
225 }
226}
227
228} // namespace gc
229} // namespace art