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