blob: 867571a33bc2f0c8abd5512f8e7550dbea0c21e1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
riddle_hsu6793fc32015-03-31 11:54:14 +080019import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;
Yunfan Chen75157d72018-07-27 14:47:21 +090020
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070021import static com.android.server.Watchdog.NATIVE_STACKS_OF_INTEREST;
22import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ANR;
Jorim Jaggi589c5ba2019-07-30 16:50:13 +020023import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_OOM_ADJ;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080024import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
25import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070026import static com.android.server.am.ActivityManagerService.MY_PID;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.app.ActivityManager;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070029import android.app.ApplicationErrorReport;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.app.Dialog;
31import android.app.IApplicationThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.ComponentName;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070033import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.pm.ApplicationInfo;
Amith Yamasania0a30a12019-01-22 11:38:06 -080035import android.content.pm.ServiceInfo;
Zimuzo972e1cd2019-01-28 16:30:01 +000036import android.content.pm.VersionedPackage;
Dianne Hackborne2515ee2011-04-27 18:52:56 -040037import android.content.res.CompatibilityInfo;
Joe Onorato57190282016-04-20 15:37:49 -070038import android.os.Binder;
Yunfan Chen75157d72018-07-27 14:47:21 +090039import android.os.Debug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.IBinder;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070041import android.os.Message;
Dianne Hackborna0c283e2012-02-09 10:47:01 -080042import android.os.Process;
Joe Onorato57190282016-04-20 15:37:49 -070043import android.os.RemoteException;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070044import android.os.SystemClock;
Yusuke Satocf4b2542015-06-17 00:21:28 -070045import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070046import android.os.UserHandle;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070047import android.provider.Settings;
Yang Lu732d6382018-11-05 07:53:12 -080048import android.server.ServerProtoEnums;
Dianne Hackborn390517b2013-05-30 15:03:32 -070049import android.util.ArrayMap;
Yunfan Chen75157d72018-07-27 14:47:21 +090050import android.util.ArraySet;
51import android.util.DebugUtils;
52import android.util.EventLog;
53import android.util.Slog;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070054import android.util.SparseArray;
Chenjie Yubd1a28f2018-07-17 14:55:19 -070055import android.util.StatsLog;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070056import android.util.TimeUtils;
Yi Jin129fc6c2017-09-28 15:48:38 -070057import android.util.proto.ProtoOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
Yunfan Chen585f2932019-01-29 16:04:45 +090059import com.android.internal.annotations.VisibleForTesting;
Yunfan Chen75157d72018-07-27 14:47:21 +090060import com.android.internal.app.procstats.ProcessState;
61import com.android.internal.app.procstats.ProcessStats;
62import com.android.internal.os.BatteryStatsImpl;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070063import com.android.internal.os.ProcessCpuTracker;
Sudheer Shanka87915d62018-11-06 10:57:35 -080064import com.android.internal.os.Zygote;
Wale Ogunwale59507092018-10-29 09:00:30 -070065import com.android.server.wm.WindowProcessController;
66import com.android.server.wm.WindowProcessListener;
Yunfan Chen75157d72018-07-27 14:47:21 +090067
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070068import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import java.io.PrintWriter;
70import java.util.ArrayList;
Torne (Richard Coles)466cbe42017-05-31 14:09:14 -040071import java.util.Arrays;
Zimuzo972e1cd2019-01-28 16:30:01 +000072import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073
74/**
75 * Full information about a particular process that
76 * is currently running.
77 */
Yunfan Chen585f2932019-01-29 16:04:45 +090078class ProcessRecord implements WindowProcessListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -080079 private static final String TAG = TAG_WITH_CLASS_NAME ? "ProcessRecord" : TAG_AM;
80
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -070081 private final ActivityManagerService mService; // where we came from
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 final ApplicationInfo info; // all about the first app in the process
Dianne Hackborna0c283e2012-02-09 10:47:01 -080083 final boolean isolated; // true if this is a special isolated process
Martijn Coenen01e719b2018-12-05 16:01:38 +010084 final boolean appZygote; // true if this is forked from the app zygote
Dianne Hackborna0c283e2012-02-09 10:47:01 -080085 final int uid; // uid of process; may be different from 'info' if isolated
86 final int userId; // user of process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 final String processName; // name of the process
88 // List of packages running in the process
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070089 final PackageList pkgList = new PackageList();
90 final class PackageList {
91 final ArrayMap<String, ProcessStats.ProcessStateHolder> mPkgList = new ArrayMap<>();
92
93 ProcessStats.ProcessStateHolder put(String key, ProcessStats.ProcessStateHolder value) {
94 mWindowProcessController.addPackage(key);
95 return mPkgList.put(key, value);
96 }
97
98 void clear() {
99 mPkgList.clear();
100 mWindowProcessController.clearPackageList();
101 }
102
103 int size() {
104 return mPkgList.size();
105 }
106
107 String keyAt(int index) {
108 return mPkgList.keyAt(index);
109 }
110
111 public ProcessStats.ProcessStateHolder valueAt(int index) {
112 return mPkgList.valueAt(index);
113 }
114
Dianne Hackborn2aec55a2018-06-26 10:35:35 -0700115 ProcessStats.ProcessStateHolder get(String pkgName) {
116 return mPkgList.get(pkgName);
117 }
118
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700119 boolean containsKey(Object key) {
120 return mPkgList.containsKey(key);
121 }
122 }
123
Dianne Hackborne17b4452018-01-10 13:15:40 -0800124 final ProcessList.ProcStateMemTracker procStateMemTracker
125 = new ProcessList.ProcStateMemTracker();
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700126 UidRecord uidRecord; // overall state of process's uid.
Dianne Hackbornfee756f2014-07-16 17:31:10 -0700127 ArraySet<String> pkgDeps; // additional packages we have a dependency on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 IApplicationThread thread; // the actual proc... may be null only if
129 // 'persistent' is true (in which case we
130 // are in the process of launching the app)
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700131 ProcessState baseProcessTracker;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800132 BatteryStatsImpl.Uid.Proc curProcBatteryStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 int pid; // The process of this application; 0 if none
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700134 String procStatFile; // path to /proc/<pid>/stat
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800135 int[] gids; // The gids this process was launched with
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700136 private String mRequiredAbi;// The ABI this process was launched with
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800137 String instructionSet; // The instruction set this process was launched with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 boolean starting; // True if the process is being started
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800139 long lastActivityTime; // For managing the LRU list
Dianne Hackbornf1cca182013-08-01 10:50:28 -0700140 long lastPssTime; // Last time we retrieved PSS data
141 long nextPssTime; // Next time we want to request PSS data
142 long lastStateTime; // Last time setProcState changed
Dianne Hackborn35f72be2013-09-16 10:57:39 -0700143 long initialIdlePss; // Initial memory pss of process for idle maintenance.
144 long lastPss; // Last computed memory pss.
Thierry Strudelf0a44182016-01-07 18:08:04 -0800145 long lastSwapPss; // Last computed SwapPss.
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700146 long lastCachedPss; // Last computed pss when in cached state.
Thierry Strudelf0a44182016-01-07 18:08:04 -0800147 long lastCachedSwapPss; // Last computed SwapPss when in cached state.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 int maxAdj; // Maximum OOM adjustment for this process
Wale Ogunwale31913b52018-10-13 08:29:31 -0700149 private int mCurRawAdj; // Current OOM unlimited adjustment for this process
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 int setRawAdj; // Last set OOM unlimited adjustment for this process
151 int curAdj; // Current OOM adjustment for this process
152 int setAdj; // Last set OOM adjustment for this process
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700153 int verifiedAdj; // The last adjustment that was verified as actually being set
Hui Yu26969322019-08-21 14:56:35 -0700154 int curCapability; // Current capability flags of this process. For example,
155 // PROCESS_CAPABILITY_FOREGROUND_LOCATION is one capability.
156 int setCapability; // Last set capability flags.
Tim Murray0b1c8aa2018-11-20 10:28:38 -0800157 long lastCompactTime; // The last time that this process was compacted
158 int reqCompactAction; // The most recent compaction action requested for this app.
159 int lastCompactAction; // The most recent compaction action performed for this app.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700160 private int mCurSchedGroup; // Currently desired scheduling class
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700161 int setSchedGroup; // Last set to background scheduling class
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700162 int trimMemoryLevel; // Last selected memory trimming level
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700163 private int mCurProcState = PROCESS_STATE_NONEXISTENT; // Currently computed process state
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700164 private int mRepProcState = PROCESS_STATE_NONEXISTENT; // Last reported process state
Amith Yamasani08888c02018-12-07 09:38:25 -0800165 private int mCurRawProcState = PROCESS_STATE_NONEXISTENT; // Temp state during computation
riddle_hsu6793fc32015-03-31 11:54:14 +0800166 int setProcState = PROCESS_STATE_NONEXISTENT; // Last set process state in process tracker
167 int pssProcState = PROCESS_STATE_NONEXISTENT; // Currently requesting pss for
Dianne Hackborne17b4452018-01-10 13:15:40 -0800168 int pssStatType; // The type of stat collection that we are currently requesting
Tim Murray33eb07f2016-06-10 10:03:20 -0700169 int savedPriority; // Previous priority value if we're switching to non-SCHED_OTHER
170 int renderThreadTid; // TID for RenderThread
Dianne Hackborna631d562018-11-20 15:58:15 -0800171 ServiceRecord connectionService; // Service that applied current connectionGroup/Importance
172 int connectionGroup; // Last group set by a connection
173 int connectionImportance; // Last importance set by a connection
Dianne Hackborne02c88a2011-10-28 13:58:15 -0700174 boolean serviceb; // Process currently is on the service B list
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700175 boolean serviceHighRam; // We are forcing to service B list due to its RAM use
Dianne Hackborn35f72be2013-09-16 10:57:39 -0700176 boolean notCachedSinceIdle; // Has this process not been in a cached state since last idle?
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700177 private boolean mHasClientActivities; // Are there any client services with activities?
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700178 boolean hasStartedServices; // Are there any started services running in this process?
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700179 private boolean mHasForegroundServices; // Running any services that are foreground?
Amith Yamasania0a30a12019-01-22 11:38:06 -0800180 private int mFgServiceTypes; // Type of foreground service, if there is a foreground service.
Narayan Kamath396ce722019-03-01 16:53:26 +0000181 private int mRepFgServiceTypes; // Last reported foreground service types.
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700182 private boolean mHasForegroundActivities; // Running any activities that are foreground?
Dianne Hackborn684bf342014-04-29 17:56:57 -0700183 boolean repForegroundActivities; // Last reported foreground activities.
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700184 boolean systemNoUi; // This is a system process, but not currently showing UI.
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -0700185 boolean hasShownUi; // Has UI been shown in this process since it was started?
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700186 private boolean mHasTopUi; // Is this process currently showing a non-activity UI that the user
Wale Ogunwaled993a572017-02-05 13:52:09 -0800187 // is interacting with? E.g. The status bar when it is expanded, but
188 // not when it is minimized. When true the
189 // process will be set to use the ProcessList#SCHED_GROUP_TOP_APP
190 // scheduling group to boost performance.
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700191 private boolean mHasOverlayUi; // Is the process currently showing a non-activity UI that
Wale Ogunwaled993a572017-02-05 13:52:09 -0800192 // overlays on-top of activity UIs on screen. E.g. display a window
193 // of type
194 // android.view.WindowManager.LayoutParams#TYPE_APPLICATION_OVERLAY
195 // When true the process will oom adj score will be set to
196 // ProcessList#PERCEPTIBLE_APP_ADJ at minimum to reduce the chance
197 // of the process getting killed.
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100198 boolean runningRemoteAnimation; // Is the process currently running a RemoteAnimation? When true
199 // the process will be set to use the
200 // ProcessList#SCHED_GROUP_TOP_APP scheduling group to boost
201 // performance, as well as oom adj score will be set to
202 // ProcessList#VISIBLE_APP_ADJ at minimum to reduce the chance
203 // of the process getting killed.
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700204 private boolean mPendingUiClean; // Want to clean up resources from showing UI?
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700205 boolean hasAboveClient; // Bound using BIND_ABOVE_CLIENT, so want to be lower
Dianne Hackbornf0f94d12014-03-17 16:04:21 -0700206 boolean treatLikeActivity; // Bound using BIND_TREAT_LIKE_ACTIVITY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 boolean bad; // True if disabled in the bad process list
Dianne Hackborn35f72be2013-09-16 10:57:39 -0700208 boolean killedByAm; // True when proc has been killed by activity manager, not for RAM
Dianne Hackbornd4125632014-09-22 12:52:49 -0700209 boolean killed; // True once we know the process has been killed
Dianne Hackbornc8230512013-07-13 21:32:12 -0700210 boolean procStateChanged; // Keep track of whether we changed 'setAdj'.
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700211 boolean reportedInteraction;// Whether we have told usage stats about it being an interaction
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -0600212 boolean unlocked; // True when proc was started in user unlocked state
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700213 private long mInteractionEventTime; // The time we sent the last interaction event
214 private long mFgInteractionTime; // When we became foreground for interaction purposes
Dianne Hackborn9357b112013-10-03 18:27:48 -0700215 String waitingToKill; // Process is waiting to be killed when in the bg, and reason
Dianne Hackbornf965f402017-05-04 23:27:23 -0700216 Object forcingToImportant; // Token that is forcing this process to be important
Dianne Hackborn906497c2010-05-10 15:57:38 -0700217 int adjSeq; // Sequence id for identifying oom_adj assignment cycles
Tim Murrayb52f0002018-05-02 15:14:35 -0700218 int completedAdjSeq; // Sequence id for identifying oom_adj assignment cycles
219 boolean containsCycle; // Whether this app has encountered a cycle in the most recent update
Dianne Hackborn906497c2010-05-10 15:57:38 -0700220 int lruSeq; // Sequence id for identifying LRU update cycles
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700221 CompatibilityInfo compat; // last used compatibility mode
Dianne Hackborn1b64e0d2011-07-17 15:23:59 -0700222 IBinder.DeathRecipient deathRecipient; // Who is watching for the death.
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700223 private ActiveInstrumentation mInstr; // Set to currently active instrumentation running in
224 // process.
225 private boolean mUsingWrapper; // Set to true when process was launched with a wrapper attached
yangzhenyud509bc92016-08-31 18:26:46 +0800226 final ArraySet<BroadcastRecord> curReceivers = new ArraySet<BroadcastRecord>();// receivers currently running in the app
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700227 private long mWhenUnimportant; // When (uptime) the process last became unimportant
Dianne Hackborn287952c2010-09-22 22:34:31 -0700228 long lastCpuTime; // How long proc has run CPU at last check
229 long curCpuTime; // How long proc has run CPU most recently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 long lastRequestedGc; // When we last asked the app to do a gc
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700231 long lastLowMemory; // When we last told the app that memory is low
Dianne Hackborn1a405642015-11-04 12:32:45 -0800232 long lastProviderTime; // The last time someone else was using a provider in this process.
Amith Yamasanie5bfeee2018-09-05 18:52:35 -0700233 long lastTopTime; // The last time the process was in the TOP state or greater.
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700234 boolean reportLowMemory; // Set to true when waiting to report low mem
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800235 boolean empty; // Is this an empty background process?
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700236 boolean cached; // Is this a cached process?
Dianne Hackbornde42bb62009-08-05 12:26:15 -0700237 String adjType; // Debugging: primary thing impacting oom_adj.
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700238 int adjTypeCode; // Debugging: adj code to report to app.
Dianne Hackbornde42bb62009-08-05 12:26:15 -0700239 Object adjSource; // Debugging: option dependent object.
Dianne Hackbornfee756f2014-07-16 17:31:10 -0700240 int adjSourceProcState; // Debugging: proc state of adjSource's process.
Dianne Hackbornde42bb62009-08-05 12:26:15 -0700241 Object adjTarget; // Debugging: target component impacting oom_adj.
Primiano Tucci810c0522014-07-25 18:03:16 +0100242 Runnable crashHandler; // Optional local handler to be invoked in the process crash.
243
Dianne Hackborn8c76d912018-08-23 15:20:05 -0700244 // Cache of last retrieve memory info and uptime, to throttle how frequently
245 // apps can requyest it.
246 Debug.MemoryInfo lastMemInfo;
247 long lastMemInfoTime;
248
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700249 // Controller for driving the process state on the window manager side.
250 final private WindowProcessController mWindowProcessController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 // all ServiceRecord running in this process
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700252 final ArraySet<ServiceRecord> services = new ArraySet<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 // services that are currently executing code (need to remain foreground).
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700254 final ArraySet<ServiceRecord> executingServices = new ArraySet<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 // All ConnectionRecord this process holds
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700256 final ArraySet<ConnectionRecord> connections = new ArraySet<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 // all IIntentReceivers that are registered from this process.
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700258 final ArraySet<ReceiverList> receivers = new ArraySet<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 // class (String) -> ContentProviderRecord
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700260 final ArrayMap<String, ContentProviderRecord> pubProviders = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 // All ContentProviderRecord process is using
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700262 final ArrayList<ContentProviderConnection> conProviders = new ArrayList<>();
Michal Karpinskiac116df2018-12-10 17:51:42 +0000263 // A set of tokens that currently contribute to this process being temporarily whitelisted
264 // to start activities even if it's not in the foreground
265 final ArraySet<Binder> mAllowBackgroundActivityStartsTokens = new ArraySet<>();
Michal Karpinskib7daac22019-03-25 10:12:41 +0000266 // a set of UIDs of all bound clients
267 private ArraySet<Integer> mBoundClientUids = new ArraySet<>();
Dianne Hackbornbf36ee22013-07-26 18:24:10 -0700268
Torne (Richard Coles)466cbe42017-05-31 14:09:14 -0400269 String isolatedEntryPoint; // Class to run on start if this is a special isolated process.
270 String[] isolatedEntryPointArgs; // Arguments to pass to isolatedEntryPoint's main().
271
Dianne Hackbornbf36ee22013-07-26 18:24:10 -0700272 boolean execServicesFg; // do we need to be executing services in the foreground?
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700273 private boolean mPersistent;// always keep this application running?
274 private boolean mCrashing; // are we in the process of crashing?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 Dialog crashDialog; // dialog being displayed due to crash.
Christopher Tate58d380d2013-03-19 13:10:03 -0700276 boolean forceCrashReport; // suppress normal auto-dismiss of crash dialog & report UI?
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700277 private boolean mNotResponding; // does the app have a not responding dialog?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 Dialog anrDialog; // dialog being displayed due to app not resp.
Riddle Hsuaaef7312019-01-24 19:00:58 +0800279 volatile boolean removed; // Whether this process should be killed and removed from process
280 // list. It is set when the package is force-stopped or the process
281 // has crashed too many times.
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700282 private boolean mDebugging; // was app launched for debugging?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 boolean waitedForDebugger; // has process show wait for debugger dialog?
284 Dialog waitDialog; // current wait for debugger dialog
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700285
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800286 String shortStringName; // caching of toShortString() result.
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700287 String stringName; // caching of toString() result.
Sudheer Shankaf6690102017-10-16 10:20:32 -0700288 boolean pendingStart; // Process start is pending.
289 long startSeq; // Seq no. indicating the latest process start associated with
290 // this process record.
Sudheer Shanka87915d62018-11-06 10:57:35 -0800291 int mountMode; // Indicates how the external storage was mounted for this process.
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 // These reports are generated & stored when an app gets into an error condition.
294 // They will be "null" when all is OK.
295 ActivityManager.ProcessErrorStateInfo crashingReport;
296 ActivityManager.ProcessErrorStateInfo notRespondingReport;
297
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200298 // Who will be notified of the error. This is usually an activity in the
299 // app that installed the package.
300 ComponentName errorReportReceiver;
301
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700302 // Process is currently hosting a backup agent for backup or restore
303 public boolean inFullBackup;
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700304 // App is allowed to manage whitelists such as temporary Power Save mode whitelist.
305 boolean whitelistManager;
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700306
Sudheer Shankaf6690102017-10-16 10:20:32 -0700307 // Params used in starting this process.
Martijn Coenene8431c22019-03-28 14:09:38 +0100308 HostingRecord hostingRecord;
Sudheer Shankaf6690102017-10-16 10:20:32 -0700309 String seInfo;
310 long startTime;
311 // This will be same as {@link #uid} usually except for some apps used during factory testing.
312 int startUid;
313
Jing Ji094c3ef2019-08-27 17:02:09 -0700314 // Cached task info for OomAdjuster
315 private static final int VALUE_INVALID = -1;
316 private static final int VALUE_FALSE = 0;
317 private static final int VALUE_TRUE = 1;
318 private int mCachedHasActivities = VALUE_INVALID;
319 private int mCachedIsHeavyWeight = VALUE_INVALID;
320 private int mCachedHasVisibleActivities = VALUE_INVALID;
321 private int mCachedIsHomeProcess = VALUE_INVALID;
322 private int mCachedIsPreviousProcess = VALUE_INVALID;
323 private int mCachedHasRecentTasks = VALUE_INVALID;
324 private int mCachedIsReceivingBroadcast = VALUE_INVALID;
325 int mCachedAdj = ProcessList.INVALID_ADJ;
326 boolean mCachedForegroundActivities = false;
327 int mCachedProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;
328 int mCachedSchedGroup = ProcessList.SCHED_GROUP_BACKGROUND;
329
Jing Ji619d6442019-11-21 18:26:40 -0800330 boolean mReachable; // Whether or not this process is reachable from given process
331
Jing Jibf7a1552019-11-15 14:36:24 -0800332 // A callback that should be executed on app died; after that it'll be set to null
333 Runnable mAppDiedCallback;
334
Martijn Coenene8431c22019-03-28 14:09:38 +0100335 void setStartParams(int startUid, HostingRecord hostingRecord, String seInfo,
Sudheer Shankaf6690102017-10-16 10:20:32 -0700336 long startTime) {
337 this.startUid = startUid;
Martijn Coenene8431c22019-03-28 14:09:38 +0100338 this.hostingRecord = hostingRecord;
Sudheer Shankaf6690102017-10-16 10:20:32 -0700339 this.seInfo = seInfo;
340 this.startTime = startTime;
341 }
342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700344 final long nowUptime = SystemClock.uptimeMillis();
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700345
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800346 pw.print(prefix); pw.print("user #"); pw.print(userId);
347 pw.print(" uid="); pw.print(info.uid);
348 if (uid != info.uid) {
349 pw.print(" ISOLATED uid="); pw.print(uid);
350 }
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800351 pw.print(" gids={");
352 if (gids != null) {
353 for (int gi=0; gi<gids.length; gi++) {
354 if (gi != 0) pw.print(", ");
355 pw.print(gids[gi]);
356
357 }
358 }
359 pw.println("}");
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700360 pw.print(prefix); pw.print("mRequiredAbi="); pw.print(mRequiredAbi);
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800361 pw.print(" instructionSet="); pw.println(instructionSet);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700362 if (info.className != null) {
363 pw.print(prefix); pw.print("class="); pw.println(info.className);
364 }
365 if (info.manageSpaceActivityName != null) {
366 pw.print(prefix); pw.print("manageSpaceActivityName=");
367 pw.println(info.manageSpaceActivityName);
368 }
Yunfan Chen75157d72018-07-27 14:47:21 +0900369
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700370 pw.print(prefix); pw.print("dir="); pw.print(info.sourceDir);
371 pw.print(" publicDir="); pw.print(info.publicSourceDir);
372 pw.print(" data="); pw.println(info.dataDir);
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700373 pw.print(prefix); pw.print("packageList={");
374 for (int i=0; i<pkgList.size(); i++) {
375 if (i > 0) pw.print(", ");
376 pw.print(pkgList.keyAt(i));
377 }
378 pw.println("}");
Dianne Hackbornfee756f2014-07-16 17:31:10 -0700379 if (pkgDeps != null) {
380 pw.print(prefix); pw.print("packageDependencies={");
381 for (int i=0; i<pkgDeps.size(); i++) {
382 if (i > 0) pw.print(", ");
383 pw.print(pkgDeps.valueAt(i));
384 }
385 pw.println("}");
386 }
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700387 pw.print(prefix); pw.print("compat="); pw.println(compat);
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700388 if (mInstr != null) {
389 pw.print(prefix); pw.print("mInstr="); pw.println(mInstr);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700390 }
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700391 pw.print(prefix); pw.print("thread="); pw.println(thread);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700392 pw.print(prefix); pw.print("pid="); pw.print(pid); pw.print(" starting=");
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700393 pw.println(starting);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700394 pw.print(prefix); pw.print("lastActivityTime=");
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700395 TimeUtils.formatDuration(lastActivityTime, nowUptime, pw);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700396 pw.print(" lastPssTime=");
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700397 TimeUtils.formatDuration(lastPssTime, nowUptime, pw);
Dianne Hackborne17b4452018-01-10 13:15:40 -0800398 pw.print(" pssStatType="); pw.print(pssStatType);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700399 pw.print(" nextPssTime=");
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700400 TimeUtils.formatDuration(nextPssTime, nowUptime, pw);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700401 pw.println();
402 pw.print(prefix); pw.print("adjSeq="); pw.print(adjSeq);
403 pw.print(" lruSeq="); pw.print(lruSeq);
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -0800404 pw.print(" lastPss="); DebugUtils.printSizeValue(pw, lastPss*1024);
Thierry Strudelf0a44182016-01-07 18:08:04 -0800405 pw.print(" lastSwapPss="); DebugUtils.printSizeValue(pw, lastSwapPss*1024);
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -0800406 pw.print(" lastCachedPss="); DebugUtils.printSizeValue(pw, lastCachedPss*1024);
Thierry Strudelf0a44182016-01-07 18:08:04 -0800407 pw.print(" lastCachedSwapPss="); DebugUtils.printSizeValue(pw, lastCachedSwapPss*1024);
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -0800408 pw.println();
Dianne Hackborne17b4452018-01-10 13:15:40 -0800409 pw.print(prefix); pw.print("procStateMemTracker: ");
410 procStateMemTracker.dumpLine(pw);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700411 pw.print(prefix); pw.print("cached="); pw.print(cached);
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800412 pw.print(" empty="); pw.println(empty);
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700413 if (serviceb) {
414 pw.print(prefix); pw.print("serviceb="); pw.print(serviceb);
415 pw.print(" serviceHighRam="); pw.println(serviceHighRam);
416 }
Dianne Hackborn35f72be2013-09-16 10:57:39 -0700417 if (notCachedSinceIdle) {
418 pw.print(prefix); pw.print("notCachedSinceIdle="); pw.print(notCachedSinceIdle);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700419 pw.print(" initialIdlePss="); pw.println(initialIdlePss);
Dianne Hackborn35f72be2013-09-16 10:57:39 -0700420 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700421 pw.print(prefix); pw.print("oom: max="); pw.print(maxAdj);
Wale Ogunwale31913b52018-10-13 08:29:31 -0700422 pw.print(" curRaw="); pw.print(mCurRawAdj);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700423 pw.print(" setRaw="); pw.print(setRawAdj);
424 pw.print(" cur="); pw.print(curAdj);
425 pw.print(" set="); pw.println(setAdj);
Tim Murray0b1c8aa2018-11-20 10:28:38 -0800426 pw.print(prefix); pw.print("lastCompactTime="); pw.print(lastCompactTime);
427 pw.print(" lastCompactAction="); pw.print(lastCompactAction);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700428 pw.print(prefix); pw.print("mCurSchedGroup="); pw.print(mCurSchedGroup);
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700429 pw.print(" setSchedGroup="); pw.print(setSchedGroup);
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700430 pw.print(" systemNoUi="); pw.print(systemNoUi);
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700431 pw.print(" trimMemoryLevel="); pw.println(trimMemoryLevel);
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700432 pw.print(prefix); pw.print("curProcState="); pw.print(getCurProcState());
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700433 pw.print(" mRepProcState="); pw.print(mRepProcState);
Dianne Hackbornf1cca182013-08-01 10:50:28 -0700434 pw.print(" pssProcState="); pw.print(pssProcState);
435 pw.print(" setProcState="); pw.print(setProcState);
Hui Yu26969322019-08-21 14:56:35 -0700436 pw.print(" curCapability="); pw.print(curCapability);
437 pw.print(" setCapability="); pw.print(setCapability);
Dianne Hackbornf1cca182013-08-01 10:50:28 -0700438 pw.print(" lastStateTime=");
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700439 TimeUtils.formatDuration(lastStateTime, nowUptime, pw);
Dianne Hackbornf1cca182013-08-01 10:50:28 -0700440 pw.println();
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700441 if (hasShownUi || mPendingUiClean || hasAboveClient || treatLikeActivity) {
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700442 pw.print(prefix); pw.print("hasShownUi="); pw.print(hasShownUi);
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700443 pw.print(" pendingUiClean="); pw.print(mPendingUiClean);
Dianne Hackbornf0f94d12014-03-17 16:04:21 -0700444 pw.print(" hasAboveClient="); pw.print(hasAboveClient);
445 pw.print(" treatLikeActivity="); pw.println(treatLikeActivity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700446 }
Dianne Hackborna631d562018-11-20 15:58:15 -0800447 if (connectionService != null || connectionGroup != 0) {
448 pw.print(prefix); pw.print("connectionGroup="); pw.print(connectionGroup);
449 pw.print(" Importance="); pw.print(connectionImportance);
450 pw.print(" Service="); pw.println(connectionService);
451 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700452 if (hasTopUi() || hasOverlayUi() || runningRemoteAnimation) {
453 pw.print(prefix); pw.print("hasTopUi="); pw.print(hasTopUi());
454 pw.print(" hasOverlayUi="); pw.print(hasOverlayUi());
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100455 pw.print(" runningRemoteAnimation="); pw.println(runningRemoteAnimation);
Dianne Hackborna1baf732017-05-25 15:33:53 -0700456 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700457 if (mHasForegroundServices || forcingToImportant != null) {
458 pw.print(prefix); pw.print("mHasForegroundServices="); pw.print(mHasForegroundServices);
Dianne Hackbornf965f402017-05-04 23:27:23 -0700459 pw.print(" forcingToImportant="); pw.println(forcingToImportant);
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700460 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700461 if (reportedInteraction || mFgInteractionTime != 0) {
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700462 pw.print(prefix); pw.print("reportedInteraction=");
463 pw.print(reportedInteraction);
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700464 if (mInteractionEventTime != 0) {
Dianne Hackbornfcc95a62015-11-02 13:43:29 -0800465 pw.print(" time=");
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700466 TimeUtils.formatDuration(mInteractionEventTime, SystemClock.elapsedRealtime(), pw);
Dianne Hackbornfcc95a62015-11-02 13:43:29 -0800467 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700468 if (mFgInteractionTime != 0) {
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700469 pw.print(" fgInteractionTime=");
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700470 TimeUtils.formatDuration(mFgInteractionTime, SystemClock.elapsedRealtime(), pw);
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700471 }
472 pw.println();
473 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700474 if (mPersistent || removed) {
475 pw.print(prefix); pw.print("persistent="); pw.print(mPersistent);
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700476 pw.print(" removed="); pw.println(removed);
477 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700478 if (mHasClientActivities || mHasForegroundActivities || repForegroundActivities) {
479 pw.print(prefix); pw.print("hasClientActivities="); pw.print(mHasClientActivities);
480 pw.print(" foregroundActivities="); pw.print(mHasForegroundActivities);
Dianne Hackborn684bf342014-04-29 17:56:57 -0700481 pw.print(" (rep="); pw.print(repForegroundActivities); pw.println(")");
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700482 }
Dianne Hackborn1a405642015-11-04 12:32:45 -0800483 if (lastProviderTime > 0) {
484 pw.print(prefix); pw.print("lastProviderTime=");
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700485 TimeUtils.formatDuration(lastProviderTime, nowUptime, pw);
Dianne Hackborn1a405642015-11-04 12:32:45 -0800486 pw.println();
487 }
Amith Yamasanie5bfeee2018-09-05 18:52:35 -0700488 if (lastTopTime > 0) {
489 pw.print(prefix); pw.print("lastTopTime=");
490 TimeUtils.formatDuration(lastTopTime, nowUptime, pw);
491 pw.println();
492 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700493 if (hasStartedServices) {
494 pw.print(prefix); pw.print("hasStartedServices="); pw.println(hasStartedServices);
495 }
Sudheer Shankaf6690102017-10-16 10:20:32 -0700496 if (pendingStart) {
497 pw.print(prefix); pw.print("pendingStart="); pw.println(pendingStart);
498 }
499 pw.print(prefix); pw.print("startSeq="); pw.println(startSeq);
Sudheer Shanka87915d62018-11-06 10:57:35 -0800500 pw.print(prefix); pw.print("mountMode="); pw.println(
501 DebugUtils.valueToString(Zygote.class, "MOUNT_EXTERNAL_", mountMode));
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700502 if (setProcState > ActivityManager.PROCESS_STATE_SERVICE) {
Dianne Hackborn287952c2010-09-22 22:34:31 -0700503 pw.print(prefix); pw.print("lastCpuTime="); pw.print(lastCpuTime);
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700504 if (lastCpuTime > 0) {
505 pw.print(" timeUsed=");
506 TimeUtils.formatDuration(curCpuTime - lastCpuTime, pw);
507 }
508 pw.print(" whenUnimportant=");
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700509 TimeUtils.formatDuration(mWhenUnimportant - nowUptime, pw);
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700510 pw.println();
Dianne Hackborn287952c2010-09-22 22:34:31 -0700511 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700512 pw.print(prefix); pw.print("lastRequestedGc=");
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700513 TimeUtils.formatDuration(lastRequestedGc, nowUptime, pw);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700514 pw.print(" lastLowMemory=");
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700515 TimeUtils.formatDuration(lastLowMemory, nowUptime, pw);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700516 pw.print(" reportLowMemory="); pw.println(reportLowMemory);
Dianne Hackbornd4125632014-09-22 12:52:49 -0700517 if (killed || killedByAm || waitingToKill != null) {
518 pw.print(prefix); pw.print("killed="); pw.print(killed);
519 pw.print(" killedByAm="); pw.print(killedByAm);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700520 pw.print(" waitingToKill="); pw.println(waitingToKill);
Dianne Hackborn149427c2010-04-23 14:20:03 -0700521 }
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700522 if (mDebugging || mCrashing || crashDialog != null || mNotResponding
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700523 || anrDialog != null || bad) {
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700524 pw.print(prefix); pw.print("mDebugging="); pw.print(mDebugging);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700525 pw.print(" mCrashing="); pw.print(mCrashing);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700526 pw.print(" "); pw.print(crashDialog);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700527 pw.print(" mNotResponding="); pw.print(mNotResponding);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700528 pw.print(" " ); pw.print(anrDialog);
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200529 pw.print(" bad="); pw.print(bad);
530
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700531 // mCrashing or mNotResponding is always set before errorReportReceiver
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200532 if (errorReportReceiver != null) {
533 pw.print(" errorReportReceiver=");
534 pw.print(errorReportReceiver.flattenToShortString());
535 }
536 pw.println();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700537 }
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700538 if (whitelistManager) {
539 pw.print(prefix); pw.print("whitelistManager="); pw.println(whitelistManager);
540 }
Torne (Richard Coles)466cbe42017-05-31 14:09:14 -0400541 if (isolatedEntryPoint != null || isolatedEntryPointArgs != null) {
542 pw.print(prefix); pw.print("isolatedEntryPoint="); pw.println(isolatedEntryPoint);
543 pw.print(prefix); pw.print("isolatedEntryPointArgs=");
544 pw.println(Arrays.toString(isolatedEntryPointArgs));
545 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700546 mWindowProcessController.dump(pw, prefix);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700547 if (services.size() > 0) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700548 pw.print(prefix); pw.println("Services:");
Dianne Hackbornc8230512013-07-13 21:32:12 -0700549 for (int i=0; i<services.size(); i++) {
550 pw.print(prefix); pw.print(" - "); pw.println(services.valueAt(i));
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700551 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700552 }
553 if (executingServices.size() > 0) {
Dianne Hackbornbf36ee22013-07-26 18:24:10 -0700554 pw.print(prefix); pw.print("Executing Services (fg=");
555 pw.print(execServicesFg); pw.println(")");
Dianne Hackbornc8230512013-07-13 21:32:12 -0700556 for (int i=0; i<executingServices.size(); i++) {
557 pw.print(prefix); pw.print(" - "); pw.println(executingServices.valueAt(i));
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700558 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700559 }
560 if (connections.size() > 0) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700561 pw.print(prefix); pw.println("Connections:");
Dianne Hackbornc8230512013-07-13 21:32:12 -0700562 for (int i=0; i<connections.size(); i++) {
563 pw.print(prefix); pw.print(" - "); pw.println(connections.valueAt(i));
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700564 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700565 }
566 if (pubProviders.size() > 0) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700567 pw.print(prefix); pw.println("Published Providers:");
Dianne Hackborn390517b2013-05-30 15:03:32 -0700568 for (int i=0; i<pubProviders.size(); i++) {
569 pw.print(prefix); pw.print(" - "); pw.println(pubProviders.keyAt(i));
570 pw.print(prefix); pw.print(" -> "); pw.println(pubProviders.valueAt(i));
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700571 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700572 }
573 if (conProviders.size() > 0) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700574 pw.print(prefix); pw.println("Connected Providers:");
575 for (int i=0; i<conProviders.size(); i++) {
576 pw.print(prefix); pw.print(" - "); pw.println(conProviders.get(i).toShortString());
577 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700578 }
yangzhenyud509bc92016-08-31 18:26:46 +0800579 if (!curReceivers.isEmpty()) {
580 pw.print(prefix); pw.println("Current Receivers:");
581 for (int i=0; i < curReceivers.size(); i++) {
582 pw.print(prefix); pw.print(" - "); pw.println(curReceivers.valueAt(i));
583 }
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700584 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700585 if (receivers.size() > 0) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700586 pw.print(prefix); pw.println("Receivers:");
Dianne Hackbornc8230512013-07-13 21:32:12 -0700587 for (int i=0; i<receivers.size(); i++) {
588 pw.print(prefix); pw.print(" - "); pw.println(receivers.valueAt(i));
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700589 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700590 }
Edward Cunninghamde661762019-03-27 15:51:56 +0000591 if (mAllowBackgroundActivityStartsTokens.size() > 0) {
592 pw.print(prefix); pw.println("Background activity start whitelist tokens:");
593 for (int i = 0; i < mAllowBackgroundActivityStartsTokens.size(); i++) {
594 pw.print(prefix); pw.print(" - ");
595 pw.println(mAllowBackgroundActivityStartsTokens.valueAt(i));
596 }
597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700599
Bookatz152623f2018-07-23 15:57:44 -0700600 ProcessRecord(ActivityManagerService _service, ApplicationInfo _info, String _processName,
Wale Ogunwale387b34c2018-10-25 19:59:40 -0700601 int _uid) {
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -0700602 mService = _service;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 info = _info;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800604 isolated = _info.uid != _uid;
Martijn Coenen01e719b2018-12-05 16:01:38 +0100605 appZygote = (UserHandle.getAppId(_uid) >= Process.FIRST_APP_ZYGOTE_ISOLATED_UID
606 && UserHandle.getAppId(_uid) <= Process.LAST_APP_ZYGOTE_ISOLATED_UID);
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800607 uid = _uid;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700608 userId = UserHandle.getUserId(_uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 processName = _processName;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700610 maxAdj = ProcessList.UNKNOWN_ADJ;
Wale Ogunwale31913b52018-10-13 08:29:31 -0700611 mCurRawAdj = setRawAdj = ProcessList.INVALID_ADJ;
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700612 curAdj = setAdj = verifiedAdj = ProcessList.INVALID_ADJ;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700613 mPersistent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 removed = false;
Dianne Hackbornf1cca182013-08-01 10:50:28 -0700615 lastStateTime = lastPssTime = nextPssTime = SystemClock.uptimeMillis();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700616 mWindowProcessController = new WindowProcessController(
Wale Ogunwale387b34c2018-10-25 19:59:40 -0700617 mService.mActivityTaskManager, info, processName, uid, userId, this, this);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700618 pkgList.put(_info.packageName, new ProcessStats.ProcessStateHolder(_info.longVersionCode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 }
620
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700621 public void setPid(int _pid) {
622 pid = _pid;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700623 mWindowProcessController.setPid(pid);
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700624 procStatFile = null;
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800625 shortStringName = null;
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700626 stringName = null;
627 }
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700628
629 public void makeActive(IApplicationThread _thread, ProcessStatsService tracker) {
630 if (thread == null) {
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700631 final ProcessState origBase = baseProcessTracker;
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700632 if (origBase != null) {
633 origBase.setState(ProcessStats.STATE_NOTHING,
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700634 tracker.getMemFactorLocked(), SystemClock.uptimeMillis(), pkgList.mPkgList);
Chenjie Yubd1a28f2018-07-17 14:55:19 -0700635 for (int ipkg = pkgList.size() - 1; ipkg >= 0; ipkg--) {
636 StatsLog.write(StatsLog.PROCESS_STATE_CHANGED,
637 uid, processName, pkgList.keyAt(ipkg),
638 ActivityManager.processStateAmToProto(ProcessStats.STATE_NOTHING),
639 pkgList.valueAt(ipkg).appVersion);
640 }
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700641 origBase.makeInactive();
642 }
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800643 baseProcessTracker = tracker.getProcessStateLocked(info.packageName, info.uid,
Patrick Baumannc2def582018-04-04 12:14:15 -0700644 info.longVersionCode, processName);
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700645 baseProcessTracker.makeActive();
646 for (int i=0; i<pkgList.size(); i++) {
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700647 ProcessStats.ProcessStateHolder holder = pkgList.valueAt(i);
648 if (holder.state != null && holder.state != origBase) {
649 holder.state.makeInactive();
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700650 }
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800651 tracker.updateProcessStateHolderLocked(holder, pkgList.keyAt(i), info.uid,
Patrick Baumannc2def582018-04-04 12:14:15 -0700652 info.longVersionCode, processName);
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700653 if (holder.state != baseProcessTracker) {
654 holder.state.makeActive();
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700655 }
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700656 }
657 }
658 thread = _thread;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700659 mWindowProcessController.setThread(thread);
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700660 }
661
662 public void makeInactive(ProcessStatsService tracker) {
Dianne Hackborn53459a72013-09-17 17:14:57 -0700663 thread = null;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700664 mWindowProcessController.setThread(null);
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700665 final ProcessState origBase = baseProcessTracker;
Dianne Hackborn53459a72013-09-17 17:14:57 -0700666 if (origBase != null) {
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700667 if (origBase != null) {
668 origBase.setState(ProcessStats.STATE_NOTHING,
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700669 tracker.getMemFactorLocked(), SystemClock.uptimeMillis(), pkgList.mPkgList);
Chenjie Yubd1a28f2018-07-17 14:55:19 -0700670 for (int ipkg = pkgList.size() - 1; ipkg >= 0; ipkg--) {
671 StatsLog.write(StatsLog.PROCESS_STATE_CHANGED,
672 uid, processName, pkgList.keyAt(ipkg),
673 ActivityManager.processStateAmToProto(ProcessStats.STATE_NOTHING),
674 pkgList.valueAt(ipkg).appVersion);
675 }
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700676 origBase.makeInactive();
677 }
678 baseProcessTracker = null;
679 for (int i=0; i<pkgList.size(); i++) {
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700680 ProcessStats.ProcessStateHolder holder = pkgList.valueAt(i);
681 if (holder.state != null && holder.state != origBase) {
682 holder.state.makeInactive();
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700683 }
Dianne Hackborn2aec55a2018-06-26 10:35:35 -0700684 holder.pkg = null;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700685 holder.state = null;
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700686 }
687 }
688 }
689
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700690 boolean hasActivities() {
691 return mWindowProcessController.hasActivities();
692 }
693
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700694 boolean hasActivitiesOrRecentTasks() {
695 return mWindowProcessController.hasActivitiesOrRecentTasks();
696 }
697
698 boolean hasRecentTasks() {
699 return mWindowProcessController.hasRecentTasks();
700 }
701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 /**
703 * This method returns true if any of the activities within the process record are interesting
704 * to the user. See HistoryRecord.isInterestingToUserLocked()
705 */
706 public boolean isInterestingToUserLocked() {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700707 if (mWindowProcessController.isInterestingToUser()) {
708 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
Narayan Kamatha22d9fb2017-04-26 15:14:26 +0100710
711 final int servicesSize = services.size();
712 for (int i = 0; i < servicesSize; i++) {
713 ServiceRecord r = services.valueAt(i);
714 if (r.isForeground) {
715 return true;
716 }
717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 return false;
719 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800720
Dianne Hackborn1b64e0d2011-07-17 15:23:59 -0700721 public void unlinkDeathRecipient() {
722 if (deathRecipient != null && thread != null) {
723 thread.asBinder().unlinkToDeath(deathRecipient, 0);
724 }
725 deathRecipient = null;
726 }
727
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700728 void updateHasAboveClientLocked() {
729 hasAboveClient = false;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700730 for (int i=connections.size()-1; i>=0; i--) {
731 ConnectionRecord cr = connections.valueAt(i);
732 if ((cr.flags&Context.BIND_ABOVE_CLIENT) != 0) {
733 hasAboveClient = true;
734 break;
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700735 }
736 }
737 }
738
Dianne Hackbornc8230512013-07-13 21:32:12 -0700739 int modifyRawOomAdj(int adj) {
740 if (hasAboveClient) {
741 // If this process has bound to any services with BIND_ABOVE_CLIENT,
742 // then we need to drop its adjustment to be lower than the service's
743 // in order to honor the request. We want to drop it by one adjustment
744 // level... but there is special meaning applied to various levels so
745 // we will skip some of them.
746 if (adj < ProcessList.FOREGROUND_APP_ADJ) {
747 // System process will not get dropped, ever
748 } else if (adj < ProcessList.VISIBLE_APP_ADJ) {
749 adj = ProcessList.VISIBLE_APP_ADJ;
750 } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) {
751 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
Amith Yamasani0567ec62019-01-23 11:11:02 -0800752 } else if (adj < ProcessList.PERCEPTIBLE_LOW_APP_ADJ) {
753 adj = ProcessList.PERCEPTIBLE_LOW_APP_ADJ;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700754 } else if (adj < ProcessList.CACHED_APP_MIN_ADJ) {
755 adj = ProcessList.CACHED_APP_MIN_ADJ;
756 } else if (adj < ProcessList.CACHED_APP_MAX_ADJ) {
757 adj++;
758 }
759 }
760 return adj;
761 }
762
Joe Onorato57190282016-04-20 15:37:49 -0700763 void scheduleCrash(String message) {
764 // Checking killedbyAm should keep it from showing the crash dialog if the process
765 // was already dead for a good / normal reason.
766 if (!killedByAm) {
767 if (thread != null) {
768 if (pid == Process.myPid()) {
769 Slog.w(TAG, "scheduleCrash: trying to crash system process!");
770 return;
771 }
772 long ident = Binder.clearCallingIdentity();
773 try {
774 thread.scheduleCrash(message);
775 } catch (RemoteException e) {
776 // If it's already dead our work is done. If it's wedged just kill it.
777 // We won't get the crash dialog or the error reporting.
778 kill("scheduleCrash for '" + message + "' failed", true);
779 } finally {
780 Binder.restoreCallingIdentity(ident);
781 }
782 }
783 }
784 }
785
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700786 void kill(String reason, boolean noisy) {
787 if (!killedByAm) {
Yusuke Satocf4b2542015-06-17 00:21:28 -0700788 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "kill");
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -0700789 if (mService != null && (noisy || info.uid == mService.mCurOomAdjUid)) {
790 mService.reportUidInfoMessageLocked(TAG,
791 "Killing " + toShortString() + " (adj " + setAdj + "): " + reason,
792 info.uid);
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700793 }
Sudheer Shankaf6690102017-10-16 10:20:32 -0700794 if (pid > 0) {
795 EventLog.writeEvent(EventLogTags.AM_KILL, userId, pid, processName, setAdj, reason);
796 Process.killProcessQuiet(pid);
Amith Yamasani98a00922018-08-21 12:50:30 -0400797 ProcessList.killProcessGroup(uid, pid);
Sudheer Shankaf6690102017-10-16 10:20:32 -0700798 } else {
799 pendingStart = false;
800 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700801 if (!mPersistent) {
Dianne Hackbornd4125632014-09-22 12:52:49 -0700802 killed = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700803 killedByAm = true;
804 }
Yusuke Satocf4b2542015-06-17 00:21:28 -0700805 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700806 }
807 }
808
Wale Ogunwale51cc98a2018-10-15 10:41:05 -0700809 @Override
Yi Jin129fc6c2017-09-28 15:48:38 -0700810 public void writeToProto(ProtoOutputStream proto, long fieldId) {
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800811 writeToProto(proto, fieldId, -1);
812 }
813
814 public void writeToProto(ProtoOutputStream proto, long fieldId, int lruIndex) {
Yi Jin129fc6c2017-09-28 15:48:38 -0700815 long token = proto.start(fieldId);
816 proto.write(ProcessRecordProto.PID, pid);
817 proto.write(ProcessRecordProto.PROCESS_NAME, processName);
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800818 proto.write(ProcessRecordProto.UID, info.uid);
819 if (UserHandle.getAppId(info.uid) >= Process.FIRST_APPLICATION_UID) {
Yi Jin129fc6c2017-09-28 15:48:38 -0700820 proto.write(ProcessRecordProto.USER_ID, userId);
821 proto.write(ProcessRecordProto.APP_ID, UserHandle.getAppId(info.uid));
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800822 }
823 if (uid != info.uid) {
824 proto.write(ProcessRecordProto.ISOLATED_APP_ID, UserHandle.getAppId(uid));
Yi Jin129fc6c2017-09-28 15:48:38 -0700825 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700826 proto.write(ProcessRecordProto.PERSISTENT, mPersistent);
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800827 if (lruIndex >= 0) {
828 proto.write(ProcessRecordProto.LRU_INDEX, lruIndex);
829 }
Yi Jin129fc6c2017-09-28 15:48:38 -0700830 proto.end(token);
831 }
832
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800833 public String toShortString() {
834 if (shortStringName != null) {
835 return shortStringName;
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700836 }
837 StringBuilder sb = new StringBuilder(128);
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800838 toShortString(sb);
839 return shortStringName = sb.toString();
840 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700841
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800842 void toShortString(StringBuilder sb) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700843 sb.append(pid);
844 sb.append(':');
845 sb.append(processName);
846 sb.append('/');
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800847 if (info.uid < Process.FIRST_APPLICATION_UID) {
848 sb.append(uid);
849 } else {
850 sb.append('u');
851 sb.append(userId);
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700852 int appId = UserHandle.getAppId(info.uid);
853 if (appId >= Process.FIRST_APPLICATION_UID) {
854 sb.append('a');
855 sb.append(appId - Process.FIRST_APPLICATION_UID);
856 } else {
857 sb.append('s');
858 sb.append(appId);
859 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800860 if (uid != info.uid) {
861 sb.append('i');
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700862 sb.append(UserHandle.getAppId(uid) - Process.FIRST_ISOLATED_UID);
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800863 }
864 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800865 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700866
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800867 public String toString() {
868 if (stringName != null) {
869 return stringName;
870 }
871 StringBuilder sb = new StringBuilder(128);
872 sb.append("ProcessRecord{");
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700873 sb.append(Integer.toHexString(System.identityHashCode(this)));
874 sb.append(' ');
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800875 toShortString(sb);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700876 sb.append('}');
877 return stringName = sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
Dianne Hackborn9210bc82013-09-05 12:31:16 -0700879
880 public String makeAdjReason() {
Dianne Hackborn9210bc82013-09-05 12:31:16 -0700881 if (adjSource != null || adjTarget != null) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700882 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn9210bc82013-09-05 12:31:16 -0700883 sb.append(' ');
884 if (adjTarget instanceof ComponentName) {
885 sb.append(((ComponentName)adjTarget).flattenToShortString());
886 } else if (adjTarget != null) {
887 sb.append(adjTarget.toString());
888 } else {
889 sb.append("{null}");
890 }
891 sb.append("<=");
892 if (adjSource instanceof ProcessRecord) {
893 sb.append("Proc{");
894 sb.append(((ProcessRecord)adjSource).toShortString());
895 sb.append("}");
896 } else if (adjSource != null) {
897 sb.append(adjSource.toString());
898 } else {
899 sb.append("{null}");
900 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700901 return sb.toString();
Dianne Hackborn9210bc82013-09-05 12:31:16 -0700902 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700903 return null;
Dianne Hackborn9210bc82013-09-05 12:31:16 -0700904 }
905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 /*
907 * Return true if package has been added false if not
908 */
Dianne Hackborn3accca02013-09-20 09:32:11 -0700909 public boolean addPackage(String pkg, long versionCode, ProcessStatsService tracker) {
Dianne Hackborn78a369c2013-06-11 17:10:32 -0700910 if (!pkgList.containsKey(pkg)) {
Dianne Hackborn3251b902014-06-20 14:40:53 -0700911 ProcessStats.ProcessStateHolder holder = new ProcessStats.ProcessStateHolder(
912 versionCode);
Dianne Hackborn53459a72013-09-17 17:14:57 -0700913 if (baseProcessTracker != null) {
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800914 tracker.updateProcessStateHolderLocked(holder, pkg, info.uid, versionCode,
915 processName);
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700916 pkgList.put(pkg, holder);
917 if (holder.state != baseProcessTracker) {
918 holder.state.makeActive();
Dianne Hackborn53459a72013-09-17 17:14:57 -0700919 }
920 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -0700921 pkgList.put(pkg, holder);
Dianne Hackborn53459a72013-09-17 17:14:57 -0700922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 return true;
924 }
925 return false;
926 }
Dianne Hackborn78a369c2013-06-11 17:10:32 -0700927
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700928 public int getSetAdjWithServices() {
Dianne Hackborn20cdcee2013-07-10 18:47:04 -0700929 if (setAdj >= ProcessList.CACHED_APP_MIN_ADJ) {
930 if (hasStartedServices) {
931 return ProcessList.SERVICE_B_ADJ;
932 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700933 }
934 return setAdj;
935 }
936
Dianne Hackborna413dc02013-07-12 12:02:55 -0700937 public void forceProcessStateUpTo(int newState) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700938 if (mRepProcState > newState) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700939 mRepProcState = newState;
940 setCurProcState(newState);
Amith Yamasani08888c02018-12-07 09:38:25 -0800941 setCurRawProcState(newState);
Chenjie Yubd1a28f2018-07-17 14:55:19 -0700942 for (int ipkg = pkgList.size() - 1; ipkg >= 0; ipkg--) {
943 StatsLog.write(StatsLog.PROCESS_STATE_CHANGED,
944 uid, processName, pkgList.keyAt(ipkg),
945 ActivityManager.processStateAmToProto(mRepProcState),
946 pkgList.valueAt(ipkg).appVersion);
947 }
Dianne Hackborna413dc02013-07-12 12:02:55 -0700948 }
949 }
950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 /*
952 * Delete all packages from list except the package indicated in info
953 */
Dianne Hackbornd2932242013-08-05 18:18:42 -0700954 public void resetPackageList(ProcessStatsService tracker) {
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700955 final int N = pkgList.size();
Dianne Hackborne56c2c32013-09-17 10:56:49 -0700956 if (baseProcessTracker != null) {
957 long now = SystemClock.uptimeMillis();
958 baseProcessTracker.setState(ProcessStats.STATE_NOTHING,
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700959 tracker.getMemFactorLocked(), now, pkgList.mPkgList);
Chenjie Yubd1a28f2018-07-17 14:55:19 -0700960 for (int ipkg = pkgList.size() - 1; ipkg >= 0; ipkg--) {
961 StatsLog.write(StatsLog.PROCESS_STATE_CHANGED,
962 uid, processName, pkgList.keyAt(ipkg),
963 ActivityManager.processStateAmToProto(ProcessStats.STATE_NOTHING),
964 pkgList.valueAt(ipkg).appVersion);
965 }
Dianne Hackborne56c2c32013-09-17 10:56:49 -0700966 if (N != 1) {
967 for (int i=0; i<N; i++) {
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700968 ProcessStats.ProcessStateHolder holder = pkgList.valueAt(i);
969 if (holder.state != null && holder.state != baseProcessTracker) {
970 holder.state.makeInactive();
Dianne Hackborne56c2c32013-09-17 10:56:49 -0700971 }
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700972
Dianne Hackborne56c2c32013-09-17 10:56:49 -0700973 }
974 pkgList.clear();
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700975 ProcessStats.ProcessStateHolder holder = new ProcessStats.ProcessStateHolder(
Patrick Baumannc2def582018-04-04 12:14:15 -0700976 info.longVersionCode);
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800977 tracker.updateProcessStateHolderLocked(holder, info.packageName, info.uid,
Dianne Hackborn2aec55a2018-06-26 10:35:35 -0700978 info.longVersionCode, processName);
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700979 pkgList.put(info.packageName, holder);
Dianne Hackborn2aec55a2018-06-26 10:35:35 -0700980 if (holder.state != baseProcessTracker) {
981 holder.state.makeActive();
Dianne Hackborne56c2c32013-09-17 10:56:49 -0700982 }
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700983 }
Dianne Hackborne56c2c32013-09-17 10:56:49 -0700984 } else if (N != 1) {
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700985 pkgList.clear();
Patrick Baumannc2def582018-04-04 12:14:15 -0700986 pkgList.put(info.packageName, new ProcessStats.ProcessStateHolder(info.longVersionCode));
Dianne Hackborn78a369c2013-06-11 17:10:32 -0700987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 public String[] getPackageList() {
991 int size = pkgList.size();
992 if (size == 0) {
993 return null;
994 }
995 String list[] = new String[size];
Dianne Hackborn78a369c2013-06-11 17:10:32 -0700996 for (int i=0; i<pkgList.size(); i++) {
997 list[i] = pkgList.keyAt(i);
998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 return list;
1000 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001001
Zimuzo972e1cd2019-01-28 16:30:01 +00001002 public List<VersionedPackage> getPackageListWithVersionCode() {
1003 int size = pkgList.size();
1004 if (size == 0) {
1005 return null;
1006 }
1007 List<VersionedPackage> list = new ArrayList<>();
1008 for (int i = 0; i < pkgList.size(); i++) {
1009 list.add(new VersionedPackage(pkgList.keyAt(i), pkgList.valueAt(i).appVersion));
1010 }
1011 return list;
1012 }
1013
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001014 WindowProcessController getWindowProcessController() {
1015 return mWindowProcessController;
1016 }
1017
1018 void setCurrentSchedulingGroup(int curSchedGroup) {
1019 mCurSchedGroup = curSchedGroup;
1020 mWindowProcessController.setCurrentSchedulingGroup(curSchedGroup);
1021 }
1022
1023 int getCurrentSchedulingGroup() {
1024 return mCurSchedGroup;
1025 }
1026
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001027 void setCurProcState(int curProcState) {
1028 mCurProcState = curProcState;
1029 mWindowProcessController.setCurrentProcState(mCurProcState);
1030 }
1031
1032 int getCurProcState() {
1033 return mCurProcState;
1034 }
1035
Amith Yamasani08888c02018-12-07 09:38:25 -08001036 void setCurRawProcState(int curRawProcState) {
1037 mCurRawProcState = curRawProcState;
1038 }
1039
1040 int getCurRawProcState() {
1041 return mCurRawProcState;
1042 }
1043
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001044 void setReportedProcState(int repProcState) {
1045 mRepProcState = repProcState;
Chenjie Yubd1a28f2018-07-17 14:55:19 -07001046 for (int ipkg = pkgList.size() - 1; ipkg >= 0; ipkg--) {
1047 StatsLog.write(StatsLog.PROCESS_STATE_CHANGED,
1048 uid, processName, pkgList.keyAt(ipkg),
1049 ActivityManager.processStateAmToProto(mRepProcState),
1050 pkgList.valueAt(ipkg).appVersion);
1051 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001052 mWindowProcessController.setReportedProcState(repProcState);
1053 }
1054
1055 int getReportedProcState() {
1056 return mRepProcState;
1057 }
1058
1059 void setCrashing(boolean crashing) {
1060 mCrashing = crashing;
1061 mWindowProcessController.setCrashing(crashing);
1062 }
1063
1064 boolean isCrashing() {
1065 return mCrashing;
1066 }
1067
1068 void setNotResponding(boolean notResponding) {
1069 mNotResponding = notResponding;
1070 mWindowProcessController.setNotResponding(notResponding);
1071 }
1072
1073 boolean isNotResponding() {
1074 return mNotResponding;
1075 }
1076
1077 void setPersistent(boolean persistent) {
1078 mPersistent = persistent;
1079 mWindowProcessController.setPersistent(persistent);
1080 }
1081
1082 boolean isPersistent() {
1083 return mPersistent;
1084 }
1085
1086 public void setRequiredAbi(String requiredAbi) {
1087 mRequiredAbi = requiredAbi;
1088 mWindowProcessController.setRequiredAbi(requiredAbi);
1089 }
1090
1091 String getRequiredAbi() {
1092 return mRequiredAbi;
1093 }
1094
Amith Yamasania0a30a12019-01-22 11:38:06 -08001095 void setHasForegroundServices(boolean hasForegroundServices, int fgServiceTypes) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001096 mHasForegroundServices = hasForegroundServices;
Amith Yamasania0a30a12019-01-22 11:38:06 -08001097 mFgServiceTypes = fgServiceTypes;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001098 mWindowProcessController.setHasForegroundServices(hasForegroundServices);
1099 }
1100
1101 boolean hasForegroundServices() {
1102 return mHasForegroundServices;
1103 }
1104
Amith Yamasania0a30a12019-01-22 11:38:06 -08001105 boolean hasLocationForegroundServices() {
1106 return mHasForegroundServices
1107 && (mFgServiceTypes & ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION) != 0;
1108 }
1109
Hui Yu26969322019-08-21 14:56:35 -07001110 boolean hasLocationCapability() {
1111 return (setCapability & ActivityManager.PROCESS_CAPABILITY_FOREGROUND_LOCATION) != 0;
1112 }
1113
Narayan Kamath396ce722019-03-01 16:53:26 +00001114 int getForegroundServiceTypes() {
1115 return mHasForegroundServices ? mFgServiceTypes : 0;
1116 }
1117
1118 int getReportedForegroundServiceTypes() {
1119 return mRepFgServiceTypes;
1120 }
1121
1122 void setReportedForegroundServiceTypes(int foregroundServiceTypes) {
1123 mRepFgServiceTypes = foregroundServiceTypes;
1124 }
1125
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001126 void setHasForegroundActivities(boolean hasForegroundActivities) {
1127 mHasForegroundActivities = hasForegroundActivities;
1128 mWindowProcessController.setHasForegroundActivities(hasForegroundActivities);
1129 }
1130
1131 boolean hasForegroundActivities() {
1132 return mHasForegroundActivities;
1133 }
1134
1135 void setHasClientActivities(boolean hasClientActivities) {
1136 mHasClientActivities = hasClientActivities;
1137 mWindowProcessController.setHasClientActivities(hasClientActivities);
1138 }
1139
1140 boolean hasClientActivities() {
1141 return mHasClientActivities;
1142 }
1143
1144 void setHasTopUi(boolean hasTopUi) {
1145 mHasTopUi = hasTopUi;
1146 mWindowProcessController.setHasTopUi(hasTopUi);
1147 }
1148
1149 boolean hasTopUi() {
1150 return mHasTopUi;
1151 }
1152
1153 void setHasOverlayUi(boolean hasOverlayUi) {
1154 mHasOverlayUi = hasOverlayUi;
1155 mWindowProcessController.setHasOverlayUi(hasOverlayUi);
1156 }
1157
1158 boolean hasOverlayUi() {
1159 return mHasOverlayUi;
1160 }
1161
1162 void setInteractionEventTime(long interactionEventTime) {
1163 mInteractionEventTime = interactionEventTime;
1164 mWindowProcessController.setInteractionEventTime(interactionEventTime);
1165 }
1166
1167 long getInteractionEventTime() {
1168 return mInteractionEventTime;
1169 }
1170
1171 void setFgInteractionTime(long fgInteractionTime) {
1172 mFgInteractionTime = fgInteractionTime;
1173 mWindowProcessController.setFgInteractionTime(fgInteractionTime);
1174 }
1175
1176 long getFgInteractionTime() {
1177 return mFgInteractionTime;
1178 }
1179
1180 void setWhenUnimportant(long whenUnimportant) {
1181 mWhenUnimportant = whenUnimportant;
1182 mWindowProcessController.setWhenUnimportant(whenUnimportant);
1183 }
1184
1185 long getWhenUnimportant() {
1186 return mWhenUnimportant;
1187 }
1188
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001189 void setDebugging(boolean debugging) {
1190 mDebugging = debugging;
1191 mWindowProcessController.setDebugging(debugging);
1192 }
1193
1194 boolean isDebugging() {
1195 return mDebugging;
1196 }
1197
1198 void setUsingWrapper(boolean usingWrapper) {
1199 mUsingWrapper = usingWrapper;
1200 mWindowProcessController.setUsingWrapper(usingWrapper);
1201 }
1202
1203 boolean isUsingWrapper() {
1204 return mUsingWrapper;
1205 }
1206
Michal Karpinskiac116df2018-12-10 17:51:42 +00001207 void addAllowBackgroundActivityStartsToken(Binder entity) {
Michal Karpinskic8aa91b2019-01-10 16:45:59 +00001208 if (entity == null) return;
Michal Karpinskiac116df2018-12-10 17:51:42 +00001209 mAllowBackgroundActivityStartsTokens.add(entity);
1210 mWindowProcessController.setAllowBackgroundActivityStarts(true);
1211 }
1212
1213 void removeAllowBackgroundActivityStartsToken(Binder entity) {
Michal Karpinskic8aa91b2019-01-10 16:45:59 +00001214 if (entity == null) return;
Michal Karpinskiac116df2018-12-10 17:51:42 +00001215 mAllowBackgroundActivityStartsTokens.remove(entity);
1216 mWindowProcessController.setAllowBackgroundActivityStarts(
1217 !mAllowBackgroundActivityStartsTokens.isEmpty());
1218 }
1219
Alan Stokes8473a2c2019-04-29 13:51:18 +01001220 void addBoundClientUid(int clientUid) {
1221 mBoundClientUids.add(clientUid);
Michal Karpinskib7daac22019-03-25 10:12:41 +00001222 mWindowProcessController.setBoundClientUids(mBoundClientUids);
1223 }
1224
1225 void updateBoundClientUids() {
1226 if (services.isEmpty()) {
1227 clearBoundClientUids();
1228 return;
1229 }
1230 // grab a set of clientUids of all connections of all services
1231 ArraySet<Integer> boundClientUids = new ArraySet<>();
1232 final int K = services.size();
1233 for (int j = 0; j < K; j++) {
1234 ArrayMap<IBinder, ArrayList<ConnectionRecord>> conns =
1235 services.valueAt(j).getConnections();
1236 final int N = conns.size();
1237 for (int conni = 0; conni < N; conni++) {
1238 ArrayList<ConnectionRecord> c = conns.valueAt(conni);
1239 for (int i = 0; i < c.size(); i++) {
1240 boundClientUids.add(c.get(i).clientUid);
1241 }
1242 }
1243 }
1244 mBoundClientUids = boundClientUids;
1245 mWindowProcessController.setBoundClientUids(mBoundClientUids);
1246 }
1247
1248 void addBoundClientUidsOfNewService(ServiceRecord sr) {
1249 if (sr == null) {
1250 return;
1251 }
1252 ArrayMap<IBinder, ArrayList<ConnectionRecord>> conns = sr.getConnections();
1253 for (int conni = conns.size() - 1; conni >= 0; conni--) {
1254 ArrayList<ConnectionRecord> c = conns.valueAt(conni);
1255 for (int i = 0; i < c.size(); i++) {
1256 mBoundClientUids.add(c.get(i).clientUid);
1257 }
1258 }
1259 mWindowProcessController.setBoundClientUids(mBoundClientUids);
1260 }
1261
1262 void clearBoundClientUids() {
1263 mBoundClientUids.clear();
1264 mWindowProcessController.setBoundClientUids(mBoundClientUids);
1265 }
1266
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001267 void setActiveInstrumentation(ActiveInstrumentation instr) {
1268 mInstr = instr;
Michal Karpinski4bc56492019-01-31 12:07:33 +00001269 boolean isInstrumenting = instr != null;
1270 mWindowProcessController.setInstrumenting(isInstrumenting,
1271 isInstrumenting && instr.mHasBackgroundActivityStartsPermission);
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001272 }
1273
1274 ActiveInstrumentation getActiveInstrumentation() {
1275 return mInstr;
1276 }
1277
Wale Ogunwale31913b52018-10-13 08:29:31 -07001278 void setCurRawAdj(int curRawAdj) {
1279 mCurRawAdj = curRawAdj;
1280 mWindowProcessController.setPerceptible(curRawAdj <= ProcessList.PERCEPTIBLE_APP_ADJ);
1281 }
1282
1283 int getCurRawAdj() {
1284 return mCurRawAdj;
1285 }
1286
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001287 @Override
1288 public void clearProfilerIfNeeded() {
1289 synchronized (mService) {
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08001290 if (mService.mProfileData.getProfileProc() == null
1291 || mService.mProfileData.getProfilerInfo() == null
1292 || mService.mProfileData.getProfileProc() != this) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001293 return;
1294 }
1295 mService.clearProfilerLocked();
1296 }
1297 }
1298
1299 @Override
1300 public void updateServiceConnectionActivities() {
1301 synchronized (mService) {
1302 mService.mServices.updateServiceConnectionActivitiesLocked(this);
1303 }
1304 }
1305
1306 @Override
1307 public void setPendingUiClean(boolean pendingUiClean) {
1308 synchronized (mService) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001309 mPendingUiClean = pendingUiClean;
1310 mWindowProcessController.setPendingUiClean(pendingUiClean);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001311 }
1312 }
1313
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001314 boolean hasPendingUiClean() {
1315 return mPendingUiClean;
1316 }
1317
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001318 @Override
1319 public void setPendingUiCleanAndForceProcessStateUpTo(int newState) {
1320 synchronized (mService) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001321 setPendingUiClean(true);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001322 forceProcessStateUpTo(newState);
1323 }
1324 }
1325
1326 @Override
Riddle Hsu17e38422019-04-12 16:55:11 +08001327 public void updateProcessInfo(boolean updateServiceConnectionActivities, boolean activityChange,
1328 boolean updateOomAdj) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001329 synchronized (mService) {
1330 if (updateServiceConnectionActivities) {
1331 mService.mServices.updateServiceConnectionActivitiesLocked(this);
1332 }
Riddle Hsu17e38422019-04-12 16:55:11 +08001333 mService.mProcessList.updateLruProcessLocked(this, activityChange, null /* client */);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001334 if (updateOomAdj) {
Jing Ji094c3ef2019-08-27 17:02:09 -07001335 mService.updateOomAdjLocked(this, OomAdjuster.OOM_ADJ_REASON_ACTIVITY);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001336 }
1337 }
1338 }
1339
1340 @Override
Riddle Hsuaaef7312019-01-24 19:00:58 +08001341 public boolean isRemoved() {
1342 return removed;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001343 }
1344
Wale Ogunwale86b74462018-07-02 08:42:43 -07001345 /**
1346 * Returns the total time (in milliseconds) spent executing in both user and system code.
1347 * Safe to call without lock held.
1348 */
Wale Ogunwale9c103022018-10-18 07:44:54 -07001349 @Override
Wale Ogunwale86b74462018-07-02 08:42:43 -07001350 public long getCpuTime() {
1351 return mService.mProcessCpuTracker.getCpuTimeForPid(pid);
1352 }
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001353
Wale Ogunwale9c103022018-10-18 07:44:54 -07001354 @Override
Riddle Hsu17e38422019-04-12 16:55:11 +08001355 public void onStartActivity(int topProcessState, boolean setProfileProc, String packageName,
1356 long versionCode) {
Wale Ogunwale9c103022018-10-18 07:44:54 -07001357 synchronized (mService) {
1358 waitingToKill = null;
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08001359 if (setProfileProc) {
1360 mService.mProfileData.setProfileProc(this);
Wale Ogunwale9c103022018-10-18 07:44:54 -07001361 }
Riddle Hsu17e38422019-04-12 16:55:11 +08001362 if (packageName != null) {
1363 addPackage(packageName, versionCode, mService.mProcessStats);
1364 }
Wale Ogunwale9c103022018-10-18 07:44:54 -07001365
Riddle Hsu17e38422019-04-12 16:55:11 +08001366 // Update oom adj first, we don't want the additional states are involved in this round.
1367 updateProcessInfo(false /* updateServiceConnectionActivities */,
1368 true /* activityChange */, true /* updateOomAdj */);
Wale Ogunwale9c103022018-10-18 07:44:54 -07001369 hasShownUi = true;
1370 setPendingUiClean(true);
1371 forceProcessStateUpTo(topProcessState);
Wale Ogunwale9c103022018-10-18 07:44:54 -07001372 }
1373 }
1374
1375 @Override
1376 public void appDied() {
1377 synchronized (mService) {
1378 mService.appDiedLocked(this);
1379 }
1380 }
1381
Jorim Jaggi589c5ba2019-07-30 16:50:13 +02001382 @Override
1383 public void setRunningRemoteAnimation(boolean runningRemoteAnimation) {
1384 if (pid == Process.myPid()) {
1385 Slog.wtf(TAG, "system can't run remote animation");
1386 return;
1387 }
1388 synchronized (mService) {
1389 if (this.runningRemoteAnimation == runningRemoteAnimation) {
1390 return;
1391 }
1392 this.runningRemoteAnimation = runningRemoteAnimation;
1393 if (DEBUG_OOM_ADJ) {
1394 Slog.i(TAG, "Setting runningRemoteAnimation=" + runningRemoteAnimation
1395 + " for pid=" + pid);
1396 }
1397 mService.updateOomAdjLocked(this, true, OomAdjuster.OOM_ADJ_REASON_UI_VISIBILITY);
1398 }
1399 }
1400
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001401 public long getInputDispatchingTimeout() {
1402 return mWindowProcessController.getInputDispatchingTimeout();
1403 }
1404
Yang Lu732d6382018-11-05 07:53:12 -08001405 public int getProcessClassEnum() {
1406 if (pid == MY_PID) {
1407 return ServerProtoEnums.SYSTEM_SERVER;
1408 }
1409 if (info == null) {
1410 return ServerProtoEnums.ERROR_SOURCE_UNKNOWN;
1411 }
1412 return (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0 ? ServerProtoEnums.SYSTEM_APP :
1413 ServerProtoEnums.DATA_APP;
1414 }
1415
Yunfan Chen585f2932019-01-29 16:04:45 +09001416 /**
1417 * Unless configured otherwise, swallow ANRs in background processes & kill the process.
1418 * Non-private access is for tests only.
1419 */
1420 @VisibleForTesting
1421 boolean isSilentAnr() {
1422 return !getShowBackground() && !isInterestingForBackgroundTraces();
1423 }
1424
1425 /** Non-private access is for tests only. */
1426 @VisibleForTesting
1427 List<ProcessRecord> getLruProcessList() {
1428 return mService.mProcessList.mLruProcesses;
1429 }
1430
1431 /** Non-private access is for tests only. */
1432 @VisibleForTesting
1433 boolean isMonitorCpuUsage() {
1434 return mService.MONITOR_CPU_USAGE;
1435 }
1436
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001437 void appNotResponding(String activityShortComponentName, ApplicationInfo aInfo,
1438 String parentShortComponentName, WindowProcessController parentProcess,
1439 boolean aboveSystem, String annotation) {
1440 ArrayList<Integer> firstPids = new ArrayList<>(5);
1441 SparseArray<Boolean> lastPids = new SparseArray<>(20);
1442
Wale Ogunwalee2172292018-10-25 10:11:10 -07001443 mWindowProcessController.appEarlyNotResponding(annotation, () -> kill("anr", true));
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001444
1445 long anrTime = SystemClock.uptimeMillis();
Yunfan Chen585f2932019-01-29 16:04:45 +09001446 if (isMonitorCpuUsage()) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001447 mService.updateCpuStatsNow();
1448 }
1449
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001450 synchronized (mService) {
1451 // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down.
Wale Ogunwale387b34c2018-10-25 19:59:40 -07001452 if (mService.mAtmInternal.isShuttingDown()) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001453 Slog.i(TAG, "During shutdown skipping ANR: " + this + " " + annotation);
1454 return;
1455 } else if (isNotResponding()) {
1456 Slog.i(TAG, "Skipping duplicate ANR: " + this + " " + annotation);
1457 return;
1458 } else if (isCrashing()) {
1459 Slog.i(TAG, "Crashing app skipping ANR: " + this + " " + annotation);
1460 return;
1461 } else if (killedByAm) {
1462 Slog.i(TAG, "App already killed by AM skipping ANR: " + this + " " + annotation);
1463 return;
1464 } else if (killed) {
1465 Slog.i(TAG, "Skipping died app ANR: " + this + " " + annotation);
1466 return;
1467 }
1468
1469 // In case we come through here for the same app before completing
1470 // this one, mark as anring now so we will bail out.
1471 setNotResponding(true);
1472
1473 // Log the ANR to the event log.
1474 EventLog.writeEvent(EventLogTags.AM_ANR, userId, pid, processName, info.flags,
1475 annotation);
1476
1477 // Dump thread traces as quickly as we can, starting with "interesting" processes.
1478 firstPids.add(pid);
1479
1480 // Don't dump other PIDs if it's a background ANR
Yunfan Chen585f2932019-01-29 16:04:45 +09001481 if (!isSilentAnr()) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001482 int parentPid = pid;
1483 if (parentProcess != null && parentProcess.getPid() > 0) {
1484 parentPid = parentProcess.getPid();
1485 }
1486 if (parentPid != pid) firstPids.add(parentPid);
1487
1488 if (MY_PID != pid && MY_PID != parentPid) firstPids.add(MY_PID);
1489
Yunfan Chen585f2932019-01-29 16:04:45 +09001490 for (int i = getLruProcessList().size() - 1; i >= 0; i--) {
1491 ProcessRecord r = getLruProcessList().get(i);
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001492 if (r != null && r.thread != null) {
1493 int myPid = r.pid;
1494 if (myPid > 0 && myPid != pid && myPid != parentPid && myPid != MY_PID) {
1495 if (r.isPersistent()) {
1496 firstPids.add(myPid);
1497 if (DEBUG_ANR) Slog.i(TAG, "Adding persistent proc: " + r);
1498 } else if (r.treatLikeActivity) {
1499 firstPids.add(myPid);
1500 if (DEBUG_ANR) Slog.i(TAG, "Adding likely IME: " + r);
1501 } else {
1502 lastPids.put(myPid, Boolean.TRUE);
1503 if (DEBUG_ANR) Slog.i(TAG, "Adding ANR proc: " + r);
1504 }
1505 }
1506 }
1507 }
1508 }
1509 }
1510
1511 // Log the ANR to the main log.
1512 StringBuilder info = new StringBuilder();
1513 info.setLength(0);
1514 info.append("ANR in ").append(processName);
1515 if (activityShortComponentName != null) {
1516 info.append(" (").append(activityShortComponentName).append(")");
1517 }
1518 info.append("\n");
1519 info.append("PID: ").append(pid).append("\n");
1520 if (annotation != null) {
1521 info.append("Reason: ").append(annotation).append("\n");
1522 }
1523 if (parentShortComponentName != null
1524 && parentShortComponentName.equals(activityShortComponentName)) {
1525 info.append("Parent: ").append(parentShortComponentName).append("\n");
1526 }
1527
1528 ProcessCpuTracker processCpuTracker = new ProcessCpuTracker(true);
1529
1530 // don't dump native PIDs for background ANRs unless it is the process of interest
1531 String[] nativeProcs = null;
Yunfan Chen585f2932019-01-29 16:04:45 +09001532 if (isSilentAnr()) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001533 for (int i = 0; i < NATIVE_STACKS_OF_INTEREST.length; i++) {
1534 if (NATIVE_STACKS_OF_INTEREST[i].equals(processName)) {
1535 nativeProcs = new String[] { processName };
1536 break;
1537 }
1538 }
1539 } else {
1540 nativeProcs = NATIVE_STACKS_OF_INTEREST;
1541 }
1542
1543 int[] pids = nativeProcs == null ? null : Process.getPidsForCommands(nativeProcs);
1544 ArrayList<Integer> nativePids = null;
1545
1546 if (pids != null) {
1547 nativePids = new ArrayList<>(pids.length);
1548 for (int i : pids) {
1549 nativePids.add(i);
1550 }
1551 }
1552
1553 // For background ANRs, don't pass the ProcessCpuTracker to
1554 // avoid spending 1/2 second collecting stats to rank lastPids.
1555 File tracesFile = ActivityManagerService.dumpStackTraces(firstPids,
Yunfan Chen585f2932019-01-29 16:04:45 +09001556 (isSilentAnr()) ? null : processCpuTracker, (isSilentAnr()) ? null : lastPids,
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001557 nativePids);
1558
1559 String cpuInfo = null;
Yunfan Chen585f2932019-01-29 16:04:45 +09001560 if (isMonitorCpuUsage()) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001561 mService.updateCpuStatsNow();
1562 synchronized (mService.mProcessCpuTracker) {
1563 cpuInfo = mService.mProcessCpuTracker.printCurrentState(anrTime);
1564 }
1565 info.append(processCpuTracker.printCurrentLoad());
1566 info.append(cpuInfo);
1567 }
1568
1569 info.append(processCpuTracker.printCurrentState(anrTime));
1570
1571 Slog.e(TAG, info.toString());
1572 if (tracesFile == null) {
1573 // There is no trace file, so dump (only) the alleged culprit's threads to the log
1574 Process.sendSignal(pid, Process.SIGNAL_QUIT);
1575 }
1576
1577 StatsLog.write(StatsLog.ANR_OCCURRED, uid, processName,
1578 activityShortComponentName == null ? "unknown": activityShortComponentName,
1579 annotation,
1580 (this.info != null) ? (this.info.isInstantApp()
1581 ? StatsLog.ANROCCURRED__IS_INSTANT_APP__TRUE
1582 : StatsLog.ANROCCURRED__IS_INSTANT_APP__FALSE)
1583 : StatsLog.ANROCCURRED__IS_INSTANT_APP__UNAVAILABLE,
1584 isInterestingToUserLocked()
1585 ? StatsLog.ANROCCURRED__FOREGROUND_STATE__FOREGROUND
Yang Lu732d6382018-11-05 07:53:12 -08001586 : StatsLog.ANROCCURRED__FOREGROUND_STATE__BACKGROUND,
1587 getProcessClassEnum(),
1588 (this.info != null) ? this.info.packageName : "");
Wale Ogunwale97b2b742018-10-18 08:08:54 -07001589 final ProcessRecord parentPr = parentProcess != null
1590 ? (ProcessRecord) parentProcess.mOwner : null;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001591 mService.addErrorToDropBox("anr", this, processName, activityShortComponentName,
Wale Ogunwale97b2b742018-10-18 08:08:54 -07001592 parentShortComponentName, parentPr, annotation, cpuInfo, tracesFile, null);
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001593
Wale Ogunwalee2172292018-10-25 10:11:10 -07001594 if (mWindowProcessController.appNotResponding(info.toString(), () -> kill("anr", true),
1595 () -> {
1596 synchronized (mService) {
1597 mService.mServices.scheduleServiceTimeoutLocked(this);
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001598 }
Wale Ogunwalee2172292018-10-25 10:11:10 -07001599 })) {
1600 return;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001601 }
1602
1603 synchronized (mService) {
Yunfan Chen585f2932019-01-29 16:04:45 +09001604 // mBatteryStatsService can be null if the AMS is constructed with injector only. This
1605 // will only happen in tests.
1606 if (mService.mBatteryStatsService != null) {
1607 mService.mBatteryStatsService.noteProcessAnr(processName, uid);
1608 }
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001609
Hui Yu8e3e4452019-04-11 10:57:51 -07001610 if (isSilentAnr() && !isDebugging()) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001611 kill("bg anr", true);
1612 return;
1613 }
1614
1615 // Set the app's notResponding state, and look up the errorReportReceiver
1616 makeAppNotRespondingLocked(activityShortComponentName,
1617 annotation != null ? "ANR " + annotation : "ANR", info.toString());
1618
Yunfan Chen585f2932019-01-29 16:04:45 +09001619 // mUiHandler can be null if the AMS is constructed with injector only. This will only
1620 // happen in tests.
1621 if (mService.mUiHandler != null) {
1622 // Bring up the infamous App Not Responding dialog
1623 Message msg = Message.obtain();
1624 msg.what = ActivityManagerService.SHOW_NOT_RESPONDING_UI_MSG;
1625 msg.obj = new AppNotRespondingDialog.Data(this, aInfo, aboveSystem);
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001626
Yunfan Chen585f2932019-01-29 16:04:45 +09001627 mService.mUiHandler.sendMessage(msg);
1628 }
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001629 }
1630 }
1631
1632 private void makeAppNotRespondingLocked(String activity, String shortMsg, String longMsg) {
1633 setNotResponding(true);
Yunfan Chen585f2932019-01-29 16:04:45 +09001634 // mAppErrors can be null if the AMS is constructed with injector only. This will only
1635 // happen in tests.
1636 if (mService.mAppErrors != null) {
1637 notRespondingReport = mService.mAppErrors.generateProcessError(this,
1638 ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING,
1639 activity, shortMsg, longMsg, null);
1640 }
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001641 startAppProblemLocked();
1642 getWindowProcessController().stopFreezingActivities();
1643 }
1644
1645 void startAppProblemLocked() {
1646 // If this app is not running under the current user, then we can't give it a report button
1647 // because that would require launching the report UI under a different user.
1648 errorReportReceiver = null;
1649
1650 for (int userId : mService.mUserController.getCurrentProfileIds()) {
1651 if (this.userId == userId) {
1652 errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver(
1653 mService.mContext, info.packageName, info.flags);
1654 }
1655 }
1656 mService.skipCurrentReceiverLocked(this);
1657 }
1658
1659 private boolean isInterestingForBackgroundTraces() {
1660 // The system_server is always considered interesting.
1661 if (pid == MY_PID) {
1662 return true;
1663 }
1664
1665 // A package is considered interesting if any of the following is true :
1666 //
1667 // - It's displaying an activity.
1668 // - It's the SystemUI.
1669 // - It has an overlay or a top UI visible.
1670 //
1671 // NOTE: The check whether a given ProcessRecord belongs to the systemui
1672 // process is a bit of a kludge, but the same pattern seems repeated at
1673 // several places in the system server.
1674 return isInterestingToUserLocked() ||
1675 (info != null && "com.android.systemui".equals(info.packageName))
1676 || (hasTopUi() || hasOverlayUi());
1677 }
Yunfan Chen585f2932019-01-29 16:04:45 +09001678
1679 private boolean getShowBackground() {
1680 return Settings.Secure.getInt(mService.mContext.getContentResolver(),
1681 Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
1682 }
Jing Ji094c3ef2019-08-27 17:02:09 -07001683
1684 void resetCachedInfo() {
1685 mCachedHasActivities = VALUE_INVALID;
1686 mCachedIsHeavyWeight = VALUE_INVALID;
1687 mCachedHasVisibleActivities = VALUE_INVALID;
1688 mCachedIsHomeProcess = VALUE_INVALID;
1689 mCachedIsPreviousProcess = VALUE_INVALID;
1690 mCachedHasRecentTasks = VALUE_INVALID;
1691 mCachedIsReceivingBroadcast = VALUE_INVALID;
1692 mCachedAdj = ProcessList.INVALID_ADJ;
1693 mCachedForegroundActivities = false;
1694 mCachedProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;
1695 mCachedSchedGroup = ProcessList.SCHED_GROUP_BACKGROUND;
1696 }
1697
1698 boolean getCachedHasActivities() {
1699 if (mCachedHasActivities == VALUE_INVALID) {
1700 mCachedHasActivities = getWindowProcessController().hasActivities() ? VALUE_TRUE
1701 : VALUE_FALSE;
1702 }
1703 return mCachedHasActivities == VALUE_TRUE;
1704 }
1705
1706 boolean getCachedIsHeavyWeight() {
1707 if (mCachedIsHeavyWeight == VALUE_INVALID) {
1708 mCachedIsHeavyWeight = mService.mAtmInternal.isHeavyWeightProcess(
1709 getWindowProcessController()) ? VALUE_TRUE : VALUE_FALSE;
1710 }
1711 return mCachedIsHeavyWeight == VALUE_TRUE;
1712 }
1713
1714 boolean getCachedHasVisibleActivities() {
1715 if (mCachedHasVisibleActivities == VALUE_INVALID) {
1716 mCachedHasVisibleActivities = getWindowProcessController().hasVisibleActivities()
1717 ? VALUE_TRUE : VALUE_FALSE;
1718 }
1719 return mCachedHasVisibleActivities == VALUE_TRUE;
1720 }
1721
1722 boolean getCachedIsHomeProcess() {
1723 if (mCachedIsHomeProcess == VALUE_INVALID) {
1724 mCachedIsHomeProcess = getWindowProcessController().isHomeProcess()
1725 ? VALUE_TRUE : VALUE_FALSE;
1726 }
1727 return mCachedIsHomeProcess == VALUE_TRUE;
1728 }
1729
1730 boolean getCachedIsPreviousProcess() {
1731 if (mCachedIsPreviousProcess == VALUE_INVALID) {
1732 mCachedIsPreviousProcess = getWindowProcessController().isPreviousProcess()
1733 ? VALUE_TRUE : VALUE_FALSE;
1734 }
1735 return mCachedIsPreviousProcess == VALUE_TRUE;
1736 }
1737
1738 boolean getCachedHasRecentTasks() {
1739 if (mCachedHasRecentTasks == VALUE_INVALID) {
1740 mCachedHasRecentTasks = getWindowProcessController().hasRecentTasks()
1741 ? VALUE_TRUE : VALUE_FALSE;
1742 }
1743 return mCachedHasRecentTasks == VALUE_TRUE;
1744 }
1745
1746 boolean getCachedIsReceivingBroadcast(ArraySet<BroadcastQueue> tmpQueue) {
1747 if (mCachedIsReceivingBroadcast == VALUE_INVALID) {
1748 tmpQueue.clear();
1749 mCachedIsReceivingBroadcast = mService.isReceivingBroadcastLocked(this, tmpQueue)
1750 ? VALUE_TRUE : VALUE_FALSE;
1751 if (mCachedIsReceivingBroadcast == VALUE_TRUE) {
1752 mCachedSchedGroup = tmpQueue.contains(mService.mFgBroadcastQueue)
1753 ? ProcessList.SCHED_GROUP_DEFAULT : ProcessList.SCHED_GROUP_BACKGROUND;
1754 }
1755 }
1756 return mCachedIsReceivingBroadcast == VALUE_TRUE;
1757 }
1758
1759 void computeOomAdjFromActivitiesIfNecessary(OomAdjuster.ComputeOomAdjWindowCallback callback,
1760 int adj, boolean foregroundActivities, int procState, int schedGroup, int appUid,
1761 int logUid, int processCurTop) {
1762 if (mCachedAdj != ProcessList.INVALID_ADJ) {
1763 return;
1764 }
1765 callback.initialize(this, adj, foregroundActivities, procState, schedGroup, appUid, logUid,
1766 processCurTop);
1767 final int minLayer = getWindowProcessController().computeOomAdjFromActivities(
1768 ProcessList.VISIBLE_APP_LAYER_MAX, callback);
1769
1770 mCachedAdj = callback.adj;
1771 mCachedForegroundActivities = callback.foregroundActivities;
1772 mCachedProcState = callback.procState;
1773 mCachedSchedGroup = callback.schedGroup;
1774
1775 if (mCachedAdj == ProcessList.VISIBLE_APP_ADJ) {
1776 mCachedAdj += minLayer;
1777 }
1778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779}