Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.util.leak; |
| 18 | |
| 19 | |
| 20 | import android.os.Build; |
| 21 | import android.os.Handler; |
| 22 | import android.os.Looper; |
Adrian Roos | fb2bb3f | 2017-02-14 17:19:16 +0100 | [diff] [blame] | 23 | import android.os.SystemProperties; |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 24 | import android.provider.Settings; |
Aurimas Liutikas | fd52c14 | 2018-04-17 09:50:46 -0700 | [diff] [blame^] | 25 | import androidx.annotation.VisibleForTesting; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 26 | |
| 27 | import com.android.systemui.Dependency; |
| 28 | import com.android.systemui.SystemUI; |
| 29 | |
| 30 | public 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 Roos | fb2bb3f | 2017-02-14 17:19:16 +0100 | [diff] [blame] | 85 | // 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 Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 88 | private static final String FORCE_ENABLE = "sysui_force_garbage_monitor"; |
Adrian Roos | fb2bb3f | 2017-02-14 17:19:16 +0100 | [diff] [blame] | 89 | |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 90 | private GarbageMonitor mGarbageMonitor; |
| 91 | |
| 92 | @Override |
| 93 | public void start() { |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 94 | boolean forceEnable = Settings.Secure.getInt(mContext.getContentResolver(), |
| 95 | FORCE_ENABLE, 0) != 0; |
| 96 | if (!ENABLED && !forceEnable) { |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | mGarbageMonitor = Dependency.get(GarbageMonitor.class); |
| 100 | mGarbageMonitor.start(); |
| 101 | } |
| 102 | } |
| 103 | } |