xref: /aosp_15_r20/art/runtime/gc/system_weak_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "system_weak.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <stdint.h>
20*795d594fSAndroid Build Coastguard Worker #include <stdio.h>
21*795d594fSAndroid Build Coastguard Worker #include <memory>
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
24*795d594fSAndroid Build Coastguard Worker #include "collector_type.h"
25*795d594fSAndroid Build Coastguard Worker #include "common_runtime_test.h"
26*795d594fSAndroid Build Coastguard Worker #include "gc_root-inl.h"
27*795d594fSAndroid Build Coastguard Worker #include "handle_scope-inl.h"
28*795d594fSAndroid Build Coastguard Worker #include "heap.h"
29*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
30*795d594fSAndroid Build Coastguard Worker #include "mirror/string.h"
31*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
32*795d594fSAndroid Build Coastguard Worker #include "thread_list.h"
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
35*795d594fSAndroid Build Coastguard Worker namespace gc {
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker class SystemWeakTest : public CommonRuntimeTest {
38*795d594fSAndroid Build Coastguard Worker  protected:
SystemWeakTest()39*795d594fSAndroid Build Coastguard Worker   SystemWeakTest() {
40*795d594fSAndroid Build Coastguard Worker     use_boot_image_ = true;  // Make the Runtime creation cheaper.
41*795d594fSAndroid Build Coastguard Worker   }
42*795d594fSAndroid Build Coastguard Worker };
43*795d594fSAndroid Build Coastguard Worker 
44*795d594fSAndroid Build Coastguard Worker struct CountingSystemWeakHolder : public SystemWeakHolder {
CountingSystemWeakHolderart::gc::CountingSystemWeakHolder45*795d594fSAndroid Build Coastguard Worker   CountingSystemWeakHolder()
46*795d594fSAndroid Build Coastguard Worker       : SystemWeakHolder(kAllocTrackerLock),
47*795d594fSAndroid Build Coastguard Worker         allow_count_(0),
48*795d594fSAndroid Build Coastguard Worker         disallow_count_(0),
49*795d594fSAndroid Build Coastguard Worker         sweep_count_(0) {}
50*795d594fSAndroid Build Coastguard Worker 
Allowart::gc::CountingSystemWeakHolder51*795d594fSAndroid Build Coastguard Worker   void Allow() override
52*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_)
53*795d594fSAndroid Build Coastguard Worker       REQUIRES(!allow_disallow_lock_) {
54*795d594fSAndroid Build Coastguard Worker     SystemWeakHolder::Allow();
55*795d594fSAndroid Build Coastguard Worker 
56*795d594fSAndroid Build Coastguard Worker     allow_count_++;
57*795d594fSAndroid Build Coastguard Worker   }
58*795d594fSAndroid Build Coastguard Worker 
Disallowart::gc::CountingSystemWeakHolder59*795d594fSAndroid Build Coastguard Worker   void Disallow() override
60*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_)
61*795d594fSAndroid Build Coastguard Worker       REQUIRES(!allow_disallow_lock_) {
62*795d594fSAndroid Build Coastguard Worker     SystemWeakHolder::Disallow();
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker     disallow_count_++;
65*795d594fSAndroid Build Coastguard Worker   }
66*795d594fSAndroid Build Coastguard Worker 
Broadcastart::gc::CountingSystemWeakHolder67*795d594fSAndroid Build Coastguard Worker   void Broadcast(bool broadcast_for_checkpoint) override
68*795d594fSAndroid Build Coastguard Worker       REQUIRES(!allow_disallow_lock_) {
69*795d594fSAndroid Build Coastguard Worker     SystemWeakHolder::Broadcast(broadcast_for_checkpoint);
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker     if (!broadcast_for_checkpoint) {
72*795d594fSAndroid Build Coastguard Worker       // Don't count the broadcasts for running checkpoints.
73*795d594fSAndroid Build Coastguard Worker       allow_count_++;
74*795d594fSAndroid Build Coastguard Worker     }
75*795d594fSAndroid Build Coastguard Worker   }
76*795d594fSAndroid Build Coastguard Worker 
Sweepart::gc::CountingSystemWeakHolder77*795d594fSAndroid Build Coastguard Worker   void Sweep(IsMarkedVisitor* visitor) override
78*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_)
79*795d594fSAndroid Build Coastguard Worker       REQUIRES(!allow_disallow_lock_) {
80*795d594fSAndroid Build Coastguard Worker     MutexLock mu(Thread::Current(), allow_disallow_lock_);
81*795d594fSAndroid Build Coastguard Worker     mirror::Object* old_object = weak_.Read<kWithoutReadBarrier>();
82*795d594fSAndroid Build Coastguard Worker     mirror::Object* new_object = old_object == nullptr ? nullptr : visitor->IsMarked(old_object);
83*795d594fSAndroid Build Coastguard Worker     weak_ = GcRoot<mirror::Object>(new_object);
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker     sweep_count_++;
86*795d594fSAndroid Build Coastguard Worker   }
87*795d594fSAndroid Build Coastguard Worker 
Getart::gc::CountingSystemWeakHolder88*795d594fSAndroid Build Coastguard Worker   GcRoot<mirror::Object> Get()
89*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_)
90*795d594fSAndroid Build Coastguard Worker       REQUIRES(!allow_disallow_lock_) {
91*795d594fSAndroid Build Coastguard Worker     Thread* self = Thread::Current();
92*795d594fSAndroid Build Coastguard Worker     MutexLock mu(self, allow_disallow_lock_);
93*795d594fSAndroid Build Coastguard Worker     Wait(self);
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker     return weak_;
96*795d594fSAndroid Build Coastguard Worker   }
97*795d594fSAndroid Build Coastguard Worker 
Setart::gc::CountingSystemWeakHolder98*795d594fSAndroid Build Coastguard Worker   void Set(GcRoot<mirror::Object> obj)
99*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_)
100*795d594fSAndroid Build Coastguard Worker       REQUIRES(!allow_disallow_lock_) {
101*795d594fSAndroid Build Coastguard Worker     Thread* self = Thread::Current();
102*795d594fSAndroid Build Coastguard Worker     MutexLock mu(self, allow_disallow_lock_);
103*795d594fSAndroid Build Coastguard Worker     Wait(self);
104*795d594fSAndroid Build Coastguard Worker 
105*795d594fSAndroid Build Coastguard Worker     weak_ = obj;
106*795d594fSAndroid Build Coastguard Worker   }
107*795d594fSAndroid Build Coastguard Worker 
108*795d594fSAndroid Build Coastguard Worker   size_t allow_count_;
109*795d594fSAndroid Build Coastguard Worker   size_t disallow_count_;
110*795d594fSAndroid Build Coastguard Worker   size_t sweep_count_;
111*795d594fSAndroid Build Coastguard Worker   GcRoot<mirror::Object> weak_ GUARDED_BY(allow_disallow_lock_);
112*795d594fSAndroid Build Coastguard Worker };
113*795d594fSAndroid Build Coastguard Worker 
CollectorDoesAllowOrBroadcast()114*795d594fSAndroid Build Coastguard Worker static bool CollectorDoesAllowOrBroadcast() {
115*795d594fSAndroid Build Coastguard Worker   CollectorType type = Runtime::Current()->GetHeap()->CurrentCollectorType();
116*795d594fSAndroid Build Coastguard Worker   switch (type) {
117*795d594fSAndroid Build Coastguard Worker     case CollectorType::kCollectorTypeCMS:
118*795d594fSAndroid Build Coastguard Worker     case CollectorType::kCollectorTypeCMC:
119*795d594fSAndroid Build Coastguard Worker     case CollectorType::kCollectorTypeCC:
120*795d594fSAndroid Build Coastguard Worker     case CollectorType::kCollectorTypeSS:
121*795d594fSAndroid Build Coastguard Worker       return true;
122*795d594fSAndroid Build Coastguard Worker 
123*795d594fSAndroid Build Coastguard Worker     default:
124*795d594fSAndroid Build Coastguard Worker       return false;
125*795d594fSAndroid Build Coastguard Worker   }
126*795d594fSAndroid Build Coastguard Worker }
127*795d594fSAndroid Build Coastguard Worker 
CollectorDoesDisallow()128*795d594fSAndroid Build Coastguard Worker static bool CollectorDoesDisallow() {
129*795d594fSAndroid Build Coastguard Worker   CollectorType type = Runtime::Current()->GetHeap()->CurrentCollectorType();
130*795d594fSAndroid Build Coastguard Worker   switch (type) {
131*795d594fSAndroid Build Coastguard Worker     case CollectorType::kCollectorTypeCMS:
132*795d594fSAndroid Build Coastguard Worker     case CollectorType::kCollectorTypeCMC:
133*795d594fSAndroid Build Coastguard Worker       return true;
134*795d594fSAndroid Build Coastguard Worker 
135*795d594fSAndroid Build Coastguard Worker     default:
136*795d594fSAndroid Build Coastguard Worker       return false;
137*795d594fSAndroid Build Coastguard Worker   }
138*795d594fSAndroid Build Coastguard Worker }
139*795d594fSAndroid Build Coastguard Worker 
TEST_F(SystemWeakTest,Keep)140*795d594fSAndroid Build Coastguard Worker TEST_F(SystemWeakTest, Keep) {
141*795d594fSAndroid Build Coastguard Worker   CountingSystemWeakHolder cswh;
142*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->AddSystemWeakHolder(&cswh);
143*795d594fSAndroid Build Coastguard Worker 
144*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
145*795d594fSAndroid Build Coastguard Worker 
146*795d594fSAndroid Build Coastguard Worker   StackHandleScope<1> hs(soa.Self());
147*795d594fSAndroid Build Coastguard Worker 
148*795d594fSAndroid Build Coastguard Worker   // We use Strings because they are very easy to allocate.
149*795d594fSAndroid Build Coastguard Worker   Handle<mirror::String> s(hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "ABC")));
150*795d594fSAndroid Build Coastguard Worker   cswh.Set(GcRoot<mirror::Object>(s.Get()));
151*795d594fSAndroid Build Coastguard Worker 
152*795d594fSAndroid Build Coastguard Worker   // Trigger a GC.
153*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references= */ false);
154*795d594fSAndroid Build Coastguard Worker 
155*795d594fSAndroid Build Coastguard Worker   // Expect the holder to have been called.
156*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(CollectorDoesAllowOrBroadcast() ? 1U : 0U, cswh.allow_count_);
157*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(CollectorDoesDisallow() ? 1U : 0U, cswh.disallow_count_);
158*795d594fSAndroid Build Coastguard Worker   // Userfaultfd GC uses SweepSystemWeaks also for concurrent updation.
159*795d594fSAndroid Build Coastguard Worker   // TODO: Explore this can be reverted back to unconditionally compare with 1
160*795d594fSAndroid Build Coastguard Worker   // once concurrent updation of native roots is full implemented in userfaultfd
161*795d594fSAndroid Build Coastguard Worker   // GC.
162*795d594fSAndroid Build Coastguard Worker   size_t expected_sweep_count = gUseUserfaultfd ? 2U : 1U;
163*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(expected_sweep_count, cswh.sweep_count_);
164*795d594fSAndroid Build Coastguard Worker 
165*795d594fSAndroid Build Coastguard Worker   // Expect the weak to not be cleared.
166*795d594fSAndroid Build Coastguard Worker   EXPECT_FALSE(cswh.Get().IsNull());
167*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(cswh.Get().Read(), s.Get());
168*795d594fSAndroid Build Coastguard Worker }
169*795d594fSAndroid Build Coastguard Worker 
TEST_F(SystemWeakTest,Discard)170*795d594fSAndroid Build Coastguard Worker TEST_F(SystemWeakTest, Discard) {
171*795d594fSAndroid Build Coastguard Worker   CountingSystemWeakHolder cswh;
172*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->AddSystemWeakHolder(&cswh);
173*795d594fSAndroid Build Coastguard Worker 
174*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
175*795d594fSAndroid Build Coastguard Worker 
176*795d594fSAndroid Build Coastguard Worker   cswh.Set(GcRoot<mirror::Object>(mirror::String::AllocFromModifiedUtf8(soa.Self(), "ABC")));
177*795d594fSAndroid Build Coastguard Worker 
178*795d594fSAndroid Build Coastguard Worker   // Trigger a GC.
179*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references= */ false);
180*795d594fSAndroid Build Coastguard Worker 
181*795d594fSAndroid Build Coastguard Worker   // Expect the holder to have been called.
182*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(CollectorDoesAllowOrBroadcast() ? 1U : 0U, cswh.allow_count_);
183*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(CollectorDoesDisallow() ? 1U : 0U, cswh.disallow_count_);
184*795d594fSAndroid Build Coastguard Worker   // Userfaultfd GC uses SweepSystemWeaks also for concurrent updation.
185*795d594fSAndroid Build Coastguard Worker   // TODO: Explore this can be reverted back to unconditionally compare with 1
186*795d594fSAndroid Build Coastguard Worker   // once concurrent updation of native roots is full implemented in userfaultfd
187*795d594fSAndroid Build Coastguard Worker   // GC.
188*795d594fSAndroid Build Coastguard Worker   size_t expected_sweep_count = gUseUserfaultfd ? 2U : 1U;
189*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(expected_sweep_count, cswh.sweep_count_);
190*795d594fSAndroid Build Coastguard Worker 
191*795d594fSAndroid Build Coastguard Worker   // Expect the weak to be cleared.
192*795d594fSAndroid Build Coastguard Worker   EXPECT_TRUE(cswh.Get().IsNull());
193*795d594fSAndroid Build Coastguard Worker }
194*795d594fSAndroid Build Coastguard Worker 
TEST_F(SystemWeakTest,Remove)195*795d594fSAndroid Build Coastguard Worker TEST_F(SystemWeakTest, Remove) {
196*795d594fSAndroid Build Coastguard Worker   CountingSystemWeakHolder cswh;
197*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->AddSystemWeakHolder(&cswh);
198*795d594fSAndroid Build Coastguard Worker 
199*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
200*795d594fSAndroid Build Coastguard Worker 
201*795d594fSAndroid Build Coastguard Worker   StackHandleScope<1> hs(soa.Self());
202*795d594fSAndroid Build Coastguard Worker 
203*795d594fSAndroid Build Coastguard Worker   // We use Strings because they are very easy to allocate.
204*795d594fSAndroid Build Coastguard Worker   Handle<mirror::String> s(hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "ABC")));
205*795d594fSAndroid Build Coastguard Worker   cswh.Set(GcRoot<mirror::Object>(s.Get()));
206*795d594fSAndroid Build Coastguard Worker 
207*795d594fSAndroid Build Coastguard Worker   // Trigger a GC.
208*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references= */ false);
209*795d594fSAndroid Build Coastguard Worker 
210*795d594fSAndroid Build Coastguard Worker   // Expect the holder to have been called.
211*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(CollectorDoesAllowOrBroadcast() ? 1U : 0U, cswh.allow_count_);
212*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(CollectorDoesDisallow() ? 1U : 0U, cswh.disallow_count_);
213*795d594fSAndroid Build Coastguard Worker   // Userfaultfd GC uses SweepSystemWeaks also for concurrent updation.
214*795d594fSAndroid Build Coastguard Worker   // TODO: Explore this can be reverted back to unconditionally compare with 1
215*795d594fSAndroid Build Coastguard Worker   // once concurrent updation of native roots is full implemented in userfaultfd
216*795d594fSAndroid Build Coastguard Worker   // GC.
217*795d594fSAndroid Build Coastguard Worker   size_t expected_sweep_count = gUseUserfaultfd ? 2U : 1U;
218*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(expected_sweep_count, cswh.sweep_count_);
219*795d594fSAndroid Build Coastguard Worker 
220*795d594fSAndroid Build Coastguard Worker   // Expect the weak to not be cleared.
221*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(cswh.Get().IsNull());
222*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(cswh.Get().Read(), s.Get());
223*795d594fSAndroid Build Coastguard Worker 
224*795d594fSAndroid Build Coastguard Worker   // Remove the holder.
225*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->RemoveSystemWeakHolder(&cswh);
226*795d594fSAndroid Build Coastguard Worker 
227*795d594fSAndroid Build Coastguard Worker   // Trigger another GC.
228*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references= */ false);
229*795d594fSAndroid Build Coastguard Worker 
230*795d594fSAndroid Build Coastguard Worker   // Expectation: no change in the numbers.
231*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(CollectorDoesAllowOrBroadcast() ? 1U : 0U, cswh.allow_count_);
232*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(CollectorDoesDisallow() ? 1U : 0U, cswh.disallow_count_);
233*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(expected_sweep_count, cswh.sweep_count_);
234*795d594fSAndroid Build Coastguard Worker }
235*795d594fSAndroid Build Coastguard Worker 
236*795d594fSAndroid Build Coastguard Worker }  // namespace gc
237*795d594fSAndroid Build Coastguard Worker }  // namespace art
238