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