源码基于:Android U
参考:
dumpsys meminfo 详解(R)
dumpsys meminfo 详解(U)
1. 命令入口 MemBinder
frameworks/base/services/core/java/com/android/server/am/AMS.java
static class MemBinder extends Binder {
ActivityManagerService mActivityManagerService;
MemBinder(ActivityManagerService activityManagerService) {
mActivityManagerService = activityManagerService;
}
@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
...
}
}
2. 根据参数收集 procs
dump 流程首先会调用 collectProcesses() 函数来解析需要dump 的进程 ProcessRecord,并且会将该 ProcessRecord list 以参数形式传入 dumpApplicationMemoryUsage() 函数,这也是 dump 的核心处理函数。
frameworks/base/services/core/java/com/android/server/am/AMS.java
ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, boolean allPkgs,
String[] args) {
synchronized (mProcLock) {
return mProcessList.collectProcessesLOSP(start, allPkgs, args);
}
}
代码还是比较清晰的:
- 如果命令行指定了pid,那么就收集这些进程;
- 如果设定了packageName,就收集这些package;
- 如果没有设定,则收集所有的 LRU process;
注意,前两点有可能收集的 proc 为空,因为设定的参数有可能是假的或者无法匹配。
这个时候终端上还提示No process:
shift:/ # dumpsys meminfo 12345
No process found for: 12345
3. oomOnly 影响内存收集
当收集好 procs 之后,会通过 for 循环进行轮询,通过Debug 提供的内存获取函数,分别统计每个 pid 对应的 smaps 信息。
但,收集内存的函数有两个选择:
- Debug.getMemoryInfo()
- Debug.getPss()
if (!brief && !opts.oomOnly) {
...
if (!Debug.getMemoryInfo(st.pid, info)) {
return;
}
} else {
long pss = Debug.getPss(st.pid, tmpLong, memtrackTmp);
...
}
brief 是从 MemBinder 传入,默认为 false;
opts.oomOnly 是命令行是否带有 --oom 参数的flag,如果不带则调用 getMemoryInfo() 函数,如果带有则调用 getPss() 函数。区别在于 oomOnly 如果为 true时,不需要dump category 信息,而当 oomOnly 为false 时,需要 dump category 信息:
Total PSS by category:
330,928K: EGL mtrack
270,815K: Native
240,812K: Dalvik
168,739K: .apk mmap
154,683K: .art mmap
140,548K: .so mmap
109,070K: .dex mmap
99,964K: GL mtrack
92,736K: Dalvik Other
44,257K: .jar mmap
36,406K: Other mmap
33,019K: Stack
31,307K: Unknown
15,729K: .oat mmap
1,670K: Other dev
1,013K: Ashmem
848K: .ttf mmap
0K: Cursor
0K: Gfx dev
0K: Other mtrack
所以,getMemoryInfo() 中会统计更详细的每个 which_heap 的 pss、rss、swap 等等信息,而 getPss() 函数则只需要统计每个进程的 rss、pss、swap 整体信息即可。
3.1 Debug.getMemoryInfo()
frameworks/base/core/jni/android_os_Debug.cpp
static jboolean android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
jint pid, jobject object)
{
bool foundSwapPss;
stats_t stats[_NUM_HEAP];
memset(&stats, 0, sizeof(stats));
if (!load_maps(pid, stats, &foundSwapPss)) {
return JNI_FALSE;
}
struct graphics_memory_pss graphics_mem;
if (read_memtrack_memory(pid, &graphics_mem) == 0) {
stats[HEAP_GRAPHICS].pss = graphics_mem.graphics;
stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics;
stats[HEAP_GRAPHICS].rss = graphics_mem.graphics;
stats[HEAP_GL].pss = graphics_mem.gl;
stats[HEAP_GL].privateDirty = graphics_mem.gl;
stats[HEAP_GL].rss = graphics_mem.gl;
stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other;
stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other;
stats[HEAP_OTHER_MEMTRACK].rss = graphics_mem.other;
}
for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
stats[HEAP_UNKNOWN].pss += stats[i].pss;
stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss;
stats[HEAP_UNKNOWN].rss += stats[i].rss;
stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean;
stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean;
stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut;
stats[HEAP_UNKNOWN].swappedOutPss += stats[i].swappedOutPss;
}
for (int i=0; i<_NUM_CORE_HEAP; i++) {
env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss);
env->SetIntField(object, stat_fields[i].rss_field, stats[i].rss);
env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean);
env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean);
env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut);
env->SetIntField(object, stat_fields[i].swappedOutPss_field, stats[i].swappedOutPss);
}
env->SetBooleanField(object, hasSwappedOutPss_field, foundSwapPss);
jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
if (otherArray == NULL) {
return JNI_FALSE;
}
int j=0;
for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
otherArray[j++] = stats[i].pss;
otherArray[j++] = stats[i].swappablePss;
otherArray[j++] = stats[i].rss;
otherArray[j++] = stats[i].privateDirty;
otherArray[j++] = stats[i].sharedDirty;
otherArray[j++] = stats[i].privateClean;
otherArray[j++] = stats[i].sharedClean;
otherArray[j++] = stats[i].swappedOut;
otherArray[j++] = stats[i].swappedOutPss;
}
env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
return JNI_TRUE;
}
- 通过 load_maps() 从 /proc/pid/smaps 中获取进程的内存信息;
- 通过 read_memtrack_memory() 通过 libmemtrack.so 获取 graphics 的内存信息;
- 通过 env->SetIntField() 将 UNKNOWN、dalvik、heap 所属组的信息设置到 java 接口的参数 Meminfo 中;
- 通过 otherArray 数组对应 java 端 MemoryInfo.otherStats[] 数组;
注意:
除了 HEAP_DALVIK 和 HEAP_NATIVE,其他所有 which_heap 的信息都会累加到 HEAP_UNKNOWN 中。这样,在 AMS 中会统计重要的字段信息,其他都认为是 unknown,如下:
Total PSS by category:
330,928K: EGL mtrack
270,815K: Native
240,812K: Dalvik
168,739K: .apk mmap
154,683K: .art mmap
140,548K: .so mmap
109,070K: .dex mmap
99,964K: GL mtrack
92,736K: Dalvik Other
44,257K: .jar mmap
36,406K: Other mmap
33,019K: Stack
31,307K: Unknown
15,729K: .oat mmap
1,670K: Other dev
1,013K: Ashmem
848K: .ttf mmap
0K: Cursor
0K: Gfx dev
0K: Other mtrack
3.2 Debug.getPss()
frameworks/base/core/jni/android_os_Debug.cpp
static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid,
jlongArray outUssSwapPssRss, jlongArray outMemtrack)
{
jlong pss = 0;
jlong rss = 0;
jlong swapPss = 0;
jlong uss = 0;
jlong memtrack = 0;
struct graphics_memory_pss graphics_mem;
if (read_memtrack_memory(pid, &graphics_mem) == 0) {
pss = uss = rss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other;
}
::android::meminfo::ProcMemInfo proc_mem(pid);
::android::meminfo::MemUsage stats;
if (proc_mem.SmapsOrRollup(&stats)) {
pss += stats.pss;
uss += stats.uss;
rss += stats.rss;
swapPss = stats.swap_pss;
pss += swapPss; // Also in swap, those pages would be accounted as Pss without SWAP
} else {
return 0;
}
if (outUssSwapPssRss != NULL) {
int outLen = env->GetArrayLength(outUssSwapPssRss);
if (outLen >= 1) {
jlong* outUssSwapPssRssArray = env->GetLongArrayElements(outUssSwapPssRss, 0);
if (outUssSwapPssRssArray != NULL) {
outUssSwapPssRssArray[0] = uss;
if (outLen >= 2) {
outUssSwapPssRssArray[1] = swapPss;
}
if (outLen >= 3) {
outUssSwapPssRssArray[2] = rss;
}
}
env->ReleaseLongArrayElements(outUssSwapPssRss, outUssSwapPssRssArray, 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 pss;
}
- 通过 read_memtrace_memory() 统计graphics 的内存信息,并都累计到总的 pss、rss、uss、memtrack 中;
- 通过 proc_mem.SmapsOrRollup() 函数统计 /proc/pid/smaps 中 Pss、Private_Clean、Private_Dirty、Rss、SwapPss 等信息;
- 该函数返回的是进程的 pss,包括 swapPss。其他数据通过两个数组参数回传;
注意:
无论 getMemoryInfo() 之后在 AMS 中计算,还是getPss() 在 android_os_Debug.cpp 中计算,total 的pss,都是需要加上 swap out pss;
4. graphics 的内存收集函数
如上,无论是 getMemoryInfo() 还是 getPss() 函数,都会调用 read_memtrace_memory() 函数收集 graphics 的内存。
主要是通过libmemtrack.so,详细可以查看 dumpsys meminfo 详解(U) 一文。
5. dumpDetails 影响细节打印
opts.dumpDetails 是命令行是否带有 -a 或 -s 参数的 flag,当带有该参数时需要收集进程的所有category 信息。所以,收集函数是调用 getMemoryInfo(),而不是 getPss()。
另外,当该 flag 为true时,dump 流程会调用 thread.dumpMemInfo() 函数,会通过 getRuntime() 获取app 进程dalvik 的 totalMemory 和 freeMemory,并计算出 dalvikAllocated,得到app 进程虚拟机内存使用情况。 详细可以查看 dumpsys meminfo 详解(U) 一文。
最终结果如下图:
详细代码可以查看 android_os_Debug.cpp,或者 dumpsys meminfo 详解(U) 一文。