summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/applications/ProcStatsEntry.java
blob: 41f7a53b45423e57a8dc03ebdeb56fb40dc4b1ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.applications;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.ArrayMap;
import android.util.Log;
import android.util.SparseArray;
import com.android.internal.app.ProcessStats;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public final class ProcStatsEntry implements Parcelable {
    private static final String TAG = "ProcStatsEntry";
    private static boolean DEBUG = ProcessStatsUi.DEBUG;

    final String mPackage;
    final int mUid;
    final String mName;
    final ArrayList<String> mPackages = new ArrayList<String>();
    final long mBgDuration;
    final long mAvgBgMem;
    final long mMaxBgMem;
    final double mBgWeight;
    final long mRunDuration;
    final long mAvgRunMem;
    final long mMaxRunMem;
    final double mRunWeight;

    String mBestTargetPackage;

    ArrayMap<String, ArrayList<Service>> mServices = new ArrayMap<String, ArrayList<Service>>(1);

    public ProcStatsEntry(ProcessStats.ProcessState proc, String packageName,
            ProcessStats.ProcessDataCollection tmpBgTotals,
            ProcessStats.ProcessDataCollection tmpRunTotals, boolean useUss) {
        ProcessStats.computeProcessData(proc, tmpBgTotals, 0);
        ProcessStats.computeProcessData(proc, tmpRunTotals, 0);
        mPackage = proc.mPackage;
        mUid = proc.mUid;
        mName = proc.mName;
        mPackages.add(packageName);
        mBgDuration = tmpBgTotals.totalTime;
        mAvgBgMem = useUss ? tmpBgTotals.avgUss : tmpBgTotals.avgPss;
        mMaxBgMem = useUss ? tmpBgTotals.maxUss : tmpBgTotals.maxPss;
        mBgWeight = mAvgBgMem * (double) mBgDuration;
        mRunDuration = tmpRunTotals.totalTime;
        mAvgRunMem = useUss ? tmpRunTotals.avgUss : tmpRunTotals.avgPss;
        mMaxRunMem = useUss ? tmpRunTotals.maxUss : tmpRunTotals.maxPss;
        mRunWeight = mAvgRunMem * (double) mRunDuration;
        if (DEBUG) Log.d(TAG, "New proc entry " + proc.mName + ": dur=" + mBgDuration
                + " avgpss=" + mAvgBgMem + " weight=" + mBgWeight);
    }

    public ProcStatsEntry(String pkgName, int uid, String procName, long duration, long mem) {
        mPackage = pkgName;
        mUid = uid;
        mName = procName;
        mBgDuration = mRunDuration = duration;
        mAvgBgMem = mMaxBgMem = mAvgRunMem = mMaxRunMem = mem;
        mBgWeight = mRunWeight = ((double)duration) * mem;
        if (DEBUG) Log.d(TAG, "New proc entry " + procName + ": dur=" + mBgDuration
                + " avgpss=" + mAvgBgMem + " weight=" + mBgWeight);
    }

    public ProcStatsEntry(Parcel in) {
        mPackage = in.readString();
        mUid = in.readInt();
        mName = in.readString();
        in.readStringList(mPackages);
        mBgDuration = in.readLong();
        mAvgBgMem = in.readLong();
        mMaxBgMem = in.readLong();
        mBgWeight = in.readDouble();
        mRunDuration = in.readLong();
        mAvgRunMem = in.readLong();
        mMaxRunMem = in.readLong();
        mRunWeight = in.readDouble();
        mBestTargetPackage = in.readString();
        final int N = in.readInt();
        if (N > 0) {
            mServices.ensureCapacity(N);
            for (int i=0; i<N; i++) {
                String key = in.readString();
                ArrayList<Service> value = new ArrayList<Service>();
                in.readTypedList(value, Service.CREATOR);
                mServices.append(key, value);
            }
        }
    }

    public void addPackage(String packageName) {
        mPackages.add(packageName);
    }

    public void evaluateTargetPackage(PackageManager pm, ProcessStats stats,
            ProcessStats.ProcessDataCollection bgTotals,
            ProcessStats.ProcessDataCollection runTotals, Comparator<ProcStatsEntry> compare,
            boolean useUss) {
        mBestTargetPackage = null;
        if (mPackages.size() == 1) {
            if (DEBUG) Log.d(TAG, "Eval pkg of " + mName + ": single pkg " + mPackages.get(0));
            mBestTargetPackage = mPackages.get(0);
            return;
        }

        // If one of the packages is the framework itself, that wins.
        // See if there is one significant package that was running here.
        for (int ipkg=0; ipkg<mPackages.size(); ipkg++) {
            if ("android".equals(mPackages.get(ipkg))) {
                mBestTargetPackage = mPackages.get(ipkg);
                return;
            }
        }

        // Collect information about each package running in the process.
        ArrayList<ProcStatsEntry> subProcs = new ArrayList<>();
        for (int ipkg=0; ipkg<mPackages.size(); ipkg++) {
            SparseArray<ProcessStats.PackageState> vpkgs
                    = stats.mPackages.get(mPackages.get(ipkg), mUid);
            for (int ivers=0;  ivers<vpkgs.size(); ivers++) {
                ProcessStats.PackageState pkgState = vpkgs.valueAt(ivers);
                if (DEBUG) Log.d(TAG, "Eval pkg of " + mName + ", pkg "
                        + pkgState + ":");
                if (pkgState == null) {
                    Log.w(TAG, "No package state found for " + mPackages.get(ipkg) + "/"
                            + mUid + " in process " + mName);
                    continue;
                }
                ProcessStats.ProcessState pkgProc = pkgState.mProcesses.get(mName);
                if (pkgProc == null) {
                    Log.w(TAG, "No process " + mName + " found in package state "
                            + mPackages.get(ipkg) + "/" + mUid);
                    continue;
                }
                subProcs.add(new ProcStatsEntry(pkgProc, pkgState.mPackageName, bgTotals,
                        runTotals, useUss));
            }
        }

        if (subProcs.size() > 1) {
            Collections.sort(subProcs, compare);
            if (subProcs.get(0).mRunWeight > (subProcs.get(1).mRunWeight *3)) {
                if (DEBUG) Log.d(TAG, "Eval pkg of " + mName + ": best pkg "
                        + subProcs.get(0).mPackage + " weight " + subProcs.get(0).mRunWeight
                        + " better than " + subProcs.get(1).mPackage
                        + " weight " + subProcs.get(1).mRunWeight);
                mBestTargetPackage = subProcs.get(0).mPackage;
                return;
            }
            // Couldn't find one that is best by weight, let's decide on best another
            // way: the one that has the longest running service, accounts for at least
            // half of the maximum weight, and has specified an explicit app icon.
            double maxWeight = subProcs.get(0).mRunWeight;
            long bestRunTime = -1;
            boolean bestPersistent = false;
            for (int i=0; i<subProcs.size(); i++) {
                final ProcStatsEntry subProc = subProcs.get(i);
                if (subProc.mRunWeight < (maxWeight/2)) {
                    if (DEBUG) Log.d(TAG, "Eval pkg of " + mName + ": pkg "
                            + subProc.mPackage + " weight " + subProc.mRunWeight
                            + " too small");
                    continue;
                }
                try {
                    ApplicationInfo ai = pm.getApplicationInfo(subProc.mPackage, 0);
                    if (ai.icon == 0) {
                        if (DEBUG) Log.d(TAG, "Eval pkg of " + mName + ": pkg "
                                + subProc.mPackage + " has no icon");
                        continue;
                    }
                    if ((ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0) {
                        long thisRunTime = subProc.mRunDuration;
                        if (!bestPersistent || thisRunTime > bestRunTime) {
                            if (DEBUG) Log.d(TAG, "Eval pkg of " + mName + ": pkg "
                                    + subProc.mPackage + " new best pers run time "
                                    + thisRunTime);
                            bestRunTime = thisRunTime;
                            bestPersistent = true;
                        } else {
                            if (DEBUG) Log.d(TAG, "Eval pkg of " + mName + ": pkg "
                                    + subProc.mPackage + " pers run time " + thisRunTime
                                    + " not as good as last " + bestRunTime);
                        }
                        continue;
                    } else if (bestPersistent) {
                        if (DEBUG) Log.d(TAG, "Eval pkg of " + mName + ": pkg "
                                + subProc.mPackage + " is not persistent");
                        continue;
                    }
                } catch (PackageManager.NameNotFoundException e) {
                    if (DEBUG) Log.d(TAG, "Eval pkg of " + mName + ": pkg "
                            + subProc.mPackage + " failed finding app info");
                    continue;
                }
                ArrayList<Service> subProcServices = null;
                for (int isp=0, NSP=mServices.size(); isp<NSP; isp++) {
                    ArrayList<Service> subServices = mServices.valueAt(isp);
                    if (subServices.get(0).mPackage.equals(subProc.mPackage)) {
                        subProcServices = subServices;
                        break;
                    }
                }
                long thisRunTime = 0;
                if (subProcServices != null) {
                    for (int iss=0, NSS=subProcServices.size(); iss<NSS; iss++) {
                        Service service = subProcServices.get(iss);
                        if (service.mDuration > thisRunTime) {
                            if (DEBUG) Log.d(TAG, "Eval pkg of " + mName + ": pkg "
                                    + subProc.mPackage + " service " + service.mName
                                    + " run time is " + service.mDuration);
                            thisRunTime = service.mDuration;
                            break;
                        }
                    }
                }
                if (thisRunTime > bestRunTime) {
                    if (DEBUG) Log.d(TAG, "Eval pkg of " + mName + ": pkg "
                            + subProc.mPackage + " new best run time " + thisRunTime);
                    mBestTargetPackage = subProc.mPackage;
                    bestRunTime = thisRunTime;
                } else {
                    if (DEBUG) Log.d(TAG, "Eval pkg of " + mName + ": pkg "
                            + subProc.mPackage + " run time " + thisRunTime
                            + " not as good as last " + bestRunTime);
                }
            }
        } else if (subProcs.size() == 1) {
            mBestTargetPackage = subProcs.get(0).mPackage;
        }
    }

    public void addService(ProcessStats.ServiceState svc) {
        ArrayList<Service> services = mServices.get(svc.mPackage);
        if (services == null) {
            services = new ArrayList<Service>();
            mServices.put(svc.mPackage, services);
        }
        services.add(new Service(svc));
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mPackage);
        dest.writeInt(mUid);
        dest.writeString(mName);
        dest.writeStringList(mPackages);
        dest.writeLong(mBgDuration);
        dest.writeLong(mAvgBgMem);
        dest.writeLong(mMaxBgMem);
        dest.writeDouble(mBgWeight);
        dest.writeLong(mRunDuration);
        dest.writeLong(mAvgRunMem);
        dest.writeLong(mMaxRunMem);
        dest.writeDouble(mRunWeight);
        dest.writeString(mBestTargetPackage);
        final int N = mServices.size();
        dest.writeInt(N);
        for (int i=0; i<N; i++) {
            dest.writeString(mServices.keyAt(i));
            dest.writeTypedList(mServices.valueAt(i));
        }
    }

    public static final Parcelable.Creator<ProcStatsEntry> CREATOR
            = new Parcelable.Creator<ProcStatsEntry>() {
        public ProcStatsEntry createFromParcel(Parcel in) {
            return new ProcStatsEntry(in);
        }

        public ProcStatsEntry[] newArray(int size) {
            return new ProcStatsEntry[size];
        }
    };

    public static final class Service implements Parcelable {
        final String mPackage;
        final String mName;
        final String mProcess;
        final long mDuration;

        public Service(ProcessStats.ServiceState service) {
            mPackage = service.mPackage;
            mName = service.mName;
            mProcess = service.mProcessName;
            mDuration = ProcessStats.dumpSingleServiceTime(null, null, service,
                    ProcessStats.ServiceState.SERVICE_RUN,
                    ProcessStats.STATE_NOTHING, 0, 0);
        }

        public Service(Parcel in) {
            mPackage = in.readString();
            mName = in.readString();
            mProcess = in.readString();
            mDuration = in.readLong();
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(mPackage);
            dest.writeString(mName);
            dest.writeString(mProcess);
            dest.writeLong(mDuration);
        }

        public static final Parcelable.Creator<Service> CREATOR
                = new Parcelable.Creator<Service>() {
            public Service createFromParcel(Parcel in) {
                return new Service(in);
            }

            public Service[] newArray(int size) {
                return new Service[size];
            }
        };
    }
}