blob: 39ba7432a100093a709c40d771c1e2cd843e7c17 [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
Vladimir Marko80afd022015-05-19 18:08:00 +010019#include "base/time_utils.h"
Mathieu Chartier97509952015-07-13 14:35:43 -070020#include "collector/garbage_collector.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "mirror/class-inl.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070022#include "mirror/object-inl.h"
23#include "mirror/reference-inl.h"
Fred Shih4ee7a662014-07-11 09:59:27 -070024#include "reference_processor-inl.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070025#include "reflection.h"
26#include "ScopedLocalRef.h"
27#include "scoped_thread_state_change.h"
Mathieu Chartiera5eae692014-12-17 17:56:03 -080028#include "task_processor.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010029#include "utils.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070030#include "well_known_classes.h"
31
32namespace art {
33namespace gc {
34
Mathieu Chartiera5eae692014-12-17 17:56:03 -080035static constexpr bool kAsyncReferenceQueueAdd = false;
36
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070037ReferenceProcessor::ReferenceProcessor()
Mathieu Chartier97509952015-07-13 14:35:43 -070038 : collector_(nullptr),
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070039 preserving_references_(false),
40 condition_("reference processor condition", *Locks::reference_processor_lock_) ,
41 soft_reference_queue_(Locks::reference_queue_soft_references_lock_),
42 weak_reference_queue_(Locks::reference_queue_weak_references_lock_),
43 finalizer_reference_queue_(Locks::reference_queue_finalizer_references_lock_),
44 phantom_reference_queue_(Locks::reference_queue_phantom_references_lock_),
45 cleared_references_(Locks::reference_queue_cleared_references_lock_) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070046}
47
48void ReferenceProcessor::EnableSlowPath() {
Fred Shih4ee7a662014-07-11 09:59:27 -070049 mirror::Reference::GetJavaLangRefReference()->SetSlowPath(true);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070050}
51
52void ReferenceProcessor::DisableSlowPath(Thread* self) {
Fred Shih4ee7a662014-07-11 09:59:27 -070053 mirror::Reference::GetJavaLangRefReference()->SetSlowPath(false);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070054 condition_.Broadcast(self);
55}
56
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -070057void ReferenceProcessor::BroadcastForSlowPath(Thread* self) {
58 CHECK(kUseReadBarrier);
59 MutexLock mu(self, *Locks::reference_processor_lock_);
60 condition_.Broadcast(self);
61}
62
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070063mirror::Object* ReferenceProcessor::GetReferent(Thread* self, mirror::Reference* reference) {
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -070064 if (!kUseReadBarrier || self->GetWeakRefAccessEnabled()) {
65 // Under read barrier / concurrent copying collector, it's not safe to call GetReferent() when
66 // weak ref access is disabled as the call includes a read barrier which may push a ref onto the
67 // mark stack and interfere with termination of marking.
68 mirror::Object* const referent = reference->GetReferent();
69 // If the referent is null then it is already cleared, we can just return null since there is no
70 // scenario where it becomes non-null during the reference processing phase.
71 if (UNLIKELY(!SlowPathEnabled()) || referent == nullptr) {
72 return referent;
73 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070074 }
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070075 MutexLock mu(self, *Locks::reference_processor_lock_);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -070076 while ((!kUseReadBarrier && SlowPathEnabled()) ||
77 (kUseReadBarrier && !self->GetWeakRefAccessEnabled())) {
Mathieu Chartier308351a2014-06-15 12:39:02 -070078 mirror::HeapReference<mirror::Object>* const referent_addr =
79 reference->GetReferentReferenceAddr();
80 // If the referent became cleared, return it. Don't need barrier since thread roots can't get
81 // updated until after we leave the function due to holding the mutator lock.
82 if (referent_addr->AsMirrorPtr() == nullptr) {
Mathieu Chartier2175f522014-05-09 11:01:06 -070083 return nullptr;
84 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070085 // 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 -070086 // it to the mutator as long as the GC is not preserving references.
Mathieu Chartier97509952015-07-13 14:35:43 -070087 if (LIKELY(collector_ != nullptr)) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070088 // 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 -070089 // by finalizer referents. So we can not return in this case and must block. Otherwise, we
90 // can return it to the mutator as long as the GC is not preserving references, in which
91 // case only black nodes can be safely returned. If the GC is preserving references, the
92 // mutator could take a white field from a grey or white node and move it somewhere else
93 // in the heap causing corruption since this field would get swept.
Mathieu Chartier97509952015-07-13 14:35:43 -070094 if (collector_->IsMarkedHeapReference(referent_addr)) {
Fred Shih530e1b52014-06-09 15:19:54 -070095 if (!preserving_references_ ||
96 (LIKELY(!reference->IsFinalizerReferenceInstance()) && !reference->IsEnqueued())) {
Mathieu Chartier308351a2014-06-15 12:39:02 -070097 return referent_addr->AsMirrorPtr();
Fred Shih530e1b52014-06-09 15:19:54 -070098 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070099 }
100 }
Mathieu Chartier2d1ab0a2014-05-08 15:27:31 -0700101 condition_.WaitHoldingLocks(self);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700102 }
103 return reference->GetReferent();
104}
105
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700106void ReferenceProcessor::StartPreservingReferences(Thread* self) {
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -0700107 MutexLock mu(self, *Locks::reference_processor_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700108 preserving_references_ = true;
109}
110
111void ReferenceProcessor::StopPreservingReferences(Thread* self) {
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -0700112 MutexLock mu(self, *Locks::reference_processor_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700113 preserving_references_ = false;
114 // We are done preserving references, some people who are blocked may see a marked referent.
115 condition_.Broadcast(self);
116}
117
118// Process reference class instances and schedule finalizations.
119void ReferenceProcessor::ProcessReferences(bool concurrent, TimingLogger* timings,
120 bool clear_soft_references,
Mathieu Chartier97509952015-07-13 14:35:43 -0700121 collector::GarbageCollector* collector) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700122 TimingLogger::ScopedTiming t(concurrent ? __FUNCTION__ : "(Paused)ProcessReferences", timings);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700123 Thread* self = Thread::Current();
124 {
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -0700125 MutexLock mu(self, *Locks::reference_processor_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700126 collector_ = collector;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700127 if (!kUseReadBarrier) {
128 CHECK_EQ(SlowPathEnabled(), concurrent) << "Slow path must be enabled iff concurrent";
129 } else {
130 // Weak ref access is enabled at Zygote compaction by SemiSpace (concurrent == false).
131 CHECK_EQ(!self->GetWeakRefAccessEnabled(), concurrent);
132 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700133 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700134 // Unless required to clear soft references with white references, preserve some white referents.
135 if (!clear_soft_references) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700136 TimingLogger::ScopedTiming split(concurrent ? "ForwardSoftReferences" :
Fred Shih530e1b52014-06-09 15:19:54 -0700137 "(Paused)ForwardSoftReferences", timings);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700138 if (concurrent) {
139 StartPreservingReferences(self);
140 }
Mathieu Chartier81187812015-07-15 14:24:07 -0700141 // TODO: Add smarter logic for preserving soft references. The behavior should be a conditional
142 // mark if the SoftReference is supposed to be preserved.
Mathieu Chartier97509952015-07-13 14:35:43 -0700143 soft_reference_queue_.ForwardSoftReferences(collector);
144 collector->ProcessMarkStack();
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700145 if (concurrent) {
146 StopPreservingReferences(self);
147 }
148 }
149 // Clear all remaining soft and weak references with white referents.
Mathieu Chartier97509952015-07-13 14:35:43 -0700150 soft_reference_queue_.ClearWhiteReferences(&cleared_references_, collector);
151 weak_reference_queue_.ClearWhiteReferences(&cleared_references_, collector);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700152 {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800153 TimingLogger::ScopedTiming t2(concurrent ? "EnqueueFinalizerReferences" :
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700154 "(Paused)EnqueueFinalizerReferences", timings);
155 if (concurrent) {
156 StartPreservingReferences(self);
157 }
158 // Preserve all white objects with finalize methods and schedule them for finalization.
Mathieu Chartier97509952015-07-13 14:35:43 -0700159 finalizer_reference_queue_.EnqueueFinalizerReferences(&cleared_references_, collector);
160 collector->ProcessMarkStack();
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700161 if (concurrent) {
162 StopPreservingReferences(self);
163 }
164 }
165 // Clear all finalizer referent reachable soft and weak references with white referents.
Mathieu Chartier97509952015-07-13 14:35:43 -0700166 soft_reference_queue_.ClearWhiteReferences(&cleared_references_, collector);
167 weak_reference_queue_.ClearWhiteReferences(&cleared_references_, collector);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700168 // Clear all phantom references with white referents.
Mathieu Chartier97509952015-07-13 14:35:43 -0700169 phantom_reference_queue_.ClearWhiteReferences(&cleared_references_, collector);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700170 // At this point all reference queues other than the cleared references should be empty.
171 DCHECK(soft_reference_queue_.IsEmpty());
172 DCHECK(weak_reference_queue_.IsEmpty());
173 DCHECK(finalizer_reference_queue_.IsEmpty());
174 DCHECK(phantom_reference_queue_.IsEmpty());
Mathieu Chartier2175f522014-05-09 11:01:06 -0700175 {
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -0700176 MutexLock mu(self, *Locks::reference_processor_lock_);
Mathieu Chartier2175f522014-05-09 11:01:06 -0700177 // Need to always do this since the next GC may be concurrent. Doing this for only concurrent
178 // could result in a stale is_marked_callback_ being called before the reference processing
179 // starts since there is a small window of time where slow_path_enabled_ is enabled but the
180 // callback isn't yet set.
Mathieu Chartier97509952015-07-13 14:35:43 -0700181 collector_ = nullptr;
182 if (!kUseReadBarrier && concurrent) {
183 // Done processing, disable the slow path and broadcast to the waiters.
184 DisableSlowPath(self);
Mathieu Chartier2175f522014-05-09 11:01:06 -0700185 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700186 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700187}
188
189// Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been
190// marked, put it on the appropriate list in the heap for later processing.
191void ReferenceProcessor::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* ref,
Mathieu Chartier97509952015-07-13 14:35:43 -0700192 collector::GarbageCollector* collector) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700193 // 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 -0700194 DCHECK(klass != nullptr);
Fred Shih4ee7a662014-07-11 09:59:27 -0700195 DCHECK(klass->IsTypeOfReferenceClass());
Mathieu Chartier308351a2014-06-15 12:39:02 -0700196 mirror::HeapReference<mirror::Object>* referent = ref->GetReferentReferenceAddr();
Mathieu Chartier97509952015-07-13 14:35:43 -0700197 if (referent->AsMirrorPtr() != nullptr && !collector->IsMarkedHeapReference(referent)) {
Mathieu Chartier308351a2014-06-15 12:39:02 -0700198 Thread* self = Thread::Current();
199 // TODO: Remove these locks, and use atomic stacks for storing references?
200 // We need to check that the references haven't already been enqueued since we can end up
201 // scanning the same reference multiple times due to dirty cards.
202 if (klass->IsSoftReferenceClass()) {
203 soft_reference_queue_.AtomicEnqueueIfNotEnqueued(self, ref);
204 } else if (klass->IsWeakReferenceClass()) {
205 weak_reference_queue_.AtomicEnqueueIfNotEnqueued(self, ref);
206 } else if (klass->IsFinalizerReferenceClass()) {
207 finalizer_reference_queue_.AtomicEnqueueIfNotEnqueued(self, ref);
208 } else if (klass->IsPhantomReferenceClass()) {
209 phantom_reference_queue_.AtomicEnqueueIfNotEnqueued(self, ref);
210 } else {
211 LOG(FATAL) << "Invalid reference type " << PrettyClass(klass) << " " << std::hex
212 << klass->GetAccessFlags();
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700213 }
214 }
215}
216
Mathieu Chartier97509952015-07-13 14:35:43 -0700217void ReferenceProcessor::UpdateRoots(IsMarkedVisitor* visitor) {
218 cleared_references_.UpdateRoots(visitor);
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700219}
220
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800221class ClearedReferenceTask : public HeapTask {
222 public:
223 explicit ClearedReferenceTask(jobject cleared_references)
224 : HeapTask(NanoTime()), cleared_references_(cleared_references) {
225 }
226 virtual void Run(Thread* thread) {
227 ScopedObjectAccess soa(thread);
228 jvalue args[1];
229 args[0].l = cleared_references_;
230 InvokeWithJValues(soa, nullptr, WellKnownClasses::java_lang_ref_ReferenceQueue_add, args);
231 soa.Env()->DeleteGlobalRef(cleared_references_);
232 }
233
234 private:
235 const jobject cleared_references_;
236};
237
Mathieu Chartier308351a2014-06-15 12:39:02 -0700238void ReferenceProcessor::EnqueueClearedReferences(Thread* self) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700239 Locks::mutator_lock_->AssertNotHeld(self);
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800240 // When a runtime isn't started there are no reference queues to care about so ignore.
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700241 if (!cleared_references_.IsEmpty()) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700242 if (LIKELY(Runtime::Current()->IsStarted())) {
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800243 jobject cleared_references;
244 {
245 ReaderMutexLock mu(self, *Locks::mutator_lock_);
246 cleared_references = self->GetJniEnv()->vm->AddGlobalRef(
247 self, cleared_references_.GetList());
248 }
249 if (kAsyncReferenceQueueAdd) {
250 // TODO: This can cause RunFinalization to terminate before newly freed objects are
251 // finalized since they may not be enqueued by the time RunFinalization starts.
252 Runtime::Current()->GetHeap()->GetTaskProcessor()->AddTask(
253 self, new ClearedReferenceTask(cleared_references));
254 } else {
255 ClearedReferenceTask task(cleared_references);
256 task.Run(self);
257 }
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700258 }
259 cleared_references_.Clear();
260 }
261}
262
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -0700263bool ReferenceProcessor::MakeCircularListIfUnenqueued(mirror::FinalizerReference* reference) {
264 Thread* self = Thread::Current();
265 MutexLock mu(self, *Locks::reference_processor_lock_);
266 // Wait untul we are done processing reference.
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700267 while ((!kUseReadBarrier && SlowPathEnabled()) ||
268 (kUseReadBarrier && !self->GetWeakRefAccessEnabled())) {
Pavel Vyssotskid64ba382014-12-15 18:00:17 +0600269 condition_.WaitHoldingLocks(self);
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -0700270 }
271 // At this point, since the sentinel of the reference is live, it is guaranteed to not be
272 // enqueued if we just finished processing references. Otherwise, we may be doing the main GC
273 // phase. Since we are holding the reference processor lock, it guarantees that reference
274 // processing can't begin. The GC could have just enqueued the reference one one of the internal
275 // GC queues, but since we hold the lock finalizer_reference_queue_ lock it also prevents this
276 // race.
277 MutexLock mu2(self, *Locks::reference_queue_finalizer_references_lock_);
278 if (!reference->IsEnqueued()) {
279 CHECK(reference->IsFinalizerReferenceInstance());
280 if (Runtime::Current()->IsActiveTransaction()) {
281 reference->SetPendingNext<true>(reference);
282 } else {
283 reference->SetPendingNext<false>(reference);
284 }
285 return true;
286 }
287 return false;
288}
289
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700290} // namespace gc
291} // namespace art