blob: 7a89d5223051243a86fe8d48a557eb1a833c93ff [file] [log] [blame]
Adrian Roos91250682017-02-06 14:48:15 -08001/*
2 * Copyright (C) 2017 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
17package com.android.systemui.util.leak;
18
19
20import android.os.Build;
21import android.os.Handler;
22import android.os.Looper;
Adrian Roosfb2bb3f2017-02-14 17:19:16 +010023import android.os.SystemProperties;
Jason Monk3b9357f2017-07-14 09:40:54 -040024import android.provider.Settings;
Aurimas Liutikasfd52c142018-04-17 09:50:46 -070025import androidx.annotation.VisibleForTesting;
Adrian Roos91250682017-02-06 14:48:15 -080026
27import com.android.systemui.Dependency;
28import com.android.systemui.SystemUI;
29
30public class GarbageMonitor {
31
32 private static final String TAG = "GarbageMonitor";
33
34 private static final long GARBAGE_INSPECTION_INTERVAL = 5 * 60 * 1000; // 5min
35 private static final int GARBAGE_ALLOWANCE = 5;
36
37 private final Handler mHandler;
38 private final TrackedGarbage mTrackedGarbage;
39 private final LeakReporter mLeakReporter;
40
41 public GarbageMonitor(Looper bgLooper, LeakDetector leakDetector,
42 LeakReporter leakReporter) {
43 mHandler = bgLooper != null ? new Handler(bgLooper): null;
44 mTrackedGarbage = leakDetector.getTrackedGarbage();
45 mLeakReporter = leakReporter;
46 }
47
48 public void start() {
49 if (mTrackedGarbage == null) {
50 return;
51 }
52
53 scheduleInspectGarbage(this::inspectGarbage);
54 }
55
56 @VisibleForTesting
57 void scheduleInspectGarbage(Runnable runnable) {
58 mHandler.postDelayed(runnable, GARBAGE_INSPECTION_INTERVAL);
59 }
60
61 private void inspectGarbage() {
62 if (mTrackedGarbage.countOldGarbage() > GARBAGE_ALLOWANCE) {
63 Runtime.getRuntime().gc();
64
65 // Allow some time to for ReferenceQueue to catch up.
66 scheduleReinspectGarbage(this::reinspectGarbageAfterGc);
67 }
68 scheduleInspectGarbage(this::inspectGarbage);
69 }
70
71 @VisibleForTesting
72 void scheduleReinspectGarbage(Runnable runnable) {
73 mHandler.postDelayed(runnable, (long) 100);
74 }
75
76 private void reinspectGarbageAfterGc() {
77 int count = mTrackedGarbage.countOldGarbage();
78 if (count > GARBAGE_ALLOWANCE) {
79 mLeakReporter.dumpLeak(count);
80 }
81 }
82
83 public static class Service extends SystemUI {
84
Adrian Roosfb2bb3f2017-02-14 17:19:16 +010085 // TODO(b/35345376): Turn this back on for debuggable builds after known leak fixed.
86 private static final boolean ENABLED = Build.IS_DEBUGGABLE
87 && SystemProperties.getBoolean("debug.enable_leak_reporting", false);
Jason Monk3b9357f2017-07-14 09:40:54 -040088 private static final String FORCE_ENABLE = "sysui_force_garbage_monitor";
Adrian Roosfb2bb3f2017-02-14 17:19:16 +010089
Adrian Roos91250682017-02-06 14:48:15 -080090 private GarbageMonitor mGarbageMonitor;
91
92 @Override
93 public void start() {
Jason Monk3b9357f2017-07-14 09:40:54 -040094 boolean forceEnable = Settings.Secure.getInt(mContext.getContentResolver(),
95 FORCE_ENABLE, 0) != 0;
96 if (!ENABLED && !forceEnable) {
Adrian Roos91250682017-02-06 14:48:15 -080097 return;
98 }
99 mGarbageMonitor = Dependency.get(GarbageMonitor.class);
100 mGarbageMonitor.start();
101 }
102 }
103}