From d78a8cfe60a0df14416cd093b85458c072ad6c97 Mon Sep 17 00:00:00 2001 From: Robert Snoeberger Date: Fri, 4 Oct 2019 16:28:31 -0400 Subject: [PATCH] Change heap dump QS tile to collect/report RSS Bug: 137665250 Test: looked at memory shown under heap dump tile in QS Test: looked at GarbageMonitor dump in dumpsys Change-Id: Ia07d4169cf0b05fe1913e74b9059f8c6d5969897 --- .../android/systemui/util/leak/DumpTruck.java | 19 ++-- .../systemui/util/leak/GarbageMonitor.java | 106 ++++++++---------- 2 files changed, 57 insertions(+), 68 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/util/leak/DumpTruck.java b/packages/SystemUI/src/com/android/systemui/util/leak/DumpTruck.java index fa7af0be77f10..be5e0a0b12c5a 100644 --- a/packages/SystemUI/src/com/android/systemui/util/leak/DumpTruck.java +++ b/packages/SystemUI/src/com/android/systemui/util/leak/DumpTruck.java @@ -36,6 +36,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -53,7 +54,7 @@ public class DumpTruck { private final Context context; private Uri hprofUri; - private long pss; + private long rss; final StringBuilder body = new StringBuilder(); public DumpTruck(Context context) { @@ -66,7 +67,7 @@ public class DumpTruck { * @param pids * @return this, for chaining */ - public DumpTruck captureHeaps(int[] pids) { + public DumpTruck captureHeaps(List pids) { final GarbageMonitor gm = Dependency.get(GarbageMonitor.class); final File dumpDir = new File(context.getCacheDir(), FILEPROVIDER_PATH); @@ -79,8 +80,8 @@ public class DumpTruck { final ArrayList paths = new ArrayList(); final int myPid = android.os.Process.myPid(); - final int[] pids_copy = Arrays.copyOf(pids, pids.length); - for (int pid : pids_copy) { + for (Long pidL : pids) { + final int pid = pidL.intValue(); body.append(" pid ").append(pid); if (gm != null) { GarbageMonitor.ProcessMemInfo info = gm.getMemInfo(pid); @@ -88,11 +89,9 @@ public class DumpTruck { body.append(":") .append(" up=") .append(info.getUptime()) - .append(" pss=") - .append(info.currentPss) - .append(" uss=") - .append(info.currentUss); - pss = info.currentPss; + .append(" rss=") + .append(info.currentRss); + rss = info.currentRss; } } if (pid == myPid) { @@ -147,7 +146,7 @@ public class DumpTruck { shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); shareIntent.putExtra(Intent.EXTRA_SUBJECT, - String.format("SystemUI memory dump (pss=%dM)", pss / 1024)); + String.format("SystemUI memory dump (rss=%dM)", rss / 1024)); shareIntent.putExtra(Intent.EXTRA_TEXT, body.toString()); diff --git a/packages/SystemUI/src/com/android/systemui/util/leak/GarbageMonitor.java b/packages/SystemUI/src/com/android/systemui/util/leak/GarbageMonitor.java index 583f6b340d47e..c8490572a7847 100644 --- a/packages/SystemUI/src/com/android/systemui/util/leak/GarbageMonitor.java +++ b/packages/SystemUI/src/com/android/systemui/util/leak/GarbageMonitor.java @@ -35,7 +35,6 @@ import android.graphics.PorterDuff; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.Build; -import android.os.Debug; import android.os.Handler; import android.os.Looper; import android.os.Message; @@ -57,6 +56,7 @@ import com.android.systemui.qs.tileimpl.QSTileImpl; import java.io.FileDescriptor; import java.io.PrintWriter; import java.util.ArrayList; +import java.util.List; import javax.inject.Inject; import javax.inject.Named; @@ -102,7 +102,6 @@ public class GarbageMonitor implements Dumpable { private final LongSparseArray mData = new LongSparseArray<>(); private final ArrayList mPids = new ArrayList<>(); - private int[] mPidsArray = new int[1]; private long mHeapLimit; @@ -164,8 +163,8 @@ public class GarbageMonitor implements Dumpable { return mData.get(pid); } - public int[] getTrackedProcesses() { - return mPidsArray; + public List getTrackedProcesses() { + return mPids; } public void startTrackingProcess(long pid, String name, long start) { @@ -173,43 +172,40 @@ public class GarbageMonitor implements Dumpable { if (mPids.contains(pid)) return; mPids.add(pid); - updatePidsArrayL(); + logPids(); mData.put(pid, new ProcessMemInfo(pid, name, start)); } } - private void updatePidsArrayL() { - final int N = mPids.size(); - mPidsArray = new int[N]; - StringBuffer sb = new StringBuffer("Now tracking processes: "); - for (int i = 0; i < N; i++) { - final int p = mPids.get(i).intValue(); - mPidsArray[i] = p; - sb.append(p); - sb.append(" "); + private void logPids() { + if (DEBUG) { + StringBuffer sb = new StringBuffer("Now tracking processes: "); + for (int i = 0; i < mPids.size(); i++) { + final int p = mPids.get(i).intValue(); + sb.append(" "); + } + Log.v(TAG, sb.toString()); } - if (DEBUG) Log.v(TAG, sb.toString()); } private void update() { synchronized (mPids) { - Debug.MemoryInfo[] dinfos = mAm.getProcessMemoryInfo(mPidsArray); - for (int i = 0; i < dinfos.length; i++) { - Debug.MemoryInfo dinfo = dinfos[i]; - if (i > mPids.size()) { - if (DEBUG) Log.e(TAG, "update: unknown process info received: " + dinfo); + for (int i = 0; i < mPids.size(); i++) { + final int pid = mPids.get(i).intValue(); + // rssValues contains [VmRSS, RssFile, RssAnon, VmSwap]. + long[] rssValues = Process.getRss(pid); + if (rssValues == null && rssValues.length == 0) { + if (DEBUG) Log.e(TAG, "update: Process.getRss() didn't provide any values."); break; } - final long pid = mPids.get(i).intValue(); + long rss = rssValues[0]; final ProcessMemInfo info = mData.get(pid); - info.pss[info.head] = info.currentPss = dinfo.getTotalPss(); - info.uss[info.head] = info.currentUss = dinfo.getTotalPrivateDirty(); - info.head = (info.head + 1) % info.pss.length; - if (info.currentPss > info.max) info.max = info.currentPss; - if (info.currentUss > info.max) info.max = info.currentUss; - if (info.currentPss == 0) { - if (DEBUG) Log.v(TAG, "update: pid " + pid + " has pss=0, it probably died"); + info.rss[info.head] = info.currentRss = rss; + info.head = (info.head + 1) % info.rss.length; + if (info.currentRss > info.max) info.max = info.currentRss; + if (info.currentRss == 0) { + if (DEBUG) Log.v(TAG, "update: pid " + pid + " has rss=0, it probably died"); mData.remove(pid); } } @@ -217,7 +213,7 @@ public class GarbageMonitor implements Dumpable { final long pid = mPids.get(i).intValue(); if (mData.get(pid) == null) { mPids.remove(i); - updatePidsArrayL(); + logPids(); } } } @@ -270,7 +266,7 @@ public class GarbageMonitor implements Dumpable { private static class MemoryIconDrawable extends Drawable { - long pss, limit; + long rss, limit; final Drawable baseIcon; final Paint paint = new Paint(); final float dp; @@ -281,9 +277,9 @@ public class GarbageMonitor implements Dumpable { paint.setColor(QSTileImpl.getColorForState(context, STATE_ACTIVE)); } - public void setPss(long pss) { - if (pss != this.pss) { - this.pss = pss; + public void setRss(long rss) { + if (rss != this.rss) { + this.rss = rss; invalidateSelf(); } } @@ -299,8 +295,8 @@ public class GarbageMonitor implements Dumpable { public void draw(Canvas canvas) { baseIcon.draw(canvas); - if (limit > 0 && pss > 0) { - float frac = Math.min(1f, (float) pss / limit); + if (limit > 0 && rss > 0) { + float frac = Math.min(1f, (float) rss / limit); final Rect bounds = getBounds(); canvas.translate(bounds.left + 8 * dp, bounds.top + 5 * dp); @@ -361,10 +357,10 @@ public class GarbageMonitor implements Dumpable { } private static class MemoryGraphIcon extends QSTile.Icon { - long pss, limit; + long rss, limit; - public void setPss(long pss) { - this.pss = pss; + public void setRss(long rss) { + this.rss = rss; } public void setHeapLimit(long limit) { @@ -374,7 +370,7 @@ public class GarbageMonitor implements Dumpable { @Override public Drawable getDrawable(Context context) { final MemoryIconDrawable drawable = new MemoryIconDrawable(context); - drawable.setPss(pss); + drawable.setRss(rss); drawable.setLimit(limit); return drawable; } @@ -462,14 +458,14 @@ public class GarbageMonitor implements Dumpable { ? "Dumping..." : mContext.getString(R.string.heap_dump_tile_name); if (pmi != null) { - icon.setPss(pmi.currentPss); + icon.setRss(pmi.currentRss); state.secondaryLabel = String.format( - "pss: %s / %s", - formatBytes(pmi.currentPss * 1024), + "rss: %s / %s", + formatBytes(pmi.currentRss * 1024), formatBytes(gm.mHeapLimit * 1024)); } else { - icon.setPss(0); + icon.setRss(0); state.secondaryLabel = null; } state.icon = icon; @@ -479,8 +475,8 @@ public class GarbageMonitor implements Dumpable { refreshState(); } - public long getPss() { - return pmi != null ? pmi.currentPss : 0; + public long getRss() { + return pmi != null ? pmi.currentRss : 0; } public long getHeapLimit() { @@ -493,9 +489,8 @@ public class GarbageMonitor implements Dumpable { public long pid; public String name; public long startTime; - public long currentPss, currentUss; - public long[] pss = new long[HEAP_TRACK_HISTORY_LEN]; - public long[] uss = new long[HEAP_TRACK_HISTORY_LEN]; + public long currentRss; + public long[] rss = new long[HEAP_TRACK_HISTORY_LEN]; public long max = 1; public int head = 0; @@ -517,17 +512,12 @@ public class GarbageMonitor implements Dumpable { pw.print(name.replace('"', '-')); pw.print("\", \"start\": "); pw.print(startTime); - pw.print(", \"pss\": ["); - // write pss values starting from the oldest, which is pss[head], wrapping around to - // pss[(head-1) % pss.length] - for (int i = 0; i < pss.length; i++) { + pw.print(", \"rss\": ["); + // write rss values starting from the oldest, which is rss[head], wrapping around to + // rss[(head-1) % rss.length] + for (int i = 0; i < rss.length; i++) { if (i > 0) pw.print(","); - pw.print(pss[(head + i) % pss.length]); - } - pw.print("], \"uss\": ["); - for (int i = 0; i < uss.length; i++) { - if (i > 0) pw.print(","); - pw.print(uss[(head + i) % uss.length]); + pw.print(rss[(head + i) % rss.length]); } pw.println("] }"); }