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 | |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 19 | import static android.service.quicksettings.Tile.STATE_ACTIVE; |
| 20 | import static android.telephony.ims.feature.ImsFeature.STATE_UNAVAILABLE; |
| 21 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 22 | import static com.android.internal.logging.MetricsLogger.VIEW_UNKNOWN; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 23 | |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 24 | import android.annotation.Nullable; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 25 | import android.app.ActivityManager; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.res.ColorStateList; |
| 29 | import android.graphics.Canvas; |
| 30 | import android.graphics.ColorFilter; |
| 31 | import android.graphics.Paint; |
| 32 | import android.graphics.PixelFormat; |
| 33 | import android.graphics.PorterDuff; |
| 34 | import android.graphics.Rect; |
| 35 | import android.graphics.drawable.Drawable; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 36 | import android.os.Build; |
| 37 | import android.os.Handler; |
| 38 | import android.os.Looper; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 39 | import android.os.Message; |
| 40 | import android.os.Process; |
Adrian Roos | fb2bb3f | 2017-02-14 17:19:16 +0100 | [diff] [blame] | 41 | import android.os.SystemProperties; |
Jason Monk | 3b9357f | 2017-07-14 09:40:54 -0400 | [diff] [blame] | 42 | import android.provider.Settings; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 43 | import android.text.format.DateUtils; |
| 44 | import android.util.Log; |
| 45 | import android.util.LongSparseArray; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 46 | |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 47 | import com.android.systemui.Dumpable; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 48 | import com.android.systemui.R; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 49 | import com.android.systemui.SystemUI; |
Dave Mankoff | 00e8a2f | 2019-12-18 16:59:49 -0500 | [diff] [blame] | 50 | import com.android.systemui.dagger.qualifiers.Background; |
Fabian Kozynski | 8f70363 | 2019-09-27 10:26:44 -0400 | [diff] [blame] | 51 | import com.android.systemui.plugins.ActivityStarter; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 52 | import com.android.systemui.plugins.qs.QSTile; |
| 53 | import com.android.systemui.qs.QSHost; |
| 54 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
| 55 | |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 56 | import java.io.FileDescriptor; |
| 57 | import java.io.PrintWriter; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 58 | import java.util.ArrayList; |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 59 | import java.util.List; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 60 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 61 | import javax.inject.Inject; |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 62 | import javax.inject.Singleton; |
| 63 | |
| 64 | /** |
| 65 | */ |
| 66 | @Singleton |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 67 | public class GarbageMonitor implements Dumpable { |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 68 | private static final boolean LEAK_REPORTING_ENABLED = |
| 69 | Build.IS_DEBUGGABLE |
| 70 | && SystemProperties.getBoolean("debug.enable_leak_reporting", false); |
| 71 | private static final String FORCE_ENABLE_LEAK_REPORTING = "sysui_force_enable_leak_reporting"; |
| 72 | |
| 73 | private static final boolean HEAP_TRACKING_ENABLED = Build.IS_DEBUGGABLE; |
Dan Sandler | 1e5d012 | 2019-06-06 23:35:22 -0400 | [diff] [blame] | 74 | |
| 75 | // whether to use ActivityManager.setHeapLimit |
| 76 | private static final boolean ENABLE_AM_HEAP_LIMIT = Build.IS_DEBUGGABLE; |
| 77 | // heap limit value, in KB (overrides R.integer.watch_heap_limit) |
| 78 | private static final String SETTINGS_KEY_AM_HEAP_LIMIT = "systemui_am_heap_limit"; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 79 | |
| 80 | private static final String TAG = "GarbageMonitor"; |
| 81 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 82 | private static final long GARBAGE_INSPECTION_INTERVAL = |
| 83 | 15 * DateUtils.MINUTE_IN_MILLIS; // 15 min |
| 84 | private static final long HEAP_TRACK_INTERVAL = 1 * DateUtils.MINUTE_IN_MILLIS; // 1 min |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 85 | private static final int HEAP_TRACK_HISTORY_LEN = 720; // 12 hours |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 86 | |
| 87 | private static final int DO_GARBAGE_INSPECTION = 1000; |
| 88 | private static final int DO_HEAP_TRACK = 3000; |
| 89 | |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 90 | private static final int GARBAGE_ALLOWANCE = 5; |
| 91 | |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 92 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 93 | |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 94 | private final Handler mHandler; |
| 95 | private final TrackedGarbage mTrackedGarbage; |
| 96 | private final LeakReporter mLeakReporter; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 97 | private final Context mContext; |
| 98 | private final ActivityManager mAm; |
| 99 | private MemoryTile mQSTile; |
| 100 | private DumpTruck mDumpTruck; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 101 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 102 | private final LongSparseArray<ProcessMemInfo> mData = new LongSparseArray<>(); |
| 103 | private final ArrayList<Long> mPids = new ArrayList<>(); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 104 | |
| 105 | private long mHeapLimit; |
| 106 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 107 | /** |
| 108 | */ |
| 109 | @Inject |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 110 | public GarbageMonitor( |
| 111 | Context context, |
Dave Mankoff | 00e8a2f | 2019-12-18 16:59:49 -0500 | [diff] [blame] | 112 | @Background Looper bgLooper, |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 113 | LeakDetector leakDetector, |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 114 | LeakReporter leakReporter) { |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 115 | mContext = context.getApplicationContext(); |
| 116 | mAm = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
| 117 | |
| 118 | mHandler = new BackgroundHeapCheckHandler(bgLooper); |
| 119 | |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 120 | mTrackedGarbage = leakDetector.getTrackedGarbage(); |
| 121 | mLeakReporter = leakReporter; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 122 | |
| 123 | mDumpTruck = new DumpTruck(mContext); |
| 124 | |
| 125 | if (ENABLE_AM_HEAP_LIMIT) { |
Dan Sandler | 1e5d012 | 2019-06-06 23:35:22 -0400 | [diff] [blame] | 126 | mHeapLimit = Settings.Global.getInt(context.getContentResolver(), |
| 127 | SETTINGS_KEY_AM_HEAP_LIMIT, |
| 128 | mContext.getResources().getInteger(R.integer.watch_heap_limit)); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 129 | } |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 132 | public void startLeakMonitor() { |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 133 | if (mTrackedGarbage == null) { |
| 134 | return; |
| 135 | } |
| 136 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 137 | mHandler.sendEmptyMessage(DO_GARBAGE_INSPECTION); |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 140 | public void startHeapTracking() { |
| 141 | startTrackingProcess( |
| 142 | android.os.Process.myPid(), mContext.getPackageName(), System.currentTimeMillis()); |
| 143 | mHandler.sendEmptyMessage(DO_HEAP_TRACK); |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 146 | private boolean gcAndCheckGarbage() { |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 147 | if (mTrackedGarbage.countOldGarbage() > GARBAGE_ALLOWANCE) { |
| 148 | Runtime.getRuntime().gc(); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 149 | return true; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 150 | } |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 151 | return false; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 154 | void reinspectGarbageAfterGc() { |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 155 | int count = mTrackedGarbage.countOldGarbage(); |
| 156 | if (count > GARBAGE_ALLOWANCE) { |
| 157 | mLeakReporter.dumpLeak(count); |
| 158 | } |
| 159 | } |
| 160 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 161 | public ProcessMemInfo getMemInfo(int pid) { |
| 162 | return mData.get(pid); |
| 163 | } |
| 164 | |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 165 | public List<Long> getTrackedProcesses() { |
| 166 | return mPids; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | public void startTrackingProcess(long pid, String name, long start) { |
| 170 | synchronized (mPids) { |
| 171 | if (mPids.contains(pid)) return; |
| 172 | |
| 173 | mPids.add(pid); |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 174 | logPids(); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 175 | |
| 176 | mData.put(pid, new ProcessMemInfo(pid, name, start)); |
| 177 | } |
| 178 | } |
| 179 | |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 180 | private void logPids() { |
| 181 | if (DEBUG) { |
| 182 | StringBuffer sb = new StringBuffer("Now tracking processes: "); |
| 183 | for (int i = 0; i < mPids.size(); i++) { |
| 184 | final int p = mPids.get(i).intValue(); |
| 185 | sb.append(" "); |
| 186 | } |
| 187 | Log.v(TAG, sb.toString()); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 188 | } |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | private void update() { |
| 192 | synchronized (mPids) { |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 193 | for (int i = 0; i < mPids.size(); i++) { |
| 194 | final int pid = mPids.get(i).intValue(); |
| 195 | // rssValues contains [VmRSS, RssFile, RssAnon, VmSwap]. |
| 196 | long[] rssValues = Process.getRss(pid); |
| 197 | if (rssValues == null && rssValues.length == 0) { |
| 198 | if (DEBUG) Log.e(TAG, "update: Process.getRss() didn't provide any values."); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 199 | break; |
| 200 | } |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 201 | long rss = rssValues[0]; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 202 | final ProcessMemInfo info = mData.get(pid); |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 203 | info.rss[info.head] = info.currentRss = rss; |
| 204 | info.head = (info.head + 1) % info.rss.length; |
| 205 | if (info.currentRss > info.max) info.max = info.currentRss; |
| 206 | if (info.currentRss == 0) { |
| 207 | if (DEBUG) Log.v(TAG, "update: pid " + pid + " has rss=0, it probably died"); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 208 | mData.remove(pid); |
| 209 | } |
| 210 | } |
| 211 | for (int i = mPids.size() - 1; i >= 0; i--) { |
| 212 | final long pid = mPids.get(i).intValue(); |
| 213 | if (mData.get(pid) == null) { |
| 214 | mPids.remove(i); |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 215 | logPids(); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } |
| 219 | if (mQSTile != null) mQSTile.update(); |
| 220 | } |
| 221 | |
| 222 | private void setTile(MemoryTile tile) { |
| 223 | mQSTile = tile; |
| 224 | if (tile != null) tile.update(); |
| 225 | } |
| 226 | |
| 227 | private static String formatBytes(long b) { |
| 228 | String[] SUFFIXES = {"B", "K", "M", "G", "T"}; |
| 229 | int i; |
| 230 | for (i = 0; i < SUFFIXES.length; i++) { |
| 231 | if (b < 1024) break; |
| 232 | b /= 1024; |
| 233 | } |
| 234 | return b + SUFFIXES[i]; |
| 235 | } |
| 236 | |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 237 | private Intent dumpHprofAndGetShareIntent() { |
| 238 | return mDumpTruck.captureHeaps(getTrackedProcesses()).createShareIntent(); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 241 | @Override |
| 242 | public void dump(@Nullable FileDescriptor fd, PrintWriter pw, @Nullable String[] args) { |
| 243 | pw.println("GarbageMonitor params:"); |
| 244 | pw.println(String.format(" mHeapLimit=%d KB", mHeapLimit)); |
| 245 | pw.println(String.format(" GARBAGE_INSPECTION_INTERVAL=%d (%.1f mins)", |
| 246 | GARBAGE_INSPECTION_INTERVAL, |
| 247 | (float) GARBAGE_INSPECTION_INTERVAL / DateUtils.MINUTE_IN_MILLIS)); |
| 248 | final float htiMins = HEAP_TRACK_INTERVAL / DateUtils.MINUTE_IN_MILLIS; |
| 249 | pw.println(String.format(" HEAP_TRACK_INTERVAL=%d (%.1f mins)", |
| 250 | HEAP_TRACK_INTERVAL, |
| 251 | htiMins)); |
| 252 | pw.println(String.format(" HEAP_TRACK_HISTORY_LEN=%d (%.1f hr total)", |
| 253 | HEAP_TRACK_HISTORY_LEN, |
| 254 | (float) HEAP_TRACK_HISTORY_LEN * htiMins / 60f)); |
| 255 | |
| 256 | pw.println("GarbageMonitor tracked processes:"); |
| 257 | |
| 258 | for (long pid : mPids) { |
| 259 | final ProcessMemInfo pmi = mData.get(pid); |
| 260 | if (pmi != null) { |
| 261 | pmi.dump(fd, pw, args); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 267 | private static class MemoryIconDrawable extends Drawable { |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 268 | long rss, limit; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 269 | final Drawable baseIcon; |
| 270 | final Paint paint = new Paint(); |
| 271 | final float dp; |
| 272 | |
| 273 | MemoryIconDrawable(Context context) { |
| 274 | baseIcon = context.getDrawable(R.drawable.ic_memory).mutate(); |
| 275 | dp = context.getResources().getDisplayMetrics().density; |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 276 | paint.setColor(QSTileImpl.getColorForState(context, STATE_ACTIVE)); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 277 | } |
| 278 | |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 279 | public void setRss(long rss) { |
| 280 | if (rss != this.rss) { |
| 281 | this.rss = rss; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 282 | invalidateSelf(); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | public void setLimit(long limit) { |
| 287 | if (limit != this.limit) { |
| 288 | this.limit = limit; |
| 289 | invalidateSelf(); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public void draw(Canvas canvas) { |
| 295 | baseIcon.draw(canvas); |
| 296 | |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 297 | if (limit > 0 && rss > 0) { |
| 298 | float frac = Math.min(1f, (float) rss / limit); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 299 | |
| 300 | final Rect bounds = getBounds(); |
| 301 | canvas.translate(bounds.left + 8 * dp, bounds.top + 5 * dp); |
| 302 | //android:pathData="M16.0,5.0l-8.0,0.0l0.0,14.0l8.0,0.0z" |
| 303 | canvas.drawRect(0, 14 * dp * (1 - frac), 8 * dp + 1, 14 * dp + 1, paint); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public void setBounds(int left, int top, int right, int bottom) { |
| 309 | super.setBounds(left, top, right, bottom); |
| 310 | baseIcon.setBounds(left, top, right, bottom); |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public int getIntrinsicHeight() { |
| 315 | return baseIcon.getIntrinsicHeight(); |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public int getIntrinsicWidth() { |
| 320 | return baseIcon.getIntrinsicWidth(); |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public void setAlpha(int i) { |
| 325 | baseIcon.setAlpha(i); |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public void setColorFilter(ColorFilter colorFilter) { |
| 330 | baseIcon.setColorFilter(colorFilter); |
| 331 | paint.setColorFilter(colorFilter); |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public void setTint(int tint) { |
| 336 | super.setTint(tint); |
| 337 | baseIcon.setTint(tint); |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public void setTintList(ColorStateList tint) { |
| 342 | super.setTintList(tint); |
| 343 | baseIcon.setTintList(tint); |
| 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public void setTintMode(PorterDuff.Mode tintMode) { |
| 348 | super.setTintMode(tintMode); |
| 349 | baseIcon.setTintMode(tintMode); |
| 350 | } |
| 351 | |
| 352 | @Override |
| 353 | public int getOpacity() { |
| 354 | return PixelFormat.TRANSLUCENT; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | private static class MemoryGraphIcon extends QSTile.Icon { |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 359 | long rss, limit; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 360 | |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 361 | public void setRss(long rss) { |
| 362 | this.rss = rss; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | public void setHeapLimit(long limit) { |
| 366 | this.limit = limit; |
| 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public Drawable getDrawable(Context context) { |
| 371 | final MemoryIconDrawable drawable = new MemoryIconDrawable(context); |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 372 | drawable.setRss(rss); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 373 | drawable.setLimit(limit); |
| 374 | return drawable; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | public static class MemoryTile extends QSTileImpl<QSTile.State> { |
| 379 | public static final String TILE_SPEC = "dbg:mem"; |
| 380 | |
Dan Sandler | 1e5d012 | 2019-06-06 23:35:22 -0400 | [diff] [blame] | 381 | // Tell QSTileHost.java to toss this into the default tileset? |
| 382 | public static final boolean ADD_TO_DEFAULT_ON_DEBUGGABLE_BUILDS = true; |
| 383 | |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 384 | private final GarbageMonitor gm; |
Fabian Kozynski | 8f70363 | 2019-09-27 10:26:44 -0400 | [diff] [blame] | 385 | private final ActivityStarter mActivityStarter; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 386 | private ProcessMemInfo pmi; |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 387 | private boolean dumpInProgress; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 388 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 389 | @Inject |
Fabian Kozynski | 8f70363 | 2019-09-27 10:26:44 -0400 | [diff] [blame] | 390 | public MemoryTile(QSHost host, GarbageMonitor monitor, ActivityStarter starter) { |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 391 | super(host); |
Robert Snoeberger | 81d7615 | 2019-09-30 15:43:34 -0400 | [diff] [blame] | 392 | gm = monitor; |
Fabian Kozynski | 8f70363 | 2019-09-27 10:26:44 -0400 | [diff] [blame] | 393 | mActivityStarter = starter; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public State newTileState() { |
| 398 | return new QSTile.State(); |
| 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public Intent getLongClickIntent() { |
| 403 | return new Intent(); |
| 404 | } |
| 405 | |
| 406 | @Override |
| 407 | protected void handleClick() { |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 408 | if (dumpInProgress) return; |
| 409 | |
| 410 | dumpInProgress = true; |
| 411 | refreshState(); |
| 412 | new Thread("HeapDumpThread") { |
| 413 | @Override |
| 414 | public void run() { |
| 415 | try { |
| 416 | // wait for animations & state changes |
| 417 | Thread.sleep(500); |
| 418 | } catch (InterruptedException ignored) { } |
| 419 | final Intent shareIntent = gm.dumpHprofAndGetShareIntent(); |
| 420 | mHandler.post(() -> { |
| 421 | dumpInProgress = false; |
| 422 | refreshState(); |
| 423 | getHost().collapsePanels(); |
Fabian Kozynski | 8f70363 | 2019-09-27 10:26:44 -0400 | [diff] [blame] | 424 | mActivityStarter.postStartActivityDismissingKeyguard(shareIntent, 0); |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 425 | }); |
| 426 | } |
| 427 | }.start(); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public int getMetricsCategory() { |
| 432 | return VIEW_UNKNOWN; |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public void handleSetListening(boolean listening) { |
| 437 | if (gm != null) gm.setTile(listening ? this : null); |
| 438 | |
| 439 | final ActivityManager am = mContext.getSystemService(ActivityManager.class); |
| 440 | if (listening && gm.mHeapLimit > 0) { |
| 441 | am.setWatchHeapLimit(1024 * gm.mHeapLimit); // why is this in bytes? |
| 442 | } else { |
| 443 | am.clearWatchHeapLimit(); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | @Override |
| 448 | public CharSequence getTileLabel() { |
| 449 | return getState().label; |
| 450 | } |
| 451 | |
| 452 | @Override |
| 453 | protected void handleUpdateState(State state, Object arg) { |
| 454 | pmi = gm.getMemInfo(Process.myPid()); |
| 455 | final MemoryGraphIcon icon = new MemoryGraphIcon(); |
| 456 | icon.setHeapLimit(gm.mHeapLimit); |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 457 | state.state = dumpInProgress ? STATE_UNAVAILABLE : STATE_ACTIVE; |
| 458 | state.label = dumpInProgress |
| 459 | ? "Dumping..." |
| 460 | : mContext.getString(R.string.heap_dump_tile_name); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 461 | if (pmi != null) { |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 462 | icon.setRss(pmi.currentRss); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 463 | state.secondaryLabel = |
| 464 | String.format( |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 465 | "rss: %s / %s", |
| 466 | formatBytes(pmi.currentRss * 1024), |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 467 | formatBytes(gm.mHeapLimit * 1024)); |
| 468 | } else { |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 469 | icon.setRss(0); |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 470 | state.secondaryLabel = null; |
| 471 | } |
| 472 | state.icon = icon; |
| 473 | } |
| 474 | |
| 475 | public void update() { |
| 476 | refreshState(); |
| 477 | } |
| 478 | |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 479 | public long getRss() { |
| 480 | return pmi != null ? pmi.currentRss : 0; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | public long getHeapLimit() { |
| 484 | return gm != null ? gm.mHeapLimit : 0; |
| 485 | } |
| 486 | } |
| 487 | |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 488 | /** */ |
| 489 | public static class ProcessMemInfo implements Dumpable { |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 490 | public long pid; |
| 491 | public String name; |
| 492 | public long startTime; |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 493 | public long currentRss; |
| 494 | public long[] rss = new long[HEAP_TRACK_HISTORY_LEN]; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 495 | public long max = 1; |
| 496 | public int head = 0; |
| 497 | |
| 498 | public ProcessMemInfo(long pid, String name, long start) { |
| 499 | this.pid = pid; |
| 500 | this.name = name; |
| 501 | this.startTime = start; |
| 502 | } |
| 503 | |
| 504 | public long getUptime() { |
| 505 | return System.currentTimeMillis() - startTime; |
| 506 | } |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 507 | |
| 508 | @Override |
| 509 | public void dump(@Nullable FileDescriptor fd, PrintWriter pw, @Nullable String[] args) { |
| 510 | pw.print("{ \"pid\": "); |
| 511 | pw.print(pid); |
| 512 | pw.print(", \"name\": \""); |
| 513 | pw.print(name.replace('"', '-')); |
| 514 | pw.print("\", \"start\": "); |
| 515 | pw.print(startTime); |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 516 | pw.print(", \"rss\": ["); |
| 517 | // write rss values starting from the oldest, which is rss[head], wrapping around to |
| 518 | // rss[(head-1) % rss.length] |
| 519 | for (int i = 0; i < rss.length; i++) { |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 520 | if (i > 0) pw.print(","); |
Robert Snoeberger | d78a8cf | 2019-10-04 16:28:31 -0400 | [diff] [blame] | 521 | pw.print(rss[(head + i) % rss.length]); |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 522 | } |
| 523 | pw.println("] }"); |
| 524 | } |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 525 | } |
| 526 | |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 527 | /** */ |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 528 | @Singleton |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 529 | public static class Service extends SystemUI implements Dumpable { |
Robert Snoeberger | 81d7615 | 2019-09-30 15:43:34 -0400 | [diff] [blame] | 530 | private final GarbageMonitor mGarbageMonitor; |
| 531 | |
| 532 | @Inject |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 533 | public Service(Context context, GarbageMonitor garbageMonitor) { |
| 534 | super(context); |
Robert Snoeberger | 81d7615 | 2019-09-30 15:43:34 -0400 | [diff] [blame] | 535 | mGarbageMonitor = garbageMonitor; |
| 536 | } |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 537 | |
| 538 | @Override |
| 539 | public void start() { |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 540 | boolean forceEnable = |
| 541 | Settings.Secure.getInt( |
| 542 | mContext.getContentResolver(), FORCE_ENABLE_LEAK_REPORTING, 0) |
| 543 | != 0; |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 544 | if (LEAK_REPORTING_ENABLED || forceEnable) { |
| 545 | mGarbageMonitor.startLeakMonitor(); |
| 546 | } |
| 547 | if (HEAP_TRACKING_ENABLED || forceEnable) { |
| 548 | mGarbageMonitor.startHeapTracking(); |
| 549 | } |
| 550 | } |
Dan Sandler | 646d66b | 2019-06-07 15:51:27 -0400 | [diff] [blame] | 551 | |
| 552 | @Override |
| 553 | public void dump(@Nullable FileDescriptor fd, PrintWriter pw, @Nullable String[] args) { |
| 554 | if (mGarbageMonitor != null) mGarbageMonitor.dump(fd, pw, args); |
| 555 | } |
Dan Sandler | 4d90d1e | 2018-03-23 16:29:06 -0400 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | private class BackgroundHeapCheckHandler extends Handler { |
| 559 | BackgroundHeapCheckHandler(Looper onLooper) { |
| 560 | super(onLooper); |
| 561 | if (Looper.getMainLooper().equals(onLooper)) { |
| 562 | throw new RuntimeException( |
| 563 | "BackgroundHeapCheckHandler may not run on the ui thread"); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | @Override |
| 568 | public void handleMessage(Message m) { |
| 569 | switch (m.what) { |
| 570 | case DO_GARBAGE_INSPECTION: |
| 571 | if (gcAndCheckGarbage()) { |
| 572 | postDelayed(GarbageMonitor.this::reinspectGarbageAfterGc, 100); |
| 573 | } |
| 574 | |
| 575 | removeMessages(DO_GARBAGE_INSPECTION); |
| 576 | sendEmptyMessageDelayed(DO_GARBAGE_INSPECTION, GARBAGE_INSPECTION_INTERVAL); |
| 577 | break; |
| 578 | |
| 579 | case DO_HEAP_TRACK: |
| 580 | update(); |
| 581 | removeMessages(DO_HEAP_TRACK); |
| 582 | sendEmptyMessageDelayed(DO_HEAP_TRACK, HEAP_TRACK_INTERVAL); |
| 583 | break; |
| 584 | } |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | } |