本篇主要分三部分
- oomadj的计算时机
- oomadj的计算过程
- oomadj计算结果应用
一 架构图
二 流程图
三 常用命令
- adb shell dumpsys meminfo com.google.android.apps.maps -d
- cat /proc/meminfo
四 源码解析
4.1oomadj的更新范围
oomadj的更新分为两部分
- 指定proc更新oomadj
- 更新全部进程的oomadj
@GuardedBy("this")
final void updateOomAdjLocked(@OomAdjReason int oomAdjReason) {
mOomAdjuster.updateOomAdjLocked(oomAdjReason);
}
/**
* Update OomAdj for a specific process and its reachable processes.
*
* @param app The process to update
* @param oomAdjReason
* @return whether updateOomAdjLocked(app) was successful.
*/
@GuardedBy("this")
final boolean updateOomAdjLocked(ProcessRecord app, @OomAdjReason int oomAdjReason) {
return mOomAdjuster.updateOomAdjLocked(app, oomAdjReason);
}
ams提供两个api方法以供调用,参数含有proc则指定更新进程,否则更新全部进程。
4.2 oomadj的更新过程。
冷启动一个activity进程,观察其oomadj的变化。
4.2.1 AMS-attachApplicationLocked]
app.mState.setCurAdj(ProcessList.INVALID_ADJ);
app.mState.setSetAdj(ProcessList.INVALID_ADJ);
app.mState.setVerifiedAdj(ProcessList.INVALID_ADJ);
mOomAdjuster.setAttachingSchedGroupLSP(app);
app.mState.setForcingToImportant(null);
clearProcessForegroundLocked(app);
app.mState.setHasShownUi(false);
app.mState.setCached(false);
在应用进程启动后绑定AMS时,adj是默认的-10000。 是否cached进程设置为false。
4.2.2 ActivityTaskSupervisor-realStartActivityLocked
// Perform OOM scoring after the activity state is set, so the process can be updated with
// the latest state.
proc.onStartActivity(mService.mTopProcessState, r.info);
realStartActivityLocked ->
WindowProcessController.onStartActivity->
ProcessRecord.onStartActivity->
ProcessRecord.updateProcessInfo->
AMS.updateOomAdjLocked(this, OOM_ADJ_REASON_ACTIVITY);
mOomAdjuster.updateOomAdjLocked(app, oomAdjReason);
4.2.3 OomAdjuster.updateOomAdjLocked
/**
* Update OomAdj for specific process and its reachable processes (with direction/indirect
* bindings from this process); Note its clients' proc state won't be re-evaluated if this proc
* is hosting any service/content provider.
*
* @param app The process to update, or null to update all processes
* @param oomAdjReason
*/
@GuardedBy("mService")
boolean updateOomAdjLocked(ProcessRecord app, @OomAdjReason int oomAdjReason) {
synchronized (mProcLock) {
return updateOomAdjLSP(app, oomAdjReason);
}
}
更新指定进程的adj并且将它client端的oomadj也进行更新。
@GuardedBy({"mService", "mProcLock"})
private boolean updateOomAdjLSP(ProcessRecord app, @OomAdjReason int oomAdjReason) {
if (app == null || !mConstants.OOMADJ_UPDATE_QUICK) {
updateOomAdjLSP(oomAdjReason);
return true;
}
//如果正在更新oomadj那么退出。
if (checkAndEnqueueOomAdjTargetLocked(app)) {
// Simply return true as there is an oomAdjUpdate ongoing
return true;
}
try {
//正在更新oomadj
mOomAdjUpdateOngoing = true;
//执行更新oomadj
return performUpdateOomAdjLSP(app, oomAdjReason);
} finally {
// Kick off the handling of any pending targets enqueued during the above update
mOomAdjUpdateOngoing = false;
updateOomAdjPendingTargetsLocked(oomAdjReason);
}
}
【performUpdateOomAdjLSP】
@GuardedBy({"mService", "mProcLock"})
private boolean performUpdateOomAdjLSP(ProcessRecord app, @OomAdjReason int oomAdjReason) {
//从ams拿到topApp。
final ProcessRecord topApp = mService.getTopApp();
//开始抓trace
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, oomAdjReasonToString(oomAdjReason));
mService.mOomAdjProfiler.oomAdjStarted();
mAdjSeq++;
// Firstly, try to see if the importance of itself gets changed
final ProcessStateRecord state = app.mState;
final boolean wasCached = state.isCached();
final int oldAdj = state.getCurRawAdj();
//新启动的proc,adj开始在ams被赋值INVALID_ADJ -10000.这个理给他赋值UNKNOWN_ADJ
final int cachedAdj = oldAdj >= CACHED_APP_MIN_ADJ
? oldAdj : UNKNOWN_ADJ;
//...省略
boolean success = performUpdateOomAdjLSP(app, cachedAdj, topApp,
SystemClock.uptimeMillis(), oomAdjReason);
【performUpdateOomAdjLSP】
private boolean performUpdateOomAdjLSP(ProcessRecord app, int cachedAdj,
ProcessRecord topApp, long now, @OomAdjReason int oomAdjReason) {
if (app.getThread() == null) {
return false;
}
// ...省略
computeOomAdjLSP(app, cachedAdj, topApp, false, now, false, true);
//。。。省略
return applyOomAdjLSP(app, false, now, SystemClock.elapsedRealtime(), oomAdjReason);
4.2.4 计算oomadj-computeOomAdjLSP
【computeOomAdjLSP】
这个函数是真正为proc计算oomadj的函数。
@GuardedBy({"mService", "mProcLock"})
private boolean computeOomAdjLSP(ProcessRecord app, int cachedAdj,
ProcessRecord topApp, boolean doingAll, long now, boolean cycleReEval,
boolean computeClients) {
final ProcessStateRecord state = app.mState;
//。。。省略
//1.如果进程是为启动activity,则会走这里
if (app == topApp && PROCESS_STATE_CUR_TOP == PROCESS_STATE_TOP) {
// The last app on the list is the foreground app.
adj = FOREGROUND_APP_ADJ;
if (mService.mAtmInternal.useTopSchedGroupForTopProcess()) {
schedGroup = SCHED_GROUP_TOP_APP;
state.setAdjType("top-activity");
} else {
// Demote the scheduling group to avoid CPU contention if there is another more
// important process which also uses top-app, such as if SystemUI is animating.
schedGroup = SCHED_GROUP_DEFAULT;
state.setAdjType("intermediate-top-activity");
}
foregroundActivities = true;
hasVisibleActivities = true;
procState = PROCESS_STATE_TOP;
if (DEBUG_OOM_ADJ_REASON || logUid == appUid) {
reportOomAdjMessageLocked(TAG_OOM_ADJ, "Making top: " + app);
}
//是否正在运行动画
} else if (state.isRunningRemoteAnimation()) {
adj = VISIBLE_APP_ADJ;
schedGroup = SCHED_GROUP_TOP_APP;
state.setAdjType("running-remote-anim");
procState = PROCESS_STATE_CUR_TOP;
if (DEBUG_OOM_ADJ_REASON || logUid == appUid) {
reportOomAdjMessageLocked(TAG_OOM_ADJ, "Making running remote anim: " + app);
}
//是否正在做测试
} else if (app.getActiveInstrumentation() != null) {
// Don't want to kill running instrumentation.
adj = FOREGROUND_APP_ADJ;
schedGroup = SCHED_GROUP_DEFAULT;
state.setAdjType("instrumentation");
procState = PROCESS_STATE_FOREGROUND_SERVICE;
capability |= PROCESS_CAPABILITY_BFSL;
if (DEBUG_OOM_ADJ_REASON || logUid == appUid) {
reportOomAdjMessageLocked(TAG_OOM_ADJ, "Making instrumentation: " + app);
}
//是否启动进程接收广播
} else if (state.getCachedIsReceivingBroadcast(mTmpSchedGroup)) {
// An app that is currently receiving a broadcast also
// counts as being in the foreground for OOM killer purposes.
// It's placed in a sched group based on the nature of the
// broadcast as reflected by which queue it's active in.
adj = FOREGROUND_APP_ADJ;
schedGroup = mTmpSchedGroup[0];
state.setAdjType("broadcast");
procState = ActivityManager.PROCESS_STATE_RECEIVER;
if (DEBUG_OOM_ADJ_REASON || logUid == appUid) {
reportOomAdjMessageLocked(TAG_OOM_ADJ, "Making broadcast: " + app);
}
//是否启动进程执行service
} else if (psr.numberOfExecutingServices() > 0) {
// An app that is currently executing a service callback also
// counts as being in the foreground.
adj = FOREGROUND_APP_ADJ;
schedGroup = psr.shouldExecServicesFg()
? SCHED_GROUP_DEFAULT : SCHED_GROUP_BACKGROUND;
state.setAdjType("exec-service");
procState = PROCESS_STATE_SERVICE;
if (DEBUG_OOM_ADJ_REASON || logUid == appUid) {
reportOomAdjMessageLocked(TAG_OOM_ADJ, "Making exec-service: " + app);
}
//是否进程是前台进程
} else if (app == topApp) {
adj = FOREGROUND_APP_ADJ;
schedGroup = SCHED_GROUP_BACKGROUND;
state.setAdjType("top-sleeping");
foregroundActivities = true;
procState = PROCESS_STATE_CUR_TOP;
if (DEBUG_OOM_ADJ_REASON || logUid == appUid) {
reportOomAdjMessageLocked(TAG_OOM_ADJ, "Making top (sleeping): " + app);
}
//启动了一个空进程
} else {
// As far as we know the process is empty. We may change our mind later.
schedGroup = SCHED_GROUP_BACKGROUND;
// At this point we don't actually know the adjustment. Use the cached adj
// value that the caller wants us to.
adj = cachedAdj;
procState = PROCESS_STATE_CACHED_EMPTY;
if (!state.containsCycle()) {
state.setCached(true);
state.setEmpty(true);
state.setAdjType("cch-empty");
}
if (DEBUG_OOM_ADJ_REASON || logUid == appUid) {
reportOomAdjMessageLocked(TAG_OOM_ADJ, "Making empty: " + app);
}
}
//。。。省略
//。。。 为此进程相关的绑定进程提权
// Do final modification to adj. Everything we do between here and applying
// the final setAdj must be done in this function, because we will also use
// it when computing the final cached adj later. Note that we don't need to
// worry about this for max adj above, since max adj will always be used to
// keep it out of the cached vaues.
state.setCurAdj(adj);
state.setCurCapability(capability);
state.setCurrentSchedulingGroup(schedGroup);
state.setCurProcState(procState);
state.setCurRawProcState(procState);
state.updateLastInvisibleTime(hasVisibleActivities);
state.setHasForegroundActivities(foregroundActivities);
state.setCompletedAdjSeq(mAdjSeq);
state.setCurBoundByNonBgRestrictedApp(boundByNonBgRestricted);
// if curAdj or curProcState improved, then this process was promoted
// 如果此进程的adj提高了,返回true。
return state.getCurAdj() < prevAppAdj || state.getCurProcState() < prevProcState
|| state.getCurCapability() != prevCapability;
这里我们启动的是activity进程,因此会为进程赋值以下数据
- adj = FOREGROUND_APP_ADJ;
- schedGroup = SCHED_GROUP_TOP_APP;
- state.setAdjType("top-activity");
- procState = PROCESS_STATE_TOP;
4.2.5 applyOomAdjLSP
通过computeOomAdjLSP为proc经确计算了adj和绑定到它之上组件的proc adj。这个函数
ProcessList
OOM adjustments for processes in various states:
// OOM adjustments for processes in various states:
// Uninitialized value for any major or minor adj fields
public static final int INVALID_ADJ = -10000;
// Adjustment used in certain places where we don't know it yet.
// (Generally this is something that is going to be cached, but we
// don't know the exact value in the cached range to assign yet.)
public static final int UNKNOWN_ADJ = 1001;
// This is a process only hosting activities that are not visible,
// so it can be killed without any disruption.
public static final int CACHED_APP_MAX_ADJ = 999;
public static final int CACHED_APP_MIN_ADJ = 900;
// This is the oom_adj level that we allow to die first. This cannot be equal to
// CACHED_APP_MAX_ADJ unless processes are actively being assigned an oom_score_adj of
// CACHED_APP_MAX_ADJ.
public static final int CACHED_APP_LMK_FIRST_ADJ = 950;
// Number of levels we have available for different service connection group importance
// levels.
static final int CACHED_APP_IMPORTANCE_LEVELS = 5;
// The B list of SERVICE_ADJ -- these are the old and decrepit
// services that aren't as shiny and interesting as the ones in the A list.
public static final int SERVICE_B_ADJ = 800;
// This is the process of the previous application that the user was in.
// This process is kept above other things, because it is very common to
// switch back to the previous app. This is important both for recent
// task switch (toggling between the two top recent apps) as well as normal
// UI flow such as clicking on a URI in the e-mail app to view in the browser,
// and then pressing back to return to e-mail.
public static final int PREVIOUS_APP_ADJ = 700;
// This is a process holding the home application -- we want to try
// avoiding killing it, even if it would normally be in the background,
// because the user interacts with it so much.
public static final int HOME_APP_ADJ = 600;
// This is a process holding an application service -- killing it will not
// have much of an impact as far as the user is concerned.
public static final int SERVICE_ADJ = 500;
// This is a process with a heavy-weight application. It is in the
// background, but we want to try to avoid killing it. Value set in
// system/rootdir/init.rc on startup.
public static final int HEAVY_WEIGHT_APP_ADJ = 400;
// This is a process currently hosting a backup operation. Killing it
// is not entirely fatal but is generally a bad idea.
public static final int BACKUP_APP_ADJ = 300;
// This is a process bound by the system (or other app) that's more important than services but
// not so perceptible that it affects the user immediately if killed.
public static final int PERCEPTIBLE_LOW_APP_ADJ = 250;
// This is a process hosting services that are not perceptible to the user but the
// client (system) binding to it requested to treat it as if it is perceptible and avoid killing
// it if possible.
public static final int PERCEPTIBLE_MEDIUM_APP_ADJ = 225;
// This is a process only hosting components that are perceptible to the
// user, and we really want to avoid killing them, but they are not
// immediately visible. An example is background music playback.
public static final int PERCEPTIBLE_APP_ADJ = 200;
// This is a process only hosting activities that are visible to the
// user, so we'd prefer they don't disappear.
public static final int VISIBLE_APP_ADJ = 100;
static final int VISIBLE_APP_LAYER_MAX = PERCEPTIBLE_APP_ADJ - VISIBLE_APP_ADJ - 1;
// This is a process that was recently TOP and moved to FGS. Continue to treat it almost
// like a foreground app for a while.
// @see TOP_TO_FGS_GRACE_PERIOD
public static final int PERCEPTIBLE_RECENT_FOREGROUND_APP_ADJ = 50;
// This is the process running the current foreground app. We'd really
// rather not kill it!
public static final int FOREGROUND_APP_ADJ = 0;
// This is a process that the system or a persistent process has bound to,
// and indicated it is important.
public static final int PERSISTENT_SERVICE_ADJ = -700;
// This is a system persistent process, such as telephony. Definitely
// don't want to kill it, but doing so is not completely fatal.
public static final int PERSISTENT_PROC_ADJ = -800;
// The system process runs at the default adjustment.
public static final int SYSTEM_ADJ = -900;
// Special code for native processes that are not being managed by the system (so
// don't have an oom adj assigned by the system).
public static final int NATIVE_ADJ = -1000;
// Memory pages are 4K.
static final int PAGE_SIZE = 4 * 1024;
// Activity manager's version of an undefined schedule group
static final int SCHED_GROUP_UNDEFINED = Integer.MIN_VALUE;
// Activity manager's version of Process.THREAD_GROUP_BACKGROUND
static final int SCHED_GROUP_BACKGROUND = 0;
// Activity manager's version of Process.THREAD_GROUP_RESTRICTED
static final int SCHED_GROUP_RESTRICTED = 1;
// Activity manager's version of Process.THREAD_GROUP_DEFAULT
static final int SCHED_GROUP_DEFAULT = 2;
// Activity manager's version of Process.THREAD_GROUP_TOP_APP
public static final int SCHED_GROUP_TOP_APP = 3;
// Activity manager's version of Process.THREAD_GROUP_TOP_APP
// Disambiguate between actual top app and processes bound to the top app
static final int SCHED_GROUP_TOP_APP_BOUND = 4;
Process
Keep in sync with SP_* constants of enum type SchedPolicy
declared in system/core/include/cutils/sched_policy.h,
except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
/**
* Default thread group -
* has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
* When used with setProcessGroup(), the group of each thread in the process
* is conditionally changed based on that thread's current priority, as follows:
* threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
* are moved to foreground thread group. All other threads are left unchanged.
* @hide
*/
public static final int THREAD_GROUP_DEFAULT = -1;
/**
* Background thread group - All threads in
* this group are scheduled with a reduced share of the CPU.
* Value is same as constant SP_BACKGROUND of enum SchedPolicy.
* @hide
*/
public static final int THREAD_GROUP_BACKGROUND = 0;
/**
* Foreground thread group - All threads in
* this group are scheduled with a normal share of the CPU.
* Value is same as constant SP_FOREGROUND of enum SchedPolicy.
* Not used at this level.
* @hide
**/
private static final int THREAD_GROUP_FOREGROUND = 1;
/**
* System thread group.
* @hide
**/
public static final int THREAD_GROUP_SYSTEM = 2;
/**
* Application audio thread group.
* @hide
**/
public static final int THREAD_GROUP_AUDIO_APP = 3;
/**
* System audio thread group.
* @hide
**/
public static final int THREAD_GROUP_AUDIO_SYS = 4;
/**
* Thread group for top foreground app.
* @hide
**/
public static final int THREAD_GROUP_TOP_APP = 5;
/**
* Thread group for RT app.
* @hide
**/
public static final int THREAD_GROUP_RT_APP = 6;
/**
* Thread group for bound foreground services that should
* have additional CPU restrictions during screen off
* @hide
**/
public static final int THREAD_GROUP_RESTRICTED = 7;
为ams开启的线程设置进程组和cpust
// bind background threads to little cores
// this is expected to fail inside of framework tests because apps can't touch cpusets directly
// make sure we've already adjusted system_server's internal view of itself first
updateOomAdjLocked(OOM_ADJ_REASON_SYSTEM_INIT);
try {
Process.setThreadGroupAndCpuset(BackgroundThread.get().getThreadId(),
Process.THREAD_GROUP_SYSTEM);
Process.setThreadGroupAndCpuset(
mOomAdjuster.mCachedAppOptimizer.mCachedAppOptimizerThread.getThreadId(),
Process.THREAD_GROUP_SYSTEM);
} catch (Exception e) {
Slog.w(TAG, "Setting background thread cpuset failed");
}
将后台线程绑定到小核上。例如UIThread则会绑定到大核上,如下:
[UIThread.java]
@Override
public void run() {
// Make sure UiThread is in the fg stune boost group
Process.setThreadGroup(Process.myTid(), Process.THREAD_GROUP_TOP_APP);
super.run();
}
ProcessRecord
/**
* Profiling info of the process, such as PSS, cpu, etc.
*/
final ProcessProfileRecord mProfile;
/**
* All about the services in this process.
*/
final ProcessServiceRecord mServices;
/**
* All about the providers in this process.
*/
final ProcessProviderRecord mProviders;
/**
* All about the receivers in this process.
*/
final ProcessReceiverRecord mReceivers;
/**
* All about the error state(crash, ANR) in this process.
*/
final ProcessErrorStateRecord mErrorState;
/**
* All about the process state info (proc state, oom adj score) in this process.
*/
final ProcessStateRecord mState;
ProcessProfileRecord :Profiling info of the process, such as PSS, cpu, etc.
ProcessStateRecord 用于记录oom adj score。
AppProfiler
/**
* A helper class taking care of the profiling, memory and cpu sampling of apps
*/
public class AppProfiler {
cpuSet介绍
根据我搜索到的结果,cpuset 是 Linux cgroup 子系统的一部分,它可以为 cgroup 任务分配单独的 CPU 和内存1。Android 系统使用 cpuset 来控制不同优先级的应用可以使用的 CPU 范围,以保证更好的交互响应和性能23。cpuset 的设置和操作是通过 cgroup 抽象层和任务配置文件来实现的,这些文件可以在 Android 10 及更高版本中自定义12。cpuset 的名称和属性可以在 cgroups.json 文件和 task_profiles.json 文件中查看12。例如,Android 系统中有以下几种 cpuset:
- foreground:表示前台应用的 cpuset,它可以使用所有的 CPU 核心12。
- background:表示后台应用的 cpuset,它只能使用小核心12。
- system-background:表示系统后台服务的 cpuset,它也只能使用小核心12。
- top-app:表示当前用户可见的最前台的应用的 cpuset,它可以使用所有的 CPU 核心,并且有最高的优先级12。
- restricted:表示受限制的 cpuset,它只能使用一个小核心,并且有最低的优先级12。
你可以参考这篇文章来了解更多关于 Android/Linux EAS 优化的知识。
<cib-overlay></cib-overlay>