summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Kevin Jeon <kevinjeon@google.com> 2023-08-25 15:23:21 -0400
committer Kevin Jeon <kevinjeon@google.com> 2023-09-15 20:46:15 -0400
commit7aa6d65722f142c8d813d84855d33bc75a7e85c3 (patch)
tree1642352757167c657c6e3a84ee76454b28846336
parent71d19fb98cac5b0aeef49e5a5de210f146033701 (diff)
Add Debug.getRss()
This change adds the getRss() method, which will return RSS (as estimated by /proc/<pid>/status) and memtrack graphics, memtrack gl, and memtrack other. This is intended to be used a drop-in replacement for getPss() where efficiency is a higher priority than accuracy. Unlike getPss(), this method does not provide the option for collecting USS or SwapPss as these are not emitted in status. There are some formatting changes included here that were required for 'repo upload'. Test: Added method call in CTS's DebugTest; included unit test for backing ProcMemInfo::StatusRss method. Bug: 296454553 Change-Id: I20e7999dce46f6cdf01cb740ab9576937711dfef
-rw-r--r--core/api/current.txt1
-rw-r--r--core/java/android/os/Debug.java18
-rw-r--r--core/jni/android_os_Debug.cpp138
3 files changed, 101 insertions, 56 deletions
diff --git a/core/api/current.txt b/core/api/current.txt
index 6c6784e10c59..1a9612e2240f 100644
--- a/core/api/current.txt
+++ b/core/api/current.txt
@@ -32285,6 +32285,7 @@ package android.os {
method public static long getNativeHeapFreeSize();
method public static long getNativeHeapSize();
method public static long getPss();
+ method @FlaggedApi(Flags.FLAG_REMOVE_APP_PROFILER_PSS_COLLECTION) public static long getRss();
method public static String getRuntimeStat(String);
method public static java.util.Map<java.lang.String,java.lang.String> getRuntimeStats();
method @Deprecated public static int getThreadAllocCount();
diff --git a/core/java/android/os/Debug.java b/core/java/android/os/Debug.java
index 62d9c69565da..c527cb5344c1 100644
--- a/core/java/android/os/Debug.java
+++ b/core/java/android/os/Debug.java
@@ -16,6 +16,7 @@
package android.os;
+import android.annotation.FlaggedApi;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.AppGlobals;
@@ -1953,6 +1954,23 @@ public final class Debug
*/
public static native long getPss(int pid, long[] outUssSwapPssRss, long[] outMemtrack);
+ /**
+ * Retrieves the RSS memory used by the process as given by the status file.
+ */
+ @FlaggedApi(Flags.FLAG_REMOVE_APP_PROFILER_PSS_COLLECTION)
+ public static native long getRss();
+
+ /**
+ * Retrieves the RSS memory used by the process as given by the status file. Optionally supply a
+ * long array of up to 4 entries to retrieve the total memtrack reported size, memtrack
+ * graphics, memtrack gl, and memtrack other.
+ *
+ * @return The RSS memory usage, or 0 if retrieval failed (i.e. the PID is gone).
+ * @hide
+ */
+ @FlaggedApi(Flags.FLAG_REMOVE_APP_PROFILER_PSS_COLLECTION)
+ public static native long getRss(int pid, long[] outMemtrack);
+
/** @hide */
public static final int MEMINFO_TOTAL = 0;
/** @hide */
diff --git a/core/jni/android_os_Debug.cpp b/core/jni/android_os_Debug.cpp
index fe957624cf4b..e0bcef642d82 100644
--- a/core/jni/android_os_Debug.cpp
+++ b/core/jni/android_os_Debug.cpp
@@ -565,6 +565,51 @@ static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
return android_os_Debug_getPssPid(env, clazz, getpid(), NULL, NULL);
}
+static jlong android_os_Debug_getRssPid(JNIEnv* env, jobject clazz, jint pid,
+ jlongArray outMemtrack) {
+ jlong rss = 0;
+ jlong memtrack = 0;
+
+ struct graphics_memory_pss graphics_mem;
+ if (read_memtrack_memory(pid, &graphics_mem) == 0) {
+ rss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other;
+ }
+
+ ::android::meminfo::ProcMemInfo proc_mem(pid);
+ uint64_t status_rss;
+ if (proc_mem.StatusVmRSS(&status_rss)) {
+ rss += status_rss;
+ } else {
+ return 0;
+ }
+
+ if (outMemtrack != NULL) {
+ int outLen = env->GetArrayLength(outMemtrack);
+ if (outLen >= 1) {
+ jlong* outMemtrackArray = env->GetLongArrayElements(outMemtrack, 0);
+ if (outMemtrackArray != NULL) {
+ outMemtrackArray[0] = memtrack;
+ if (outLen >= 2) {
+ outMemtrackArray[1] = graphics_mem.graphics;
+ }
+ if (outLen >= 3) {
+ outMemtrackArray[2] = graphics_mem.gl;
+ }
+ if (outLen >= 4) {
+ outMemtrackArray[3] = graphics_mem.other;
+ }
+ }
+ env->ReleaseLongArrayElements(outMemtrack, outMemtrackArray, 0);
+ }
+ }
+
+ return rss;
+}
+
+static jlong android_os_Debug_getRss(JNIEnv* env, jobject clazz) {
+ return android_os_Debug_getRssPid(env, clazz, getpid(), NULL);
+}
+
// The 1:1 mapping of MEMINFO_* enums here must match with the constants from
// Debug.java.
enum {
@@ -974,62 +1019,43 @@ static jboolean android_os_Debug_isVmapStack(JNIEnv *env, jobject clazz)
*/
static const JNINativeMethod gMethods[] = {
- { "getNativeHeapSize", "()J",
- (void*) android_os_Debug_getNativeHeapSize },
- { "getNativeHeapAllocatedSize", "()J",
- (void*) android_os_Debug_getNativeHeapAllocatedSize },
- { "getNativeHeapFreeSize", "()J",
- (void*) android_os_Debug_getNativeHeapFreeSize },
- { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
- (void*) android_os_Debug_getDirtyPages },
- { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)Z",
- (void*) android_os_Debug_getDirtyPagesPid },
- { "getPss", "()J",
- (void*) android_os_Debug_getPss },
- { "getPss", "(I[J[J)J",
- (void*) android_os_Debug_getPssPid },
- { "getMemInfo", "([J)V",
- (void*) android_os_Debug_getMemInfo },
- { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
- (void*) android_os_Debug_dumpNativeHeap },
- { "dumpNativeMallocInfo", "(Ljava/io/FileDescriptor;)V",
- (void*) android_os_Debug_dumpNativeMallocInfo },
- { "getBinderSentTransactions", "()I",
- (void*) android_os_Debug_getBinderSentTransactions },
- { "getBinderReceivedTransactions", "()I",
- (void*) android_os_getBinderReceivedTransactions },
- { "getBinderLocalObjectCount", "()I",
- (void*)android_os_Debug_getLocalObjectCount },
- { "getBinderProxyObjectCount", "()I",
- (void*)android_os_Debug_getProxyObjectCount },
- { "getBinderDeathObjectCount", "()I",
- (void*)android_os_Debug_getDeathObjectCount },
- { "dumpJavaBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
- (void*)android_os_Debug_dumpJavaBacktraceToFileTimeout },
- { "dumpNativeBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
- (void*)android_os_Debug_dumpNativeBacktraceToFileTimeout },
- { "getUnreachableMemory", "(IZ)Ljava/lang/String;",
- (void*)android_os_Debug_getUnreachableMemory },
- { "getZramFreeKb", "()J",
- (void*)android_os_Debug_getFreeZramKb },
- { "getIonHeapsSizeKb", "()J",
- (void*)android_os_Debug_getIonHeapsSizeKb },
- { "getDmabufTotalExportedKb", "()J",
- (void*)android_os_Debug_getDmabufTotalExportedKb },
- { "getGpuPrivateMemoryKb", "()J",
- (void*)android_os_Debug_getGpuPrivateMemoryKb },
- { "getDmabufHeapTotalExportedKb", "()J",
- (void*)android_os_Debug_getDmabufHeapTotalExportedKb },
- { "getIonPoolsSizeKb", "()J",
- (void*)android_os_Debug_getIonPoolsSizeKb },
- { "getDmabufMappedSizeKb", "()J",
- (void*)android_os_Debug_getDmabufMappedSizeKb },
- { "getDmabufHeapPoolsSizeKb", "()J",
- (void*)android_os_Debug_getDmabufHeapPoolsSizeKb },
- { "getGpuTotalUsageKb", "()J",
- (void*)android_os_Debug_getGpuTotalUsageKb },
- { "isVmapStack", "()Z",
- (void*)android_os_Debug_isVmapStack },
+ {"getNativeHeapSize", "()J", (void*)android_os_Debug_getNativeHeapSize},
+ {"getNativeHeapAllocatedSize", "()J", (void*)android_os_Debug_getNativeHeapAllocatedSize},
+ {"getNativeHeapFreeSize", "()J", (void*)android_os_Debug_getNativeHeapFreeSize},
+ {"getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
+ (void*)android_os_Debug_getDirtyPages},
+ {"getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)Z",
+ (void*)android_os_Debug_getDirtyPagesPid},
+ {"getPss", "()J", (void*)android_os_Debug_getPss},
+ {"getPss", "(I[J[J)J", (void*)android_os_Debug_getPssPid},
+ {"getRss", "()J", (void*)android_os_Debug_getRss},
+ {"getRss", "(I[J)J", (void*)android_os_Debug_getRssPid},
+ {"getMemInfo", "([J)V", (void*)android_os_Debug_getMemInfo},
+ {"dumpNativeHeap", "(Ljava/io/FileDescriptor;)V", (void*)android_os_Debug_dumpNativeHeap},
+ {"dumpNativeMallocInfo", "(Ljava/io/FileDescriptor;)V",
+ (void*)android_os_Debug_dumpNativeMallocInfo},
+ {"getBinderSentTransactions", "()I", (void*)android_os_Debug_getBinderSentTransactions},
+ {"getBinderReceivedTransactions", "()I", (void*)android_os_getBinderReceivedTransactions},
+ {"getBinderLocalObjectCount", "()I", (void*)android_os_Debug_getLocalObjectCount},
+ {"getBinderProxyObjectCount", "()I", (void*)android_os_Debug_getProxyObjectCount},
+ {"getBinderDeathObjectCount", "()I", (void*)android_os_Debug_getDeathObjectCount},
+ {"dumpJavaBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
+ (void*)android_os_Debug_dumpJavaBacktraceToFileTimeout},
+ {"dumpNativeBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
+ (void*)android_os_Debug_dumpNativeBacktraceToFileTimeout},
+ {"getUnreachableMemory", "(IZ)Ljava/lang/String;",
+ (void*)android_os_Debug_getUnreachableMemory},
+ {"getZramFreeKb", "()J", (void*)android_os_Debug_getFreeZramKb},
+ {"getIonHeapsSizeKb", "()J", (void*)android_os_Debug_getIonHeapsSizeKb},
+ {"getDmabufTotalExportedKb", "()J", (void*)android_os_Debug_getDmabufTotalExportedKb},
+ {"getGpuPrivateMemoryKb", "()J", (void*)android_os_Debug_getGpuPrivateMemoryKb},
+ {"getDmabufHeapTotalExportedKb", "()J",
+ (void*)android_os_Debug_getDmabufHeapTotalExportedKb},
+ {"getIonPoolsSizeKb", "()J", (void*)android_os_Debug_getIonPoolsSizeKb},
+ {"getDmabufMappedSizeKb", "()J", (void*)android_os_Debug_getDmabufMappedSizeKb},
+ {"getDmabufHeapPoolsSizeKb", "()J", (void*)android_os_Debug_getDmabufHeapPoolsSizeKb},
+ {"getGpuTotalUsageKb", "()J", (void*)android_os_Debug_getGpuTotalUsageKb},
+ {"isVmapStack", "()Z", (void*)android_os_Debug_isVmapStack},
};
int register_android_os_Debug(JNIEnv *env)