[Android]实时监测一个app运行进程状态

因为没有系统分析过ActivityManagerService,简单看来一点source code, 所以就简单记录在此,大概有谬误,不做实际应用参考。

注:分析基于Android 9.0


一、进程状态 Process states


参考源码:http://aosp.opersys.com/xref/android-9.0.0_r61/xref/frameworks/base/core/java/android/app/ActivityManager.java#470

定义:进程状态,描述特定进程所处的状态类型。

各个状态的定义如下:

 1 /**
 2  * @hide
 3  * Process states, describing the kind of state a particular process is in.
 4  * When updating these, make sure to also check all related references to the
 5  * constant in code, and update these arrays:
 6  *
 7  * @see com.android.internal.app.procstats.ProcessState#PROCESS_STATE_TO_STATE
 8  * @see com.android.server.am.ProcessList#sProcStateToProcMem
 9  * @see com.android.server.am.ProcessList#sFirstAwakePssTimes
10  * @see com.android.server.am.ProcessList#sSameAwakePssTimes
11  * @see com.android.server.am.ProcessList#sTestFirstPssTimes
12  * @see com.android.server.am.ProcessList#sTestSamePssTimes
13  */
14 
15 /** @hide Not a real process state. */
16 public static final int PROCESS_STATE_UNKNOWN = -1; //未知状态,不是真正的进程状态
17 
18 /** @hide Process is a persistent system process. */
19 public static final int PROCESS_STATE_PERSISTENT = 0; //常驻的系统进程
20 
21 /** @hide Process is a persistent system process and is doing UI. */
22 public static final int PROCESS_STATE_PERSISTENT_UI = 1; //进程是一个持久的系统进程,正在执行UI
23 
24 /** @hide Process is hosting the current top activities.  Note that this covers
25  * all activities that are visible to the user. */
26 public static final int PROCESS_STATE_TOP = 2; //进程正在持有当前的top activities。请注意,这涵盖了对用户可见的所有活动。
27 
28 /** @hide Process is hosting a foreground service. */
29 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 3; //进程持有前台服务
30 
31 /** @hide Process is hosting a foreground service due to a system binding. */
32 public static final int PROCESS_STATE_BOUND_FOREGROUND_SERVICE = 4; //由于系统绑定,进程正在持有前台服务
33 
34 /** @hide Process is important to the user, and something they are aware of. */
35 public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 5; //进程对用户来说很重要,而且是他们知道的
36 
37 /** @hide Process is important to the user, but not something they are aware of. */
38 public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 6; //进程对用户来说很重要,但不是他们知道的。
39 
40 /** @hide Process is in the background transient so we will try to keep running. */
41 public static final int PROCESS_STATE_TRANSIENT_BACKGROUND = 7; //进程处于后台瞬态,因此我们将尝试继续运行。
42 
43 /** @hide Process is in the background running a backup/restore operation. */
44 public static final int PROCESS_STATE_BACKUP = 8; //进程正在后台运行备份/还原操作。
45 
46 /** @hide Process is in the background running a service.  Unlike oom_adj, this level
47  * is used for both the normal running in background state and the executing
48  * operations state. */
49 public static final int PROCESS_STATE_SERVICE = 9; //进程在后台运行服务。与oom_adj不同的是,此级别同时用于正常的后台运行状态和正在执行的操作状态。
50 
51 /** @hide Process is in the background running a receiver.   Note that from the
52  * perspective of oom_adj, receivers run at a higher foreground level, but for our
53  * prioritization here that is not necessary and putting them below services means
54  * many fewer changes in some process states as they receive broadcasts. */
55 public static final int PROCESS_STATE_RECEIVER = 10; //进程在后台运行一个接收器。
56 
57 /** @hide Same as {@link #PROCESS_STATE_TOP} but while device is sleeping. */
58 public static final int PROCESS_STATE_TOP_SLEEPING = 11; //与PROCESS _STATE _TOP相同,但设备处于睡眠状态
59 
60 /** @hide Process is in the background, but it can't restore its state so we want
61  * to try to avoid killing it. */
62 public static final int PROCESS_STATE_HEAVY_WEIGHT = 12; //进程在后台,但它无法恢复其状态,因此我们希望尝试避免杀死它。
63 
64 /** @hide Process is in the background but hosts the home activity. */
65 public static final int PROCESS_STATE_HOME = 13; //进程在后台,但持有home活动
66 
67 /** @hide Process is in the background but hosts the last shown activity. */
68 public static final int PROCESS_STATE_LAST_ACTIVITY = 14; //进程位于后台,但承载最后显示的活动。
69 
70 /** @hide Process is being cached for later use and contains activities. */
71 public static final int PROCESS_STATE_CACHED_ACTIVITY = 15; //进程正在缓存以供以后使用,并且包含活动。
72 
73 /** @hide Process is being cached for later use and is a client of another cached
74  * process that contains activities. */
75 public static final int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 16; //进程正在缓存以供以后使用,并且是另一个包含活动的缓存进程的客户端。
76 
77 /** @hide Process is being cached for later use and has an activity that corresponds
78  * to an existing recent task. */
79 public static final int PROCESS_STATE_CACHED_RECENT = 17; //进程正在缓存以供以后使用,并且有一个活动对应现有的最近任务
80 
81 /** @hide Process is being cached for later use and is empty. */
82 public static final int PROCESS_STATE_CACHED_EMPTY = 18; //进程正在缓存以供以后使用,并且为空。
83 
84 /** @hide Process does not exist. */
85 public static final int PROCESS_STATE_NONEXISTENT = 19; //进程不存在
View Code

 

二、将进程状态转换为相应的IMPORTANCE_*常量

直接看代码:http://aosp.opersys.com/xref/android-9.0.0_r61/xref/frameworks/base/core/java/android/app/ActivityManager.java#3143

  1         /**
  2          * Constant for {@link #importance}: This process is running the
  3          * foreground UI; that is, it is the thing currently at the top of the screen
  4          * that the user is interacting with.
  5          */
  6         public static final int IMPORTANCE_FOREGROUND = 100;
  7 
  8         /**
  9          * Constant for {@link #importance}: This process is running a foreground
 10          * service, for example to perform music playback even while the user is
 11          * not immediately in the app.  This generally indicates that the process
 12          * is doing something the user actively cares about.
 13          */
 14         public static final int IMPORTANCE_FOREGROUND_SERVICE = 125;
 15 
 16         /**
 17          * @deprecated Pre-{@link android.os.Build.VERSION_CODES#P} version of
 18          * {@link #IMPORTANCE_TOP_SLEEPING}.  As of Android
 19          * {@link android.os.Build.VERSION_CODES#P}, this is considered much less
 20          * important since we want to reduce what apps can do when the screen is off.
 21          */
 22         @Deprecated
 23         public static final int IMPORTANCE_TOP_SLEEPING_PRE_28 = 150;
 24 
 25         /**
 26          * Constant for {@link #importance}: This process is running something
 27          * that is actively visible to the user, though not in the immediate
 28          * foreground.  This may be running a window that is behind the current
 29          * foreground (so paused and with its state saved, not interacting with
 30          * the user, but visible to them to some degree); it may also be running
 31          * other services under the system's control that it inconsiders important.
 32          */
 33         public static final int IMPORTANCE_VISIBLE = 200;
 34 
 35         /**
 36          * Constant for {@link #importance}: {@link #IMPORTANCE_PERCEPTIBLE} had this wrong value
 37          * before {@link Build.VERSION_CODES#O}.  Since the {@link Build.VERSION_CODES#O} SDK,
 38          * the value of {@link #IMPORTANCE_PERCEPTIBLE} has been fixed.
 39          *
 40          * <p>The system will return this value instead of {@link #IMPORTANCE_PERCEPTIBLE}
 41          * on Android versions below {@link Build.VERSION_CODES#O}.
 42          *
 43          * <p>On Android version {@link Build.VERSION_CODES#O} and later, this value will still be
 44          * returned for apps with the target API level below {@link Build.VERSION_CODES#O}.
 45          * For apps targeting version {@link Build.VERSION_CODES#O} and later,
 46          * the correct value {@link #IMPORTANCE_PERCEPTIBLE} will be returned.
 47          */
 48         public static final int IMPORTANCE_PERCEPTIBLE_PRE_26 = 130;
 49 
 50         /**
 51          * Constant for {@link #importance}: This process is not something the user
 52          * is directly aware of, but is otherwise perceptible to them to some degree.
 53          */
 54         public static final int IMPORTANCE_PERCEPTIBLE = 230;
 55 
 56         /**
 57          * Constant for {@link #importance}: {@link #IMPORTANCE_CANT_SAVE_STATE} had
 58          * this wrong value
 59          * before {@link Build.VERSION_CODES#O}.  Since the {@link Build.VERSION_CODES#O} SDK,
 60          * the value of {@link #IMPORTANCE_CANT_SAVE_STATE} has been fixed.
 61          *
 62          * <p>The system will return this value instead of {@link #IMPORTANCE_CANT_SAVE_STATE}
 63          * on Android versions below {@link Build.VERSION_CODES#O}.
 64          *
 65          * <p>On Android version {@link Build.VERSION_CODES#O} after, this value will still be
 66          * returned for apps with the target API level below {@link Build.VERSION_CODES#O}.
 67          * For apps targeting version {@link Build.VERSION_CODES#O} and later,
 68          * the correct value {@link #IMPORTANCE_CANT_SAVE_STATE} will be returned.
 69          *
 70          * @hide
 71          */
 72         public static final int IMPORTANCE_CANT_SAVE_STATE_PRE_26 = 170;
 73 
 74         /**
 75          * Constant for {@link #importance}: This process is contains services
 76          * that should remain running.  These are background services apps have
 77          * started, not something the user is aware of, so they may be killed by
 78          * the system relatively freely (though it is generally desired that they
 79          * stay running as long as they want to).
 80          */
 81         public static final int IMPORTANCE_SERVICE = 300;
 82 
 83         /**
 84          * Constant for {@link #importance}: This process is running the foreground
 85          * UI, but the device is asleep so it is not visible to the user.  Though the
 86          * system will try hard to keep its process from being killed, in all other
 87          * ways we consider it a kind of cached process, with the limitations that go
 88          * along with that state: network access, running background services, etc.
 89          */
 90         public static final int IMPORTANCE_TOP_SLEEPING = 325;
 91 
 92         /**
 93          * Constant for {@link #importance}: This process is running an
 94          * application that can not save its state, and thus can't be killed
 95          * while in the background.  This will be used with apps that have
 96          * {@link android.R.attr#cantSaveState} set on their application tag.
 97          */
 98         public static final int IMPORTANCE_CANT_SAVE_STATE = 350;
 99 
100         /**
101          * Constant for {@link #importance}: This process process contains
102          * cached code that is expendable, not actively running any app components
103          * we care about.
104          */
105         public static final int IMPORTANCE_CACHED = 400;
106 
107         /**
108          * @deprecated Renamed to {@link #IMPORTANCE_CACHED}.
109          */
110         public static final int IMPORTANCE_BACKGROUND = IMPORTANCE_CACHED;
111 
112         /**
113          * Constant for {@link #importance}: This process is empty of any
114          * actively running code.
115          * @deprecated This value is no longer reported, use {@link #IMPORTANCE_CACHED} instead.
116          */
117         @Deprecated
118         public static final int IMPORTANCE_EMPTY = 500;
119 
120         /**
121          * Constant for {@link #importance}: This process does not exist.
122          */
123         public static final int IMPORTANCE_GONE = 1000;
124 
125         /**
126          * Convert a proc state to the correspondent IMPORTANCE_* constant.  If the return value
127          * will be passed to a client, use {@link #procStateToImportanceForClient}.
128          * @hide
129          */
130         public static @Importance int procStateToImportance(int procState) {
131             if (procState == PROCESS_STATE_NONEXISTENT) {
132                 return IMPORTANCE_GONE;
133             } else if (procState >= PROCESS_STATE_HOME) {
134                 return IMPORTANCE_CACHED;
135             } else if (procState == PROCESS_STATE_HEAVY_WEIGHT) {
136                 return IMPORTANCE_CANT_SAVE_STATE;
137             } else if (procState >= PROCESS_STATE_TOP_SLEEPING) {
138                 return IMPORTANCE_TOP_SLEEPING;
139             } else if (procState >= PROCESS_STATE_SERVICE) {
140                 return IMPORTANCE_SERVICE;
141             } else if (procState >= PROCESS_STATE_TRANSIENT_BACKGROUND) {
142                 return IMPORTANCE_PERCEPTIBLE;
143             } else if (procState >= PROCESS_STATE_IMPORTANT_FOREGROUND) {
144                 return IMPORTANCE_VISIBLE;
145             } else if (procState >= PROCESS_STATE_FOREGROUND_SERVICE) {
146                 return IMPORTANCE_FOREGROUND_SERVICE;
147             } else {
148                 return IMPORTANCE_FOREGROUND;
149             }
150         }
View Code

 

三、监测进程状态

监测位置:http://aosp.opersys.com/xref/android-9.0.0_r61/xref/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java#24809

maybeUpdateUsageStatsLocked 函数中做监测

ProcessRecord app;
app.setProcState ==> 原来的状态
app.curProcState ==> 当前要转入的状态

比如,监测一个app process是否运行在foreground,可以加入如下patch

if(Arrays.asList(app.getPackageList()).contains("app package name")) {
    Slog.d("roger", "Checking proc [" + Arrays.toString(app.getPackageList())
            + "] state changes: old = " + app.setProcState + ", new = "
            + app.curProcState);
    if(app.setProcState == ActivityManager.PROCESS_STATE_TOP && app.curProcState != ActivityManager.PROCESS_STATE_TOP) {
        Slog.d("roger", "your apk process state change from TOP  to others");
    }
}

 

posted on 2021-07-01 13:56  二的次方  阅读(2772)  评论(0编辑  收藏  举报