xref: /aosp_15_r20/art/test/2261-badcleaner-in-systemcleaner/src-art/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2023 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 import android.system.SystemCleaner;
18*795d594fSAndroid Build Coastguard Worker import dalvik.system.VMRuntime;
19*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.CountDownLatch;
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker import static java.util.concurrent.TimeUnit.MINUTES;
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker /**
24*795d594fSAndroid Build Coastguard Worker  * Test SystemCleaner with a bad cleaning action.
25*795d594fSAndroid Build Coastguard Worker  *
26*795d594fSAndroid Build Coastguard Worker  * This test is inherently very slightly flaky. It assumes that the system will schedule the
27*795d594fSAndroid Build Coastguard Worker  * finalizer daemon and finalizer watchdog daemon soon and often enough to reach the timeout and
28*795d594fSAndroid Build Coastguard Worker  * throw the fatal exception before we time out. Since we build in a 100 second buffer, failures
29*795d594fSAndroid Build Coastguard Worker  * should be very rare.
30*795d594fSAndroid Build Coastguard Worker  */
31*795d594fSAndroid Build Coastguard Worker public class Main {
main(String[] args)32*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) throws Exception {
33*795d594fSAndroid Build Coastguard Worker         CountDownLatch cleanerWait = new CountDownLatch(1);
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker         registerBadCleaner(cleanerWait);
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker         // Should have at least two iterations to trigger finalization, but just to make sure run
38*795d594fSAndroid Build Coastguard Worker         // some more.
39*795d594fSAndroid Build Coastguard Worker         for (int i = 0; i < 5; i++) {
40*795d594fSAndroid Build Coastguard Worker             Runtime.getRuntime().gc();
41*795d594fSAndroid Build Coastguard Worker         }
42*795d594fSAndroid Build Coastguard Worker 
43*795d594fSAndroid Build Coastguard Worker         // Now wait for the finalizer to start running. Give it a minute.
44*795d594fSAndroid Build Coastguard Worker         cleanerWait.await(1, MINUTES);
45*795d594fSAndroid Build Coastguard Worker 
46*795d594fSAndroid Build Coastguard Worker         // Now fall asleep with a timeout. The timeout is large enough that we expect the
47*795d594fSAndroid Build Coastguard Worker         // finalizer daemon to have killed the process before the deadline elapses.
48*795d594fSAndroid Build Coastguard Worker         // The timeout is also large enough to cover the extra 5 seconds we wait
49*795d594fSAndroid Build Coastguard Worker         // to dump threads, plus potentially substantial gcstress overhead.
50*795d594fSAndroid Build Coastguard Worker         // Note: the timeout is here (instead of an infinite sleep) to protect the test
51*795d594fSAndroid Build Coastguard Worker         //       environment (e.g., in case this is run without a timeout wrapper).
52*795d594fSAndroid Build Coastguard Worker         final long timeout = 100 * 1000 + VMRuntime.getRuntime().getFinalizerTimeoutMs();
53*795d594fSAndroid Build Coastguard Worker         long remainingWait = timeout;
54*795d594fSAndroid Build Coastguard Worker         final long waitStart = System.currentTimeMillis();
55*795d594fSAndroid Build Coastguard Worker         while (remainingWait > 0) {
56*795d594fSAndroid Build Coastguard Worker             synchronized (args) {  // Just use an already existing object for simplicity...
57*795d594fSAndroid Build Coastguard Worker                 try {
58*795d594fSAndroid Build Coastguard Worker                     args.wait(remainingWait);
59*795d594fSAndroid Build Coastguard Worker                 } catch (Exception e) {
60*795d594fSAndroid Build Coastguard Worker                     System.out.println("UNEXPECTED EXCEPTION");
61*795d594fSAndroid Build Coastguard Worker                 }
62*795d594fSAndroid Build Coastguard Worker             }
63*795d594fSAndroid Build Coastguard Worker             remainingWait = timeout - (System.currentTimeMillis() - waitStart);
64*795d594fSAndroid Build Coastguard Worker         }
65*795d594fSAndroid Build Coastguard Worker 
66*795d594fSAndroid Build Coastguard Worker         // We should not get here.
67*795d594fSAndroid Build Coastguard Worker         System.out.println("UNREACHABLE");
68*795d594fSAndroid Build Coastguard Worker         System.exit(0);
69*795d594fSAndroid Build Coastguard Worker     }
70*795d594fSAndroid Build Coastguard Worker 
registerBadCleaner(CountDownLatch cleanerWait)71*795d594fSAndroid Build Coastguard Worker     private static void registerBadCleaner(CountDownLatch cleanerWait) {
72*795d594fSAndroid Build Coastguard Worker         Object obj = new Object();
73*795d594fSAndroid Build Coastguard Worker         SystemCleaner.cleaner().register(obj, () -> neverEndingCleanup(cleanerWait));
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker         System.out.println("About to null reference.");
76*795d594fSAndroid Build Coastguard Worker         obj = null;  // Not that this would make a difference, could be eliminated earlier.
77*795d594fSAndroid Build Coastguard Worker     }
78*795d594fSAndroid Build Coastguard Worker 
snooze(int ms)79*795d594fSAndroid Build Coastguard Worker     public static void snooze(int ms) {
80*795d594fSAndroid Build Coastguard Worker         try {
81*795d594fSAndroid Build Coastguard Worker             Thread.sleep(ms);
82*795d594fSAndroid Build Coastguard Worker         } catch (InterruptedException ie) {
83*795d594fSAndroid Build Coastguard Worker             System.out.println("Unexpected interrupt");
84*795d594fSAndroid Build Coastguard Worker         }
85*795d594fSAndroid Build Coastguard Worker     }
86*795d594fSAndroid Build Coastguard Worker 
neverEndingCleanup(CountDownLatch cleanerWait)87*795d594fSAndroid Build Coastguard Worker     private static void neverEndingCleanup(CountDownLatch cleanerWait) {
88*795d594fSAndroid Build Coastguard Worker         cleanerWait.countDown();
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker         System.out.println("Cleaner started and sleeping briefly...");
91*795d594fSAndroid Build Coastguard Worker 
92*795d594fSAndroid Build Coastguard Worker         long start, end;
93*795d594fSAndroid Build Coastguard Worker         start = System.nanoTime();
94*795d594fSAndroid Build Coastguard Worker         snooze(2000);
95*795d594fSAndroid Build Coastguard Worker         end = System.nanoTime();
96*795d594fSAndroid Build Coastguard Worker         System.out.println("Cleaner done snoozing.");
97*795d594fSAndroid Build Coastguard Worker 
98*795d594fSAndroid Build Coastguard Worker         System.out.println("Cleaner sleeping forever now.");
99*795d594fSAndroid Build Coastguard Worker         while (true) {
100*795d594fSAndroid Build Coastguard Worker             snooze(10000);
101*795d594fSAndroid Build Coastguard Worker         }
102*795d594fSAndroid Build Coastguard Worker     }
103*795d594fSAndroid Build Coastguard Worker }
104