进程调度:
进程优先级:没有发现一个和进程优先级直接挂钩的成员。但是应该跟一下内容有关:
* Android将应用进程分为五大类,分别是Foreground类,Visible类,Service类,
Background类,Empty类。重要性是由高到低,应该算是进程优先级由高到低。
这个可以作为进程调度优先级的依据,也可以作为OOM控制的依据。
* Process类中一些方法,如:
/**
* Sets the scheduling group for a process and all child threads
* @hide
* @param pid The identifier of the process to change.
* @param group The target group for this process from THREAD_GROUP_*.
*
* @throws IllegalArgumentException Throws IllegalArgumentException if
* <var>tid</var> does not exist.
* @throws SecurityException Throws SecurityException if your process does
* not have permission to modify the given thread, or to use the given
* priority.
*
* group == THREAD_GROUP_DEFAULT means to move all non-background priority
* threads to the foreground scheduling group, but to leave background
* priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
* threads, regardless of priority, to the background scheduling group.
* group == THREAD_GROUP_FOREGROUND is not allowed.
*/
setProcessGroup(int pid, int group) 对应ProcessRecord类中的setSchedGroup
由注释可以知道该函数是用于设置进程属于那个组的,而只有两个组可以设置,就是
THREAD_GROUP_DEFAULT和THREAD_GROUP_BG_NONINTERACTIVE,分别意
味着将所有非后台优先级的线程都就移到到前台调度组,优先级为后台级别的线程
则留下,和将所有线程不分优先级是否后台都移到后台调度组。而下面的setThreadGroup
方法也是一样,指不过上面方法是对指定进程的所有线程进行操作,而下面的则是将指定的
线程移到哪个组。移到不同的组的意义应该和调度有关。
setThreadGroup(int tid, int group)
setThreadPriority(int priority)
* ActivityManagerService#mLruProcesses : ArrayList<ProcessRecord>
这是跟进程调度有关的一个数据结构,使用lru算法作为主要排序依据来对
进程进行管理,应该是调度得越少的进程,会排越前面,更有机会被分配
cpu资源。由ActivityManagerService#updateLurProcessInternalLocked(...)
管理这个数据结构。
OOM控制:
在内存不足的情况下,杀掉oom优先级较低的进程,这个控制和进程调度是相对独立的。
* ProcessList类
定义了不同状态下的进程的oom_adj值,值越小,越不容易被杀掉。
下面是不同进程的oom_adj
// 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.)
static final int UNKNOWN_ADJ = 16;
// This is a process only hosting activities that are not visible,
// so it can be killed without any disruption.
static final int CACHED_APP_MAX_ADJ = 15;
static final int CACHED_APP_MIN_ADJ = 9;
// 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.
static final int SERVICE_B_ADJ = 8;
// 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.
static final int PREVIOUS_APP_ADJ = 7;
// 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.
static final int HOME_APP_ADJ = 6;
// This is a process holding an application service -- killing it will not
// have much of an impact as far as the user is concerned.
static final int SERVICE_ADJ = 5;//service类进程
// 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.
static final int HEAVY_WEIGHT_APP_ADJ = 4;
// This is a process currently hosting a backup operation. Killing it
// is not entirely fatal but is generally a bad idea.
static final int BACKUP_APP_ADJ = 3;//正在执行backup操作的进程
// 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.
static final int PERCEPTIBLE_APP_ADJ = 2;
// This is a process only hosting activities that are visible to the
// user, so we'd prefer they don't disappear.
static final int VISIBLE_APP_ADJ = 1;//visible类进程
// This is the process running the current foreground app. We'd really
// rather not kill it!
static final int FOREGROUND_APP_ADJ = 0;//foreground类进程
// This is a process that the system or a persistent process has bound to,
// and indicated it is important.
static final int PERSISTENT_SERVICE_ADJ = -11;//这种类型的进程退出后
//系统需要重启,是重要进程
// This is a system persistent process, such as telephony. Definitely
// don't want to kill it, but doing so is not completely fatal.
static final int PERSISTENT_PROC_ADJ = -12;
// The system process runs at the default adjustment.
static final int SYSTEM_ADJ = -16;
// Special code for native processes that are not being managed by the system (so
// don't have an oom adj assigned by the system).
static final int NATIVE_ADJ = -17;
* LKM
是判断内存不足的标准,就是说他控制了杀掉进程的内存大小阀值。