• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.server.am;
18 
19 import com.android.internal.os.BatteryStatsImpl;
20 import com.android.server.Watchdog;
21 
22 import android.app.ActivityManager;
23 import android.app.Dialog;
24 import android.app.IApplicationThread;
25 import android.app.IInstrumentationWatcher;
26 import android.content.ComponentName;
27 import android.content.pm.ApplicationInfo;
28 import android.os.Bundle;
29 import android.os.IBinder;
30 import android.os.RemoteException;
31 import android.util.PrintWriterPrinter;
32 
33 import java.io.PrintWriter;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 
38 /**
39  * Full information about a particular process that
40  * is currently running.
41  */
42 class ProcessRecord implements Watchdog.PssRequestor {
43     final BatteryStatsImpl.Uid.Proc batteryStats; // where to collect runtime statistics
44     final ApplicationInfo info; // all about the first app in the process
45     final String processName;   // name of the process
46     // List of packages running in the process
47     final HashSet<String> pkgList = new HashSet();
48     IApplicationThread thread;  // the actual proc...  may be null only if
49                                 // 'persistent' is true (in which case we
50                                 // are in the process of launching the app)
51     int pid;                    // The process of this application; 0 if none
52     boolean starting;           // True if the process is being started
53     int maxAdj;                 // Maximum OOM adjustment for this process
54     int hiddenAdj;              // If hidden, this is the adjustment to use
55     int curRawAdj;              // Current OOM unlimited adjustment for this process
56     int setRawAdj;              // Last set OOM unlimited adjustment for this process
57     int curAdj;                 // Current OOM adjustment for this process
58     int setAdj;                 // Last set OOM adjustment for this process
59     int curSchedGroup;          // Currently desired scheduling class
60     int setSchedGroup;          // Last set to background scheduling class
61     boolean setIsForeground;    // Running foreground UI when last set?
62     boolean foregroundServices; // Running any services that are foreground?
63     boolean bad;                // True if disabled in the bad process list
64     IBinder forcingToForeground;// Token that is forcing this process to be foreground
65     int adjSeq;                 // Sequence id for identifying repeated trav
66     ComponentName instrumentationClass;// class installed to instrument app
67     ApplicationInfo instrumentationInfo; // the application being instrumented
68     String instrumentationProfileFile; // where to save profiling
69     IInstrumentationWatcher instrumentationWatcher; // who is waiting
70     Bundle instrumentationArguments;// as given to us
71     ComponentName instrumentationResultClass;// copy of instrumentationClass
72     BroadcastRecord curReceiver;// receiver currently running in the app
73     long lastRequestedGc;       // When we last asked the app to do a gc
74     long lastLowMemory;         // When we last told the app that memory is low
75     boolean reportLowMemory;    // Set to true when waiting to report low mem
76     int lastPss;                // Last pss size reported by app.
77     String adjType;             // Debugging: primary thing impacting oom_adj.
78     int adjTypeCode;            // Debugging: adj code to report to app.
79     Object adjSource;           // Debugging: option dependent object.
80     Object adjTarget;           // Debugging: target component impacting oom_adj.
81 
82     // contains HistoryRecord objects
83     final ArrayList activities = new ArrayList();
84     // all ServiceRecord running in this process
85     final HashSet services = new HashSet();
86     // services that are currently executing code (need to remain foreground).
87     final HashSet<ServiceRecord> executingServices
88              = new HashSet<ServiceRecord>();
89     // All ConnectionRecord this process holds
90     final HashSet<ConnectionRecord> connections
91             = new HashSet<ConnectionRecord>();
92     // all IIntentReceivers that are registered from this process.
93     final HashSet<ReceiverList> receivers = new HashSet<ReceiverList>();
94     // class (String) -> ContentProviderRecord
95     final HashMap pubProviders = new HashMap();
96     // All ContentProviderRecord process is using
97     final HashMap<ContentProviderRecord, Integer> conProviders
98             = new HashMap<ContentProviderRecord, Integer>();
99 
100     boolean persistent;         // always keep this application running?
101     boolean crashing;           // are we in the process of crashing?
102     Dialog crashDialog;         // dialog being displayed due to crash.
103     boolean notResponding;      // does the app have a not responding dialog?
104     Dialog anrDialog;           // dialog being displayed due to app not resp.
105     boolean removed;            // has app package been removed from device?
106     boolean debugging;          // was app launched for debugging?
107     int persistentActivities;   // number of activities that are persistent
108     boolean waitedForDebugger;  // has process show wait for debugger dialog?
109     Dialog waitDialog;          // current wait for debugger dialog
110 
111     String stringName;          // caching of toString() result.
112 
113     // These reports are generated & stored when an app gets into an error condition.
114     // They will be "null" when all is OK.
115     ActivityManager.ProcessErrorStateInfo crashingReport;
116     ActivityManager.ProcessErrorStateInfo notRespondingReport;
117 
118     // Who will be notified of the error. This is usually an activity in the
119     // app that installed the package.
120     ComponentName errorReportReceiver;
121 
dump(PrintWriter pw, String prefix)122     void dump(PrintWriter pw, String prefix) {
123         if (info.className != null) {
124             pw.print(prefix); pw.print("class="); pw.println(info.className);
125         }
126         if (info.manageSpaceActivityName != null) {
127             pw.print(prefix); pw.print("manageSpaceActivityName=");
128             pw.println(info.manageSpaceActivityName);
129         }
130         pw.print(prefix); pw.print("dir="); pw.print(info.sourceDir);
131                 pw.print(" publicDir="); pw.print(info.publicSourceDir);
132                 pw.print(" data="); pw.println(info.dataDir);
133         pw.print(prefix); pw.print("packageList="); pw.println(pkgList);
134         if (instrumentationClass != null || instrumentationProfileFile != null
135                 || instrumentationArguments != null) {
136             pw.print(prefix); pw.print("instrumentationClass=");
137                     pw.print(instrumentationClass);
138                     pw.print(" instrumentationProfileFile=");
139                     pw.println(instrumentationProfileFile);
140             pw.print(prefix); pw.print("instrumentationArguments=");
141                     pw.println(instrumentationArguments);
142             pw.print(prefix); pw.print("instrumentationInfo=");
143                     pw.println(instrumentationInfo);
144             if (instrumentationInfo != null) {
145                 instrumentationInfo.dump(new PrintWriterPrinter(pw), prefix + "  ");
146             }
147         }
148         pw.print(prefix); pw.print("thread="); pw.print(thread);
149                 pw.print(" curReceiver="); pw.println(curReceiver);
150         pw.print(prefix); pw.print("pid="); pw.print(pid); pw.print(" starting=");
151                 pw.print(starting); pw.print(" lastPss="); pw.println(lastPss);
152         pw.print(prefix); pw.print("oom: max="); pw.print(maxAdj);
153                 pw.print(" hidden="); pw.print(hiddenAdj);
154                 pw.print(" curRaw="); pw.print(curRawAdj);
155                 pw.print(" setRaw="); pw.print(setRawAdj);
156                 pw.print(" cur="); pw.print(curAdj);
157                 pw.print(" set="); pw.println(setAdj);
158         pw.print(prefix); pw.print("curSchedGroup="); pw.print(curSchedGroup);
159                 pw.print(" setSchedGroup="); pw.println(setSchedGroup);
160         pw.print(prefix); pw.print("setIsForeground="); pw.print(setIsForeground);
161                 pw.print(" foregroundServices="); pw.print(foregroundServices);
162                 pw.print(" forcingToForeground="); pw.println(forcingToForeground);
163         pw.print(prefix); pw.print("persistent="); pw.print(persistent);
164                 pw.print(" removed="); pw.print(removed);
165                 pw.print(" persistentActivities="); pw.println(persistentActivities);
166         if (debugging || crashing || crashDialog != null || notResponding
167                 || anrDialog != null || bad) {
168             pw.print(prefix); pw.print("debugging="); pw.print(debugging);
169                     pw.print(" crashing="); pw.print(crashing);
170                     pw.print(" "); pw.print(crashDialog);
171                     pw.print(" notResponding="); pw.print(notResponding);
172                     pw.print(" " ); pw.print(anrDialog);
173                     pw.print(" bad="); pw.print(bad);
174 
175                     // crashing or notResponding is always set before errorReportReceiver
176                     if (errorReportReceiver != null) {
177                         pw.print(" errorReportReceiver=");
178                         pw.print(errorReportReceiver.flattenToShortString());
179                     }
180                     pw.println();
181         }
182         if (activities.size() > 0) {
183             pw.print(prefix); pw.print("activities="); pw.println(activities);
184         }
185         if (services.size() > 0) {
186             pw.print(prefix); pw.print("services="); pw.println(services);
187         }
188         if (executingServices.size() > 0) {
189             pw.print(prefix); pw.print("executingServices="); pw.println(executingServices);
190         }
191         if (connections.size() > 0) {
192             pw.print(prefix); pw.print("connections="); pw.println(connections);
193         }
194         if (pubProviders.size() > 0) {
195             pw.print(prefix); pw.print("pubProviders="); pw.println(pubProviders);
196         }
197         if (conProviders.size() > 0) {
198             pw.print(prefix); pw.print("conProviders="); pw.println(conProviders);
199         }
200         if (receivers.size() > 0) {
201             pw.print(prefix); pw.print("receivers="); pw.println(receivers);
202         }
203     }
204 
ProcessRecord(BatteryStatsImpl.Uid.Proc _batteryStats, IApplicationThread _thread, ApplicationInfo _info, String _processName)205     ProcessRecord(BatteryStatsImpl.Uid.Proc _batteryStats, IApplicationThread _thread,
206             ApplicationInfo _info, String _processName) {
207         batteryStats = _batteryStats;
208         info = _info;
209         processName = _processName;
210         pkgList.add(_info.packageName);
211         thread = _thread;
212         maxAdj = ActivityManagerService.EMPTY_APP_ADJ;
213         hiddenAdj = ActivityManagerService.HIDDEN_APP_MIN_ADJ;
214         curRawAdj = setRawAdj = -100;
215         curAdj = setAdj = -100;
216         persistent = false;
217         removed = false;
218         persistentActivities = 0;
219     }
220 
setPid(int _pid)221     public void setPid(int _pid) {
222         pid = _pid;
223         stringName = null;
224     }
225 
226     /**
227      * This method returns true if any of the activities within the process record are interesting
228      * to the user. See HistoryRecord.isInterestingToUserLocked()
229      */
isInterestingToUserLocked()230     public boolean isInterestingToUserLocked() {
231         final int size = activities.size();
232         for (int i = 0 ; i < size ; i++) {
233             HistoryRecord r = (HistoryRecord) activities.get(i);
234             if (r.isInterestingToUserLocked()) {
235                 return true;
236             }
237         }
238         return false;
239     }
240 
stopFreezingAllLocked()241     public void stopFreezingAllLocked() {
242         int i = activities.size();
243         while (i > 0) {
244             i--;
245             ((HistoryRecord)activities.get(i)).stopFreezingScreenLocked(true);
246         }
247     }
248 
requestPss()249     public void requestPss() {
250         IApplicationThread localThread = thread;
251         if (localThread != null) {
252             try {
253                 localThread.requestPss();
254             } catch (RemoteException e) {
255             }
256         }
257     }
258 
toString()259     public String toString() {
260         if (stringName != null) {
261             return stringName;
262         }
263         StringBuilder sb = new StringBuilder(128);
264         sb.append("ProcessRecord{");
265         sb.append(Integer.toHexString(System.identityHashCode(this)));
266         sb.append(' ');
267         sb.append(pid);
268         sb.append(':');
269         sb.append(processName);
270         sb.append('/');
271         sb.append(info.uid);
272         sb.append('}');
273         return stringName = sb.toString();
274     }
275 
276     /*
277      *  Return true if package has been added false if not
278      */
addPackage(String pkg)279     public boolean addPackage(String pkg) {
280         if (!pkgList.contains(pkg)) {
281             pkgList.add(pkg);
282             return true;
283         }
284         return false;
285     }
286 
287     /*
288      *  Delete all packages from list except the package indicated in info
289      */
resetPackageList()290     public void resetPackageList() {
291         pkgList.clear();
292         pkgList.add(info.packageName);
293     }
294 
getPackageList()295     public String[] getPackageList() {
296         int size = pkgList.size();
297         if (size == 0) {
298             return null;
299         }
300         String list[] = new String[size];
301         pkgList.toArray(list);
302         return list;
303     }
304 }
305