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